[cfe-commits] r155269 - in /cfe/trunk: lib/Sema/SemaTemplate.cpp test/SemaTemplate/class-template-decl.cpp

Richard Smith richard-llvm at metafoo.co.uk
Fri Apr 20 18:27:54 PDT 2012


Author: rsmith
Date: Fri Apr 20 20:27:54 2012
New Revision: 155269

URL: http://llvm.org/viewvc/llvm-project?rev=155269&view=rev
Log:
When declaring a template, check that the context doesn't already contain a
declaration of the same name. r155187 caused us to miss this if the prior
declaration did not declare a type.

Modified:
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/SemaTemplate/class-template-decl.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=155269&r1=155268&r2=155269&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Apr 20 20:27:54 2012
@@ -865,9 +865,13 @@
     return true;
   }
 
-  // Find any previous declaration with this name.
+  // Find any previous declaration with this name. For a friend with no
+  // scope explicitly specified, we only look for tag declarations (per
+  // C++11 [basic.lookup.elab]p2).
   DeclContext *SemanticContext;
-  LookupResult Previous(*this, Name, NameLoc, LookupTagName,
+  LookupResult Previous(*this, Name, NameLoc,
+                        (SS.isEmpty() && TUK == TUK_Friend)
+                          ? LookupTagName : LookupOrdinaryName,
                         ForRedeclaration);
   if (SS.isNotEmpty() && !SS.isInvalid()) {
     SemanticContext = computeDeclContext(SS, true);
@@ -893,7 +897,7 @@
         Invalid = true;
     } else if (TUK != TUK_Friend && TUK != TUK_Reference)
       diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc);
-        
+
     LookupQualifiedName(Previous, SemanticContext);
   } else {
     SemanticContext = CurContext;
@@ -948,6 +952,21 @@
         // declaration.
         PrevDecl = PrevClassTemplate = 0;
         SemanticContext = OutermostContext;
+
+        // Check that the chosen semantic context doesn't already contain a
+        // declaration of this name as a non-tag type.
+        LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
+                              ForRedeclaration);
+        DeclContext *LookupContext = SemanticContext;
+        while (LookupContext->isTransparentContext())
+          LookupContext = LookupContext->getLookupParent();
+        LookupQualifiedName(Previous, LookupContext);
+
+        if (Previous.isAmbiguous())
+          return true;
+
+        if (Previous.begin() != Previous.end())
+          PrevDecl = (*Previous.begin())->getUnderlyingDecl();
       }
     }
 

Modified: cfe/trunk/test/SemaTemplate/class-template-decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/class-template-decl.cpp?rev=155269&r1=155268&r2=155269&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/class-template-decl.cpp (original)
+++ cfe/trunk/test/SemaTemplate/class-template-decl.cpp Fri Apr 20 20:27:54 2012
@@ -94,3 +94,46 @@
     };
   };
 }
+
+namespace redecl {
+  int A; // expected-note {{here}}
+  template<typename T> struct A; // expected-error {{different kind of symbol}}
+
+  int B;
+  template<typename T> struct B { // expected-error {{different kind of symbol}}
+  };
+
+  template<typename T> struct F;
+  template<typename T> struct K;
+
+  int G, H;
+
+  struct S {
+    int C; // expected-note {{here}}
+    template<typename T> struct C; // expected-error {{different kind of symbol}}
+
+    int D;
+    template<typename T> struct D { // expected-error {{different kind of symbol}}
+    };
+
+    int E;
+    template<typename T> friend struct E { // expected-error {{cannot define a type in a friend}}
+    };
+
+    int F;
+    template<typename T> friend struct F; // ok, redecl::F
+
+    template<typename T> struct G; // ok
+
+    template<typename T> friend struct H; // expected-error {{different kind of symbol}}
+
+    int I, J, K;
+
+    struct U {
+      template<typename T> struct I; // ok
+      template<typename T> struct J { // ok
+      };
+      template<typename T> friend struct K; // ok, redecl::K
+    };
+  };
+}





More information about the cfe-commits mailing list