* How to use pragma in a compound literal in GCC 9
@ 2019-02-12 7:22 Daurnimator
2019-02-12 8:24 ` Jakub Jelinek
0 siblings, 1 reply; 2+ messages in thread
From: Daurnimator @ 2019-02-12 7:22 UTC (permalink / raw)
To: gcc
[-- Attachment #1: Type: text/plain, Size: 429 bytes --]
A project I help maintain has had a report that it fails to compile with GCC 9
https://github.com/wahern/cqueues/issues/212#issuecomment-461693111
I've attached a minimal reproduction of the issue.
Trying to compile it results in:
<source>: In function 'main':
<source>:46:15: error: lvalue required as unary '&' operand
46 | void *x = &quietinit((struct bar){ 0, .a = 0 });
| ^
Compiler returned: 1
[-- Attachment #2: pragma-compound-literal.c --]
[-- Type: text/x-csrc, Size: 1704 bytes --]
/*
* C O M P I L E R A N N O T A T I O N S
*
* GCC with -Wextra, and clang by default, complain about overrides in
* initializer lists. Overriding previous member initializers is well
* defined behavior in C. We rely on this behavior to define default,
* overrideable member values when instantiating configuration objects.
*
* quietinit() guards a compound literal expression with pragmas to
* silence these shrill warnings. This alleviates the burden of requiring
* third-party projects to adjust their compiler flags.
*
* NOTE: If you take the address of the compound literal, take the address
* of the transformed expression, otherwise the compound literal lifetime is
* tied to the scope of the GCC statement expression.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#if defined __clang__
#define PRAGMA_PUSH _Pragma("clang diagnostic push")
#define PRAGMA_QUIET _Pragma("clang diagnostic ignored \"-Winitializer-overrides\"")
#define PRAGMA_POP _Pragma("clang diagnostic pop")
#define quietinit(...) \
PRAGMA_PUSH PRAGMA_QUIET __VA_ARGS__ PRAGMA_POP
#elif (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4
#define PRAGMA_PUSH _Pragma("GCC diagnostic push")
#define PRAGMA_QUIET _Pragma("GCC diagnostic ignored \"-Woverride-init\"")
#define PRAGMA_POP _Pragma("GCC diagnostic pop")
/* GCC parses the _Pragma operator less elegantly than clang. */
#define quietinit(...) \
__extension__ ({ PRAGMA_PUSH PRAGMA_QUIET __VA_ARGS__; PRAGMA_POP })
#else
#define PRAGMA_PUSH
#define PRAGMA_QUIET
#define PRAGMA_POP
#define quietinit(...) __VA_ARGS__
#endif
struct bar {
int a;
};
int main() {
void *x = &quietinit((struct bar){ 0, .a = 0 });
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: How to use pragma in a compound literal in GCC 9
2019-02-12 7:22 How to use pragma in a compound literal in GCC 9 Daurnimator
@ 2019-02-12 8:24 ` Jakub Jelinek
0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2019-02-12 8:24 UTC (permalink / raw)
To: Daurnimator; +Cc: gcc
On Tue, Feb 12, 2019 at 06:19:56PM +1100, Daurnimator wrote:
> A project I help maintain has had a report that it fails to compile with GCC 9
> https://github.com/wahern/cqueues/issues/212#issuecomment-461693111
>
> I've attached a minimal reproduction of the issue.
> Trying to compile it results in:
>
> <source>: In function 'main':
> <source>:46:15: error: lvalue required as unary '&' operand
> 46 | void *x = &quietinit((struct bar){ 0, .a = 0 });
> | ^
> Compiler returned: 1
Guess you need to restructure the code somewhat, with the way these macros
are used it is not possible. GCC doesn't accept _Pragma at arbitrary spots
within a single expression.
You could just add -Wno-override-init to the options and use the macro
without pragma.
Or better, don't do what the macro complains about, if I understand it
right, at least in the package I was looking at, there is just 0, 0, { 0, 0 }
style initialization in some macros and a macro that uses quietinit
sometimes ammends it with , .a = whatever, .b = whateverelse.
Those 0, 0, { 0, 0 } are useless, that is the default behavior, if the
compound literal is just (struct bar){}, it will be initialized to all
zeros, and (struct bar){ .a = 1 } will initialize just the designated
elements. And you won't get any warnings for that (at least not those in
-Wall/-Wextra).
Or use those quietinit-like macros as toplevel statements only and pass
to the macro also a name of the pointer you assign the address to, so in the
end it will be:
_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Woverride-init\"")
void *x = &(struct bar){ 0, .a = 0 };
_Pragma("GCC diagnostic pop")
Or use the no pragmas quietinit and add another set of macros,
quietinit_start/quietinit_end or whatever, define those macros to empty
on everything but GCC9+, on GCC9+ define those to the starting or ending
pragmas and put those in the source around the whole statements with that,
rather than just in subexpressions.
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-02-12 8:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-12 7:22 How to use pragma in a compound literal in GCC 9 Daurnimator
2019-02-12 8:24 ` Jakub Jelinek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).