Skip to content

Commit 39dfaf0

Browse files
mzyKimiaozhiyuan
andauthored
[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. (#68774)
Fixes: #68769 Co-authored-by: miaozhiyuan <[email protected]>
1 parent d47e2ff commit 39dfaf0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ Bug Fixes to C++ Support
670670
default initializing a base class in a constant expression context. Fixes:
671671
(`#69890 <https://github.com/llvm/llvm-project/issues/69890>`_)
672672

673+
- Fix crash when template class static member imported to other translation unit.
674+
Fixes:
675+
(`#68769 <https://github.com/llvm/llvm-project/issues/68769>`_)
676+
673677
Bug Fixes to AST Handling
674678
^^^^^^^^^^^^^^^^^^^^^^^^^
675679
- Fixed an import failure of recursive friend class template.

clang/lib/AST/ASTImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4476,6 +4476,17 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
44764476
auto ToVTOrErr = import(D->getDescribedVarTemplate());
44774477
if (!ToVTOrErr)
44784478
return ToVTOrErr.takeError();
4479+
} else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) {
4480+
TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind();
4481+
VarDecl *FromInst = D->getInstantiatedFromStaticDataMember();
4482+
if (Expected<VarDecl *> ToInstOrErr = import(FromInst))
4483+
ToVar->setInstantiationOfStaticDataMember(*ToInstOrErr, SK);
4484+
else
4485+
return ToInstOrErr.takeError();
4486+
if (ExpectedSLoc POIOrErr = import(MSI->getPointOfInstantiation()))
4487+
ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
4488+
else
4489+
return POIOrErr.takeError();
44794490
}
44804491

44814492
if (Error Err = ImportInitializer(D, ToVar))

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,40 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplatedDecl) {
13701370
ASSERT_EQ(ToTemplated1, ToTemplated);
13711371
}
13721372

1373+
TEST_P(ASTImporterOptionSpecificTestBase,
1374+
ImportTemplateSpecializationStaticMember) {
1375+
auto FromCode =
1376+
R"(
1377+
template <typename H> class Test{
1378+
public:
1379+
static const unsigned int length;
1380+
};
1381+
1382+
template<> const unsigned int Test<int>::length;
1383+
template<> const unsigned int Test<int>::length = 0;
1384+
)";
1385+
auto ToCode =
1386+
R"(
1387+
template <typename H> class Test {
1388+
public:
1389+
static const unsigned int length;
1390+
};
1391+
1392+
template <> const unsigned int Test<int>::length;
1393+
1394+
void foo() { int i = 1 / Test<int>::length; }
1395+
)";
1396+
Decl *FromTU = getTuDecl(FromCode, Lang_CXX14);
1397+
auto FromDecl = FirstDeclMatcher<VarDecl>().match(
1398+
FromTU, varDecl(hasName("length"), isDefinition()));
1399+
Decl *ToTu = getToTuDecl(ToCode, Lang_CXX14);
1400+
auto ToX = Import(FromDecl, Lang_CXX03);
1401+
auto ToDecl = FirstDeclMatcher<VarDecl>().match(
1402+
ToTu, varDecl(hasName("length"), isDefinition()));
1403+
EXPECT_TRUE(ToX);
1404+
EXPECT_EQ(ToX, ToDecl);
1405+
}
1406+
13731407
TEST_P(ASTImporterOptionSpecificTestBase, ImportChooseExpr) {
13741408
// This tests the import of isConditionTrue directly to make sure the importer
13751409
// gets it right.

0 commit comments

Comments
 (0)