[clangd] Fix the range for include reference to itself.

Differential Revision: https://reviews.llvm.org/D155215
This commit is contained in:
Viktoriia Bakalova
2023-07-13 16:26:36 +00:00
parent af744f0b84
commit 96e50797d6
6 changed files with 21 additions and 19 deletions

View File

@@ -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.

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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));

View File

@@ -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]]();