diff mbox

[1/4] selftest.h: Add ASSERT_TRUE_AT and ASSERT_FALSE_AT

Message ID 1470239113-42666-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm Aug. 3, 2016, 3:45 p.m. UTC
I split out the selftest.h changes from v2 of the kit for ease of review;
here they are.

Successfully bootstrapped&regrtested in conjunction with the rest of the
patch kit on x86_64-pc-linux-gnu.

OK for trunk?

gcc/ChangeLog:
	* selftest.h (ASSERT_TRUE): Reimplement in terms of...
	(ASSERT_TRUE_AT): New macro.
	(ASSERT_FALSE): Reimplement in terms of...
	(ASSERT_FALSE_AT): New macro.
	(ASSERT_STREQ_AT): Fix typo in comment.
---
 gcc/selftest.h | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

Comments

Jeff Law Aug. 3, 2016, 4:06 p.m. UTC | #1
On 08/03/2016 09:45 AM, David Malcolm wrote:
> I split out the selftest.h changes from v2 of the kit for ease of review;
> here they are.
>
> Successfully bootstrapped&regrtested in conjunction with the rest of the
> patch kit on x86_64-pc-linux-gnu.
>
> OK for trunk?
>
> gcc/ChangeLog:
> 	* selftest.h (ASSERT_TRUE): Reimplement in terms of...
> 	(ASSERT_TRUE_AT): New macro.
> 	(ASSERT_FALSE): Reimplement in terms of...
> 	(ASSERT_FALSE_AT): New macro.
> 	(ASSERT_STREQ_AT): Fix typo in comment.
OK.  Though I do wonder if these should just be normal functions...  I 
assume there's a good reason for the macro pain :)


jeff
David Malcolm Aug. 4, 2016, 7:02 p.m. UTC | #2
On Wed, 2016-08-03 at 10:06 -0600, Jeff Law wrote:
> On 08/03/2016 09:45 AM, David Malcolm wrote:
> > I split out the selftest.h changes from v2 of the kit for ease of
> > review;
> > here they are.
> > 
> > Successfully bootstrapped&regrtested in conjunction with the rest
> > of the
> > patch kit on x86_64-pc-linux-gnu.
> > 
> > OK for trunk?
> > 
> > gcc/ChangeLog:
> > 	* selftest.h (ASSERT_TRUE): Reimplement in terms of...
> > 	(ASSERT_TRUE_AT): New macro.
> > 	(ASSERT_FALSE): Reimplement in terms of...
> > 	(ASSERT_FALSE_AT): New macro.
> > 	(ASSERT_STREQ_AT): Fix typo in comment.
> OK.  Though I do wonder if these should just be normal functions... 
>  I 
> assume there's a good reason for the macro pain :)

I tried to do it with an inline function, but a macro seems to be
better: as a macro, we can capture the stringification of the input
expression, so that we can print it (and its evaluated value) if it
fails.
diff mbox

Patch

diff --git a/gcc/selftest.h b/gcc/selftest.h
index 0bee476..397e998 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -104,13 +104,19 @@  extern int num_passes;
    ::selftest::fail if it false.  */
 
 #define ASSERT_TRUE(EXPR)				\
+  ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
+
+/* Like ASSERT_TRUE, but treat LOC as the effective location of the
+   selftest.  */
+
+#define ASSERT_TRUE_AT(LOC, EXPR)			\
   SELFTEST_BEGIN_STMT					\
   const char *desc = "ASSERT_TRUE (" #EXPR ")";		\
   bool actual = (EXPR);					\
   if (actual)						\
-    ::selftest::pass (SELFTEST_LOCATION, desc);	\
+    ::selftest::pass ((LOC), desc);			\
   else							\
-    ::selftest::fail (SELFTEST_LOCATION, desc);		\
+    ::selftest::fail ((LOC), desc);			\
   SELFTEST_END_STMT
 
 /* Evaluate EXPR and coerce to bool, calling
@@ -118,13 +124,19 @@  extern int num_passes;
    ::selftest::fail if it true.  */
 
 #define ASSERT_FALSE(EXPR)					\
+  ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
+
+/* Like ASSERT_FALSE, but treat LOC as the effective location of the
+   selftest.  */
+
+#define ASSERT_FALSE_AT(LOC, EXPR)				\
   SELFTEST_BEGIN_STMT						\
-  const char *desc = "ASSERT_FALSE (" #EXPR ")";		\
-  bool actual = (EXPR);					\
-  if (actual)							\
-    ::selftest::fail (SELFTEST_LOCATION, desc);				\
-  else								\
-    ::selftest::pass (SELFTEST_LOCATION, desc);				\
+  const char *desc = "ASSERT_FALSE (" #EXPR ")";			\
+  bool actual = (EXPR);							\
+  if (actual)								\
+    ::selftest::fail ((LOC), desc);			\
+  else									\
+    ::selftest::pass ((LOC), desc);					\
   SELFTEST_END_STMT
 
 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
@@ -169,7 +181,7 @@  extern int num_passes;
 			    (EXPECTED), (ACTUAL));		    \
   SELFTEST_END_STMT
 
-/* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the
+/* Like ASSERT_STREQ, but treat LOC as the effective location of the
    selftest.  */
 
 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL)			    \