public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Daurnimator <quae@daurnimator.com>
To: gcc@gcc.gnu.org
Subject: How to use pragma in a compound literal in GCC 9
Date: Tue, 12 Feb 2019 07:22:00 -0000	[thread overview]
Message-ID: <CAEnbY+e1OF2zguukTD0_UgzHoJ+dhWp7SEZd8MhDBmDKi8MQFQ@mail.gmail.com> (raw)

[-- 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 });
}

             reply	other threads:[~2019-02-12  7:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-12  7:22 Daurnimator [this message]
2019-02-12  8:24 ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEnbY+e1OF2zguukTD0_UgzHoJ+dhWp7SEZd8MhDBmDKi8MQFQ@mail.gmail.com \
    --to=quae@daurnimator.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).