Diagnose the presence of multiple initializations of static data
members, from Faisal Vali! Fixes PR6904.
llvm-svn: 111900
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b2c3191..0ed9391 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4077,6 +4077,8 @@
return;
}
+
+
// A definition must end up with a complete type, which means it must be
// complete with the restriction that an array type might be completed by the
// initializer; note that later code assumes this restriction.
@@ -4103,6 +4105,25 @@
VDecl->setInvalidDecl();
return;
}
+
+ // C++ [class.static.data]p4
+ // If a static data member is of const integral or const
+ // enumeration type, its declaration in the class definition can
+ // specify a constant-initializer which shall be an integral
+ // constant expression (5.19). In that case, the member can appear
+ // in integral constant expressions. The member shall still be
+ // defined in a namespace scope if it is used in the program and the
+ // namespace scope definition shall not contain an initializer.
+ //
+ // We already performed a redefinition check above, but for static
+ // data members we also need to check whether there was an in-class
+ // declaration with an initializer.
+ const VarDecl* PrevInit = 0;
+ if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) {
+ Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName();
+ Diag(PrevInit->getLocation(), diag::note_previous_definition);
+ return;
+ }
if (getLangOptions().CPlusPlus && VDecl->hasLocalStorage())
setFunctionHasBranchProtectedScope();