[clang] [clang][ASTImporter] Fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From bbde18aade35c431e9c113a9d200102330aa3e3a Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 2 ++ clang/unittests/AST/ASTImporterTest.cpp | 43 + 2 files changed, 45 insertions(+) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..7b102ccc65d7cf2 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6240,6 +6240,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..1ed0a84a580f686 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4988,6 +4988,49 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] Fix clash when import `VarTemplateDecl` in record (PR #67522)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff a904bb464fe46052803e49b99ec255d2078c3c3b 0a00f5c846cb53a96133bc8050be74672c2c8343 -- clang/lib/AST/ASTImporter.cpp clang/unittests/AST/ASTImporterTest.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index f2fafaf4329b..6f0686697417 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4974,8 +4974,7 @@ TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { FromTU, varTemplateDecl(hasName("X"))); auto *ToA = Import(FromA, Lang_CXX11); EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl(), -ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl(), ToA->getTemplatedDecl()); EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), ToA->getTemplatedDecl()->getDefinition()); } `` https://github.com/llvm/llvm-project/pull/67522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] Fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky edited https://github.com/llvm/llvm-project/pull/67522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] Fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky edited https://github.com/llvm/llvm-project/pull/67522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From f9ff9c8a328a8c3658da8989a96af548eb685272 Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 2 ++ clang/unittests/AST/ASTImporterTest.cpp | 44 + 2 files changed, 46 insertions(+) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..7b102ccc65d7cf2 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6240,6 +6240,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..5f2aa2359fe9343 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4988,6 +4988,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From 23c100253486cf6fb9834d995127e9325a45aec2 Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 2 + clang/unittests/AST/ASTImporterTest.cpp | 51 + 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..7b102ccc65d7cf2 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6240,6 +6240,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..e6675ccf61ebf10 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4360,15 +4360,12 @@ TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { R"( template class A; - template class A { public: template friend class A; - A(T x) :x(x) {} - private: T x; }; @@ -4382,19 +4379,15 @@ TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { R"( template class A; - template class A { public: template friend class A; - A(T x) : x(x) {} - private: T x; }; - A a1(0); )", Lang_CXX11, "input1.cc"); @@ -4988,6 +4981,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From 8e7bc769bca076d6e2d15d38694e6cf3b67ffdbc Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 2 + clang/unittests/AST/ASTImporterTest.cpp | 96 - 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..7b102ccc65d7cf2 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6240,6 +6240,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..f2fafaf4329b740 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4355,58 +4355,6 @@ TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) { EXPECT_TRUE(Imported->getPreviousDecl()); } -TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { - Decl *ToTU = getToTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) :x(x) {} - - private: -T x; - }; - )", - Lang_CXX11); - - auto *Fwd = FirstDeclMatcher().match( - ToTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - Decl *FromTU = getTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) : x(x) {} - - private: -T x; - }; - - A a1(0); - )", - Lang_CXX11, "input1.cc"); - auto *FromA = FirstDeclMatcher().match( - FromTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - auto *ToA = Import(FromA, Lang_CXX11); - EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl()->getTypeForDecl(), -ToA->getTemplatedDecl()->getTypeForDecl()); -} - TEST_P(ImportFriendClasses, ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) { Decl *ToTU = getToTuDecl( @@ -4988,6 +4936,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From f158c9bca3461452b3b2b316f616f08b39e6228e Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 3 + clang/unittests/AST/ASTImporterTest.cpp | 96 - 2 files changed, 47 insertions(+), 52 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..b06cd9f0a3113dd 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6236,10 +6236,13 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // The Decl in the "From" context has a definition, but in the // "To" context we already have a definition. VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); + if (D->isThisDeclarationADefinition() && FoundDef) // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..f2fafaf4329b740 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4355,58 +4355,6 @@ TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) { EXPECT_TRUE(Imported->getPreviousDecl()); } -TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { - Decl *ToTU = getToTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) :x(x) {} - - private: -T x; - }; - )", - Lang_CXX11); - - auto *Fwd = FirstDeclMatcher().match( - ToTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - Decl *FromTU = getTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) : x(x) {} - - private: -T x; - }; - - A a1(0); - )", - Lang_CXX11, "input1.cc"); - auto *FromA = FirstDeclMatcher().match( - FromTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - auto *ToA = Import(FromA, Lang_CXX11); - EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl()->getTypeForDecl(), -ToA->getTemplatedDecl()->getTypeForDecl()); -} - TEST_P(ImportFriendClasses, ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) { Decl *ToTU = getToTuDecl( @@ -4988,6 +4936,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522 >From 77224ec6297a4fb9a86f2634f4a0278b49e81625 Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 5 +- clang/unittests/AST/ASTImporterTest.cpp | 96 - 2 files changed, 48 insertions(+), 53 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..441c61c16917bea 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6236,10 +6236,13 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // The Decl in the "From" context has a definition, but in the // "To" context we already have a definition. VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); -if (D->isThisDeclarationADefinition() && FoundDef) + +if ((D->isThisDeclarationADefinition() && FoundDef)) // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); +if (FoundTemplate->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..f2fafaf4329b740 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4355,58 +4355,6 @@ TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) { EXPECT_TRUE(Imported->getPreviousDecl()); } -TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { - Decl *ToTU = getToTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) :x(x) {} - - private: -T x; - }; - )", - Lang_CXX11); - - auto *Fwd = FirstDeclMatcher().match( - ToTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - Decl *FromTU = getTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) : x(x) {} - - private: -T x; - }; - - A a1(0); - )", - Lang_CXX11, "input1.cc"); - auto *FromA = FirstDeclMatcher().match( - FromTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - auto *ToA = Import(FromA, Lang_CXX11); - EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl()->getTypeForDecl(), -ToA->getTemplatedDecl()->getTypeForDecl()); -} - TEST_P(ImportFriendClasses, ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) { Decl *ToTU = getToTuDecl( @@ -4988,6 +4936,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
llvmbot wrote: @llvm/pr-subscribers-clang Changes [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record VarTemplateDecl in Record isn't a definition, when imported, it will crash in `ASTContext::setTemplateOrSpecializationInfo` due to setting specialization while it already exists. This patch skip this specific case. --- Full diff: https://github.com/llvm/llvm-project/pull/67522.diff 2 Files Affected: - (modified) clang/lib/AST/ASTImporter.cpp (+4-2) - (modified) clang/unittests/AST/ASTImporterTest.cpp (+44-52) ``diff diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..b207c39cdf90154 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6236,10 +6236,12 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // The Decl in the "From" context has a definition, but in the // "To" context we already have a definition. VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); -if (D->isThisDeclarationADefinition() && FoundDef) + +if ((D->isThisDeclarationADefinition() && FoundDef) || +FoundTemplate->getDeclContext()->isRecord()) // FIXME Check for ODR error if the two definitions have // different initializers? - return Importer.MapImported(D, FoundDef); + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..f2fafaf4329b740 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4355,58 +4355,6 @@ TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) { EXPECT_TRUE(Imported->getPreviousDecl()); } -TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { - Decl *ToTU = getToTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) :x(x) {} - - private: -T x; - }; - )", - Lang_CXX11); - - auto *Fwd = FirstDeclMatcher().match( - ToTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - Decl *FromTU = getTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) : x(x) {} - - private: -T x; - }; - - A a1(0); - )", - Lang_CXX11, "input1.cc"); - auto *FromA = FirstDeclMatcher().match( - FromTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - auto *ToA = Import(FromA, Lang_CXX11); - EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl()->getTypeForDecl(), -ToA->getTemplatedDecl()->getTypeForDecl()); -} - TEST_P(ImportFriendClasses, ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) { Decl *ToTU = getToTuDecl( @@ -4988,6 +4936,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( `` https://github.com/llvm/llvm-project/pull/67522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)
https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/67522 [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record VarTemplateDecl in Record isn't a definition, when imported, it will crash in `ASTContext::setTemplateOrSpecializationInfo` due to setting specialization while it already exists. This patch skip this specific case. >From 0a00f5c846cb53a96133bc8050be74672c2c8343 Mon Sep 17 00:00:00 2001 From: huqizhi Date: Wed, 27 Sep 2023 15:32:10 +0800 Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record --- clang/lib/AST/ASTImporter.cpp | 6 +- clang/unittests/AST/ASTImporterTest.cpp | 96 - 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179a4..b207c39cdf90154 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6236,10 +6236,12 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // The Decl in the "From" context has a definition, but in the // "To" context we already have a definition. VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); -if (D->isThisDeclarationADefinition() && FoundDef) + +if ((D->isThisDeclarationADefinition() && FoundDef) || +FoundTemplate->getDeclContext()->isRecord()) // FIXME Check for ODR error if the two definitions have // different initializers? - return Importer.MapImported(D, FoundDef); + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb624306..f2fafaf4329b740 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4355,58 +4355,6 @@ TEST_P(ImportFriendClasses, DeclsFromFriendsShouldBeInRedeclChains) { EXPECT_TRUE(Imported->getPreviousDecl()); } -TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) { - Decl *ToTU = getToTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) :x(x) {} - - private: -T x; - }; - )", - Lang_CXX11); - - auto *Fwd = FirstDeclMatcher().match( - ToTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - Decl *FromTU = getTuDecl( - R"( - template - class A; - - template - class A { - public: -template -friend class A; - -A(T x) : x(x) {} - - private: -T x; - }; - - A a1(0); - )", - Lang_CXX11, "input1.cc"); - auto *FromA = FirstDeclMatcher().match( - FromTU, - classTemplateDecl(has(cxxRecordDecl(hasDefinition(), hasName("A"); - auto *ToA = Import(FromA, Lang_CXX11); - EXPECT_TRUE(ToA); - EXPECT_EQ(Fwd->getTemplatedDecl()->getTypeForDecl(), -ToA->getTemplatedDecl()->getTypeForDecl()); -} - TEST_P(ImportFriendClasses, ImportOfClassTemplateDefinitionShouldConnectToFwdFriend) { Decl *ToTU = getToTuDecl( @@ -4988,6 +4936,50 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14); + + auto *Fwd = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: +template +struct B { + static U Value; +}; + +template +static constexpr bool X = !B::Value; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromA = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToA = Import(FromA, Lang_CXX11); + EXPECT_TRUE(ToA); + EXPECT_EQ(Fwd->getTemplatedDecl(), +ToA->getTemplatedDecl()); + EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(), +ToA->getTemplatedDecl()->getDefinition()); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits