Skip to content

Commit

Permalink
[clang][ASTImporter] fix variable inline of CXX17 (#87314)
Browse files Browse the repository at this point in the history
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 <836744285@qq.com>
  • Loading branch information
jcsxky committed Apr 5, 2024
1 parent a9d9387 commit ab80d00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,10 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
ToVar->setQualifierInfo(ToQualifierLoc);
ToVar->setAccess(D->getAccess());
ToVar->setLexicalDeclContext(LexicalDC);
if (D->isInlineSpecified())
ToVar->setInlineSpecified();
if (D->isInline())
ToVar->setImplicitlyInline();

if (FoundByLookup) {
auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
Expand Down
28 changes: 28 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5317,6 +5317,34 @@ TEST_P(ASTImporterOptionSpecificTestBase,
EXPECT_FALSE(ToX);
}

TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateDeclInlineWithCXX17) {
Decl *FromTU = getTuDecl(
R"(
struct S {
template <unsigned> static constexpr bool X = true;
};
)",
Lang_CXX17, "input1.cc");
Decl *FromTU2 = getTuDecl(
R"(
struct S {
template <unsigned> static constexpr bool X = true;
template <typename T> void get() { X<sizeof(T)>; }
};
template <typename U> U qvariant_cast(const S &v) { return v.get; }
)",
Lang_CXX17, "input2.cc");
auto *FromX = FirstDeclMatcher<VarTemplateDecl>().match(
FromTU, varTemplateDecl(hasName("X")));
auto *ToX = Import(FromX, Lang_CXX17);
ASSERT_TRUE(ToX);
auto *FromX2 = FirstDeclMatcher<VarTemplateDecl>().match(
FromTU2, varTemplateDecl(hasName("X")));
auto *ToX2 = Import(FromX2, Lang_CXX17);
EXPECT_TRUE(ToX2);
EXPECT_EQ(ToX, ToX2);
}

TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
constexpr auto Code =
R"(
Expand Down

0 comments on commit ab80d00

Please sign in to comment.