[PATCH] D116978: [clangd] Selection: Prune gtest TEST()s earlier

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 11 02:18:35 PST 2022


sammccall updated this revision to Diff 398876.
sammccall added a comment.

Rebase (offsetInSelFile)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116978/new/

https://reviews.llvm.org/D116978

Files:
  clang-tools-extra/clangd/Selection.cpp


Index: clang-tools-extra/clangd/Selection.cpp
===================================================================
--- clang-tools-extra/clangd/Selection.cpp
+++ clang-tools-extra/clangd/Selection.cpp
@@ -8,7 +8,6 @@
 
 #include "Selection.h"
 #include "AST.h"
-#include "SourceCode.h"
 #include "support/Logger.h"
 #include "support/Trace.h"
 #include "clang/AST/ASTTypeTraits.h"
@@ -303,10 +302,21 @@
   bool mayHit(SourceRange R) const {
     if (SpelledTokens.empty())
       return false;
-    auto B = offsetInSelFile(R.getBegin());
-    auto E = offsetInSelFile(R.getEnd());
-    if (B && E)
-      if (*E < SpelledTokens.front().Offset || *B > SpelledTokens.back().Offset)
+    // If the node starts after the selection ends, it is not selected.
+    // All tokens a macro location might claim are >= its expansion site.
+    // So it's safe to use the expansions location for the comparison.
+    // (This is particularly helpful for GTest's TEST macro).
+    if (auto B = offsetInSelFile(getExpansionStart(R.getBegin())))
+      if (*B > SpelledTokens.back().Offset)
+        return false;
+    // If the node ends before the selection begins, it is not selected.
+    SourceLocation EndLoc = R.getEnd();
+    while (EndLoc.isMacroID())
+      EndLoc = SM.getImmediateExpansionRange(EndLoc).getEnd();
+    // In the rare case that the expansion range is a char range, EndLoc is
+    // ~one token too far to the right. We may fail to prune, that's OK.
+    if (auto E = offsetInSelFile(EndLoc))
+      if (*E < SpelledTokens.front().Offset)
         return false;
     return true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116978.398876.patch
Type: text/x-patch
Size: 1596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220111/12593161/attachment.bin>


More information about the cfe-commits mailing list