]> granicus.if.org Git - clang/commitdiff
Fix various problems with matching out-of-line definitions of static
authorDouglas Gregor <dgregor@apple.com>
Wed, 11 Mar 2009 23:52:16 +0000 (23:52 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 11 Mar 2009 23:52:16 +0000 (23:52 +0000)
class members to the corresponding in-class declaration.

Diagnose the erroneous use of 'static' on out-of-line definitions of
class members.

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

include/clang/Basic/DiagnosticSemaKinds.def
lib/Sema/SemaDecl.cpp
test/SemaCXX/nested-name-spec.cpp
test/SemaTemplate/nested-name-spec-template.cpp

index ddf70e6a192566a4b9c162ba02b901394a0ecb7c..8408565ce04f77538f61a822e1e9cc6ebcbc0858 100644 (file)
@@ -252,6 +252,8 @@ DIAG(err_virtual_out_of_class, ERROR,
      "'virtual' can only be specified inside the class definition")
 DIAG(err_static_not_bitfield, ERROR,
      "static member %0 cannot be a bit-field")
+DIAG(err_static_out_of_line, ERROR,
+     "'static' can not be specified on an out-of-line static member definition")
 DIAG(err_typedef_not_bitfield, ERROR,
      "typedef member %0 cannot be a bit-field")
 DIAG(err_not_integral_type_bitfield, ERROR,
index 629e4237e50e3ec6bd208f8610a44a442a5a7378..7d3d1c68f2f12c66d6f620e9e1df728165104a25 100644 (file)
@@ -581,7 +581,9 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
 
     const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
     const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
-    if (OldMethod && NewMethod) {
+    if (OldMethod && NewMethod && 
+        OldMethod->getLexicalDeclContext() == 
+          NewMethod->getLexicalDeclContext()) {
       //    -- Member function declarations with the same name and the 
       //       same parameter types cannot be overloaded if any of them 
       //       is a static member function declaration.
@@ -595,21 +597,18 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
       //   [...] A member shall not be declared twice in the
       //   member-specification, except that a nested class or member
       //   class template can be declared and then later defined.
-      if (OldMethod->getLexicalDeclContext() == 
-            NewMethod->getLexicalDeclContext()) {
-        unsigned NewDiag;
-        if (isa<CXXConstructorDecl>(OldMethod))
-          NewDiag = diag::err_constructor_redeclared;
-        else if (isa<CXXDestructorDecl>(NewMethod))
-          NewDiag = diag::err_destructor_redeclared;
-        else if (isa<CXXConversionDecl>(NewMethod))
-          NewDiag = diag::err_conv_function_redeclared;
-        else
-          NewDiag = diag::err_member_redeclared;
-
-        Diag(New->getLocation(), NewDiag);
-        Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
-      }
+      unsigned NewDiag;
+      if (isa<CXXConstructorDecl>(OldMethod))
+        NewDiag = diag::err_constructor_redeclared;
+      else if (isa<CXXDestructorDecl>(NewMethod))
+        NewDiag = diag::err_destructor_redeclared;
+      else if (isa<CXXConversionDecl>(NewMethod))
+        NewDiag = diag::err_conv_function_redeclared;
+      else
+        NewDiag = diag::err_member_redeclared;
+      
+      Diag(New->getLocation(), NewDiag);
+      Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
     }
 
     // (C++98 8.3.5p3):
@@ -805,6 +804,7 @@ bool Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
     return true;
   }
   New->setType(MergedT);
+
   // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
   if (New->getStorageClass() == VarDecl::Static &&
       (Old->getStorageClass() == VarDecl::None ||
@@ -821,7 +821,10 @@ bool Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
     return true;
   }
   // Variables with external linkage are analyzed in FinalizeDeclaratorGroup.
-  if (New->getStorageClass() != VarDecl::Extern && !New->isFileVarDecl()) {
+  if (New->getStorageClass() != VarDecl::Extern && !New->isFileVarDecl() &&
+      // Don't complain about out-of-line definitions of static members.
+      !(Old->getLexicalDeclContext()->isRecord() &&
+        !New->getLexicalDeclContext()->isRecord())) {
     Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName();
     Diag(Old->getLocation(), diag::note_previous_definition);
     return true;
@@ -1331,10 +1334,6 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl,
   if (New == 0)
     return 0;
   
-  // Set the lexical context. If the declarator has a C++ scope specifier, the
-  // lexical context will be different from the semantic context.
-  New->setLexicalDeclContext(CurContext);
-
   // If this has an identifier and is not an invalid redeclaration,
   // add it to the scope stack.
   if (Name && !(Redeclaration && InvalidDecl))
@@ -1598,6 +1597,16 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
       InvalidDecl = true;
     }
   }
+  if (DC->isRecord() && !CurContext->isRecord()) {
+    // This is an out-of-line definition of a static data member.
+    if (SC == VarDecl::Static) {
+      Diag(D.getDeclSpec().getStorageClassSpecLoc(), 
+           diag::err_static_out_of_line)
+        << CodeModificationHint::CreateRemoval(
+                       SourceRange(D.getDeclSpec().getStorageClassSpecLoc()));
+    } else if (SC == VarDecl::None)
+      SC = VarDecl::Static;
+  }
   NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), 
                           II, R, SC, 
                           // FIXME: Move to DeclGroup...
@@ -1605,6 +1614,10 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
   NewVD->setThreadSpecified(ThreadSpecified);
   NewVD->setNextDeclarator(LastDeclarator);
 
+  // Set the lexical context. If the declarator has a C++ scope specifier, the
+  // lexical context will be different from the semantic context.
+  NewVD->setLexicalDeclContext(CurContext);
+
   // Handle attributes prior to checking for duplicates in MergeVarDecl
   ProcessDeclAttributes(NewVD, D);
 
@@ -1697,13 +1710,11 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
     Redeclaration = true;
     if (MergeVarDecl(NewVD, PrevDecl))
       InvalidDecl = true;
-
-    if (D.getCXXScopeSpec().isSet()) {
-      // No previous declaration in the qualifying scope.
-      Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member)
-        << Name << D.getCXXScopeSpec().getRange();
-      InvalidDecl = true;
-    }
+  } else if (D.getCXXScopeSpec().isSet()) {
+    // No previous declaration in the qualifying scope.
+    Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member)
+      << Name << D.getCXXScopeSpec().getRange();
+    InvalidDecl = true;
   }
 
   if (!InvalidDecl && R->isVoidType() && !NewVD->hasExternalStorage()) {
@@ -1743,7 +1754,7 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
   case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
   case DeclSpec::SCS_extern:      SC = FunctionDecl::Extern; break;
   case DeclSpec::SCS_static: {
-    if (DC->getLookupContext()->isFunctionOrMethod()) {
+    if (CurContext->getLookupContext()->isFunctionOrMethod()) {
       // C99 6.7.1p5:
       //   The declaration of an identifier for a function that has
       //   block scope shall have no explicit storage-class specifier
@@ -1879,6 +1890,21 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
     }
   }
 
+  if (SC == FunctionDecl::Static && isa<CXXMethodDecl>(NewFD) && 
+      !CurContext->isRecord()) {
+    // C++ [class.static]p1:
+    //   A data or function member of a class may be declared static
+    //   in a class definition, in which case it is a static member of
+    //   the class.
+
+    // Complain about the 'static' specifier if it's on an out-of-line
+    // member function definition.
+    Diag(D.getDeclSpec().getStorageClassSpecLoc(), 
+         diag::err_static_out_of_line)
+      << CodeModificationHint::CreateRemoval(
+                      SourceRange(D.getDeclSpec().getStorageClassSpecLoc()));
+  }
+
   // Handle GNU asm-label extension (encoded as an attribute).
   if (Expr *E = (Expr*) D.getAsmLabel()) {
     // The parser guarantees this is a string.
index f75b279a44933a839166141644ea3da3cfe7acc3..eaca56c72528a42ae437ad35d31f76d9f5cc59f8 100644 (file)
@@ -2,6 +2,11 @@
 namespace A {
   struct C {
     static int cx;
+
+    static int cx2;
+
+    static int Ag1();
+    static int Ag2();
   };
   int ax;
   void Af();
@@ -11,6 +16,15 @@ A:: ; // expected-error {{expected unqualified-id}}
 ::A::ax::undef ex3; // expected-error {{expected a class or namespace}} expected-error {{invalid token after top level declarator}}
 A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}} expected-error {{invalid token after top level declarator}}
 
+int A::C::Ag1() { return 0; }
+
+static int A::C::Ag2() { return 0; } // expected-error{{'static' can not be specified on an out-of-line static member}}
+
+int A::C::cx = 17;
+
+
+static int A::C::cx2 = 17; // expected-error{{'static' can not be specified on an out-of-line static member}}
+
 class C2 {
   void m(); // expected-note{{member declaration nearly matches}}
 
index 4007f232c458efdfff394f5c07132aac57b91eef..8db2bc34296e2be0daf521ff01ff4b4a4c1e970f 100644 (file)
@@ -33,8 +33,7 @@ N::M::template; // expected-error{{expected template name after 'template' keywo
 
 N::M::template Promote; // expected-error{{expected '<' after 'template Promote' in nested name specifier}} \
 // expected-error{{C++ requires a type specifier for all declarations}} \
-// expected-error{{redefinition of 'Promote' as different kind of symbol}} \
-// expected-error{{no member named 'Promote'}}
+// expected-error{{redefinition of 'Promote' as different kind of symbol}}
 
 namespace N {
   template<typename T> struct A;