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

Faisal Vali faisalv at yahoo.com
Sun Aug 22 14:45:26 PDT 2010


Hello All :)

This is my first attempt at patching Clang.

This patch is supposed to address the following bug (described as
'easy to fix' by CL in his comment):
http://llvm.org/bugs/show_bug.cgi?id=6904#c2

Essentially clang allows the following redefinition, and it shouldn't :
struct S { static const int i = 0; }; const int S::i = 5;

I am a simple hobbyist, and would appreciate it if someone could
critique or guide me through some of this.

My approach was the following:

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.


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()?

Any thoughts or feedback will be appreciated.

Thanks!
Faisal Vali.



More information about the cfe-dev mailing list