pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Improve Assert output
Date: Wed, 14 Aug 2019 22:28:55 +0200
Message-ID: <6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com> (raw)

This kind of output is usually not helpful:

TRAP: BadArgument("((context) != ((void *)0) && (((((const
Node*)((context)))->type) == T_AllocSetContext) || ((((const
Node*)((context)))->type) == T_SlabContext) || ((((const
Node*)((context)))->type) == T_GenerationContext)))", File:
"../../../../src/include/utils/memutils.h", Line: 129)

What we probably want is something like:

TRAP: BadArgument("MemoryContextIsValid(context)", File:
"../../../../src/include/utils/memutils.h", Line: 129)

The problem is that the way the Assert macros are written they
macro-expand the arguments before stringifying them.  The attached patch
fixes that.  This requires both replacing CppAsString by plain "#" and
not redirecting Assert() to Trap().

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 7db60e612e1d26c7a1859948594c28dc3b54353e Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 14 Aug 2019 22:23:34 +0200
Subject: [PATCH] Improve Assert output

If an assertion expression contained a macro, the failed assertion
message would print the expanded macro, which is usually unhelpful and
confusing.  Restructure the Assert macros to not expand any macros
when constructing the failure message.
---
 src/include/c.h | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index 2a082afab1..f461628a24 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -755,7 +755,7 @@ typedef NameData *Name;
 #define Trap(condition, errorType) \
 	do { \
 		if (condition) \
-			ExceptionalCondition(CppAsString(condition), (errorType), \
+			ExceptionalCondition(#condition, (errorType), \
 								 __FILE__, __LINE__); \
 	} while (0)
 
@@ -768,20 +768,34 @@ typedef NameData *Name;
  */
 #define TrapMacro(condition, errorType) \
 	((bool) (! (condition) || \
-			 (ExceptionalCondition(CppAsString(condition), (errorType), \
+			 (ExceptionalCondition(#condition, (errorType), \
 								   __FILE__, __LINE__), 0)))
 
 #define Assert(condition) \
-		Trap(!(condition), "FailedAssertion")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "FailedAssertion", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 #define AssertMacro(condition) \
-		((void) TrapMacro(!(condition), "FailedAssertion"))
+	((void) ((condition) || \
+			 (ExceptionalCondition(#condition, "FailedAssertion", \
+								   __FILE__, __LINE__), 0)))
 
 #define AssertArg(condition) \
-		Trap(!(condition), "BadArgument")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "BadArgument", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 #define AssertState(condition) \
-		Trap(!(condition), "BadState")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "BadState", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 /*
  * Check that `ptr' is `bndr' aligned.
-- 
2.22.0



Attachments:

  [text/plain] 0001-Improve-Assert-output.patch (2.1K, ../6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com/2-0001-Improve-Assert-output.patch)
  download | inline diff:
From 7db60e612e1d26c7a1859948594c28dc3b54353e Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 14 Aug 2019 22:23:34 +0200
Subject: [PATCH] Improve Assert output

If an assertion expression contained a macro, the failed assertion
message would print the expanded macro, which is usually unhelpful and
confusing.  Restructure the Assert macros to not expand any macros
when constructing the failure message.
---
 src/include/c.h | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index 2a082afab1..f461628a24 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -755,7 +755,7 @@ typedef NameData *Name;
 #define Trap(condition, errorType) \
 	do { \
 		if (condition) \
-			ExceptionalCondition(CppAsString(condition), (errorType), \
+			ExceptionalCondition(#condition, (errorType), \
 								 __FILE__, __LINE__); \
 	} while (0)
 
@@ -768,20 +768,34 @@ typedef NameData *Name;
  */
 #define TrapMacro(condition, errorType) \
 	((bool) (! (condition) || \
-			 (ExceptionalCondition(CppAsString(condition), (errorType), \
+			 (ExceptionalCondition(#condition, (errorType), \
 								   __FILE__, __LINE__), 0)))
 
 #define Assert(condition) \
-		Trap(!(condition), "FailedAssertion")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "FailedAssertion", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 #define AssertMacro(condition) \
-		((void) TrapMacro(!(condition), "FailedAssertion"))
+	((void) ((condition) || \
+			 (ExceptionalCondition(#condition, "FailedAssertion", \
+								   __FILE__, __LINE__), 0)))
 
 #define AssertArg(condition) \
-		Trap(!(condition), "BadArgument")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "BadArgument", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 #define AssertState(condition) \
-		Trap(!(condition), "BadState")
+	do { \
+		if (!(condition)) \
+			ExceptionalCondition(#condition, "BadState", \
+								 __FILE__, __LINE__); \
+	} while (0)
 
 /*
  * Check that `ptr' is `bndr' aligned.
-- 
2.22.0



view thread (2+ messages)  latest in thread

Message-ID: <6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com>
Permalink:  ../6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com/
Also on:    postgresql.org/message-id/6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: peter.eisentraut@2ndquadrant.com
  Subject: Re: Improve Assert output
  In-Reply-To: <6c68efe3-117a-dcc1-73d4-18ba1ec532e2@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox