[ASTImporter] Fix crash caused by unimported type of FromatAttr
authorGabor Marton <gabor.marton@ericsson.com>
Tue, 13 Oct 2020 13:57:37 +0000 (15:57 +0200)
committerGabor Marton <gabor.marton@ericsson.com>
Wed, 14 Oct 2020 11:54:48 +0000 (13:54 +0200)
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

clang/lib/AST/ASTImporter.cpp
clang/test/ASTMerge/attr/Inputs/FormatAttr.cpp [new file with mode: 0644]
clang/test/ASTMerge/attr/testFormatAttr.cpp [new file with mode: 0644]
clang/unittests/AST/ASTImporterTest.cpp

index 05cc717..275e912 100644 (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.
diff --git a/clang/test/ASTMerge/attr/Inputs/FormatAttr.cpp b/clang/test/ASTMerge/attr/Inputs/FormatAttr.cpp
new file mode 100644 (file)
index 0000000..f249a6e
--- /dev/null
@@ -0,0 +1 @@
+int foo(const char *fmt, ...) __attribute__((__format__(__scanf__, 1, 2)));
diff --git a/clang/test/ASTMerge/attr/testFormatAttr.cpp b/clang/test/ASTMerge/attr/testFormatAttr.cpp
new file mode 100644 (file)
index 0000000..18329c1
--- /dev/null
@@ -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
index 51391d2..967dc03 100644 (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;