[clang] dd96571 - [ASTImporter] Fix crash caused by unimported type of FromatAttr

Gabor Marton via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 04:55:10 PDT 2020


Author: Gabor Marton
Date: 2020-10-14T13:54:48+02:00
New Revision: dd965711c9f0e4b6e1dc9b465fe049c38e05d5aa

URL: https://github.com/llvm/llvm-project/commit/dd965711c9f0e4b6e1dc9b465fe049c38e05d5aa
DIFF: https://github.com/llvm/llvm-project/commit/dd965711c9f0e4b6e1dc9b465fe049c38e05d5aa.diff

LOG: [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

Added: 
    clang/test/ASTMerge/attr/Inputs/FormatAttr.cpp
    clang/test/ASTMerge/attr/testFormatAttr.cpp

Modified: 
    clang/lib/AST/ASTImporter.cpp
    clang/unittests/AST/ASTImporterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 05cc717581ef..275e91235690 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -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
index 000000000000..f249a6e65e0a
--- /dev/null
+++ b/clang/test/ASTMerge/attr/Inputs/FormatAttr.cpp
@@ -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
index 000000000000..18329c1e1127
--- /dev/null
+++ b/clang/test/ASTMerge/attr/testFormatAttr.cpp
@@ -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

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 51391d221626..967dc035d11f 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -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;


        


More information about the cfe-commits mailing list