Skip to content

Commit 869d168

Browse files
authored
[clang][ASTImporter] Fix crash when import VarTemplateDecl in record (#67522)
[clang][ASTImporter] Fix crash when import `VarTemplateDecl` in record static VarTemplateDecl in record isn't a definition, when imported before, it will crash in `ASTContext::setTemplateOrSpecializationInfo` due to setting specialization while it already exists. This patch skip this specific case. Co-authored-by: huqizhi <[email protected]>
1 parent 6afceba commit 869d168

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6242,6 +6242,9 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
62426242
// FIXME Check for ODR error if the two definitions have
62436243
// different initializers?
62446244
return Importer.MapImported(D, FoundDef);
6245+
if (FoundTemplate->getDeclContext()->isRecord() &&
6246+
D->getDeclContext()->isRecord())
6247+
return Importer.MapImported(D, FoundTemplate);
62456248

62466249
FoundByLookup = FoundTemplate;
62476250
break;

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4988,6 +4988,37 @@ TEST_P(ASTImporterOptionSpecificTestBase,
49884988
}
49894989
}
49904990

4991+
TEST_P(ImportFriendClasses, RecordVarTemplateDecl) {
4992+
Decl *ToTU = getToTuDecl(
4993+
R"(
4994+
template <class T>
4995+
class A {
4996+
public:
4997+
template <class U>
4998+
static constexpr bool X = true;
4999+
};
5000+
)",
5001+
Lang_CXX14);
5002+
5003+
auto *ToTUX = FirstDeclMatcher<VarTemplateDecl>().match(
5004+
ToTU, varTemplateDecl(hasName("X")));
5005+
Decl *FromTU = getTuDecl(
5006+
R"(
5007+
template <class T>
5008+
class A {
5009+
public:
5010+
template <class U>
5011+
static constexpr bool X = true;
5012+
};
5013+
)",
5014+
Lang_CXX14, "input1.cc");
5015+
auto *FromX = FirstDeclMatcher<VarTemplateDecl>().match(
5016+
FromTU, varTemplateDecl(hasName("X")));
5017+
auto *ToX = Import(FromX, Lang_CXX11);
5018+
EXPECT_TRUE(ToX);
5019+
EXPECT_EQ(ToTUX, ToX);
5020+
}
5021+
49915022
TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
49925023
constexpr auto Code =
49935024
R"(

0 commit comments

Comments
 (0)