1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Add unconstify_constexpr

unconstify_constexpr is like unconstify but for contexts where a
constant expression is required, such as for initializing global
variables.  It provides the same level of checking as unconstify, but
the checking only works on GCC, otherwise it lets anything through.
Also, if the check fails, it gives a less clear error message.  So it
should only be used when necessary.

(An unvolatize_constexpr doesn't seem necessary, but it could be added if
required.)

Reviewed-by: Rui Zhao <zhaorui126@gmail.com>
Discussion: https://www.postgresql.org/message-id/c76b72dd-5736-453a-b698-6968d42e4768%40eisentraut.org
This commit is contained in:
Peter Eisentraut
2026-10-07 08:21:37 +02:00
parent 2a8ce0f684
commit d396cb7e22

View File

@@ -1412,20 +1412,37 @@ typedef struct PGAlignedXLogBlock PGAlignedXLogBlock;
* design or language restrictions prevent you from declaring that
* (e.g. because a function returns both const and non-const variables).
*
* Note that this only works in function scope, not for global variables (it'd
* be nice, but not trivial, to improve that).
* unconstify_constexpr is for contexts where a constant expression is
* required, such as for initializing global variables. It provides the same
* level of checking as unconstify, but the checking only works on GCC,
* otherwise it lets anything through. Also, if the check fails, it gives a
* less clear error message. So it should only be used when necessary.
*
* (An unvolatize_constexpr doesn't seem necessary, but it could be added if
* required.)
*/
#if defined(__cplusplus)
#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
#define unconstify_constexpr(underlying_type, expr) const_cast<underlying_type>(expr)
#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
#else
#else /* !__cplusplus */
#define unconstify(underlying_type, expr) \
(StaticAssertVariableIsOfTypeMacro(expr, const underlying_type), \
(underlying_type) (expr))
#define unvolatize(underlying_type, expr) \
(StaticAssertVariableIsOfTypeMacro(expr, volatile underlying_type), \
(underlying_type) (expr))
#endif
#ifdef __GNUC__
#define unconstify_constexpr(underlying_type, expr) \
__builtin_choose_expr( \
_Generic((expr), const underlying_type: 1, default: 0), \
(underlying_type) (expr), \
(void) 0)
#else /* !__GNUC_ */
#define unconstify_constexpr(underlying_type, expr) \
((underlying_type) (expr))
#endif /* !__GNUC_ */
#endif /* !__cplusplus */
/*
* SSE2 instructions are part of the spec for the 64-bit x86 ISA. We assume