[clangd] Do not trigger go-to-def textual fallback inside string literals

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76098
This commit is contained in:
Nathan Ridge 2020-03-12 17:08:49 -04:00
parent b4f02d89e5
commit b89202e842
2 changed files with 15 additions and 2 deletions

View File

@ -374,6 +374,18 @@ locateSymbolNamedTextuallyAt(ParsedAST &AST, const SymbolIndex *Index,
unsigned WordOffset = Word.data() - Code.data();
SourceLocation WordStart = SM.getComposedLoc(File, WordOffset);
// Attempt to determine the kind of token that contains the word,
// and bail if it's a string literal. Note that we cannot always
// determine the token kind (e.g. comments, for which we do want
// to activate, are not retained by TokenBuffer).
for (syntax::Token T :
syntax::spelledTokensTouching(WordStart, AST.getTokens())) {
if (T.range(AST.getSourceManager()).touches(WordOffset + Word.size())) {
if (isStringLiteral(T.kind()))
return {};
}
}
// Do not consider tokens that survived preprocessing.
// We are erring on the safe side here, as a user may expect to get
// accurate (as opposed to textual-heuristic) results for such tokens.

View File

@ -644,7 +644,8 @@ TEST(LocateSymbol, Textual) {
// Comment mentioning M^yClass
)cpp",
R"cpp(// String
struct [[MyClass]] {};
struct MyClass {};
// Not triggered for string literal tokens.
const char* s = "String literal mentioning M^yClass";
)cpp",
R"cpp(// Ifdef'ed out code
@ -696,7 +697,7 @@ TEST(LocateSymbol, Textual) {
EXPECT_EQ(Results[0].PreferredDeclaration.range, *WantDecl) << Test;
}
}
}
} // namespace
TEST(LocateSymbol, Ambiguous) {
auto T = Annotations(R"cpp(