[cfe-dev] [PATCH] attempt for bug 6904 - clang allows redefinition of static const member

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Aug 24 14:50:13 PDT 2010


Hi Faisal,

Just FYI, there's another accepting-invalid-code bug regarding static data members, that should also be easy to fix, if you are interested:
http://llvm.org/PR7970

-Argiris

On Aug 23, 2010, at 6:48 PM, Faisal Vali wrote:

> On Mon, Aug 23, 2010 at 9:23 AM, Douglas Gregor <dgregor at apple.com> wrote:
>> 
>> On Aug 23, 2010, at 4:42 AM, Faisal Vali wrote:
>> 
>> 
>> Index: SemaDecl.cpp
>> 
>> ===================================================================
>> --- SemaDecl.cpp        (revision 111791)
>> +++ SemaDecl.cpp        (working copy)
>> @@ -4079,6 +4079,25 @@
>>     return;
>>   }
>> 
>> +  // FV ADDED 8/22/10
>> +  // BUG Report  Bug 6904, Bug 7954 (duplicate)
>> +  // Check to see if this VDecl has been initialized before
>> +  // This should really only happen if this is a const static class variable of int or enum type
>> +  // and was initialized in the class and now is being re-initialized
>> +  // i.e. struct S { static const int i = 5; }; const int S::i = 2; BAD!!
>> +  VarDecl *First = VDecl->getFirstDeclaration();
>> +  for (VarDecl::redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
>> +         I != E; ++I) {
>> +    if ((*I)->hasInit())
>> +    {
>> +      Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName();
>> +      Diag((*I)->getLocation(), diag::note_previous_definition);
>> +      VDecl->setInvalidDecl();
>> +      return;
>> +    }
>> +  }
>> 
>> Why walk the redeclaration chain here, rather than just using VarDecl::getAnyInitializer?
>> 
> 
> Aah - thanks for the tip - yes that would be clearer.  I was avoiding
> getPreviousDeclaration for fear of missing some corner case that might
> lead to multiple previous declarations of such variables - but
> getAnyInitializer should be robust enough.
> 
> Thanks - I will post a patch in the evening to this group - unless you
> want me to post it to cfe-commit.
> 
> regards,
> 
> -- 
> Faisal Vali
> 
> _______________________________________________
> 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