martong created this revision. martong added a reviewer: teemperor. Herald added subscribers: gamesh411, Szelethus, dkrupp, rnkovacs. Herald added a reviewer: a.sidorin. Herald added a reviewer: shafik. martong requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
The import of a typedefs with an attribute uses clang::Decl::setAttrs(). But that needs the ASTContext which we can get only from the TranslationUnitDecl. But we can get the TUDecl only thourgh the DeclContext, which is not set by the time of the setAttrs call. Fix: import the attributes only after the DC is surely imported. Btw, having the attribute import initiated from GetImportedOrCreateDecl was fundamentally flawed. Now that is implicitly fixed. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D92962 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 @@ -6085,6 +6085,20 @@ INSTANTIATE_TEST_CASE_P(ParameterizedTests, CTAD, DefaultTestValuesForRunOptions, ); +TEST_P(ASTImporterOptionSpecificTestBase, TypedefWithAttribute) { + Decl *TU = getTuDecl( + R"( + namespace N { + typedef int X __attribute__((annotate("A"))); + } + )", + Lang_CXX17, "input.cc"); + auto *FromD = + FirstDeclMatcher<TypedefDecl>().match(TU, typedefDecl(hasName("X"))); + auto *ToD = Import(FromD, Lang_CXX17); + ASSERT_TRUE(ToD); +} + INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions, ); Index: clang/lib/AST/ASTImporter.cpp =================================================================== --- clang/lib/AST/ASTImporter.cpp +++ clang/lib/AST/ASTImporter.cpp @@ -264,16 +264,6 @@ void InitializeImportedDecl(Decl *FromD, Decl *ToD) { ToD->IdentifierNamespace = FromD->IdentifierNamespace; - if (FromD->hasAttrs()) - for (const Attr *FromAttr : FromD->getAttrs()) { - // FIXME: Return of the error here is not possible until store of - // import errors is implemented. - auto ToAttrOrErr = import(FromAttr); - if (ToAttrOrErr) - ToD->addAttr(*ToAttrOrErr); - else - llvm::consumeError(ToAttrOrErr.takeError()); - } if (FromD->isUsed()) ToD->setIsUsed(); if (FromD->isImplicit()) @@ -8328,6 +8318,15 @@ // Make sure that ImportImpl registered the imported decl. assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?"); + if (FromD->hasAttrs()) + for (const Attr *FromAttr : FromD->getAttrs()) { + auto ToAttrOrErr = Import(FromAttr); + if (ToAttrOrErr) + ToD->addAttr(*ToAttrOrErr); + else + return ToAttrOrErr.takeError(); + } + // Notify subclasses. Imported(FromD, ToD);
Index: clang/unittests/AST/ASTImporterTest.cpp =================================================================== --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -6085,6 +6085,20 @@ INSTANTIATE_TEST_CASE_P(ParameterizedTests, CTAD, DefaultTestValuesForRunOptions, ); +TEST_P(ASTImporterOptionSpecificTestBase, TypedefWithAttribute) { + Decl *TU = getTuDecl( + R"( + namespace N { + typedef int X __attribute__((annotate("A"))); + } + )", + Lang_CXX17, "input.cc"); + auto *FromD = + FirstDeclMatcher<TypedefDecl>().match(TU, typedefDecl(hasName("X"))); + auto *ToD = Import(FromD, Lang_CXX17); + ASSERT_TRUE(ToD); +} + INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions, ); Index: clang/lib/AST/ASTImporter.cpp =================================================================== --- clang/lib/AST/ASTImporter.cpp +++ clang/lib/AST/ASTImporter.cpp @@ -264,16 +264,6 @@ void InitializeImportedDecl(Decl *FromD, Decl *ToD) { ToD->IdentifierNamespace = FromD->IdentifierNamespace; - if (FromD->hasAttrs()) - for (const Attr *FromAttr : FromD->getAttrs()) { - // FIXME: Return of the error here is not possible until store of - // import errors is implemented. - auto ToAttrOrErr = import(FromAttr); - if (ToAttrOrErr) - ToD->addAttr(*ToAttrOrErr); - else - llvm::consumeError(ToAttrOrErr.takeError()); - } if (FromD->isUsed()) ToD->setIsUsed(); if (FromD->isImplicit()) @@ -8328,6 +8318,15 @@ // Make sure that ImportImpl registered the imported decl. assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?"); + if (FromD->hasAttrs()) + for (const Attr *FromAttr : FromD->getAttrs()) { + auto ToAttrOrErr = Import(FromAttr); + if (ToAttrOrErr) + ToD->addAttr(*ToAttrOrErr); + else + return ToAttrOrErr.takeError(); + } + // Notify subclasses. Imported(FromD, ToD);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits