Skip to content

Commit d78d452

Browse files
committed
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. This also fixes that the existing output for Assert et al. shows the *inverted* condition, which is also confusing and not how assertions usually work. Discussion: https://www.postgresql.org/message-id/flat/6c68efe3-117a-dcc1-73d4-18ba1ec532e2%402ndquadrant.com
1 parent f7db0ac commit d78d452

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/include/c.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ typedef NameData *Name;
755755
#define Trap(condition, errorType) \
756756
do { \
757757
if (condition) \
758-
ExceptionalCondition(CppAsString(condition), (errorType), \
758+
ExceptionalCondition(#condition, (errorType), \
759759
__FILE__, __LINE__); \
760760
} while (0)
761761

@@ -768,20 +768,34 @@ typedef NameData *Name;
768768
*/
769769
#define TrapMacro(condition, errorType) \
770770
((bool) (! (condition) || \
771-
(ExceptionalCondition(CppAsString(condition), (errorType), \
771+
(ExceptionalCondition(#condition, (errorType), \
772772
__FILE__, __LINE__), 0)))
773773

774774
#define Assert(condition) \
775-
Trap(!(condition), "FailedAssertion")
775+
do { \
776+
if (!(condition)) \
777+
ExceptionalCondition(#condition, "FailedAssertion", \
778+
__FILE__, __LINE__); \
779+
} while (0)
776780

777781
#define AssertMacro(condition) \
778-
((void) TrapMacro(!(condition), "FailedAssertion"))
782+
((void) ((condition) || \
783+
(ExceptionalCondition(#condition, "FailedAssertion", \
784+
__FILE__, __LINE__), 0)))
779785

780786
#define AssertArg(condition) \
781-
Trap(!(condition), "BadArgument")
787+
do { \
788+
if (!(condition)) \
789+
ExceptionalCondition(#condition, "BadArgument", \
790+
__FILE__, __LINE__); \
791+
} while (0)
782792

783793
#define AssertState(condition) \
784-
Trap(!(condition), "BadState")
794+
do { \
795+
if (!(condition)) \
796+
ExceptionalCondition(#condition, "BadState", \
797+
__FILE__, __LINE__); \
798+
} while (0)
785799

786800
/*
787801
* Check that `ptr' is `bndr' aligned.

0 commit comments

Comments
 (0)