diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index bba72365089f9..c59d2b2639ead 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -1874,6 +1874,13 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl, LLVM_PREFERRED_TYPE(bool) unsigned StrictPackMatch : 1; + /// Indicate whether this template was instantiated, and instantiated locally. + /// Specifically, this is false if the template has not been instantiated, + /// or was intantiated externally and loaded via an AST file. This only tracks + /// class template specializations for proper handling of anonymous members. + LLVM_PREFERRED_TYPE(bool) + unsigned InstantiatedLocally : 1; + protected: ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, DeclContext *DC, SourceLocation StartLoc, @@ -1968,6 +1975,10 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl, void setStrictPackMatch(bool Val) { StrictPackMatch = Val; } + bool isInstantiatedLocally() { return InstantiatedLocally; } + + void setInstantiatedLocally() { InstantiatedLocally = true; } + /// Get the point of instantiation (if any), or null if none. SourceLocation getPointOfInstantiation() const { return PointOfInstantiation; diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp index 3162857aac5d0..e0fbca1d129f5 100644 --- a/clang/lib/AST/DeclTemplate.cpp +++ b/clang/lib/AST/DeclTemplate.cpp @@ -974,7 +974,8 @@ ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl( SpecializedTemplate->getIdentifier(), PrevDecl), SpecializedTemplate(SpecializedTemplate), TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), - SpecializationKind(TSK_Undeclared), StrictPackMatch(StrictPackMatch) { + SpecializationKind(TSK_Undeclared), StrictPackMatch(StrictPackMatch), + InstantiatedLocally(false) { assert(DK == Kind::ClassTemplateSpecialization || StrictPackMatch == false); } @@ -982,7 +983,7 @@ ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C, Kind DK) : CXXRecordDecl(DK, TagTypeKind::Struct, C, nullptr, SourceLocation(), SourceLocation(), nullptr, nullptr), - SpecializationKind(TSK_Undeclared) {} + SpecializationKind(TSK_Undeclared), InstantiatedLocally(false) {} ClassTemplateSpecializationDecl *ClassTemplateSpecializationDecl::Create( ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index a72c95d6d77cf..fce3fe4cddece 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3699,6 +3699,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, = dyn_cast(Instantiation)) { Spec->setTemplateSpecializationKind(TSK); Spec->setPointOfInstantiation(PointOfInstantiation); + Spec->setInstantiatedLocally(); } InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 6b35b205079e5..bcee65efd636d 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -3425,7 +3425,16 @@ NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader, // If this is the first time, but we have parsed a declaration of the context, // build the anonymous declaration list from the parsed declaration. auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC); - if (PrimaryDC && !cast(PrimaryDC)->isFromASTFile()) { + auto needToNumberAnonymousDeclsWithin = [](Decl *D) { + if (!D->isFromASTFile()) + return true; + // If this is a class template specialization from an AST file + // but the instantiation occurred locally, we still need to number + // the anonymous decls. + auto *CTSD = dyn_cast(D); + return CTSD && CTSD->isInstantiatedLocally(); + }; + if (PrimaryDC && needToNumberAnonymousDeclsWithin(cast(PrimaryDC))) { numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) { if (Previous.size() == Number) Previous.push_back(cast(ND->getCanonicalDecl())); diff --git a/clang/test/Modules/merge-anon-in-template-2.cpp b/clang/test/Modules/merge-anon-in-template-2.cpp new file mode 100644 index 0000000000000..15852f0120798 --- /dev/null +++ b/clang/test/Modules/merge-anon-in-template-2.cpp @@ -0,0 +1,47 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-01 -emit-header-unit -xc++-user-header %t/hu-01.h \ +// RUN: -o %t/hu-01.pcm + +// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-02 -emit-header-unit -xc++-user-header %t/hu-02.h \ +// RUN: -Wno-experimental-header-units \ +// RUN: -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \ +// RUN: -o %t/hu-02.pcm + +// RUN: %clang_cc1 -std=c++20 -emit-obj %t/main.cpp \ +// RUN: -Wno-experimental-header-units \ +// RUN: -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \ +// RUN: -fmodule-map-file=%t/hu-02.map -fmodule-file=hu-02=%t/hu-02.pcm + +//--- hu-01.map +module "hu-01" { + header "hu-01.h" + export * +} + +//--- hu-02.map +module "hu-02" { + header "hu-02.h" + export * +} + +//--- hu-01.h +template +struct S { union { T x; }; }; + +using SI = S; + +//--- hu-02.h +template +struct S { union { T x; }; }; + +inline void f(S s = {}) { s.x; } + +//--- main.cpp +import "hu-01.h"; +void g(S) {} + +import "hu-02.h"; +void h() { f(); } diff --git a/clang/test/Modules/merge-anon-in-template-3.cpp b/clang/test/Modules/merge-anon-in-template-3.cpp new file mode 100644 index 0000000000000..1ee447e3e524d --- /dev/null +++ b/clang/test/Modules/merge-anon-in-template-3.cpp @@ -0,0 +1,45 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-01 -emit-header-unit -xc++-user-header %t/hu-01.h \ +// RUN: -o %t/hu-01.pcm + +// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-02 -emit-header-unit -xc++-user-header %t/hu-02.h \ +// RUN: -Wno-experimental-header-units \ +// RUN: -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \ +// RUN: -o %t/hu-02.pcm + +// RUN: %clang_cc1 -std=c++20 -emit-obj %t/main.cpp \ +// RUN: -Wno-experimental-header-units \ +// RUN: -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \ +// RUN: -fmodule-map-file=%t/hu-02.map -fmodule-file=hu-02=%t/hu-02.pcm + +//--- hu-01.map +module "hu-01" { + header "hu-01.h" + export * +} + +//--- hu-02.map +module "hu-02" { + header "hu-02.h" + export * +} + +//--- hu-01.h +template +struct S { union { T x; }; }; + +using SI = S; + +//--- hu-02.h +import "hu-01.h"; +inline void f(S s = {}) { s.x; } + +//--- main.cpp +import "hu-01.h"; +void g(S) {} + +import "hu-02.h"; +void h() { f(); }