[clang-tools-extra] 05b1734 - [clangd] findNearbyIdentifier(): fix the word search in the token stream.
Aleksandr Platonov via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 02:47:37 PDT 2020
Author: Aleksandr Platonov
Date: 2020-07-30T12:45:58+03:00
New Revision: 05b173466142596b3297ab02e423574cb74b3799
URL: https://github.com/llvm/llvm-project/commit/05b173466142596b3297ab02e423574cb74b3799
DIFF: https://github.com/llvm/llvm-project/commit/05b173466142596b3297ab02e423574cb74b3799.diff
LOG: [clangd] findNearbyIdentifier(): fix the word search in the token stream.
Without this patch the word occurrence search always returns the first token of the file.
Despite of that, `findNeardyIdentifier()` returns the correct result (but inefficently) until there are several matched tokens with the same value `floor(log2(<token line> - <word line>))` (e.g. several matched tokens on the same line).
Reviewed By: kadircet
Differential Revision: https://reviews.llvm.org/D84912
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 aeff7ebc32a2..cf747b607f4a 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -518,7 +518,7 @@ const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
// Find where the word occurred in the token stream, to search forward & back.
auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
- return T.location() >= Word.Location; // Comparison OK: same file.
+ return T.location() < Word.Location; // Comparison OK: same file.
});
// Search for matches after the cursor.
for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 0428303f5b0a..0a8f85ed5317 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1197,7 +1197,14 @@ TEST(LocateSymbol, NearbyIdentifier) {
// h^i
)cpp",
- };
+ R"cpp(
+ // prefer nearest occurrence even if several matched tokens
+ // have the same value of `floor(log2(<token line> - <word line>))`.
+ int hello;
+ int x = hello, y = hello;
+ int z = [[hello]];
+ // h^ello
+ )cpp"};
for (const char *Test : Tests) {
Annotations T(Test);
auto AST = TestTU::withCode(T.code()).build();
More information about the cfe-commits
mailing list