Skip to content

Commit 73c6beb

Browse files
author
Gabor Marton
committed
[ASTImporter] Fix crash caused by unset AttributeSpellingListIndex
During the import of attributes we forgot to set the spelling list index. This caused a segfault when we wanted to traverse the AST (e.g. by the dump() method). Differential Revision: https://reviews.llvm.org/D89318
1 parent dd96571 commit 73c6beb

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8094,9 +8094,6 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
80948094
else
80958095
return ToTOrErr.takeError();
80968096
}
8097-
To->setInherited(From->isInherited());
8098-
To->setPackExpansion(From->isPackExpansion());
8099-
To->setImplicit(From->isImplicit());
81008097
ToAttr = To;
81018098
break;
81028099
}
@@ -8106,7 +8103,6 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
81068103
IdentifierInfo *ToAttrType = Import(From->getType());
81078104
To = FormatAttr::Create(ToContext, ToAttrType, From->getFormatIdx(),
81088105
From->getFirstArg(), ToRange, From->getSyntax());
8109-
To->setInherited(From->isInherited());
81108106
ToAttr = To;
81118107
break;
81128108
}
@@ -8117,8 +8113,15 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
81178113
ToAttr->setRange(ToRange);
81188114
break;
81198115
}
8116+
81208117
assert(ToAttr && "Attribute should be created.");
8121-
8118+
if (const auto *InheritableFromAttr = dyn_cast<InheritableAttr>(FromAttr))
8119+
cast<InheritableAttr>(ToAttr)->setInherited(
8120+
InheritableFromAttr->isInherited());
8121+
ToAttr->setAttributeSpellingListIndex(
8122+
FromAttr->getAttributeSpellingListIndex());
8123+
ToAttr->setPackExpansion(FromAttr->isPackExpansion());
8124+
ToAttr->setImplicit(FromAttr->isImplicit());
81228125
return ToAttr;
81238126
}
81248127

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void *foo(unsigned, unsigned) __attribute__((__malloc__));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// RUN: %clang -x c++-header -o %t.a.ast %S/Inputs/RestrictAttr.cpp
2+
// RUN: %clang_cc1 -x c++ -ast-merge %t.a.ast /dev/null -ast-dump

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5767,11 +5767,35 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
57675767
EXPECT_TRUE(ToA);
57685768
}
57695769

5770+
TEST_P(ASTImporterOptionSpecificTestBase, ImportRestrictAttr) {
5771+
Decl *FromTU = getTuDecl(
5772+
R"(
5773+
void *foo(unsigned, unsigned) __attribute__((__malloc__));
5774+
)",
5775+
Lang_CXX03, "input.cc");
5776+
auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
5777+
FromTU, functionDecl(hasName("foo")));
5778+
ASSERT_TRUE(FromD);
5779+
5780+
auto *ToD = Import(FromD, Lang_CXX03);
5781+
ASSERT_TRUE(ToD);
5782+
ToD->dump(); // Should not crash!
5783+
5784+
auto *FromAttr = FromD->getAttr<RestrictAttr>();
5785+
auto *ToAttr = ToD->getAttr<RestrictAttr>();
5786+
EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
5787+
EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
5788+
EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
5789+
EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
5790+
EXPECT_EQ(FromAttr->getAttributeSpellingListIndex(),
5791+
ToAttr->getAttributeSpellingListIndex());
5792+
}
5793+
57705794
TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
57715795
Decl *FromTU = getTuDecl(
57725796
R"(
57735797
int foo(const char * fmt, ...)
5774-
__attribute__ ((__format__ (__scanf__, 1, 2)));
5798+
__attribute__ ((__format__ (__scanf__, 1, 2)));
57755799
)",
57765800
Lang_CXX03, "input.cc");
57775801
auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
@@ -5792,6 +5816,7 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
57925816
ToAttr->getAttributeSpellingListIndex());
57935817
EXPECT_EQ(FromAttr->getType()->getName(), ToAttr->getType()->getName());
57945818
}
5819+
57955820
template <typename T>
57965821
auto ExtendWithOptions(const T &Values, const std::vector<std::string> &Args) {
57975822
auto Copy = Values;

0 commit comments

Comments
 (0)