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" @@ -82,6 +83,10 @@ std::optional firstMatchedProvider(const include_cleaner::Includes &Includes, llvm::ArrayRef Providers); + +// Returns the range starting at '#' and ending at EOL. Escaped newlines are not +// handled. +clangd::Range getIncludeRange(llvm::StringRef Code, unsigned HashOffset); } // namespace clangd } // namespace clang 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 @@ -58,11 +58,7 @@ static bool AnalyzeStdlib = false; 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 getIncludeRange(llvm::StringRef Code, unsigned HashOffset) { clangd::Range Result; Result.end = Result.start = offsetToPosition(Code, HashOffset); @@ -74,6 +70,8 @@ return Result; } +namespace { + 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 +222,7 @@ D.InsideMainFile = true; D.Severity = DiagnosticsEngine::Warning; D.Tags.push_back(Unnecessary); - D.Range = getDiagnosticRange(Code, Inc->HashOffset); + D.Range = getIncludeRange(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/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 = + getIncludeRange(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]]();