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

Faisal Vali faisalv at yahoo.com
Sun Aug 22 16:07:17 PDT 2010


On Sun, Aug 22, 2010 at 4:45 PM, Faisal Vali <faisalv at yahoo.com> wrote:
>
> Essentially clang allows the following redefinition, and it shouldn't :
> struct S { static const int i = 0; }; const int S::i = 5;
>
>
> In file 'SemaDecl.cpp' in the function
> void Sema::AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit)
>
> .. as soon as we get a valid VarDecl*, before we add an initializer to
> it, i simply iterate through all its prior declarations and if any of
> them has an initializer, i invalidate the current VDecl and return.
>

Apparently the above check does not catch 'const int S::i(0)' so it
needs to be added to the following function too

In file SemDeclCXX.cpp void Sema::AddCXXDirectInitializerToDecl(...)

This makes me wonder if there is a better place to add this code so as
to avoid repeating ourselves?
Also once list initialization comes into the picture, will we need
another check for that case?

Thanks!
Faisal Vali
Radiation Oncology
Loyola




>
> Here is the code:
> VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
> if (!VDecl) {
> ....
> }
> //----- My Simple Addition
>
>  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;
>    }
>  }
>
> //-----------------------------------------------------
>
>
> Here are some of my questions:
> 1) Is this the best place to insert this code?
> 2) Can I avoid having to use a loop and just query a previous
> declaration? Or is it more robust to loop through?
> i.e. VDecl->getPreviousDeclaration()->hasInit()?
>




More information about the cfe-dev mailing list