Skip to content

Commit dd96571

Browse files
author
Gabor Marton
committed
[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
1 parent 3fcca80 commit dd96571

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8100,6 +8100,16 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
81008100
ToAttr = To;
81018101
break;
81028102
}
8103+
case attr::Format: {
8104+
const auto *From = cast<FormatAttr>(FromAttr);
8105+
FormatAttr *To;
8106+
IdentifierInfo *ToAttrType = Import(From->getType());
8107+
To = FormatAttr::Create(ToContext, ToAttrType, From->getFormatIdx(),
8108+
From->getFirstArg(), ToRange, From->getSyntax());
8109+
To->setInherited(From->isInherited());
8110+
ToAttr = To;
8111+
break;
8112+
}
81038113
default:
81048114
// FIXME: 'clone' copies every member but some of them should be imported.
81058115
// Handle other Attrs that have parameters that should be imported.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int foo(const char *fmt, ...) __attribute__((__format__(__scanf__, 1, 2)));
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/FormatAttr.cpp
2+
// RUN: %clang_cc1 -x c++ -ast-merge %t.a.ast /dev/null -ast-dump

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5767,6 +5767,31 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
57675767
EXPECT_TRUE(ToA);
57685768
}
57695769

5770+
TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
5771+
Decl *FromTU = getTuDecl(
5772+
R"(
5773+
int foo(const char * fmt, ...)
5774+
__attribute__ ((__format__ (__scanf__, 1, 2)));
5775+
)",
5776+
Lang_CXX03, "input.cc");
5777+
auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
5778+
FromTU, functionDecl(hasName("foo")));
5779+
ASSERT_TRUE(FromD);
5780+
5781+
auto *ToD = Import(FromD, Lang_CXX03);
5782+
ASSERT_TRUE(ToD);
5783+
ToD->dump(); // Should not crash!
5784+
5785+
auto *FromAttr = FromD->getAttr<FormatAttr>();
5786+
auto *ToAttr = ToD->getAttr<FormatAttr>();
5787+
EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
5788+
EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
5789+
EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
5790+
EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
5791+
EXPECT_EQ(FromAttr->getAttributeSpellingListIndex(),
5792+
ToAttr->getAttributeSpellingListIndex());
5793+
EXPECT_EQ(FromAttr->getType()->getName(), ToAttr->getType()->getName());
5794+
}
57705795
template <typename T>
57715796
auto ExtendWithOptions(const T &Values, const std::vector<std::string> &Args) {
57725797
auto Copy = Values;

0 commit comments

Comments
 (0)