From 84746686088799ec9e3cc5ea0a64df81423da290 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Wed, 23 Feb 2022 16:23:26 +0800 Subject: [PATCH] [C++20] [Modules] Make the linkage consistent for template and its specialization Before the patch, the compiler would crash for the test due to inconsistent linkage. This patch tries to avoid it by make the linkage consistent for template and its specialization. After the patch, the behavior of compiler would be partially correct for the case. The correct one is: ``` export template void f() {} template<> void f() {} ``` In this case, the linkage for both declaration should be external (the wording I get by consulting in WG21 is "the linkage for name f should be external"). And for the case: ``` template void f() {} export template<> void f() {} ``` Compiler should reject it. This isn't done now. After all, this patch would stop a crash. Reviewed By: iains, aaron.ballman, dblaikie Differential Revision: https://reviews.llvm.org/D120397 --- clang/lib/AST/Decl.cpp | 15 +++++++++++---- clang/unittests/AST/DeclTest.cpp | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index cc5eca8..6b68e51 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -391,11 +391,18 @@ void LinkageComputer::mergeTemplateLV( bool considerVisibility = shouldConsiderTemplateVisibility(fn, specInfo); - // Merge information from the template parameters. FunctionTemplateDecl *temp = specInfo->getTemplate(); - LinkageInfo tempLV = - getLVForTemplateParameterList(temp->getTemplateParameters(), computation); - LV.mergeMaybeWithVisibility(tempLV, considerVisibility); + + // Merge information from the template declaration. + LinkageInfo tempLV = getLVForDecl(temp, computation); + // The linkage of the specialization should be consistent with the + // template declaration. + LV.setLinkage(tempLV.getLinkage()); + + // Merge information from the template parameters. + LinkageInfo paramsLV = + getLVForTemplateParameterList(temp->getTemplateParameters(), computation); + LV.mergeMaybeWithVisibility(paramsLV, considerVisibility); // Merge information from the template arguments. const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; diff --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp index a84ebbd..bf2826c 100644 --- a/clang/unittests/AST/DeclTest.cpp +++ b/clang/unittests/AST/DeclTest.cpp @@ -171,3 +171,26 @@ TEST(Decl, IsInExportDeclContext) { selectFirst("f", match(functionDecl().bind("f"), Ctx)); EXPECT_TRUE(f->isInExportDeclContext()); } + +TEST(Decl, InConsistLinkageForTemplates) { + llvm::Annotations Code(R"( + export module m; + export template + void f() {} + + template <> + void f() {})"); + + auto AST = + tooling::buildASTFromCodeWithArgs(Code.code(), /*Args=*/{"-std=c++20"}); + ASTContext &Ctx = AST->getASTContext(); + + llvm::SmallVector Funcs = + match(functionDecl().bind("f"), Ctx); + + EXPECT_EQ(Funcs.size(), 2); + const FunctionDecl *TemplateF = Funcs[0].getNodeAs("f"); + const FunctionDecl *SpecializedF = Funcs[1].getNodeAs("f"); + EXPECT_EQ(TemplateF->getLinkageInternal(), + SpecializedF->getLinkageInternal()); +} -- 2.7.4