[PATCH] D70727: [clangd] Keep go-to-definition working at the end of an identifier (fixes #194)

Nathan Ridge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 26 09:44:48 PST 2019


nridge created this revision.
nridge added a reviewer: hokein.
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/D70727

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
@@ -450,6 +450,17 @@
           +^+x;
         }
       )cpp",
+
+      R"cpp(// End of identifier (definition)
+        void [[func]]^() {}
+      )cpp",
+
+      R"cpp(// End of identifier (reference)
+        void [[func]]() {}
+        void test() {
+          func^();
+        }
+      )cpp",
   };
   for (const char *Test : Tests) {
     Annotations T(Test);
Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -129,11 +129,8 @@
   return Merged.CanonicalDeclaration;
 }
 
-std::vector<const Decl *> getDeclAtPosition(ParsedAST &AST, SourceLocation Pos,
-                                            DeclRelationSet Relations) {
-  FileID FID;
-  unsigned Offset;
-  std::tie(FID, Offset) = AST.getSourceManager().getDecomposedSpellingLoc(Pos);
+std::vector<const Decl *> getDeclAtOffset(ParsedAST &AST, unsigned Offset,
+                                          DeclRelationSet Relations) {
   SelectionTree Selection(AST.getASTContext(), AST.getTokens(), Offset);
   std::vector<const Decl *> Result;
   if (const SelectionTree::Node *N = Selection.commonAncestor()) {
@@ -143,6 +140,22 @@
   return Result;
 }
 
+std::vector<const Decl *> getDeclAtPosition(ParsedAST &AST, SourceLocation Pos,
+                                            DeclRelationSet Relations) {
+  FileID FID;
+  unsigned Offset;
+  std::tie(FID, Offset) = AST.getSourceManager().getDecomposedSpellingLoc(Pos);
+  std::vector<const Decl *> Result = getDeclAtOffset(AST, Offset, Relations);
+  // If no declaration was found at this offset, try the previous offset.
+  // This compensates for the fact that SelectionTree interprets an offset
+  // as applying to the character after rather than the character before,
+  // allowing go-to-definition to work at the end of an identifier.
+  if (Result.empty() && Offset > 0) {
+    Result = getDeclAtOffset(AST, Offset - 1, Relations);
+  }
+  return Result;
+}
+
 llvm::Optional<Location> makeLocation(ASTContext &AST, SourceLocation TokLoc,
                                       llvm::StringRef TUPath) {
   const SourceManager &SourceMgr = AST.getSourceManager();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70727.231094.patch
Type: text/x-patch
Size: 2470 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191126/03c6800e/attachment.bin>


More information about the cfe-commits mailing list