diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -820,6 +820,18 @@ SelectionTree::createRight(AST.getASTContext(), TB, Offset, Offset); std::vector Result; if (const SelectionTree::Node *N = ST.commonAncestor()) { + // Don't allow Decls which are not related with tokens under cursor. + if (const auto *D = N->ASTNode.get()) { + bool IsDeclTouchingCursor = false; + for (const auto &Tok : TokensTouchingCursor) { + if (D->getLocation() == Tok.location()) { + IsDeclTouchingCursor = true; + break; + } + } + if (!IsDeclTouchingCursor) + return llvm::None; + } // FIXME: Fill in HighlightRange with range coming from N->ASTNode. auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias); if (!Decls.empty()) { diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -942,6 +942,13 @@ template void foo() { (void)[[size^of]](T); })cpp", + R"cpp(// No Decl for token under cursor. + namespace ns { + #define FOO B^AR; + })cpp", + R"cpp(//error-ok + unknown f(i^nt); + )cpp", // literals "auto x = t^rue;", "auto x = '^A';",