https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118147

            Bug ID: 118147
           Summary: #pragma GCC diagnostic push causes errors when used in
                    IILE in struct member initializer
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: llvm at rifkin dot dev
  Target Milestone: ---

This errors:

struct F {
    int f = [] {
        #pragma GCC diagnostic push
        #pragma GCC diagnostic ignored "-Wparentheses"
        return 1;
        #pragma GCC diagnostic pop
    } ();
};


<source>:3:36: error: file ends in default argument
    3 |         #pragma GCC diagnostic push
      |                                    ^
<source>:3:32: error: expected ';' at end of member declaration
    3 |         #pragma GCC diagnostic push
      |                                ^~~~
      |                                    ;
<source>:3:36: error: expected '}' before end of line
    3 |         #pragma GCC diagnostic push
      |                                    ^
<source>:1:10: note: to match this '{'
    1 | struct F {
      |          ^
<source>:3:36: error: expected unqualified-id before end of line
    3 |         #pragma GCC diagnostic push
      |                                    ^
<source>:5:9: error: expected unqualified-id before 'return'
    5 |         return 1;
      |         ^~~~~~
<source>:7:5: error: expected declaration before '}' token
    7 |     } ();
      |     ^
<source>:7:8: error: expected unqualified-id before ')' token
    7 |     } ();
      |        ^
<source>:8:1: error: expected declaration before '}' token
    8 | };
      | ^
Compiler returned: 1


Naturally, this does not:

int f() {
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wparentheses"
    return 1;
    #pragma GCC diagnostic pop
}


This seems to date back to the dawn of C++11 support in gcc, 4.7.1:
https://godbolt.org/z/cEqjzsddM.


This is causing a problem with a library I maintain, libassert, where a user
has run into this bug with the following code
struct F{
  int f = [&]{
    int b = 3;
    ASSERT(b != b);
    return b;
  }();
};
due to ASSERT's expansion including _Pragma("GCC diagnostic push").

Reply via email to