diff options
author | David Malcolm <dmalcolm@redhat.com> | 2016-06-13 21:04:07 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2016-06-13 21:04:07 +0000 |
commit | 09765e3a0a8be2f7bca694a1171ecf050f5400d1 (patch) | |
tree | a7dc6a7feb0754dac0ba89c788d492dc8864951a /gcc/pretty-print.c | |
parent | 0dda258b6f3194849dc9a47aa9417d7ce942a3ec (diff) | |
download | gcc-09765e3a0a8be2f7bca694a1171ecf050f5400d1.zip gcc-09765e3a0a8be2f7bca694a1171ecf050f5400d1.tar.gz gcc-09765e3a0a8be2f7bca694a1171ecf050f5400d1.tar.bz2 |
selftests: improve reported failure locations
This patch introduce a selftest::location struct to wrap up __FILE__
and __LINE__ information (and __FUNCTION__) throughout the selftests,
allowing location information to be passed around.
It updates the helper functions in pretty-print.c to pass through
the precise location of each test, so that if a failure occurs, the
correct line number is printed, rather than a line within a helper
function.
gcc/ChangeLog:
* input.c (test_reading_source_line): Use SELFTEST_LOCATION.
* pretty-print.c (assert_pp_format_va): Add location param and use
it with ASSERT_STREQ_AT.
(assert_pp_format): Add location param and pass it to
assert_pp_format_va.
(assert_pp_format_colored): Likewise.
(ASSERT_PP_FORMAT_1): New.
(ASSERT_PP_FORMAT_2): New.
(ASSERT_PP_FORMAT_3): New.
(test_pp_format): Provide SELFTEST_LOCATION throughout, either
explicitly, or implicitly via the above macros.
* selftest.c (selftest::pass): Use a selftest::location rather
than file and line.
(selftest::fail): Likewise. Print the function name.
(selftest::fail_formatted): Likewise.
(selftest::assert_streq): Use a selftest::location rather than
file and line.
* selftest.h (selftest::location): New struct.
(SELFTEST_LOCATION): New macro.
(selftest::pass): Accept a const location & rather than file
and line.
(selftest::fail): Likewise.
(selftest::fail_formatted): Likewise.
(selftest::assert_streq): Likewise.
(ASSERT_TRUE): Update for above changes, using SELFTEST_LOCATION.
(ASSERT_FALSE): Likewise.
(ASSERT_EQ): Likewise.
(ASSERT_NE): Likewise.
(ASSERT_STREQ): Likewise.
(ASSERT_PRED1): Likewise.
(ASSERT_STREQ_AT): New macro.
From-SVN: r237410
Diffstat (limited to 'gcc/pretty-print.c')
-rw-r--r-- | gcc/pretty-print.c | 128 |
1 files changed, 78 insertions, 50 deletions
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index 8febda0..86ae3a5 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -1227,8 +1227,8 @@ test_basic_printing () prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */ static void -assert_pp_format_va (const char *expected, bool show_color, const char *fmt, - va_list *ap) +assert_pp_format_va (const location &loc, const char *expected, + bool show_color, const char *fmt, va_list *ap) { pretty_printer pp; text_info ti; @@ -1243,34 +1243,59 @@ assert_pp_format_va (const char *expected, bool show_color, const char *fmt, pp_show_color (&pp) = show_color; pp_format (&pp, &ti); pp_output_formatted_text (&pp); - ASSERT_STREQ (expected, pp_formatted_text (&pp)); + ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp)); } /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text prints EXPECTED, with show_color disabled. */ static void -assert_pp_format (const char *expected, const char *fmt, ...) +assert_pp_format (const location &loc, const char *expected, + const char *fmt, ...) { va_list ap; va_start (ap, fmt); - assert_pp_format_va (expected, false, fmt, &ap); + assert_pp_format_va (loc, expected, false, fmt, &ap); va_end (ap); } /* As above, but with colorization enabled. */ static void -assert_pp_format_colored (const char *expected, const char *fmt, ...) +assert_pp_format_colored (const location &loc, const char *expected, + const char *fmt, ...) { va_list ap; va_start (ap, fmt); - assert_pp_format_va (expected, true, fmt, &ap); + assert_pp_format_va (loc, expected, true, fmt, &ap); va_end (ap); } +/* Helper function for calling testing pp_format, + by calling assert_pp_format with various numbers of arguments. + These exist mostly to avoid having to write SELFTEST_LOCATION + throughout test_pp_format. */ + +#define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \ + SELFTEST_BEGIN_STMT \ + assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \ + (ARG1)); \ + SELFTEST_END_STMT + +#define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \ + SELFTEST_BEGIN_STMT \ + assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \ + (ARG1), (ARG2)); \ + SELFTEST_END_STMT + +#define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \ + SELFTEST_BEGIN_STMT \ + assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \ + (ARG1), (ARG2), (ARG3)); \ + SELFTEST_END_STMT + /* Verify that pp_format works, for various format codes. */ static void @@ -1284,68 +1309,71 @@ test_pp_format () close_quote = "'"; /* Verify that plain text is passed through unchanged. */ - assert_pp_format ("unformatted", "unformatted"); + assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted"); /* Verify various individual format codes, in the order listed in the comment for pp_format above. For each code, we append a second argument with a known bit pattern (0x12345678), to ensure that we are consuming arguments correctly. */ - assert_pp_format ("-27 12345678", "%d %x", -27, 0x12345678); - assert_pp_format ("-5 12345678", "%i %x", -5, 0x12345678); - assert_pp_format ("10 12345678", "%u %x", 10, 0x12345678); - assert_pp_format ("17 12345678", "%o %x", 15, 0x12345678); - assert_pp_format ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678); - assert_pp_format ("-27 12345678", "%ld %x", (long)-27, 0x12345678); - assert_pp_format ("-5 12345678", "%li %x", (long)-5, 0x12345678); - assert_pp_format ("10 12345678", "%lu %x", (long)10, 0x12345678); - assert_pp_format ("17 12345678", "%lo %x", (long)15, 0x12345678); - assert_pp_format ("cafebabe 12345678", "%lx %x", (long)0xcafebabe, - 0x12345678); - assert_pp_format ("-27 12345678", "%lld %x", (long long)-27, 0x12345678); - assert_pp_format ("-5 12345678", "%lli %x", (long long)-5, 0x12345678); - assert_pp_format ("10 12345678", "%llu %x", (long long)10, 0x12345678); - assert_pp_format ("17 12345678", "%llo %x", (long long)15, 0x12345678); - assert_pp_format ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe, - 0x12345678); - assert_pp_format ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678); - assert_pp_format ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678); - assert_pp_format ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10, - 0x12345678); - assert_pp_format ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678); - assert_pp_format ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe, - 0x12345678); - assert_pp_format ("A 12345678", "%c %x", 'A', 0x12345678); - assert_pp_format ("hello world 12345678", "%s %x", "hello world", - 0x12345678); + ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678); + ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678); + ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678); + ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678); + ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678); + ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678); + ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678); + ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678); + ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678); + ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe, + 0x12345678); + ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678); + ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678); + ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678); + ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678); + ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe, + 0x12345678); + ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678); + ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678); + ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10, + 0x12345678); + ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678); + ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe, + 0x12345678); + ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678); + ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world", + 0x12345678); /* We can't test for %p; the pointer is printed in an implementation-defined manner. */ - assert_pp_format ("normal colored normal 12345678", - "normal %rcolored%R normal %x", - "error", 0x12345678); + ASSERT_PP_FORMAT_2 ("normal colored normal 12345678", + "normal %rcolored%R normal %x", + "error", 0x12345678); /* The following assumes an empty value for GCC_COLORS. */ assert_pp_format_colored - ("normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678", + (SELFTEST_LOCATION, + "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678", "normal %rcolored%R normal %x", "error", 0x12345678); /* TODO: %m: strerror(text->err_no) - does not consume a value from args_ptr. */ - assert_pp_format ("% 12345678", "%% %x", 0x12345678); - assert_pp_format ("` 12345678", "%< %x", 0x12345678); - assert_pp_format ("' 12345678", "%> %x", 0x12345678); - assert_pp_format ("' 12345678", "%' %x", 0x12345678); - assert_pp_format ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678); - assert_pp_format ("abc 12345678", "%.3s %x", "abcdef", 0x12345678); + ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678); + ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678); + ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678); + ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678); + ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678); + ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678); /* Verify flag 'q'. */ - assert_pp_format ("`foo' 12345678", "%qs %x", "foo", 0x12345678); - assert_pp_format_colored ("`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x", + ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678); + assert_pp_format_colored (SELFTEST_LOCATION, + "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x", "foo", 0x12345678); /* Verify that combinations work, along with unformatted text. */ - assert_pp_format ("the quick brown fox jumps over the lazy dog", + assert_pp_format (SELFTEST_LOCATION, + "the quick brown fox jumps over the lazy dog", "the %s %s %s jumps over the %s %s", "quick", "brown", "fox", "lazy", "dog"); - assert_pp_format ("item 3 of 7", "item %i of %i", 3, 7); - assert_pp_format ("problem with `bar' at line 10", + assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7); + assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10", "problem with %qs at line %i", "bar", 10); /* Restore old values of open_quote and close_quote. */ |