diff --git a/clang-tools-extra/clangd/IncludeCleaner.h b/clang-tools-extra/clangd/IncludeCleaner.h --- 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/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -60,20 +60,6 @@ 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 @@ 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/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -337,6 +337,10 @@ /// 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/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -1242,5 +1242,17 @@ } 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/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -1362,10 +1362,9 @@ 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 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -2299,7 +2299,7 @@ TEST(FindReferences, UsedSymbolsFromInclude) { const char *Tests[] = { - R"cpp([[#include ^"bar.h"]] + R"cpp( [[#include ^"bar.h"]] #include int fstBar = [[bar1]](); int sndBar = [[bar2]]();