]> granicus.if.org Git - clang/commitdiff
Diagnose class members that shadow a template parameter. Fixes
authorDouglas Gregor <dgregor@apple.com>
Wed, 17 Jun 2009 23:37:01 +0000 (23:37 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 17 Jun 2009 23:37:01 +0000 (23:37 +0000)
<rdar://problem/6952203>.

To do this, we actually remove a not-quite-correct optimization in the
C++ name lookup routines. We'll revisit this optimization for the
general case once more C++ is working.

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

lib/Sema/SemaDecl.cpp
lib/Sema/SemaLookup.cpp
lib/Sema/SemaTemplate.cpp
test/Parser/cxx-template-decl.cpp
test/SemaTemplate/nested-template.cpp

index 3c6f706f1586f65127910e3061165ae55aca68f6..77460359f4988b9e9a76424c7c30bed5f7be6946 100644 (file)
@@ -3807,6 +3807,14 @@ FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
     Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
 
   NamedDecl *PrevDecl = LookupName(S, II, LookupMemberName, true);
+
+  if (PrevDecl && PrevDecl->isTemplateParameter()) {
+    // Maybe we will complain about the shadowed template parameter.
+    DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
   if (PrevDecl && !isDeclInScope(PrevDecl, Record, S))
     PrevDecl = 0;
 
index 1d26845fd8e41a5c9fd96cf8fc137b9d0d6bfa8f..f29cd17481a7b462d648f7f4c1513389f006880c 100644 (file)
@@ -685,7 +685,7 @@ Sema::CppLookupName(Scope *S, DeclarationName Name,
       // identifier chain.
       if (isa<RecordDecl>(Ctx)) {
         R = LookupQualifiedName(Ctx, Name, NameKind, RedeclarationOnly);
-        if (R || RedeclarationOnly)
+        if (R)
           return std::make_pair(true, R);
       }
       if (Ctx->getParent() != Ctx->getLexicalParent() 
@@ -697,7 +697,7 @@ Sema::CppLookupName(Scope *S, DeclarationName Name,
         for (OutOfLineCtx = Ctx; OutOfLineCtx && !OutOfLineCtx->isFileContext();
              OutOfLineCtx = OutOfLineCtx->getParent()) {
           R = LookupQualifiedName(OutOfLineCtx, Name, NameKind, RedeclarationOnly);
-          if (R || RedeclarationOnly)
+          if (R)
             return std::make_pair(true, R);
         }
       }
index 0ae9f1d0771c9873a1537259a6ae5e5fcf95c36f..fb41b2b7536f22ad489fdfedd7cd2d3e1e9c05be 100644 (file)
@@ -444,6 +444,9 @@ Sema::ActOnClassTemplate(Scope *S, unsigned TagSpec, TagKind TK,
   if (Previous.begin() != Previous.end())
     PrevDecl = *Previous.begin();
 
+  if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S))
+    PrevDecl = 0;
+  
   DeclContext *SemanticContext = CurContext;
   if (SS.isNotEmpty() && !SS.isInvalid()) {
     SemanticContext = computeDeclContext(SS);
index ae5d8f9e0cce5bb1f088033881b07744f8b34bab..75b26a98e44ec8a2588e7544c826c5f3b6ac9d51 100644 (file)
@@ -66,6 +66,17 @@ class T { // expected-error{{declaration of 'T' shadows template parameter}}
 template<int Size> // expected-note{{template parameter is declared here}}
 void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
 
+// <rdar://problem/6952203>
+template<typename T> // expected-note{{here}}
+struct shadow4 {
+  int T; // expected-error{{shadows}}
+};
+
+template<typename T> // expected-note{{here}}
+struct shadow5 {
+  int T(int, float); // expected-error{{shadows}}
+};
+
 // Non-type template parameters in scope
 template<int Size> 
 void f(int& i) {
index bd9e89f76a0a4e39b1e34568f4b2f7ba3b2982c0..84b1d35ba91db9a1441154e15d3bd484c8bb70e0 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: clang-cc -fsyntax-only %s
+// RUN: clang-cc -fsyntax-only -verify %s
 
 class A;