[PATCH] D155574: [clang][ASTImporter] Fix import of recursive field initializer.
Balázs Kéri via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 20 02:16:54 PDT 2023
balazske updated this revision to Diff 542383.
balazske added a comment.
using clang-format
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D155574/new/
https://reviews.llvm.org/D155574
Files:
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp
Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -8107,6 +8107,23 @@
EXPECT_TRUE(ToX->getInClassInitializer());
}
+TEST_P(ASTImporterOptionSpecificTestBase, ImportRecursiveFieldInitializer) {
+ const char *Code =
+ R"(
+ class A {
+ int b{b};
+ };
+ )";
+ Decl *FromTU = getTuDecl(Code, Lang_CXX11);
+
+ auto *FromB =
+ FirstDeclMatcher<FieldDecl>().match(FromTU, fieldDecl(hasName("b")));
+ EXPECT_TRUE(FromB->hasInClassInitializer());
+
+ auto *ToB = Import(FromB, Lang_CXX11);
+ EXPECT_TRUE(ToB->hasInClassInitializer());
+}
+
TEST_P(ASTImporterOptionSpecificTestBase, isNewDecl) {
Decl *FromTU = getTuDecl(
R"(
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -3902,7 +3902,6 @@
auto ToTInfo = importChecked(Err, D->getTypeSourceInfo());
auto ToBitWidth = importChecked(Err, D->getBitWidth());
auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart());
- auto ToInitializer = importChecked(Err, D->getInClassInitializer());
if (Err)
return std::move(Err);
const Type *ToCapturedVLAType = nullptr;
@@ -3925,12 +3924,17 @@
return std::move(Err);
ToField->setAccess(D->getAccess());
ToField->setLexicalDeclContext(LexicalDC);
- if (ToInitializer)
- ToField->setInClassInitializer(ToInitializer);
ToField->setImplicit(D->isImplicit());
if (ToCapturedVLAType)
ToField->setCapturedVLAType(cast<VariableArrayType>(ToCapturedVLAType));
LexicalDC->addDeclInternal(ToField);
+ // Import initializer only after the field was created, it may have recursive
+ // reference to the field.
+ auto ToInitializer = importChecked(Err, D->getInClassInitializer());
+ if (Err)
+ return std::move(Err);
+ if (ToInitializer)
+ ToField->setInClassInitializer(ToInitializer);
return ToField;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155574.542383.patch
Type: text/x-patch
Size: 2086 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230720/a43ff7e2/attachment.bin>
More information about the cfe-commits
mailing list