Skip to content

Commit 33e882d

Browse files
committed
[clangd] Add bool return type to Index::refs API.
Summary: Similar to fuzzyFind, the bool indicates whether there are more xref results. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: merge_guards_bot, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70139
1 parent f749901 commit 33e882d

File tree

12 files changed

+58
-25
lines changed

12 files changed

+58
-25
lines changed

clang-tools-extra/clangd/index/Index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void SwapIndex::lookup(const LookupRequest &R,
6565
llvm::function_ref<void(const Symbol &)> CB) const {
6666
return snapshot()->lookup(R, CB);
6767
}
68-
void SwapIndex::refs(const RefsRequest &R,
68+
bool SwapIndex::refs(const RefsRequest &R,
6969
llvm::function_ref<void(const Ref &)> CB) const {
7070
return snapshot()->refs(R, CB);
7171
}

clang-tools-extra/clangd/index/Index.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ class SymbolIndex {
107107
///
108108
/// Results should be returned in arbitrary order.
109109
/// The returned result must be deep-copied if it's used outside Callback.
110-
virtual void refs(const RefsRequest &Req,
110+
///
111+
/// Returns true if there will be more results (limited by Req.Limit);
112+
virtual bool refs(const RefsRequest &Req,
111113
llvm::function_ref<void(const Ref &)> Callback) const = 0;
112114

113115
/// Finds all relations (S, P, O) stored in the index such that S is among
@@ -136,7 +138,7 @@ class SwapIndex : public SymbolIndex {
136138
llvm::function_ref<void(const Symbol &)>) const override;
137139
void lookup(const LookupRequest &,
138140
llvm::function_ref<void(const Symbol &)>) const override;
139-
void refs(const RefsRequest &,
141+
bool refs(const RefsRequest &,
140142
llvm::function_ref<void(const Ref &)>) const override;
141143
void relations(const RelationsRequest &,
142144
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

clang-tools-extra/clangd/index/MemIndex.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,30 @@ void MemIndex::lookup(const LookupRequest &Req,
6767
}
6868
}
6969

70-
void MemIndex::refs(const RefsRequest &Req,
70+
bool MemIndex::refs(const RefsRequest &Req,
7171
llvm::function_ref<void(const Ref &)> Callback) const {
7272
trace::Span Tracer("MemIndex refs");
7373
uint32_t Remaining =
7474
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
75+
bool More = false;
7576
for (const auto &ReqID : Req.IDs) {
7677
auto SymRefs = Refs.find(ReqID);
7778
if (SymRefs == Refs.end())
7879
continue;
7980
for (const auto &O : SymRefs->second) {
80-
if (Remaining > 0 && static_cast<int>(Req.Filter & O.Kind)) {
81+
if (!static_cast<int>(Req.Filter & O.Kind))
82+
continue;
83+
if (Remaining == 0) {
84+
More = true;
85+
break;
86+
}
87+
if (Remaining > 0) {
8188
--Remaining;
8289
Callback(O);
8390
}
8491
}
8592
}
93+
return More;
8694
}
8795

8896
void MemIndex::relations(

clang-tools-extra/clangd/index/MemIndex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MemIndex : public SymbolIndex {
5555
void lookup(const LookupRequest &Req,
5656
llvm::function_ref<void(const Symbol &)> Callback) const override;
5757

58-
void refs(const RefsRequest &Req,
58+
bool refs(const RefsRequest &Req,
5959
llvm::function_ref<void(const Ref &)> Callback) const override;
6060

6161
void relations(const RelationsRequest &Req,

clang-tools-extra/clangd/index/Merge.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ void MergedIndex::lookup(
8989
Callback(*Sym);
9090
}
9191

92-
void MergedIndex::refs(const RefsRequest &Req,
92+
bool MergedIndex::refs(const RefsRequest &Req,
9393
llvm::function_ref<void(const Ref &)> Callback) const {
9494
trace::Span Tracer("MergedIndex refs");
95+
bool More = false;
9596
uint32_t Remaining =
9697
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
9798
// We don't want duplicated refs from the static/dynamic indexes,
@@ -103,21 +104,26 @@ void MergedIndex::refs(const RefsRequest &Req,
103104
// refs were removed (we will report stale ones from the static index).
104105
// Ultimately we should explicit check which index has the file instead.
105106
llvm::StringSet<> DynamicIndexFileURIs;
106-
Dynamic->refs(Req, [&](const Ref &O) {
107+
More |= Dynamic->refs(Req, [&](const Ref &O) {
107108
DynamicIndexFileURIs.insert(O.Location.FileURI);
108109
Callback(O);
109110
--Remaining;
110111
});
111-
if (Remaining == 0)
112-
return;
112+
if (Remaining == 0 && More)
113+
return More;
113114
// We return less than Req.Limit if static index returns more refs for dirty
114115
// files.
115-
Static->refs(Req, [&](const Ref &O) {
116-
if (Remaining > 0 && !DynamicIndexFileURIs.count(O.Location.FileURI)) {
116+
More |= Static->refs(Req, [&](const Ref &O) {
117+
if (DynamicIndexFileURIs.count(O.Location.FileURI))
118+
return; // ignore refs that have been seen from dynamic index.
119+
if (Remaining == 0)
120+
More = true;
121+
if (Remaining > 0) {
117122
--Remaining;
118123
Callback(O);
119124
}
120125
});
126+
return More;
121127
}
122128

123129
void MergedIndex::relations(

clang-tools-extra/clangd/index/Merge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MergedIndex : public SymbolIndex {
4040
llvm::function_ref<void(const Symbol &)>) const override;
4141
void lookup(const LookupRequest &,
4242
llvm::function_ref<void(const Symbol &)>) const override;
43-
void refs(const RefsRequest &,
43+
bool refs(const RefsRequest &,
4444
llvm::function_ref<void(const Ref &)>) const override;
4545
void relations(const RelationsRequest &,
4646
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

clang-tools-extra/clangd/index/dex/Dex.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,26 @@ void Dex::lookup(const LookupRequest &Req,
249249
}
250250
}
251251

252-
void Dex::refs(const RefsRequest &Req,
252+
bool Dex::refs(const RefsRequest &Req,
253253
llvm::function_ref<void(const Ref &)> Callback) const {
254254
trace::Span Tracer("Dex refs");
255255
uint32_t Remaining =
256256
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
257+
bool More = false;
257258
for (const auto &ID : Req.IDs)
258259
for (const auto &Ref : Refs.lookup(ID)) {
259-
if (Remaining > 0 && static_cast<int>(Req.Filter & Ref.Kind)) {
260+
if (!static_cast<int>(Req.Filter & Ref.Kind))
261+
continue;
262+
if (Remaining == 0) {
263+
More = true;
264+
break;
265+
}
266+
if (Remaining > 0) {
260267
--Remaining;
261268
Callback(Ref);
262269
}
263270
}
271+
return More;
264272
}
265273

266274
void Dex::relations(

clang-tools-extra/clangd/index/dex/Dex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Dex : public SymbolIndex {
7777
void lookup(const LookupRequest &Req,
7878
llvm::function_ref<void(const Symbol &)> Callback) const override;
7979

80-
void refs(const RefsRequest &Req,
80+
bool refs(const RefsRequest &Req,
8181
llvm::function_ref<void(const Ref &)> Callback) const override;
8282

8383
void relations(const RelationsRequest &Req,

clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,10 @@ class IndexRequestCollector : public SymbolIndex {
11931193
void lookup(const LookupRequest &,
11941194
llvm::function_ref<void(const Symbol &)>) const override {}
11951195

1196-
void refs(const RefsRequest &,
1197-
llvm::function_ref<void(const Ref &)>) const override {}
1196+
bool refs(const RefsRequest &,
1197+
llvm::function_ref<void(const Ref &)>) const override {
1198+
return false;
1199+
}
11981200

11991201
void relations(const RelationsRequest &,
12001202
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

clang-tools-extra/clangd/unittests/DexTests.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,18 @@ TEST(DexTests, Refs) {
687687
Req.Filter = RefKind::Declaration | RefKind::Definition;
688688

689689
std::vector<std::string> Files;
690-
Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
691-
.refs(Req, [&](const Ref &R) { Files.push_back(R.Location.FileURI); });
690+
EXPECT_FALSE(Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
691+
.refs(Req, [&](const Ref &R) {
692+
Files.push_back(R.Location.FileURI);
693+
}));
692694
EXPECT_THAT(Files, UnorderedElementsAre("foo.h", "foo.cc"));
693695

694696
Req.Limit = 1;
695697
Files.clear();
696-
Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
697-
.refs(Req, [&](const Ref &R) { Files.push_back(R.Location.FileURI); });
698+
EXPECT_TRUE(Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
699+
.refs(Req, [&](const Ref &R) {
700+
Files.push_back(R.Location.FileURI);
701+
}));
698702
EXPECT_THAT(Files, ElementsAre(AnyOf("foo.h", "foo.cc")));
699703
}
700704

0 commit comments

Comments
 (0)