[cfe-commits] r63242 - in /cfe/trunk: Driver/RewriteObjC.cpp include/clang/AST/Decl.h include/clang/AST/Expr.h include/clang/AST/ExprCXX.h include/clang/Basic/DiagnosticSemaKinds.def lib/AST/Expr.cpp lib/AST/StmtPrinter.cpp lib/CodeGen/CGExprAgg.

Douglas Gregor dgregor at apple.com
Wed Jan 28 16:08:06 PST 2009


On Jan 28, 2009, at 3:58 PM, Eli Friedman wrote:

> On Wed, Jan 28, 2009 at 3:46 PM, Douglas Gregor <dgregor at apple.com>  
> wrote:
>>
>> On Jan 28, 2009, at 3:42 PM, Eli Friedman wrote:
>>
>>> On Wed, Jan 28, 2009 at 3:37 PM, Douglas Gregor  
>>> <dgregor at apple.com> wrote:
>>>>>
>>>>> struct X { int a, b; };
>>>>> struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
>>>>>
>>>>> In gnu89 mode in gcc, the init list is actually equivalent to "{1,
>>>>> 3}".  clang, on the other hand, will give different results  
>>>>> (although
>>>>> it will also warn, so it might not be so bad).
>>>>
>>>> since both Clang and GCC
>>>> get the answer { 0, 3} in their default invocation modes
>>>
>>> gcc gets {1, 3}.  But yeah, it's probably not a big deal.
>>
>> Which version of GCC are you using? With Apple GCC 4.0 and 4.2,  
>> compiling
>> that code with no extra options results in {0, 3}.
>
> eli at eli-laptop:~$ gcc -x c - -o - -S
> struct X { int a, b; };
> struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
> 	.file	""
> .globl xs
> 	.data
> 	.align 4
> 	.type	xs, @object
> 	.size	xs, 8
> xs:
> 	.long	1
> 	.long	3
> 	.ident	"GCC: (Ubuntu 4.3.2-1ubuntu11) 4.3.2"
> 	.section	.note.GNU-stack,"", at progbits

Oh, how fun! It depends on whether the initializer is being treated as  
a constant initializer. Given this:

struct X { int a, b; };
struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };

int main() {
   struct X xsl[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
   fprintf(stderr, "{ %d, %d }\n", xs[0].a, xs[0].b);
   fprintf(stderr, "{ %d, %d }\n", xsl[0].a, xsl[0].b);
}

GCC gives the output:

{ 1, 3 }
{ 0, 3 }

In C99 mode, GCC (correctly) rejects the initializer for "xs" because  
it is non-constant.

   - Doug



More information about the cfe-commits mailing list