[PATCH] D155215: [clangd] Fix the range for include reference to itself.
Viktoriia Bakalova via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 14 01:45:18 PDT 2023
VitaNuo updated this revision to Diff 540321.
VitaNuo added a comment.
Re-use the logic from include cleaner in clangd.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D155215/new/
https://reviews.llvm.org/D155215
Files:
clang-tools-extra/clangd/IncludeCleaner.cpp
clang-tools-extra/clangd/IncludeCleaner.h
clang-tools-extra/clangd/XRefs.cpp
clang-tools-extra/clangd/unittests/XRefsTests.cpp
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ 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 <vector>
int fstBar = [[bar1]]();
int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ 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));
Index: clang-tools-extra/clangd/IncludeCleaner.h
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ 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<include_cleaner::Header>
firstMatchedProvider(const include_cleaner::Includes &Includes,
llvm::ArrayRef<include_cleaner::Header> 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
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155215.540321.patch
Type: text/x-patch
Size: 3506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230714/295bdce0/attachment.bin>
More information about the cfe-commits
mailing list