[clang-tools-extra] b89202e - [clangd] Do not trigger go-to-def textual fallback inside string literals

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 19 14:25:13 PDT 2020


Author: Nathan Ridge
Date: 2020-03-19T17:24:45-04:00
New Revision: b89202e842acda4d6519cb45c98128827df84a2f

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

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clangd/XRefs.cpp
    clang-tools-extra/clangd/unittests/XRefsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index acf9f6df8281..7d55a372905c 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -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.

diff  --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 32a89df424e4..fc36dfa42d7f 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -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(


        


More information about the cfe-commits mailing list