summaryrefslogtreecommitdiffstats
path: root/unittests
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2017-12-19 11:37:40 +0000
committerEric Liu <ioeric@google.com>2017-12-19 11:37:40 +0000
commitd572b55b00080681f153134669dcc4d0975725bf (patch)
tree42bcea6280fae55870c304d249d9a923bfce0b15 /unittests
parentb3f031f8443fa17026a8dbbc6fb880248bea937c (diff)
[clangd] Support filtering by fixing scopes in fuzzyFind.
Summary: When scopes are specified, only match symbols from scopes. Reviewers: sammccall Reviewed By: sammccall Subscribers: klimek, ilya-biryukov, cfe-commits Differential Revision: https://reviews.llvm.org/D41367 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@321067 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/clangd/FileIndexTests.cpp21
-rw-r--r--unittests/clangd/IndexTests.cpp95
-rw-r--r--unittests/clangd/SymbolCollectorTests.cpp11
3 files changed, 102 insertions, 25 deletions
diff --git a/unittests/clangd/FileIndexTests.cpp b/unittests/clangd/FileIndexTests.cpp
index c79de441..2371d7dc 100644
--- a/unittests/clangd/FileIndexTests.cpp
+++ b/unittests/clangd/FileIndexTests.cpp
@@ -24,7 +24,7 @@ namespace {
Symbol symbol(llvm::StringRef ID) {
Symbol Sym;
Sym.ID = SymbolID(ID);
- Sym.QualifiedName = ID;
+ Sym.Name = ID;
return Sym;
}
@@ -37,7 +37,7 @@ std::vector<std::string>
getSymbolNames(const std::vector<const Symbol *> &Symbols) {
std::vector<std::string> Names;
for (const Symbol *Sym : Symbols)
- Names.push_back(Sym->QualifiedName);
+ Names.push_back(Sym->Name);
return Names;
}
@@ -92,8 +92,9 @@ std::vector<std::string> match(const SymbolIndex &I,
const FuzzyFindRequest &Req) {
std::vector<std::string> Matches;
auto Ctx = Context::empty();
- I.fuzzyFind(Ctx, Req,
- [&](const Symbol &Sym) { Matches.push_back(Sym.QualifiedName); });
+ I.fuzzyFind(Ctx, Req, [&](const Symbol &Sym) {
+ Matches.push_back(Sym.Scope + (Sym.Scope.empty() ? "" : "::") + Sym.Name);
+ });
return Matches;
}
@@ -122,7 +123,8 @@ TEST(FileIndexTest, IndexAST) {
build("f1", "namespace ns { void f() {} class X {}; }").getPointer());
FuzzyFindRequest Req;
- Req.Query = "ns::";
+ Req.Query = "";
+ Req.Scopes = {"ns"};
EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X"));
}
@@ -150,9 +152,9 @@ TEST(FileIndexTest, IndexMultiASTAndDeduplicate) {
build("f2", "namespace ns { void ff() {} class X {}; }").getPointer());
FuzzyFindRequest Req;
- Req.Query = "ns::";
- EXPECT_THAT(match(M, Req),
- UnorderedElementsAre("ns::f", "ns::X", "ns::ff"));
+ Req.Query = "";
+ Req.Scopes = {"ns"};
+ EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X", "ns::ff"));
}
TEST(FileIndexTest, RemoveAST) {
@@ -163,7 +165,8 @@ TEST(FileIndexTest, RemoveAST) {
build("f1", "namespace ns { void f() {} class X {}; }").getPointer());
FuzzyFindRequest Req;
- Req.Query = "ns::";
+ Req.Query = "";
+ Req.Scopes = {"ns"};
EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X"));
M.update(Ctx, "f1", nullptr);
diff --git a/unittests/clangd/IndexTests.cpp b/unittests/clangd/IndexTests.cpp
index e94d2127..2852da0b 100644
--- a/unittests/clangd/IndexTests.cpp
+++ b/unittests/clangd/IndexTests.cpp
@@ -19,10 +19,17 @@ namespace clangd {
namespace {
-Symbol symbol(llvm::StringRef ID) {
+Symbol symbol(llvm::StringRef QName) {
Symbol Sym;
- Sym.ID = SymbolID(ID);
- Sym.QualifiedName = ID;
+ Sym.ID = SymbolID(QName.str());
+ size_t Pos = QName.rfind("::");
+ if (Pos == llvm::StringRef::npos) {
+ Sym.Name = QName;
+ Sym.Scope = "";
+ } else {
+ Sym.Name = QName.substr(Pos + 2);
+ Sym.Scope = QName.substr(0, Pos);
+ }
return Sym;
}
@@ -31,19 +38,19 @@ struct SlabAndPointers {
std::vector<const Symbol *> Pointers;
};
-// Create a slab of symbols with IDs and names [Begin, End]. The life time of
-// the slab is managed by the returned shared pointer. If \p WeakSymbols is
-// provided, it will be pointed to the managed object in the returned shared
-// pointer.
+// Create a slab of symbols with the given qualified names as both IDs and
+// names. The life time of the slab is managed by the returned shared pointer.
+// If \p WeakSymbols is provided, it will be pointed to the managed object in
+// the returned shared pointer.
std::shared_ptr<std::vector<const Symbol *>>
-generateNumSymbols(int Begin, int End,
- std::weak_ptr<SlabAndPointers> *WeakSymbols = nullptr) {
+generateSymbols(std::vector<std::string> QualifiedNames,
+ std::weak_ptr<SlabAndPointers> *WeakSymbols = nullptr) {
auto Slab = std::make_shared<SlabAndPointers>();
if (WeakSymbols)
*WeakSymbols = Slab;
- for (int i = Begin; i <= End; i++)
- Slab->Slab.insert(symbol(std::to_string(i)));
+ for (llvm::StringRef QName : QualifiedNames)
+ Slab->Slab.insert(symbol(QName));
for (const auto &Sym : Slab->Slab)
Slab->Pointers.push_back(&Sym.second);
@@ -52,12 +59,24 @@ generateNumSymbols(int Begin, int End,
return {std::move(Slab), Pointers};
}
+// Create a slab of symbols with IDs and names [Begin, End], otherwise identical
+// to the `generateSymbols` above.
+std::shared_ptr<std::vector<const Symbol *>>
+generateNumSymbols(int Begin, int End,
+ std::weak_ptr<SlabAndPointers> *WeakSymbols = nullptr) {
+ std::vector<std::string> Names;
+ for (int i = Begin; i <= End; i++)
+ Names.push_back(std::to_string(i));
+ return generateSymbols(Names, WeakSymbols);
+}
+
std::vector<std::string> match(const SymbolIndex &I,
const FuzzyFindRequest &Req) {
std::vector<std::string> Matches;
auto Ctx = Context::empty();
- I.fuzzyFind(Ctx, Req,
- [&](const Symbol &Sym) { Matches.push_back(Sym.QualifiedName); });
+ I.fuzzyFind(Ctx, Req, [&](const Symbol &Sym) {
+ Matches.push_back(Sym.Scope + (Sym.Scope.empty() ? "" : "::") + Sym.Name);
+ });
return Matches;
}
@@ -110,6 +129,56 @@ TEST(MemIndexTest, MemIndexLimitedNumMatches) {
EXPECT_EQ(Matches.size(), Req.MaxCandidateCount);
}
+TEST(MemIndexTest, MatchQualifiedNamesWithoutSpecificScope) {
+ MemIndex I;
+ I.build(generateSymbols({"a::xyz", "b::yz", "yz"}));
+ FuzzyFindRequest Req;
+ Req.Query = "y";
+ auto Matches = match(I, Req);
+ EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "b::yz", "yz"));
+}
+
+TEST(MemIndexTest, MatchQualifiedNamesWithGlobalScope) {
+ MemIndex I;
+ I.build(generateSymbols({"a::xyz", "b::yz", "yz"}));
+ FuzzyFindRequest Req;
+ Req.Query = "y";
+ Req.Scopes.push_back("");
+ auto Matches = match(I, Req);
+ EXPECT_THAT(match(I, Req), UnorderedElementsAre("yz"));
+}
+
+TEST(MemIndexTest, MatchQualifiedNamesWithOneScope) {
+ MemIndex I;
+ I.build(generateSymbols({"a::xyz", "a::yy", "a::xz", "b::yz", "yz"}));
+ FuzzyFindRequest Req;
+ Req.Query = "y";
+ Req.Scopes.push_back("a");
+ auto Matches = match(I, Req);
+ EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "a::yy"));
+}
+
+TEST(MemIndexTest, MatchQualifiedNamesWithMultipleScopes) {
+ MemIndex I;
+ I.build(generateSymbols({"a::xyz", "a::yy", "a::xz", "b::yz", "yz"}));
+ FuzzyFindRequest Req;
+ Req.Query = "y";
+ Req.Scopes.push_back("a");
+ Req.Scopes.push_back("b");
+ auto Matches = match(I, Req);
+ EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "a::yy", "b::yz"));
+}
+
+TEST(MemIndexTest, NoMatchNestedScopes) {
+ MemIndex I;
+ I.build(generateSymbols({"a::xyz", "a::b::yy"}));
+ FuzzyFindRequest Req;
+ Req.Query = "y";
+ Req.Scopes.push_back("a");
+ auto Matches = match(I, Req);
+ EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz"));
+}
+
} // namespace
} // namespace clangd
} // namespace clang
diff --git a/unittests/clangd/SymbolCollectorTests.cpp b/unittests/clangd/SymbolCollectorTests.cpp
index 7e97a9da..aeaffefb 100644
--- a/unittests/clangd/SymbolCollectorTests.cpp
+++ b/unittests/clangd/SymbolCollectorTests.cpp
@@ -31,7 +31,10 @@ using testing::Field;
using testing::UnorderedElementsAre;
// GMock helpers for matching Symbol.
-MATCHER_P(QName, Name, "") { return arg.second.QualifiedName == Name; }
+MATCHER_P(QName, Name, "") {
+ return (arg.second.Scope + (arg.second.Scope.empty() ? "" : "::") +
+ arg.second.Name) == Name;
+}
namespace clang {
namespace clangd {
@@ -111,7 +114,8 @@ TEST_F(SymbolCollectorTest, YAMLConversions) {
const std::string YAML1 = R"(
---
ID: 057557CEBF6E6B2DD437FBF60CC58F352D1DF856
-QualifiedName: 'clang::Foo1'
+Name: 'Foo1'
+Scope: 'clang'
SymInfo:
Kind: Function
Lang: Cpp
@@ -124,7 +128,8 @@ CanonicalDeclaration:
const std::string YAML2 = R"(
---
ID: 057557CEBF6E6B2DD437FBF60CC58F352D1DF858
-QualifiedName: 'clang::Foo2'
+Name: 'Foo2'
+Scope: 'clang'
SymInfo:
Kind: Function
Lang: Cpp