Skip to content

Commit 112f022

Browse files
committed
Add optional parameter to PG_TRY() macros
This optional parameter can be specified in cases where there are nested PG_TRY() statements within a function in order to stop the compiler from issuing warnings about shadowed local variables when compiling with -Wshadow. The optional parameter is used as a suffix on the variable names declared within the PG_TRY(), PG_CATCH(), PG_FINALLY() and PG_END_TRY() macros. The parameter, if specified, must be the same in each component macro of the given PG_TRY() block. This also adjusts the single case where we have nested PG_TRY() statements to add a parameter to the inner-most PG_TRY(). This reduces the number of compiler warnings when compiling with -Wshadow=compatible-local from 5 down to 1. Author: David Rowley Discussion: https://postgr.es/m/CAApHDvqWGMdB_pATeUqE=JCtNqNxObPOJ00jFEa2_sZ20j_Wvg@mail.gmail.com
1 parent 23f3989 commit 112f022

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/backend/tcop/utility.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,16 +1678,16 @@ ProcessUtilitySlow(ParseState *pstate,
16781678
* command itself is queued, which is enough.
16791679
*/
16801680
EventTriggerInhibitCommandCollection();
1681-
PG_TRY();
1681+
PG_TRY(2);
16821682
{
16831683
address = ExecRefreshMatView((RefreshMatViewStmt *) parsetree,
16841684
queryString, params, qc);
16851685
}
1686-
PG_FINALLY();
1686+
PG_FINALLY(2);
16871687
{
16881688
EventTriggerUndoInhibitCommandCollection();
16891689
}
1690-
PG_END_TRY();
1690+
PG_END_TRY(2);
16911691
break;
16921692

16931693
case T_CreateTrigStmt:

src/include/utils/elog.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,39 +310,47 @@ extern PGDLLIMPORT ErrorContextCallback *error_context_stack;
310310
* pedantry; we have seen bugs from compilers improperly optimizing code
311311
* away when such a variable was not marked. Beware that gcc's -Wclobbered
312312
* warnings are just about entirely useless for catching such oversights.
313+
*
314+
* Each of these macros accepts an optional argument which can be specified
315+
* to apply a suffix to the variables declared within the macros. This suffix
316+
* can be used to avoid the compiler emitting warnings about shadowed
317+
* variables when compiling with -Wshadow in situations where nested PG_TRY()
318+
* statements are required. The optional suffix may contain any character
319+
* that's allowed in a variable name. The suffix, if specified, must be the
320+
* same within each component macro of the given PG_TRY() statement.
313321
*----------
314322
*/
315-
#define PG_TRY() \
323+
#define PG_TRY(...) \
316324
do { \
317-
sigjmp_buf *_save_exception_stack = PG_exception_stack; \
318-
ErrorContextCallback *_save_context_stack = error_context_stack; \
319-
sigjmp_buf _local_sigjmp_buf; \
320-
bool _do_rethrow = false; \
321-
if (sigsetjmp(_local_sigjmp_buf, 0) == 0) \
325+
sigjmp_buf *_save_exception_stack##__VA_ARGS__ = PG_exception_stack; \
326+
ErrorContextCallback *_save_context_stack##__VA_ARGS__ = error_context_stack; \
327+
sigjmp_buf _local_sigjmp_buf##__VA_ARGS__; \
328+
bool _do_rethrow##__VA_ARGS__ = false; \
329+
if (sigsetjmp(_local_sigjmp_buf##__VA_ARGS__, 0) == 0) \
322330
{ \
323-
PG_exception_stack = &_local_sigjmp_buf
331+
PG_exception_stack = &_local_sigjmp_buf##__VA_ARGS__
324332

325-
#define PG_CATCH() \
333+
#define PG_CATCH(...) \
326334
} \
327335
else \
328336
{ \
329-
PG_exception_stack = _save_exception_stack; \
330-
error_context_stack = _save_context_stack
337+
PG_exception_stack = _save_exception_stack##__VA_ARGS__; \
338+
error_context_stack = _save_context_stack##__VA_ARGS__
331339

332-
#define PG_FINALLY() \
340+
#define PG_FINALLY(...) \
333341
} \
334342
else \
335-
_do_rethrow = true; \
343+
_do_rethrow##__VA_ARGS__ = true; \
336344
{ \
337-
PG_exception_stack = _save_exception_stack; \
338-
error_context_stack = _save_context_stack
345+
PG_exception_stack = _save_exception_stack##__VA_ARGS__; \
346+
error_context_stack = _save_context_stack##__VA_ARGS__
339347

340-
#define PG_END_TRY() \
348+
#define PG_END_TRY(...) \
341349
} \
342-
if (_do_rethrow) \
350+
if (_do_rethrow##__VA_ARGS__) \
343351
PG_RE_THROW(); \
344-
PG_exception_stack = _save_exception_stack; \
345-
error_context_stack = _save_context_stack; \
352+
PG_exception_stack = _save_exception_stack##__VA_ARGS__; \
353+
error_context_stack = _save_context_stack##__VA_ARGS__; \
346354
} while (0)
347355

348356
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy