diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -3673,6 +3673,28 @@ D->getInClassInitStyle())) return ToField; + // FieldDecl::isZeroSize may need to check these. + if (const Attr *FromAttr = D->getAttr()) { + if (auto ToAttrOrErr = Importer.Import(FromAttr)) + ToField->addAttr(*ToAttrOrErr); + else + return ToAttrOrErr.takeError(); + RecordType *FromRT = + const_cast(D->getType()->getAs()); + // Is this field a struct? FieldDecl::isZeroSize will need its definition. + if (FromRT) { + RecordDecl *FromRD = FromRT->getDecl(); + RecordType *ToRT = + const_cast(ToField->getType()->getAs()); + assert(ToRT && "RecordType import has different type"); + RecordDecl *ToRD = ToRT->getDecl(); + if (FromRD->isCompleteDefinition() && !ToRD->isCompleteDefinition()) { + if (Error Err = ImportDefinition(FromRD, ToRD, IDK_Basic)) + return std::move(Err); + } + } + } + ToField->setAccess(D->getAccess()); ToField->setLexicalDeclContext(LexicalDC); if (ToInitializer) @@ -8445,6 +8467,8 @@ // Make sure that ImportImpl registered the imported decl. assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?"); + // There may have been NoUniqueAddressAttr for FieldDecl::isZeroSize. + ToD->dropAttrs(); if (FromD->hasAttrs()) for (const Attr *FromAttr : FromD->getAttrs()) { auto ToAttrOrErr = Import(FromAttr); diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -6323,6 +6323,33 @@ ToD->getTemplateSpecializationKind()); } +TEST_P(ASTImporterOptionSpecificTestBase, ImportNoUniqueAddress) { + Decl *FromTU = getTuDecl( + R"( + struct A {}; + struct B { + [[no_unique_address]] A a; + }; + struct C : B { + char c; + } c; + )", Lang_CXX20); + + // It does not fail even without the patch! + auto *BFromD = FirstDeclMatcher().match( + FromTU, cxxRecordDecl(hasName("B"))); + ASSERT_TRUE(BFromD); + auto *BToD = Import(BFromD, Lang_CXX20); + EXPECT_TRUE(BToD); + + // It does not fail even without the patch! + auto *CFromD = FirstDeclMatcher().match( + FromTU, cxxRecordDecl(hasName("C"))); + ASSERT_TRUE(CFromD); + auto *CToD = Import(CFromD, Lang_CXX20); + EXPECT_TRUE(CToD); +} + INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions, );