r186817 - Implement the part of C89 6.5.7 p3 requiring a constant initializer list

Eli Friedman eli.friedman at gmail.com
Mon Jul 22 14:20:46 PDT 2013


On Mon, Jul 22, 2013 at 2:19 PM, Eli Friedman <eli.friedman at gmail.com> wrote:
> On Mon, Jul 22, 2013 at 3:58 AM, Enea Zaffanella <zaffanella at cs.unipr.it> wrote:
>> Author: enea
>> Date: Mon Jul 22 05:58:26 2013
>> New Revision: 186817
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=186817&view=rev
>> Log:
>> Implement the part of C89 6.5.7 p3 requiring a constant initializer list
>> when initializing aggregate/union types, no matter if static or not.
>>
>> Modified:
>>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>>     cfe/trunk/lib/Sema/SemaDecl.cpp
>>     cfe/trunk/test/Sema/c89.c
>>
>> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=186817&r1=186816&r2=186817&view=diff
>> ==============================================================================
>> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
>> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jul 22 05:58:26 2013
>> @@ -3626,6 +3626,8 @@ def warn_typecheck_zero_static_array_siz
>>  def err_array_size_non_int : Error<"size of array has non-integer type %0">;
>>  def err_init_element_not_constant : Error<
>>    "initializer element is not a compile-time constant">;
>> +def ext_aggregate_init_not_constant : Extension<
>> +  "initializer for aggregate is not a compile-time constant">, InGroup<C99>;
>>  def err_local_cant_init : Error<
>>    "'__local' variable cannot have an initializer">;
>>  def err_block_extern_cant_init : Error<
>>
>> Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=186817&r1=186816&r2=186817&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
>> +++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Jul 22 05:58:26 2013
>> @@ -7777,9 +7777,19 @@ void Sema::AddInitializerToDecl(Decl *Re
>>      // C99 6.7.8p4: All the expressions in an initializer for an object that has
>>      // static storage duration shall be constant expressions or string literals.
>>      // C++ does not have this restriction.
>> -    if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl() &&
>> -        VDecl->getStorageClass() == SC_Static)
>> -      CheckForConstantInitializer(Init, DclT);
>> +    if (!getLangOpts().CPlusPlus && !VDecl->isInvalidDecl()) {
>> +      if (VDecl->getStorageClass() == SC_Static)
>> +        CheckForConstantInitializer(Init, DclT);
>> +      // C89 is stricter than C99 for non-static aggregate types.
>> +      // C89 6.5.7p3: All the expressions [...] in an initializer list
>> +      // for an object that has aggregate or union type shall be
>> +      // constant expressions.
>> +      else if (!getLangOpts().C99 && VDecl->getType()->isAggregateType() &&
>> +               !Init->isConstantInitializer(Context, false))
>> +        Diag(Init->getExprLoc(),
>> +             diag::ext_aggregate_init_not_constant)
>> +          << Init->getSourceRange();
>> +    }
>>    } else if (VDecl->isStaticDataMember() &&
>>               VDecl->getLexicalDeclContext()->isRecord()) {
>>      // This is an in-class initialization for a static data member, e.g.,
>
> Is it intentional that we warn on "struct A a; struct A b = a;"?  I'm
> not sure the wording of the standard supports that.

Nevermind, didn't see the later commit.

-Eli



More information about the cfe-commits mailing list