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/selftest.h | |
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/selftest.h')
-rw-r--r-- | gcc/selftest.h | 60 |
1 files changed, 44 insertions, 16 deletions
diff --git a/gcc/selftest.h b/gcc/selftest.h index 6759734..e719f5f 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -27,26 +27,45 @@ along with GCC; see the file COPYING3. If not see namespace selftest { +/* A struct describing the source-location of a selftest, to make it + easier to track down failing tests. */ + +struct location +{ + location (const char *file, int line, const char *function) + : m_file (file), m_line (line), m_function (function) {} + + const char *m_file; + int m_line; + const char *m_function; +}; + +/* A macro for use in selftests and by the ASSERT_ macros below, + constructing a selftest::location for the current source location. */ + +#define SELFTEST_LOCATION \ + (::selftest::location (__FILE__, __LINE__, __FUNCTION__)) + /* The entrypoint for running all tests. */ extern void run_tests (); /* Record the successful outcome of some aspect of the test. */ -extern void pass (const char *file, int line, const char *msg); +extern void pass (const location &loc, const char *msg); /* Report the failed outcome of some aspect of the test and abort. */ -extern void fail (const char *file, int line, const char *msg); +extern void fail (const location &loc, const char *msg); /* As "fail", but using printf-style formatted output. */ -extern void fail_formatted (const char *file, int line, const char *fmt, ...) - ATTRIBUTE_PRINTF_3; +extern void fail_formatted (const location &loc, const char *fmt, ...) + ATTRIBUTE_PRINTF_2; /* Implementation detail of ASSERT_STREQ. */ -extern void assert_streq (const char *file, int line, +extern void assert_streq (const location &loc, const char *desc_expected, const char *desc_actual, const char *val_expected, const char *val_actual); @@ -85,9 +104,9 @@ extern int num_passes; const char *desc = "ASSERT_TRUE (" #EXPR ")"; \ bool actual = (EXPR); \ if (actual) \ - ::selftest::pass (__FILE__, __LINE__, desc); \ + ::selftest::pass (SELFTEST_LOCATION, desc); \ else \ - ::selftest::fail (__FILE__, __LINE__, desc); \ + ::selftest::fail (SELFTEST_LOCATION, desc); \ SELFTEST_END_STMT /* Evaluate EXPR and coerce to bool, calling @@ -99,9 +118,9 @@ extern int num_passes; const char *desc = "ASSERT_FALSE (" #EXPR ")"; \ bool actual = (EXPR); \ if (actual) \ - ::selftest::fail (__FILE__, __LINE__, desc); \ + ::selftest::fail (SELFTEST_LOCATION, desc); \ else \ - ::selftest::pass (__FILE__, __LINE__, desc); \ + ::selftest::pass (SELFTEST_LOCATION, desc); \ SELFTEST_END_STMT /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling @@ -112,9 +131,9 @@ extern int num_passes; SELFTEST_BEGIN_STMT \ const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \ if ((EXPECTED) == (ACTUAL)) \ - ::selftest::pass (__FILE__, __LINE__, desc); \ + ::selftest::pass (SELFTEST_LOCATION, desc); \ else \ - ::selftest::fail (__FILE__, __LINE__, desc); \ + ::selftest::fail (SELFTEST_LOCATION, desc); \ SELFTEST_END_STMT /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling @@ -125,9 +144,9 @@ extern int num_passes; SELFTEST_BEGIN_STMT \ const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \ if ((EXPECTED) != (ACTUAL)) \ - ::selftest::pass (__FILE__, __LINE__, desc); \ + ::selftest::pass (SELFTEST_LOCATION, desc); \ else \ - ::selftest::fail (__FILE__, __LINE__, desc); \ + ::selftest::fail (SELFTEST_LOCATION, desc); \ SELFTEST_END_STMT /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling @@ -136,7 +155,16 @@ extern int num_passes; #define ASSERT_STREQ(EXPECTED, ACTUAL) \ SELFTEST_BEGIN_STMT \ - ::selftest::assert_streq (__FILE__, __LINE__, #EXPECTED, #ACTUAL, \ + ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \ + (EXPECTED), (ACTUAL)); \ + SELFTEST_END_STMT + +/* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the + selftest. */ + +#define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \ + SELFTEST_BEGIN_STMT \ + ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \ (EXPECTED), (ACTUAL)); \ SELFTEST_END_STMT @@ -148,9 +176,9 @@ extern int num_passes; const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \ bool actual = (PRED1) (VAL1); \ if (actual) \ - ::selftest::pass (__FILE__, __LINE__, desc); \ + ::selftest::pass (SELFTEST_LOCATION, desc); \ else \ - ::selftest::fail (__FILE__, __LINE__, desc); \ + ::selftest::fail (SELFTEST_LOCATION, desc); \ SELFTEST_END_STMT #define SELFTEST_BEGIN_STMT do { |