]> granicus.if.org Git - clang/commitdiff
Add redeclaration checking for static data members and fix a corner
authorDouglas Gregor <dgregor@apple.com>
Mon, 30 Aug 2010 14:32:14 +0000 (14:32 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 30 Aug 2010 14:32:14 +0000 (14:32 +0000)
case with redeclaration checking for fields, from Faisal Vali!
Fixes PR7970.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112476 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/CXX/class/class.mem/p1.cpp [new file with mode: 0644]

index 6f8d9a92797afdc5101c8372fcc17d005ca313b2..cd64175a51271e7a940f82acf4975c1c2e15f764 100644 (file)
@@ -1468,6 +1468,17 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     return New->setInvalidDecl();
   }
 
+  // C++ [class.mem]p1:
+  //   A member shall not be declared twice in the member-specification [...]
+  // 
+  // Here, we need only consider static data members.
+  if (Old->isStaticDataMember() && !New->isOutOfLine()) {
+    Diag(New->getLocation(), diag::err_duplicate_member) 
+      << New->getIdentifier();
+    Diag(Old->getLocation(), diag::note_previous_declaration);
+    New->setInvalidDecl();
+  }
+  
   MergeDeclAttributes(New, Old, Context);
 
   // Merge the types
@@ -5972,9 +5983,17 @@ FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
 
   if (D.getDeclSpec().isThreadSpecified())
     Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
-
-  NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName,
-                                         ForRedeclaration);
+  
+  // Check to see if this name was declared as a member previously
+  LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration);
+  LookupName(Previous, S);
+  assert((Previous.empty() || Previous.isOverloadedResult() || 
+          Previous.isSingleResult()) 
+    && "Lookup of member name should be either overloaded, single or null");
+
+  // If the name is overloaded then get any declaration else get the single result
+  NamedDecl *PrevDecl = Previous.isOverloadedResult() ?
+    Previous.getRepresentativeDecl() : Previous.getAsSingle<NamedDecl>();
 
   if (PrevDecl && PrevDecl->isTemplateParameter()) {
     // Maybe we will complain about the shadowed template parameter.
diff --git a/test/CXX/class/class.mem/p1.cpp b/test/CXX/class/class.mem/p1.cpp
new file mode 100644 (file)
index 0000000..55507d4
--- /dev/null
@@ -0,0 +1,64 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s 
+
+struct S 
+{
+  static int v1; // expected-note{{previous declaration is here}}
+  int v1; //expected-error{{duplicate member 'v1'}}
+  int v;  //expected-note 2{{previous definition is here}} \
+          // expected-note{{previous declaration is here}}
+  static int v; //expected-error{{redefinition of 'v' as different kind of symbol}}
+  int v; //expected-error{{duplicate member 'v'}}
+  static int v; //expected-error{{redefinition of 'v' as different kind of symbol}}
+  enum EnumT { E = 10 };
+  friend struct M;
+  struct X;  //expected-note{{forward declaration of 'S::X'}}
+  friend struct X;
+};
+
+S::EnumT Evar = S::E; // ok
+S::EnumT Evar2 = EnumT(); //expected-error{{use of undeclared identifier 'EnumT'}}
+S::M m; //expected-error{{no type named 'M' in 'S'}}
+S::X x; //expected-error{{variable has incomplete type 'S::X'}}
+
+
+struct S2 
+{
+  static int v2; // expected-note{{previous declaration is here}}
+  static int v2; //expected-error{{duplicate member 'v2'}}
+};
+
+struct S3
+{
+  static int v3;
+  struct S4
+  {
+    static int v3;
+  };
+};
+
+struct S4
+{
+  static int v4;
+};
+
+int S4::v4; //expected-note{{previous definition is here}}
+int S4::v4; //expected-error{{redefinition of 'v4'}}
+
+struct S5
+{
+  static int v5; //expected-note{{previous definition is here}}
+  void v5() { } //expected-error{{redefinition of 'v5' as different kind of symbol}}
+  
+  void v6() { } //expected-note{{previous definition is here}}
+  static int v6; //expected-error{{redefinition of 'v6' as different kind of symbol}}
+  
+  void v7() { }
+  void v7(int) { } //expected-note{{previous definition is here}}
+  static int v7;  //expected-error{{redefinition of 'v7' as different kind of symbol}}
+  
+  void v8();
+  int v8(int); //expected-note{{previous declaration is here}}
+  int v8; //expected-error{{duplicate member 'v8'}}
+  
+  
+};