[clang-tools-extra] 7d65cc9 - [clangd] Guard against null Attrs in the AST

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 13 01:39:00 PDT 2021


Author: Sam McCall
Date: 2021-08-13T10:38:52+02:00
New Revision: 7d65cc98f3508750fcf12240cab625c6e1d5c4ed

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

LOG: [clangd] Guard against null Attrs in the AST

Added: 
    

Modified: 
    clang-tools-extra/clangd/AST.cpp
    clang-tools-extra/clangd/unittests/SelectionTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp
index a47a5a1aab1d..3b5956037746 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/clangd/AST.cpp
@@ -488,15 +488,22 @@ std::vector<const Attr *> getAttributes(const DynTypedNode &N) {
   if (const auto *TL = N.get<TypeLoc>()) {
     for (AttributedTypeLoc ATL = TL->getAs<AttributedTypeLoc>(); !ATL.isNull();
          ATL = ATL.getModifiedLoc().getAs<AttributedTypeLoc>()) {
-      Result.push_back(ATL.getAttr());
+      if (const Attr *A = ATL.getAttr())
+        Result.push_back(A);
       assert(!ATL.getModifiedLoc().isNull());
     }
   }
-  if (const auto *S = N.get<AttributedStmt>())
+  if (const auto *S = N.get<AttributedStmt>()) {
     for (; S != nullptr; S = dyn_cast<AttributedStmt>(S->getSubStmt()))
-      llvm::copy(S->getAttrs(), std::back_inserter(Result));
-  if (const auto *D = N.get<Decl>())
-    llvm::copy(D->attrs(), std::back_inserter(Result));
+      for (const Attr *A : S->getAttrs())
+        if (A)
+          Result.push_back(A);
+  }
+  if (const auto *D = N.get<Decl>()) {
+    for (const Attr *A : D->attrs())
+      if (A)
+        Result.push_back(A);
+  }
   return Result;
 }
 

diff  --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 3898388d1ad2..5a45e5bde8fa 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -465,7 +465,15 @@ TEST(SelectionTest, CommonAncestor) {
         // Digraph syntax for attributes to avoid accidental annotations.
         class <:[gsl::Owner([[in^t]])]:> X{};
       )cpp",
-       "BuiltinTypeLoc"}};
+       "BuiltinTypeLoc"},
+
+      // This case used to crash - AST has a null Attr
+      {R"cpp(
+        @interface I
+        [[@property(retain, nonnull) <:[My^Object2]:> *x]]; // error-ok
+        @end
+      )cpp",
+       "ObjCPropertyDecl"}};
 
   for (const Case &C : Cases) {
     trace::TestTracer Tracer;


        


More information about the cfe-commits mailing list