[llvm-branch-commits] [clang] 68f5396 - [ASTImporter] Fix import of a typedef that has an attribute
Gabor Marton via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Dec 14 09:31:57 PST 2020
Author: Gabor Marton
Date: 2020-12-14T18:27:05+01:00
New Revision: 68f53960e17d93c3a2727164dac4e54140bd98ba
URL: https://github.com/llvm/llvm-project/commit/68f53960e17d93c3a2727164dac4e54140bd98ba
DIFF: https://github.com/llvm/llvm-project/commit/68f53960e17d93c3a2727164dac4e54140bd98ba.diff
LOG: [ASTImporter] Fix import of a typedef that has an attribute
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.
Differential Revision: https://reviews.llvm.org/D92962
Added:
Modified:
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index ea05f2ea4552..10fa6990624d 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -264,16 +264,6 @@ namespace clang {
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 @@ Expected<Decl *> ASTImporter::Import(Decl *FromD) {
// 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);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index e81e5d028cd0..40340cb6f9bc 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -6084,6 +6084,24 @@ TEST_P(CTAD, DeductionGuideShouldCopyALocalTypedef) {
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);
+ ASSERT_EQ(ToD->getAttrs().size(), 1);
+ auto *ToAttr = dyn_cast<AnnotateAttr>(ToD->getAttrs()[0]);
+ ASSERT_TRUE(ToAttr);
+ EXPECT_EQ(ToAttr->getAnnotation(), "A");
+}
+
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions, );
More information about the llvm-branch-commits
mailing list