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

Eli Friedman eli.friedman at gmail.com
Fri Jan 11 12:29:40 PST 2008


Take the following program:

#include "stdio.h"
void a(void) {
static int *a = (int[]){1};
printf("Part a: %d", *a);
*a = 2;

int *b = (int[]){1};
printf("Part b: %d", *b);
*b = 2;
}

int main(void) {
a();
a();
return 0;
}

What does your frontend print out?   (i.e. what is the storage
duration of each of the compound literals?)

-Eli

On Jan 11, 2008 8:21 AM, Neil Booth <neil at daikokuya.co.uk> wrote:
> Steve Naroff wrote:-
>
>
> > Folks,
> >
> > Eli and I have been on improving clang's support for Compound Literals.
> >
> > GCC and EDG produce different diagnostics for the following program.
> >
> > void a(void) {
> > int tmp;
> > static int *t = &tmp;        // Both GCC and EDG issue an error
> > diagnostic ("initializer element is not constant").
> > static int *a = (int[]){1}; // GCC issues an error diagnostic for this
> > as well (but EDG allows it).
> > }
> >
> > C99 6.5.2.5p6 says the following:
> >
> > The value of the compound literal is that of an unnamed object
> > initialized by the
> > initializer list. If the compound literal occurs outside the body of a
> > function, the object
> > has static storage duration; otherwise, it has automatic storage
> > duration associated with
> > the enclosing block.
> >
> > This seems pretty clear to me...GCC is right and EDG is wrong.
> >
> > Anyone disagree?
>
> I'm curious about your reasoning.  My front end, which is pretty
> good about most things but a tad weak on compuond literals emits:
>
> neil at duron:~/src/c$ ../cfe/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 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
>             ^
>
> 1 error found compiling "/tmp/bug.c".
>
> Which agrees with GCC and seems pretty reasonable to me :)
>
> Neil.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>



More information about the cfe-dev mailing list