Author: Aleksandr Platonov Date: 2020-08-03T15:43:05+02:00 New Revision: 3ae25b7a09ded12ff63acec0efcf8c7d715114fe
URL: https://github.com/llvm/llvm-project/commit/3ae25b7a09ded12ff63acec0efcf8c7d715114fe DIFF: https://github.com/llvm/llvm-project/commit/3ae25b7a09ded12ff63acec0efcf8c7d715114fe.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 (cherry picked from commit 05b173466142596b3297ab02e423574cb74b3799) 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 c208e953f2ab..2a82dfd66499 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(); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
