Skip to content

Commit

Permalink
[clang][ASTImporter] Fix crash when template class static member impo…
Browse files Browse the repository at this point in the history
…rted to other translation unit. (#68774)

Fixes: #68769

Co-authored-by: miaozhiyuan <[email protected]>
  • Loading branch information
mzyKi and miaozhiyuan authored Nov 1, 2023
1 parent d47e2ff commit 39dfaf0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ Bug Fixes to C++ Support
default initializing a base class in a constant expression context. Fixes:
(`#69890 <https://github.com/llvm/llvm-project/issues/69890>`_)

- Fix crash when template class static member imported to other translation unit.
Fixes:
(`#68769 <https://github.com/llvm/llvm-project/issues/68769>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4476,6 +4476,17 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
auto ToVTOrErr = import(D->getDescribedVarTemplate());
if (!ToVTOrErr)
return ToVTOrErr.takeError();
} else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) {
TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind();
VarDecl *FromInst = D->getInstantiatedFromStaticDataMember();
if (Expected<VarDecl *> ToInstOrErr = import(FromInst))
ToVar->setInstantiationOfStaticDataMember(*ToInstOrErr, SK);
else
return ToInstOrErr.takeError();
if (ExpectedSLoc POIOrErr = import(MSI->getPointOfInstantiation()))
ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
else
return POIOrErr.takeError();
}

if (Error Err = ImportInitializer(D, ToVar))
Expand Down
34 changes: 34 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,40 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplatedDecl) {
ASSERT_EQ(ToTemplated1, ToTemplated);
}

TEST_P(ASTImporterOptionSpecificTestBase,
ImportTemplateSpecializationStaticMember) {
auto FromCode =
R"(
template <typename H> class Test{
public:
static const unsigned int length;
};
template<> const unsigned int Test<int>::length;
template<> const unsigned int Test<int>::length = 0;
)";
auto ToCode =
R"(
template <typename H> class Test {
public:
static const unsigned int length;
};
template <> const unsigned int Test<int>::length;
void foo() { int i = 1 / Test<int>::length; }
)";
Decl *FromTU = getTuDecl(FromCode, Lang_CXX14);
auto FromDecl = FirstDeclMatcher<VarDecl>().match(
FromTU, varDecl(hasName("length"), isDefinition()));
Decl *ToTu = getToTuDecl(ToCode, Lang_CXX14);
auto ToX = Import(FromDecl, Lang_CXX03);
auto ToDecl = FirstDeclMatcher<VarDecl>().match(
ToTu, varDecl(hasName("length"), isDefinition()));
EXPECT_TRUE(ToX);
EXPECT_EQ(ToX, ToDecl);
}

TEST_P(ASTImporterOptionSpecificTestBase, ImportChooseExpr) {
// This tests the import of isConditionTrue directly to make sure the importer
// gets it right.
Expand Down

0 comments on commit 39dfaf0

Please sign in to comment.