Skip to content

Commit 235eb4d

Browse files
committed
Simplify our Assert infrastructure a little.
Remove the Trap and TrapMacro macros, which were nearly unused and confusingly had the opposite condition polarity from the otherwise-functionally-equivalent Assert macros. Having done that, it's very hard to justify carrying the errorType argument of ExceptionalCondition, so drop that too, and just let it assume everything's an Assert. This saves about 64K of code space as of current HEAD. Discussion: https://postgr.es/m/3928703.1665345117@sss.pgh.pa.us
1 parent 6291b25 commit 235eb4d

File tree

5 files changed

+22
-51
lines changed

5 files changed

+22
-51
lines changed

contrib/amcheck/verify_heapam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ check_tuple_attribute(HeapCheckContext *ctx)
13191319
*/
13201320

13211321
/*
1322-
* Check that VARTAG_SIZE won't hit a TrapMacro on a corrupt va_tag before
1322+
* Check that VARTAG_SIZE won't hit an Assert on a corrupt va_tag before
13231323
* risking a call into att_addlength_pointer
13241324
*/
13251325
if (VARATT_IS_EXTERNAL(tp + ctx->offset))

src/backend/utils/error/assert.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@
2828
*/
2929
void
3030
ExceptionalCondition(const char *conditionName,
31-
const char *errorType,
3231
const char *fileName,
3332
int lineNumber)
3433
{
3534
/* Report the failure on stderr (or local equivalent) */
3635
if (!PointerIsValid(conditionName)
37-
|| !PointerIsValid(fileName)
38-
|| !PointerIsValid(errorType))
36+
|| !PointerIsValid(fileName))
3937
write_stderr("TRAP: ExceptionalCondition: bad arguments in PID %d\n",
4038
(int) getpid());
4139
else
42-
write_stderr("TRAP: %s(\"%s\", File: \"%s\", Line: %d, PID: %d)\n",
43-
errorType, conditionName,
44-
fileName, lineNumber, (int) getpid());
40+
write_stderr("TRAP: failed Assert(\"%s\"), File: \"%s\", Line: %d, PID: %d\n",
41+
conditionName, fileName, lineNumber, (int) getpid());
4542

4643
/* Usually this shouldn't be needed, but make sure the msg went out */
4744
fflush(stderr);

src/backend/utils/error/elog.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,8 +1832,7 @@ pg_re_throw(void)
18321832
}
18331833

18341834
/* Doesn't return ... */
1835-
ExceptionalCondition("pg_re_throw tried to return", "FailedAssertion",
1836-
__FILE__, __LINE__);
1835+
ExceptionalCondition("pg_re_throw tried to return", __FILE__, __LINE__);
18371836
}
18381837

18391838

src/include/c.h

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,6 @@ typedef NameData *Name;
796796
#define AssertArg(condition) ((void)true)
797797
#define AssertState(condition) ((void)true)
798798
#define AssertPointerAlignment(ptr, bndr) ((void)true)
799-
#define Trap(condition, errorType) ((void)true)
800-
#define TrapMacro(condition, errorType) (true)
801799

802800
#elif defined(FRONTEND)
803801

@@ -811,60 +809,38 @@ typedef NameData *Name;
811809
#else /* USE_ASSERT_CHECKING && !FRONTEND */
812810

813811
/*
814-
* Trap
815-
* Generates an exception if the given condition is true.
812+
* Assert
813+
* Generates a fatal exception if the given condition is false.
816814
*/
817-
#define Trap(condition, errorType) \
815+
#define Assert(condition) \
818816
do { \
819-
if (condition) \
820-
ExceptionalCondition(#condition, (errorType), \
821-
__FILE__, __LINE__); \
817+
if (!(condition)) \
818+
ExceptionalCondition(#condition, __FILE__, __LINE__); \
822819
} while (0)
823820

824821
/*
825-
* TrapMacro is the same as Trap but it's intended for use in macros:
822+
* AssertMacro is the same as Assert but it's suitable for use in
823+
* expression-like macros, for example:
826824
*
827825
* #define foo(x) (AssertMacro(x != 0), bar(x))
828-
*
829-
* Isn't CPP fun?
830826
*/
831-
#define TrapMacro(condition, errorType) \
832-
((bool) (! (condition) || \
833-
(ExceptionalCondition(#condition, (errorType), \
834-
__FILE__, __LINE__), 0)))
835-
836-
#define Assert(condition) \
837-
do { \
838-
if (!(condition)) \
839-
ExceptionalCondition(#condition, "FailedAssertion", \
840-
__FILE__, __LINE__); \
841-
} while (0)
842-
843827
#define AssertMacro(condition) \
844828
((void) ((condition) || \
845-
(ExceptionalCondition(#condition, "FailedAssertion", \
846-
__FILE__, __LINE__), 0)))
829+
(ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
847830

848-
#define AssertArg(condition) \
849-
do { \
850-
if (!(condition)) \
851-
ExceptionalCondition(#condition, "BadArgument", \
852-
__FILE__, __LINE__); \
853-
} while (0)
854-
855-
#define AssertState(condition) \
856-
do { \
857-
if (!(condition)) \
858-
ExceptionalCondition(#condition, "BadState", \
859-
__FILE__, __LINE__); \
860-
} while (0)
831+
/*
832+
* AssertArg and AssertState are identical to Assert. Some places use them
833+
* to indicate that the complaint is specifically about a bad argument or
834+
* unexpected state, but this usage is largely obsolescent.
835+
*/
836+
#define AssertArg(condition) Assert(condition)
837+
#define AssertState(condition) Assert(condition)
861838

862839
/*
863840
* Check that `ptr' is `bndr' aligned.
864841
*/
865842
#define AssertPointerAlignment(ptr, bndr) \
866-
Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
867-
"UnalignedPointer")
843+
Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
868844

869845
#endif /* USE_ASSERT_CHECKING && !FRONTEND */
870846

@@ -876,7 +852,6 @@ typedef NameData *Name;
876852
*/
877853
#ifndef FRONTEND
878854
extern void ExceptionalCondition(const char *conditionName,
879-
const char *errorType,
880855
const char *fileName, int lineNumber) pg_attribute_noreturn();
881856
#endif
882857

src/include/postgres.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ typedef enum vartag_external
135135
((tag) == VARTAG_INDIRECT ? sizeof(varatt_indirect) : \
136136
VARTAG_IS_EXPANDED(tag) ? sizeof(varatt_expanded) : \
137137
(tag) == VARTAG_ONDISK ? sizeof(varatt_external) : \
138-
TrapMacro(true, "unrecognized TOAST vartag"))
138+
(AssertMacro(false), 0))
139139

140140
/*
141141
* These structs describe the header of a varlena object that may have been

0 commit comments

Comments
 (0)