Skip to content

Commit 68f5396

Browse files
author
Gabor Marton
committed
[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
1 parent 59560e8 commit 68f5396

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,6 @@ namespace clang {
264264

265265
void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
266266
ToD->IdentifierNamespace = FromD->IdentifierNamespace;
267-
if (FromD->hasAttrs())
268-
for (const Attr *FromAttr : FromD->getAttrs()) {
269-
// FIXME: Return of the error here is not possible until store of
270-
// import errors is implemented.
271-
auto ToAttrOrErr = import(FromAttr);
272-
if (ToAttrOrErr)
273-
ToD->addAttr(*ToAttrOrErr);
274-
else
275-
llvm::consumeError(ToAttrOrErr.takeError());
276-
}
277267
if (FromD->isUsed())
278268
ToD->setIsUsed();
279269
if (FromD->isImplicit())
@@ -8328,6 +8318,15 @@ Expected<Decl *> ASTImporter::Import(Decl *FromD) {
83288318
// Make sure that ImportImpl registered the imported decl.
83298319
assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
83308320

8321+
if (FromD->hasAttrs())
8322+
for (const Attr *FromAttr : FromD->getAttrs()) {
8323+
auto ToAttrOrErr = Import(FromAttr);
8324+
if (ToAttrOrErr)
8325+
ToD->addAttr(*ToAttrOrErr);
8326+
else
8327+
return ToAttrOrErr.takeError();
8328+
}
8329+
83318330
// Notify subclasses.
83328331
Imported(FromD, ToD);
83338332

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,6 +6084,24 @@ TEST_P(CTAD, DeductionGuideShouldCopyALocalTypedef) {
60846084
INSTANTIATE_TEST_CASE_P(ParameterizedTests, CTAD,
60856085
DefaultTestValuesForRunOptions, );
60866086

6087+
TEST_P(ASTImporterOptionSpecificTestBase, TypedefWithAttribute) {
6088+
Decl *TU = getTuDecl(
6089+
R"(
6090+
namespace N {
6091+
typedef int X __attribute__((annotate("A")));
6092+
}
6093+
)",
6094+
Lang_CXX17, "input.cc");
6095+
auto *FromD =
6096+
FirstDeclMatcher<TypedefDecl>().match(TU, typedefDecl(hasName("X")));
6097+
auto *ToD = Import(FromD, Lang_CXX17);
6098+
ASSERT_TRUE(ToD);
6099+
ASSERT_EQ(ToD->getAttrs().size(), 1);
6100+
auto *ToAttr = dyn_cast<AnnotateAttr>(ToD->getAttrs()[0]);
6101+
ASSERT_TRUE(ToAttr);
6102+
EXPECT_EQ(ToAttr->getAnnotation(), "A");
6103+
}
6104+
60876105
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
60886106
DefaultTestValuesForRunOptions, );
60896107

0 commit comments

Comments
 (0)