]> granicus.if.org Git - clang/commitdiff
Don't instantiate members not belonging in the semantic context of the template.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 4 Nov 2010 03:18:57 +0000 (03:18 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 4 Nov 2010 03:18:57 +0000 (03:18 +0000)
e.g. for:

    template <int i> class A {
      class B *g;
    };

'class B' has the template as lexical context but semantically it is
introduced in namespace scope.

Fixes rdar://8611125 & http://llvm.org/PR8505

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

lib/Sema/SemaTemplateInstantiate.cpp
test/CodeGenCXX/template-instantiation.cpp

index 71235d92851eac89e00133488747b52e7a518eb0..f78fe81aa8b0f83bcc12396ae2582066d506d931 100644 (file)
@@ -1223,6 +1223,18 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation,
   for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
          MemberEnd = Pattern->decls_end();
        Member != MemberEnd; ++Member) {
+    // Don't instantiate members not belonging in this semantic context.
+    // e.g. for:
+    // @code
+    //    template <int i> class A {
+    //      class B *g;
+    //    };
+    // @endcode
+    // 'class B' has the template as lexical context but semantically it is
+    // introduced in namespace scope.
+    if ((*Member)->getDeclContext() != Pattern)
+      continue;
+
     Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
     if (NewMember) {
       if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
index a8729035e286755edef60b77cd2b326be3e9e76f..0df2e23c70cefe6df51127fb710964ccbe920504 100644 (file)
@@ -109,3 +109,16 @@ namespace test4 {
     A<int>::foo();
   }
 }
+
+namespace PR8505 {
+// Hits an assertion due to bogus instantiation of class B.
+template <int i> class A {
+  class B* g;
+};
+class B {
+  void f () {}
+};
+// Should not instantiate class B since it is introduced in namespace scope.
+// CHECK-NOT: _ZN6PR85051AILi0EE1B1fEv
+template class A<0>;
+}