Author: Viktoriia Bakalova Date: 2023-07-14T12:51:14Z New Revision: 96e50797d6ea39d561fc90511152fd30b77c1e62
URL: https://github.com/llvm/llvm-project/commit/96e50797d6ea39d561fc90511152fd30b77c1e62 DIFF: https://github.com/llvm/llvm-project/commit/96e50797d6ea39d561fc90511152fd30b77c1e62.diff LOG: [clangd] Fix the range for include reference to itself. Differential Revision: https://reviews.llvm.org/D155215 Added: Modified: clang-tools-extra/clangd/IncludeCleaner.cpp clang-tools-extra/clangd/IncludeCleaner.h clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/clangd/SourceCode.h clang-tools-extra/clangd/XRefs.cpp clang-tools-extra/clangd/unittests/XRefsTests.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp index 2a8499dade84c0..9a910e5850a9f1 100644 --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -60,20 +60,6 @@ void setIncludeCleanerAnalyzesStdlib(bool B) { AnalyzeStdlib = B; } namespace { -// Returns the range starting at '#' and ending at EOL. Escaped newlines are not -// handled. -clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) { - clangd::Range Result; - Result.end = Result.start = offsetToPosition(Code, HashOffset); - - // Span the warning until the EOL or EOF. - Result.end.character += - lspLength(Code.drop_front(HashOffset).take_until([](char C) { - return C == '\n' || C == '\r'; - })); - return Result; -} - bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) { // Convert the path to Unix slashes and try to match against the filter. llvm::SmallString<64> NormalizedPath(HeaderPath); @@ -224,7 +210,7 @@ std::vector<Diag> generateUnusedIncludeDiagnostics( D.InsideMainFile = true; D.Severity = DiagnosticsEngine::Warning; D.Tags.push_back(Unnecessary); - D.Range = getDiagnosticRange(Code, Inc->HashOffset); + D.Range = rangeTillEOL(Code, Inc->HashOffset); // FIXME(kirillbobyrev): Removing inclusion might break the code if the // used headers are only reachable transitively through this one. Suggest // including them directly instead. diff --git a/clang-tools-extra/clangd/IncludeCleaner.h b/clang-tools-extra/clangd/IncludeCleaner.h index edd89777faf3d0..b1acbdd5484345 100644 --- a/clang-tools-extra/clangd/IncludeCleaner.h +++ b/clang-tools-extra/clangd/IncludeCleaner.h @@ -21,6 +21,7 @@ #include "Diagnostics.h" #include "Headers.h" #include "ParsedAST.h" +#include "Protocol.h" #include "clang-include-cleaner/Types.h" #include "clang/Basic/SourceManager.h" #include "clang/Tooling/Syntax/Tokens.h" diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index 474570cb6459f7..eb0a578b0f4a12 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -1242,5 +1242,17 @@ SourceLocation translatePreamblePatchLocation(SourceLocation Loc, } return Loc; } + +clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset) { + clangd::Range Result; + Result.end = Result.start = offsetToPosition(Code, HashOffset); + + // Span the warning until the EOL or EOF. + Result.end.character += + lspLength(Code.drop_front(HashOffset).take_until([](char C) { + return C == '\n' || C == '\r'; + })); + return Result; +} } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h index 3ba6f8b80ef373..a1bb44c1761202 100644 --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -337,6 +337,10 @@ inline bool isReservedName(llvm::StringRef Name) { /// using presumed locations. Returns \p Loc if it isn't inside preamble patch. SourceLocation translatePreamblePatchLocation(SourceLocation Loc, const SourceManager &SM); + +/// Returns the range starting at offset and spanning the whole line. Escaped +/// newlines are not handled. +clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset); } // namespace clangd } // namespace clang #endif diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index b608296deefad5..29ce1f3e0d0e3b 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -1362,10 +1362,9 @@ maybeFindIncludeReferences(ParsedAST &AST, Position Pos, return std::nullopt; // Add the #include line to the references list. - auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 1; ReferencesResult::Reference Result; - Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0}, - Position{Inc.HashLine, (int)IncludeLen}}; + Result.Loc.range = + rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset); Result.Loc.uri = URIMainFile; Results.References.push_back(std::move(Result)); diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index 1424e6aeefc445..ac19bcce7ea914 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -2299,7 +2299,7 @@ TEST(FindReferences, ExplicitSymbols) { TEST(FindReferences, UsedSymbolsFromInclude) { const char *Tests[] = { - R"cpp([[#include ^"bar.h"]] + R"cpp( [[#include ^"bar.h"]] #include <vector> int fstBar = [[bar1]](); int sndBar = [[bar2]](); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits