Skip to content

Commit

Permalink
[clangd] findNearbyIdentifier(): fix the word search in the token str…
Browse files Browse the repository at this point in the history
…eam.

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

(cherry picked from commit 05b1734)
  • Loading branch information
ArcsinX authored and zmodem committed Aug 3, 2020
1 parent 15bf939 commit 3ae25b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/XRefs.cpp
Expand Up @@ -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()))
Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/unittests/XRefsTests.cpp
Expand Up @@ -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();
Expand Down

0 comments on commit 3ae25b7

Please sign in to comment.