Skip to content

Commit ab80d00

Browse files
authored
[clang][ASTImporter] fix variable inline of CXX17 (#87314)
Fix crash in the testcase from #75114 (comment) Forget to set inline of variable declaration would make `isThisDeclarationADefinition` get incorrect result and didn't get imported variable. This will lead to a new `VarTemplateDecl` being created and call `setDescribedVarTemplate` again which produces the crash. Co-authored-by: huqizhi <[email protected]>
1 parent a9d9387 commit ab80d00

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4541,6 +4541,10 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
45414541
ToVar->setQualifierInfo(ToQualifierLoc);
45424542
ToVar->setAccess(D->getAccess());
45434543
ToVar->setLexicalDeclContext(LexicalDC);
4544+
if (D->isInlineSpecified())
4545+
ToVar->setInlineSpecified();
4546+
if (D->isInline())
4547+
ToVar->setImplicitlyInline();
45444548

45454549
if (FoundByLookup) {
45464550
auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5317,6 +5317,34 @@ TEST_P(ASTImporterOptionSpecificTestBase,
53175317
EXPECT_FALSE(ToX);
53185318
}
53195319

5320+
TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateDeclInlineWithCXX17) {
5321+
Decl *FromTU = getTuDecl(
5322+
R"(
5323+
struct S {
5324+
template <unsigned> static constexpr bool X = true;
5325+
};
5326+
)",
5327+
Lang_CXX17, "input1.cc");
5328+
Decl *FromTU2 = getTuDecl(
5329+
R"(
5330+
struct S {
5331+
template <unsigned> static constexpr bool X = true;
5332+
template <typename T> void get() { X<sizeof(T)>; }
5333+
};
5334+
template <typename U> U qvariant_cast(const S &v) { return v.get; }
5335+
)",
5336+
Lang_CXX17, "input2.cc");
5337+
auto *FromX = FirstDeclMatcher<VarTemplateDecl>().match(
5338+
FromTU, varTemplateDecl(hasName("X")));
5339+
auto *ToX = Import(FromX, Lang_CXX17);
5340+
ASSERT_TRUE(ToX);
5341+
auto *FromX2 = FirstDeclMatcher<VarTemplateDecl>().match(
5342+
FromTU2, varTemplateDecl(hasName("X")));
5343+
auto *ToX2 = Import(FromX2, Lang_CXX17);
5344+
EXPECT_TRUE(ToX2);
5345+
EXPECT_EQ(ToX, ToX2);
5346+
}
5347+
53205348
TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
53215349
constexpr auto Code =
53225350
R"(

0 commit comments

Comments
 (0)