Author: Gabor Marton
Date: 2020-10-14T14:10:08+02:00
New Revision: 73c6beb2f7053fe8b5150072c2b5cd930de38a22

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

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

Added: 
    clang/test/ASTMerge/attr/Inputs/RestrictAttr.cpp
    clang/test/ASTMerge/attr/testRestrictAttr.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 275e91235690..29fcf6c2a2a9 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -8094,9 +8094,6 @@ Expected<Attr *> ASTImporter::Import(const Attr 
*FromAttr) {
       else
         return ToTOrErr.takeError();
     }
-    To->setInherited(From->isInherited());
-    To->setPackExpansion(From->isPackExpansion());
-    To->setImplicit(From->isImplicit());
     ToAttr = To;
     break;
   }
@@ -8106,7 +8103,6 @@ Expected<Attr *> ASTImporter::Import(const Attr 
*FromAttr) {
     IdentifierInfo *ToAttrType = Import(From->getType());
     To = FormatAttr::Create(ToContext, ToAttrType, From->getFormatIdx(),
                             From->getFirstArg(), ToRange, From->getSyntax());
-    To->setInherited(From->isInherited());
     ToAttr = To;
     break;
   }
@@ -8117,8 +8113,15 @@ Expected<Attr *> ASTImporter::Import(const Attr 
*FromAttr) {
     ToAttr->setRange(ToRange);
     break;
   }
+
   assert(ToAttr && "Attribute should be created.");
-  
+  if (const auto *InheritableFromAttr = dyn_cast<InheritableAttr>(FromAttr))
+    cast<InheritableAttr>(ToAttr)->setInherited(
+        InheritableFromAttr->isInherited());
+  ToAttr->setAttributeSpellingListIndex(
+      FromAttr->getAttributeSpellingListIndex());
+  ToAttr->setPackExpansion(FromAttr->isPackExpansion());
+  ToAttr->setImplicit(FromAttr->isImplicit());
   return ToAttr;
 }
 

diff  --git a/clang/test/ASTMerge/attr/Inputs/RestrictAttr.cpp 
b/clang/test/ASTMerge/attr/Inputs/RestrictAttr.cpp
new file mode 100644
index 000000000000..2055a23b7c3c
--- /dev/null
+++ b/clang/test/ASTMerge/attr/Inputs/RestrictAttr.cpp
@@ -0,0 +1 @@
+void *foo(unsigned, unsigned) __attribute__((__malloc__));

diff  --git a/clang/test/ASTMerge/attr/testRestrictAttr.cpp 
b/clang/test/ASTMerge/attr/testRestrictAttr.cpp
new file mode 100644
index 000000000000..65903d8f66ca
--- /dev/null
+++ b/clang/test/ASTMerge/attr/testRestrictAttr.cpp
@@ -0,0 +1,2 @@
+// RUN: %clang -x c++-header -o %t.a.ast %S/Inputs/RestrictAttr.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 967dc035d11f..209374dd7048 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5767,11 +5767,35 @@ TEST_P(ASTImporterOptionSpecificTestBase, 
ImportExprOfAlignmentAttr) {
   EXPECT_TRUE(ToA);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, ImportRestrictAttr) {
+  Decl *FromTU = getTuDecl(
+      R"(
+      void *foo(unsigned, unsigned) __attribute__((__malloc__));
+      )",
+      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<RestrictAttr>();
+  auto *ToAttr = ToD->getAttr<RestrictAttr>();
+  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());
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
   Decl *FromTU = getTuDecl(
       R"(
       int foo(const char * fmt, ...)
-      __attribute__ ((__format__ (__scanf__, 1, 2)));
+          __attribute__ ((__format__ (__scanf__, 1, 2)));
       )",
       Lang_CXX03, "input.cc");
   auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
@@ -5792,6 +5816,7 @@ TEST_P(ASTImporterOptionSpecificTestBase, 
ImportFormatAttr) {
             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;


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to