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; }
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; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits