[clang-tools-extra] f800ac8 - [clangd] Fix hover crash on broken code

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Tue May 4 02:46:31 PDT 2021


Author: Kadir Cetinkaya
Date: 2021-05-04T11:42:31+02:00
New Revision: f800ac8309417b78c084f2eabfd54d43a66b64a2

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

LOG: [clangd] Fix hover crash on broken code

Differential Revision: https://reviews.llvm.org/D101743

Added: 
    

Modified: 
    clang-tools-extra/clangd/Hover.cpp
    clang-tools-extra/clangd/unittests/HoverTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index f9de910835581..c58dab22f8a3e 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -505,7 +505,7 @@ llvm::Optional<StringRef> setterVariableName(const CXXMethodDecl *CMD) {
   if (auto *CE = llvm::dyn_cast<CallExpr>(RHS->IgnoreCasts())) {
     if (CE->getNumArgs() != 1)
       return llvm::None;
-    auto *ND = llvm::dyn_cast<NamedDecl>(CE->getCalleeDecl());
+    auto *ND = llvm::dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl());
     if (!ND || !ND->getIdentifier() || ND->getName() != "move" ||
         !ND->isInStdNamespace())
       return llvm::None;

diff  --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 6624290ec41c5..c2645b99926c3 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2438,6 +2438,20 @@ TEST(Hover, DocsFromAST) {
   }
 }
 
+TEST(Hover, NoCrash) {
+  Annotations T(R"cpp(
+    /* error-ok */
+    template<typename T> T foo(T);
+
+    // Setter variable heuristic might fail if the callexpr is broken.
+    struct X { int Y; void [[^setY]](float) { Y = foo(undefined); } };)cpp");
+
+  TestTU TU = TestTU::withCode(T.code());
+  auto AST = TU.build();
+  for (const auto &P : T.points())
+    getHover(AST, P, format::getLLVMStyle(), nullptr);
+}
+
 TEST(Hover, DocsFromMostSpecial) {
   Annotations T(R"cpp(
   // doc1


        


More information about the cfe-commits mailing list