[clang] [clang][ASTImporter] fix variable inline of CXX17 (PR #87314)
Qizhi Hu via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 1 22:27:49 PDT 2024
https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/87314
Fix crash int the testcase from https://github.com/llvm/llvm-project/issues/75114#issuecomment-1872595956
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 produce the crash.
>From 2f579346d6955e06d8e16bb29467ed5da332451d Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Tue, 2 Apr 2024 13:18:14 +0800
Subject: [PATCH] [clang][ASTImporter] fix variable inline of CXX17
---
clang/lib/AST/ASTImporter.cpp | 9 ++++++--
clang/unittests/AST/ASTImporterTest.cpp | 28 +++++++++++++++++++++++++
2 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 94a47a8f619018..cfdb2cb5c778e3 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4579,7 +4579,12 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
if (!RedeclOrErr)
return RedeclOrErr.takeError();
}
-
+ if (D->isInlineSpecified()) {
+ ToVar->setInlineSpecified();
+ }
+ if (D->isInline()) {
+ ToVar->setImplicitlyInline();
+ }
return ToVar;
}
@@ -6410,7 +6415,7 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
}
ToVarTD->setPreviousDecl(Recent);
}
-
+ // Importer.MapImported(D, ToVarTD);
return ToVarTD;
}
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 35ab7e3b7fe314..d57736830f0223 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -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 T> T 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);
+ EXPECT_TRUE(ToX);
+ auto *FromX2 = FirstDeclMatcher<VarTemplateDecl>().match(
+ FromTU2, varTemplateDecl(hasName("X")));
+ auto *ToX2 = Import(FromX2, Lang_CXX17);
+ EXPECT_TRUE(ToX2);
+ EXPECT_TRUE(ToX == ToX2);
+}
+
TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
constexpr auto Code =
R"(
More information about the cfe-commits
mailing list