[ASTImporter] Fix crash caused by unimported type of FromatAttr

During the import of FormatAttrs we forgot to import the type (e.g
`__scanf__`) of the attribute. This caused a segfault when we wanted to
traverse the AST (e.g. by the dump() method).

Differential Revision: https://reviews.llvm.org/D89319
This commit is contained in:
Gabor Marton 2020-10-13 15:57:37 +02:00
parent 3fcca804b2
commit dd965711c9
4 changed files with 38 additions and 0 deletions

View File

@ -8100,6 +8100,16 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
ToAttr = To;
break;
}
case attr::Format: {
const auto *From = cast<FormatAttr>(FromAttr);
FormatAttr *To;
IdentifierInfo *ToAttrType = Import(From->getType());
To = FormatAttr::Create(ToContext, ToAttrType, From->getFormatIdx(),
From->getFirstArg(), ToRange, From->getSyntax());
To->setInherited(From->isInherited());
ToAttr = To;
break;
}
default:
// FIXME: 'clone' copies every member but some of them should be imported.
// Handle other Attrs that have parameters that should be imported.

View File

@ -0,0 +1 @@
int foo(const char *fmt, ...) __attribute__((__format__(__scanf__, 1, 2)));

View File

@ -0,0 +1,2 @@
// RUN: %clang -x c++-header -o %t.a.ast %S/Inputs/FormatAttr.cpp
// RUN: %clang_cc1 -x c++ -ast-merge %t.a.ast /dev/null -ast-dump

View File

@ -5767,6 +5767,31 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
EXPECT_TRUE(ToA);
}
TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
Decl *FromTU = getTuDecl(
R"(
int foo(const char * fmt, ...)
__attribute__ ((__format__ (__scanf__, 1, 2)));
)",
Lang_CXX03, "input.cc");
auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
FromTU, functionDecl(hasName("foo")));
ASSERT_TRUE(FromD);
auto *ToD = Import(FromD, Lang_CXX03);
ASSERT_TRUE(ToD);
ToD->dump(); // Should not crash!
auto *FromAttr = FromD->getAttr<FormatAttr>();
auto *ToAttr = ToD->getAttr<FormatAttr>();
EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
EXPECT_EQ(FromAttr->getAttributeSpellingListIndex(),
ToAttr->getAttributeSpellingListIndex());
EXPECT_EQ(FromAttr->getType()->getName(), ToAttr->getType()->getName());
}
template <typename T>
auto ExtendWithOptions(const T &Values, const std::vector<std::string> &Args) {
auto Copy = Values;