Improve unreachability recognition in elog() macro.
authorTom Lane
Sat, 10 Sep 2016 21:54:23 +0000 (17:54 -0400)
committerTom Lane
Sat, 10 Sep 2016 21:54:23 +0000 (17:54 -0400)
Some experimentation with an older version of gcc showed that it is able
to determine whether "if (elevel_ >= ERROR)" is compile-time constant
if elevel_ is declared "const", but otherwise not so much.  We had
accounted for that in ereport() but were too miserly with braces to
make it so in elog().  I don't know how many currently-interesting
compilers have the same quirk, but in case it will save some code
space, let's make sure that elog() is on the same footing as ereport()
for this purpose.

Back-patch to 9.3 where we introduced pg_unreachable() calls into
elog/ereport.

src/include/utils/elog.h

index 8e90661195342335a2c8b3b1363ef551ce12b1d3..a16f92501d2de5197a93e890804ea1af5bf0e914 100644 (file)
@@ -216,12 +216,13 @@ extern int    getinternalerrposition(void);
 #else                          /* !HAVE__BUILTIN_CONSTANT_P */
 #define elog(elevel, ...)  \
    do { \
-       int     elevel_; \
        elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
-       elevel_ = (elevel); \
-       elog_finish(elevel_, __VA_ARGS__); \
-       if (elevel_ >= ERROR) \
-           pg_unreachable(); \
+       { \
+           const int elevel_ = (elevel); \
+           elog_finish(elevel_, __VA_ARGS__); \
+           if (elevel_ >= ERROR) \
+               pg_unreachable(); \
+       } \
    } while(0)
 #endif   /* HAVE__BUILTIN_CONSTANT_P */
 #else                          /* !HAVE__VA_ARGS */