[PATCH] D76098: [WIP] [clangd] Do not trigger go-to-def textual fallback inside string literals
Nathan Ridge via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 12 14:09:30 PDT 2020
nridge created this revision.
nridge added a reviewer: sammccall.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D76098
Files:
clang-tools-extra/clangd/XRefs.cpp
clang-tools-extra/clangd/unittests/XRefsTests.cpp
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -624,11 +624,12 @@
TEST(LocateSymbol, Textual) {
const char *Tests[] = {
R"cpp(// Comment
- struct [[MyClass]] {};
- // Comment mentioning M^yClass
- )cpp",
+ struct [[MyClass]] {};
+ // 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
@@ -680,7 +681,7 @@
EXPECT_EQ(Results[0].PreferredDeclaration.range, *WantDecl) << Test;
}
}
-}
+} // namespace
TEST(LocateSymbol, Ambiguous) {
auto T = Annotations(R"cpp(
Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -355,6 +355,20 @@
return !WordExpandedTokens.empty();
}
+enum TokenFlavor { Identifier, Comment, Other };
+
+TokenFlavor getTokenFlavor(SourceLocation Loc, const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ Token Tok;
+ if (Lexer::getRawToken(Loc, Tok, SM, LangOpts))
+ return Other;
+ if (Tok.is(tok::TokenKind::raw_identifier))
+ return Identifier;
+ if (Tok.is(tok::TokenKind::comment))
+ return Comment;
+ return Other;
+}
+
} // namespace
std::vector<LocatedSymbol>
@@ -363,6 +377,11 @@
const std::string &MainFilePath) {
const auto &SM = AST.getSourceManager();
+ TokenFlavor Flavor = getTokenFlavor(Loc, SM, AST.getLangOpts());
+ // Only consider comment and (raw) identifier tokens.
+ if (!(Flavor == TokenFlavor::Comment || Flavor == TokenFlavor::Identifier))
+ return {};
+
// Get the raw word at the specified location.
unsigned Pos;
FileID File;
@@ -374,12 +393,14 @@
unsigned WordOffset = Word.data() - Code.data();
SourceLocation WordStart = SM.getComposedLoc(File, WordOffset);
- // 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.
+ // If this is an identifier token, do not consider if it it 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.
// FIXME: Relax this for dependent code.
- if (tokenSurvivedPreprocessing(WordStart, AST.getTokens()))
+ if (Flavor == TokenFlavor::Identifier &&
+ tokenSurvivedPreprocessing(WordStart, AST.getTokens())) {
return {};
+ }
// Additionally filter for signals that the word is likely to be an
// identifier. This avoids triggering on e.g. random words in a comment.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76098.250052.patch
Type: text/x-patch
Size: 3065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200312/a0559602/attachment-0001.bin>
More information about the cfe-commits
mailing list