From 234ff47366f6ae6acdd22b7b83ce91044561da60 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sat, 23 Aug 2014 00:49:01 +0000 Subject: [PATCH] [modules] Put class template declarations into the scope in which they're declared, rather than putting them into the template parameter scope. We previously had *no record* in the scope for class template declarations, once those declarations completed and their template parameter scopes were popped. This in turn caused us to be unable to merge class template declarations that were declared in the global scope (where we use scope lookup rather than DeclContext lookup for merging), when loading a module. llvm-svn: 216311 --- clang/lib/Sema/SemaTemplate.cpp | 10 +++++++--- clang/test/Modules/Inputs/cxx-templates-common.h | 2 ++ .../test/Modules/Inputs/cxx-templates-textual.h | 2 ++ clang/test/Modules/cxx-templates.cpp | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 clang/test/Modules/Inputs/cxx-templates-textual.h diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 63581a44dbe5..fb05718ff1b5 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -1105,9 +1105,13 @@ Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, AddPushedVisibilityAttribute(NewClass); - if (TUK != TUK_Friend) - PushOnScopeChains(NewTemplate, S); - else { + if (TUK != TUK_Friend) { + // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. + Scope *Outer = S; + while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) + Outer = Outer->getParent(); + PushOnScopeChains(NewTemplate, Outer); + } else { if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { NewTemplate->setAccess(PrevClassTemplate->getAccess()); NewClass->setAccess(PrevClassTemplate->getAccess()); diff --git a/clang/test/Modules/Inputs/cxx-templates-common.h b/clang/test/Modules/Inputs/cxx-templates-common.h index f7c083249f8a..f3c902230911 100644 --- a/clang/test/Modules/Inputs/cxx-templates-common.h +++ b/clang/test/Modules/Inputs/cxx-templates-common.h @@ -40,3 +40,5 @@ template struct WithExplicitSpecialization; typedef WithExplicitSpecialization WithExplicitSpecializationUse; template struct WithImplicitSpecialMembers { int n; }; + +#include "cxx-templates-textual.h" diff --git a/clang/test/Modules/Inputs/cxx-templates-textual.h b/clang/test/Modules/Inputs/cxx-templates-textual.h new file mode 100644 index 000000000000..8bffb8eddc28 --- /dev/null +++ b/clang/test/Modules/Inputs/cxx-templates-textual.h @@ -0,0 +1,2 @@ +template struct MergeClassTemplateSpecializations_basic_string {}; +typedef MergeClassTemplateSpecializations_basic_string MergeClassTemplateSpecializations_string; diff --git a/clang/test/Modules/cxx-templates.cpp b/clang/test/Modules/cxx-templates.cpp index c173d670802f..fedaa03ef4fc 100644 --- a/clang/test/Modules/cxx-templates.cpp +++ b/clang/test/Modules/cxx-templates.cpp @@ -3,6 +3,11 @@ // RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump-lookups -ast-dump-filter N | FileCheck %s --check-prefix=CHECK-NAMESPACE-N // RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump | FileCheck %s --check-prefix=CHECK-DUMP // RUN: %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11 +// RUN: %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11 -DEARLY_IMPORT + +#ifdef EARLY_IMPORT +#include "cxx-templates-textual.h" +#endif @import cxx_templates_a; @import cxx_templates_b; @@ -22,8 +27,14 @@ void g() { f(1.0); f(); f(); // expected-error {{no matching function}} +#ifdef EARLY_IMPORT + // FIXME: The textual inclusion above shouldn't affect this! + // expected-note@Inputs/cxx-templates-a.h:3 {{couldn't infer template argument}} + // expected-note@Inputs/cxx-templates-a.h:4 {{requires 1 argument}} +#else // expected-note@Inputs/cxx-templates-b.h:3 {{couldn't infer template argument}} // expected-note@Inputs/cxx-templates-b.h:4 {{requires single argument}} +#endif N::f(0); N::f(1.0); @@ -108,11 +119,15 @@ void g() { int *r = PartiallyInstantiatePartialSpec::bar(); (void)&WithImplicitSpecialMembers::n; + + MergeClassTemplateSpecializations_string s; } static_assert(Outer::Inner::f() == 1, ""); static_assert(Outer::Inner::g() == 2, ""); +#ifndef EARLY_IMPORT +// FIXME: The textual inclusion above shouldn't cause this to fail! static_assert(MergeTemplateDefinitions::f() == 1, ""); static_assert(MergeTemplateDefinitions::g() == 2, ""); @@ -126,6 +141,7 @@ MergeSpecializations::partially_specialized_in_c spec_in_c_1; MergeSpecializations::explicitly_specialized_in_a spec_in_a_2; MergeSpecializations::explicitly_specialized_in_b spec_in_b_2; MergeSpecializations::explicitly_specialized_in_c spec_in_c_2; +#endif @import cxx_templates_common;