[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
This commit is contained in:
Haojian Wu 2019-11-13 14:42:26 +01:00
parent f7499011ca
commit 33e882d5ad
12 changed files with 58 additions and 25 deletions

View File

@ -65,7 +65,7 @@ void SwapIndex::lookup(const LookupRequest &R,
llvm::function_ref<void(const Symbol &)> CB) const {
return snapshot()->lookup(R, CB);
}
void SwapIndex::refs(const RefsRequest &R,
bool SwapIndex::refs(const RefsRequest &R,
llvm::function_ref<void(const Ref &)> CB) const {
return snapshot()->refs(R, CB);
}

View File

@ -107,7 +107,9 @@ public:
///
/// Results should be returned in arbitrary order.
/// The returned result must be deep-copied if it's used outside Callback.
virtual void refs(const RefsRequest &Req,
///
/// Returns true if there will be more results (limited by Req.Limit);
virtual bool refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const = 0;
/// Finds all relations (S, P, O) stored in the index such that S is among
@ -136,7 +138,7 @@ public:
llvm::function_ref<void(const Symbol &)>) const override;
void lookup(const LookupRequest &,
llvm::function_ref<void(const Symbol &)>) const override;
void refs(const RefsRequest &,
bool refs(const RefsRequest &,
llvm::function_ref<void(const Ref &)>) const override;
void relations(const RelationsRequest &,
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

View File

@ -67,22 +67,30 @@ void MemIndex::lookup(const LookupRequest &Req,
}
}
void MemIndex::refs(const RefsRequest &Req,
bool MemIndex::refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const {
trace::Span Tracer("MemIndex refs");
uint32_t Remaining =
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
bool More = false;
for (const auto &ReqID : Req.IDs) {
auto SymRefs = Refs.find(ReqID);
if (SymRefs == Refs.end())
continue;
for (const auto &O : SymRefs->second) {
if (Remaining > 0 && static_cast<int>(Req.Filter & O.Kind)) {
if (!static_cast<int>(Req.Filter & O.Kind))
continue;
if (Remaining == 0) {
More = true;
break;
}
if (Remaining > 0) {
--Remaining;
Callback(O);
}
}
}
return More;
}
void MemIndex::relations(

View File

@ -55,7 +55,7 @@ public:
void lookup(const LookupRequest &Req,
llvm::function_ref<void(const Symbol &)> Callback) const override;
void refs(const RefsRequest &Req,
bool refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const override;
void relations(const RelationsRequest &Req,

View File

@ -89,9 +89,10 @@ void MergedIndex::lookup(
Callback(*Sym);
}
void MergedIndex::refs(const RefsRequest &Req,
bool MergedIndex::refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const {
trace::Span Tracer("MergedIndex refs");
bool More = false;
uint32_t Remaining =
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
// We don't want duplicated refs from the static/dynamic indexes,
@ -103,21 +104,26 @@ void MergedIndex::refs(const RefsRequest &Req,
// refs were removed (we will report stale ones from the static index).
// Ultimately we should explicit check which index has the file instead.
llvm::StringSet<> DynamicIndexFileURIs;
Dynamic->refs(Req, [&](const Ref &O) {
More |= Dynamic->refs(Req, [&](const Ref &O) {
DynamicIndexFileURIs.insert(O.Location.FileURI);
Callback(O);
--Remaining;
});
if (Remaining == 0)
return;
if (Remaining == 0 && More)
return More;
// We return less than Req.Limit if static index returns more refs for dirty
// files.
Static->refs(Req, [&](const Ref &O) {
if (Remaining > 0 && !DynamicIndexFileURIs.count(O.Location.FileURI)) {
More |= Static->refs(Req, [&](const Ref &O) {
if (DynamicIndexFileURIs.count(O.Location.FileURI))
return; // ignore refs that have been seen from dynamic index.
if (Remaining == 0)
More = true;
if (Remaining > 0) {
--Remaining;
Callback(O);
}
});
return More;
}
void MergedIndex::relations(

View File

@ -40,7 +40,7 @@ public:
llvm::function_ref<void(const Symbol &)>) const override;
void lookup(const LookupRequest &,
llvm::function_ref<void(const Symbol &)>) const override;
void refs(const RefsRequest &,
bool refs(const RefsRequest &,
llvm::function_ref<void(const Ref &)>) const override;
void relations(const RelationsRequest &,
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

View File

@ -249,18 +249,26 @@ void Dex::lookup(const LookupRequest &Req,
}
}
void Dex::refs(const RefsRequest &Req,
bool Dex::refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const {
trace::Span Tracer("Dex refs");
uint32_t Remaining =
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
bool More = false;
for (const auto &ID : Req.IDs)
for (const auto &Ref : Refs.lookup(ID)) {
if (Remaining > 0 && static_cast<int>(Req.Filter & Ref.Kind)) {
if (!static_cast<int>(Req.Filter & Ref.Kind))
continue;
if (Remaining == 0) {
More = true;
break;
}
if (Remaining > 0) {
--Remaining;
Callback(Ref);
}
}
return More;
}
void Dex::relations(

View File

@ -77,7 +77,7 @@ public:
void lookup(const LookupRequest &Req,
llvm::function_ref<void(const Symbol &)> Callback) const override;
void refs(const RefsRequest &Req,
bool refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)> Callback) const override;
void relations(const RelationsRequest &Req,

View File

@ -1193,8 +1193,10 @@ public:
void lookup(const LookupRequest &,
llvm::function_ref<void(const Symbol &)>) const override {}
void refs(const RefsRequest &,
llvm::function_ref<void(const Ref &)>) const override {}
bool refs(const RefsRequest &,
llvm::function_ref<void(const Ref &)>) const override {
return false;
}
void relations(const RelationsRequest &,
llvm::function_ref<void(const SymbolID &, const Symbol &)>)

View File

@ -687,14 +687,18 @@ TEST(DexTests, Refs) {
Req.Filter = RefKind::Declaration | RefKind::Definition;
std::vector<std::string> Files;
Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
.refs(Req, [&](const Ref &R) { Files.push_back(R.Location.FileURI); });
EXPECT_FALSE(Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
.refs(Req, [&](const Ref &R) {
Files.push_back(R.Location.FileURI);
}));
EXPECT_THAT(Files, UnorderedElementsAre("foo.h", "foo.cc"));
Req.Limit = 1;
Files.clear();
Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
.refs(Req, [&](const Ref &R) { Files.push_back(R.Location.FileURI); });
EXPECT_TRUE(Dex(std::vector<Symbol>{Foo, Bar}, Refs, RelationSlab())
.refs(Req, [&](const Ref &R) {
Files.push_back(R.Location.FileURI);
}));
EXPECT_THAT(Files, ElementsAre(AnyOf("foo.h", "foo.cc")));
}

View File

@ -389,7 +389,8 @@ TEST(MergeIndexTest, Refs) {
RefsRequest Request;
Request.IDs = {Foo.ID};
RefSlab::Builder Results;
Merge.refs(Request, [&](const Ref &O) { Results.insert(Foo.ID, O); });
EXPECT_FALSE(
Merge.refs(Request, [&](const Ref &O) { Results.insert(Foo.ID, O); }));
EXPECT_THAT(
std::move(Results).build(),
ElementsAre(Pair(
@ -400,7 +401,8 @@ TEST(MergeIndexTest, Refs) {
Request.Limit = 1;
RefSlab::Builder Results2;
Merge.refs(Request, [&](const Ref &O) { Results2.insert(Foo.ID, O); });
EXPECT_TRUE(
Merge.refs(Request, [&](const Ref &O) { Results2.insert(Foo.ID, O); }));
EXPECT_THAT(std::move(Results2).build(),
ElementsAre(Pair(
_, ElementsAre(AnyOf(FileURI("unittest:///test.cc"),

View File

@ -2161,9 +2161,10 @@ TEST(FindReferences, NeedsIndex) {
TEST(FindReferences, NoQueryForLocalSymbols) {
struct RecordingIndex : public MemIndex {
mutable Optional<llvm::DenseSet<SymbolID>> RefIDs;
void refs(const RefsRequest &Req,
bool refs(const RefsRequest &Req,
llvm::function_ref<void(const Ref &)>) const override {
RefIDs = Req.IDs;
return false;
}
};