Skip to content

Commit f800ac8

Browse files
committed
[clangd] Fix hover crash on broken code
Differential Revision: https://reviews.llvm.org/D101743
1 parent b83b232 commit f800ac8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ llvm::Optional<StringRef> setterVariableName(const CXXMethodDecl *CMD) {
505505
if (auto *CE = llvm::dyn_cast<CallExpr>(RHS->IgnoreCasts())) {
506506
if (CE->getNumArgs() != 1)
507507
return llvm::None;
508-
auto *ND = llvm::dyn_cast<NamedDecl>(CE->getCalleeDecl());
508+
auto *ND = llvm::dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl());
509509
if (!ND || !ND->getIdentifier() || ND->getName() != "move" ||
510510
!ND->isInStdNamespace())
511511
return llvm::None;

clang-tools-extra/clangd/unittests/HoverTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,20 @@ TEST(Hover, DocsFromAST) {
24382438
}
24392439
}
24402440

2441+
TEST(Hover, NoCrash) {
2442+
Annotations T(R"cpp(
2443+
/* error-ok */
2444+
template<typename T> T foo(T);
2445+
2446+
// Setter variable heuristic might fail if the callexpr is broken.
2447+
struct X { int Y; void [[^setY]](float) { Y = foo(undefined); } };)cpp");
2448+
2449+
TestTU TU = TestTU::withCode(T.code());
2450+
auto AST = TU.build();
2451+
for (const auto &P : T.points())
2452+
getHover(AST, P, format::getLLVMStyle(), nullptr);
2453+
}
2454+
24412455
TEST(Hover, DocsFromMostSpecial) {
24422456
Annotations T(R"cpp(
24432457
// doc1

0 commit comments

Comments
 (0)