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

Douglas Gregor dgregor at apple.com
Mon Aug 23 07:23:29 PDT 2010


On Aug 23, 2010, at 4:42 AM, Faisal Vali wrote:

> On Mon, Aug 23, 2010 at 6:23 AM, Jan Bierbaum
> <s3306700 at inf.tu-dresden.de> wrote:
>> 
>> I'm not familiar with this part of Clang myself, so I cannot say
>> anything about your changes, but you really should provide them as a
>> diff [1] which is much easier to follow and to apply to the local
>> sources. Also, this is common practice and I don't think the main
>> developers will react to your postings otherwise ;-)
>> 
>> [1] http://clang.llvm.org/hacking.html#patches
> 
> Thanks for the tip - i have attached a patch file (using tortoiseSVN).

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?

	- Doug



More information about the cfe-dev mailing list