diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2014-01-23 22:48:38 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2014-01-23 22:48:38 +0000 |
commit | 8f69550afc20daa00d49ecb5eda3343fb9fb414d (patch) | |
tree | 0bf343226c764a140a563deb1bb5402606c7f869 /unittests/ASTMatchers | |
parent | 77852e7357230e3c956f50f559bcc491569dfc0f (diff) | |
download | clang-8f69550afc20daa00d49ecb5eda3343fb9fb414d.tar.gz clang-8f69550afc20daa00d49ecb5eda3343fb9fb414d.tar.bz2 clang-8f69550afc20daa00d49ecb5eda3343fb9fb414d.tar.xz |
Introduce Registry::getCompletions.
This returns a list of valid (and useful) completions for a context (a list
of outer matchers), ordered by decreasing relevance then alphabetically. It
will be used by the matcher parser to implement completion.
Differential Revision: http://llvm-reviews.chandlerc.com/D2210
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@199950 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ASTMatchers')
-rw-r--r-- | unittests/ASTMatchers/Dynamic/RegistryTest.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp index c260b467ad..8426649d3e 100644 --- a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -84,6 +84,50 @@ public: EXPECT_EQ("", DummyError.toStringFull()); return Out; } + + typedef std::vector<MatcherCompletion> CompVector; + + CompVector getCompletions() { + return Registry::getCompletions( + llvm::ArrayRef<std::pair<MatcherCtor, unsigned> >()); + } + + CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1) { + std::vector<std::pair<MatcherCtor, unsigned> > Context; + llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName1); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo1)); + return Registry::getCompletions(Context); + } + + CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1, + StringRef MatcherName2, unsigned ArgNo2) { + std::vector<std::pair<MatcherCtor, unsigned> > Context; + llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName1); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo1)); + Ctor = lookupMatcherCtor(MatcherName2); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo2)); + return Registry::getCompletions(Context); + } + + bool hasCompletion(const CompVector &Comps, StringRef TypedText, + StringRef MatcherDecl = StringRef(), unsigned *Index = 0) { + for (CompVector::const_iterator I = Comps.begin(), E = Comps.end(); I != E; + ++I) { + if (I->TypedText == TypedText && + (MatcherDecl.empty() || I->MatcherDecl == MatcherDecl)) { + if (Index) + *Index = I - Comps.begin(); + return true; + } + } + return false; + } }; TEST_F(RegistryTest, CanConstructNoArgs) { @@ -378,6 +422,47 @@ TEST_F(RegistryTest, Errors) { Error->toString()); } +TEST_F(RegistryTest, Completion) { + CompVector Comps = getCompletions(); + EXPECT_TRUE(hasCompletion( + Comps, "hasParent(", "Matcher<Decl|Stmt> hasParent(Matcher<Decl|Stmt>)")); + EXPECT_TRUE(hasCompletion(Comps, "whileStmt(", + "Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)")); + + CompVector WhileComps = getCompletions("whileStmt", 0); + + unsigned HasBodyIndex, HasParentIndex, AllOfIndex; + EXPECT_TRUE(hasCompletion(WhileComps, "hasBody(", + "Matcher<WhileStmt> hasBody(Matcher<Stmt>)", + &HasBodyIndex)); + EXPECT_TRUE(hasCompletion(WhileComps, "hasParent(", + "Matcher<Stmt> hasParent(Matcher<Decl|Stmt>)", + &HasParentIndex)); + EXPECT_TRUE(hasCompletion(WhileComps, "allOf(", + "Matcher<T> allOf(Matcher<T>...)", &AllOfIndex)); + EXPECT_GT(HasParentIndex, HasBodyIndex); + EXPECT_GT(AllOfIndex, HasParentIndex); + + EXPECT_FALSE(hasCompletion(WhileComps, "whileStmt(")); + EXPECT_FALSE(hasCompletion(WhileComps, "ifStmt(")); + + CompVector AllOfWhileComps = + getCompletions("allOf", 0, "whileStmt", 0); + ASSERT_EQ(AllOfWhileComps.size(), WhileComps.size()); + EXPECT_TRUE(std::equal(WhileComps.begin(), WhileComps.end(), + AllOfWhileComps.begin())); + + CompVector DeclWhileComps = + getCompletions("decl", 0, "whileStmt", 0); + EXPECT_EQ(0u, DeclWhileComps.size()); + + CompVector NamedDeclComps = getCompletions("namedDecl", 0); + EXPECT_TRUE( + hasCompletion(NamedDeclComps, "isPublic()", "Matcher<Decl> isPublic()")); + EXPECT_TRUE(hasCompletion(NamedDeclComps, "hasName(\"", + "Matcher<NamedDecl> hasName(string)")); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers |