Author: Chuanqi Xu Date: 2022-03-24T10:24:14+08:00 New Revision: 84746686088799ec9e3cc5ea0a64df81423da290
URL: https://github.com/llvm/llvm-project/commit/84746686088799ec9e3cc5ea0a64df81423da290 DIFF: https://github.com/llvm/llvm-project/commit/84746686088799ec9e3cc5ea0a64df81423da290.diff LOG: [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<class T> void f() {} template<> void f<int>() {} ``` 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<class T> void f() {} export template<> void f<int>() {} ``` 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 Added: Modified: clang/lib/AST/Decl.cpp clang/unittests/AST/DeclTest.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index cc5eca86abb19..6b68e511a932b 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 a84ebbd9835db..bf2826c7b329f 100644 --- a/clang/unittests/AST/DeclTest.cpp +++ b/clang/unittests/AST/DeclTest.cpp @@ -171,3 +171,26 @@ TEST(Decl, IsInExportDeclContext) { selectFirst<FunctionDecl>("f", match(functionDecl().bind("f"), Ctx)); EXPECT_TRUE(f->isInExportDeclContext()); } + +TEST(Decl, InConsistLinkageForTemplates) { + llvm::Annotations Code(R"( + export module m; + export template <class T> + void f() {} + + template <> + void f<int>() {})"); + + auto AST = + tooling::buildASTFromCodeWithArgs(Code.code(), /*Args=*/{"-std=c++20"}); + ASTContext &Ctx = AST->getASTContext(); + + llvm::SmallVector<ast_matchers::BoundNodes, 2> Funcs = + match(functionDecl().bind("f"), Ctx); + + EXPECT_EQ(Funcs.size(), 2); + const FunctionDecl *TemplateF = Funcs[0].getNodeAs<FunctionDecl>("f"); + const FunctionDecl *SpecializedF = Funcs[1].getNodeAs<FunctionDecl>("f"); + EXPECT_EQ(TemplateF->getLinkageInternal(), + SpecializedF->getLinkageInternal()); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits