[cfe-dev] Compound Literal semantic analysis...

Neil Booth neil at daikokuya.co.uk
Fri Jan 11 17:45:06 PST 2008


Chris Lattner wrote:-

> I agree with Eli here.  I don't understand why Neil and EDG are  
> accepting the constant case.
> 
> Do they accept this?
> 
> void a(void) {
> static int *a = &(int){1};    // implicit automatic variable.
> }
> 
> GCC says "initializer element is not constant"
> 
> GCC also rejects this:
> 
> void a(void) {
> static int *a = (int[]){1};
> }

You're all correct; there was a bug in my front end not saving
state on recursion (recursively parsing a top level initializer:
one for the = and one for the compound literal).  Now I've fixed
that I emit the expected two errors:

$ ./cfe /tmp/bug.c 
"/tmp/bug.c", line 3: error: expression must be a constant
static int *t = &tmp;        // Both GCC and EDG issue an error
                ^
"/tmp/bug.c", line 4: warning: declaration hides function "a" declared
       at line 1
static int *a = (int[]){1}; // GCC issues an error diagnostic for this
            ^
"/tmp/bug.c", line 4: error: expression must be a constant
static int *a = (int[]){1}; // GCC issues an error diagnostic for this
                ^
"/tmp/bug.c", line 3: warning: variable "t" declared but not used
static int *t = &tmp;        // Both GCC and EDG issue an error
            ^
"/tmp/bug.c", line 4: warning: variable "a" declared but not used
static int *a = (int[]){1}; // GCC issues an error diagnostic for this
            ^

2 errors found compiling "/tmp/bug.c".

Thanks for the test case; a nasty bug indeed!  I don't think clang
needs an extension to accept it.

Neil.



More information about the cfe-dev mailing list