diff options
author | Manuel Klimek <klimek@google.com> | 2013-02-01 13:41:35 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-02-01 13:41:35 +0000 |
commit | 191c093af13a7defa77c70e868b53102d679bb2b (patch) | |
tree | 0fb895d5f6372699db0dadad032ba21f01acf437 /clang/unittests/ASTMatchers/ASTMatchersTest.cpp | |
parent | 540bacb1b96d1d9ec323127132c0d14d2b031a3e (diff) | |
download | bcm5719-llvm-191c093af13a7defa77c70e868b53102d679bb2b.tar.gz bcm5719-llvm-191c093af13a7defa77c70e868b53102d679bb2b.zip |
Re-design the convenience interfaces on MatchFinder.
First, this implements a match() method on MatchFinder; this allows us
to get rid of the findAll implementation, as findAll is really a special
case of recursive matchers on match.
Instead of findAll, provide a convenience function match() that lets
users iterate easily over the results instead of needing to implement
callbacks.
llvm-svn: 174172
Diffstat (limited to 'clang/unittests/ASTMatchers/ASTMatchersTest.cpp')
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 4fad5446aea..670f0d40528 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3439,24 +3439,18 @@ TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3))); } -template <typename T> -class VerifyRecursiveMatch : public BoundNodesCallback { +template <typename T> class VerifyMatchOnNode : public BoundNodesCallback { public: - explicit VerifyRecursiveMatch(StringRef Id, - const internal::Matcher<T> &InnerMatcher) - : Id(Id), InnerMatcher(InnerMatcher) {} - - virtual bool run(const BoundNodes *Nodes) { - return false; + VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher) + : Id(Id), InnerMatcher(InnerMatcher) { } + virtual bool run(const BoundNodes *Nodes) { return false; } + virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { const T *Node = Nodes->getNodeAs<T>(Id); - bool Found = false; - MatchFinder Finder; - Finder.addMatcher(InnerMatcher, new VerifyMatch(0, &Found)); - Finder.findAll(*Node, *Context); - return Found; + SmallVector<BoundNodes, 1> Result = match(InnerMatcher, *Node, *Context); + return !Result.empty(); } private: std::string Id; @@ -3464,21 +3458,36 @@ private: }; TEST(MatchFinder, CanMatchDeclarationsRecursively) { - EXPECT_TRUE(matchAndVerifyResultTrue("class X { class Y {}; };", - recordDecl(hasName("::X")).bind("X"), - new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Y"))))); - EXPECT_TRUE(matchAndVerifyResultFalse("class X { class Y {}; };", - recordDecl(hasName("::X")).bind("X"), - new VerifyRecursiveMatch<clang::Decl>("X", recordDecl(hasName("X::Z"))))); + EXPECT_TRUE(matchAndVerifyResultTrue( + "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), + new VerifyMatchOnNode<clang::Decl>( + "X", decl(hasDescendant(recordDecl(hasName("X::Y"))))))); + EXPECT_TRUE(matchAndVerifyResultFalse( + "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), + new VerifyMatchOnNode<clang::Decl>( + "X", decl(hasDescendant(recordDecl(hasName("X::Z"))))))); } TEST(MatchFinder, CanMatchStatementsRecursively) { - EXPECT_TRUE(matchAndVerifyResultTrue("void f() { if (1) { for (;;) { } } }", - ifStmt().bind("if"), - new VerifyRecursiveMatch<clang::Stmt>("if", forStmt()))); - EXPECT_TRUE(matchAndVerifyResultFalse("void f() { if (1) { for (;;) { } } }", - ifStmt().bind("if"), - new VerifyRecursiveMatch<clang::Stmt>("if", declStmt()))); + EXPECT_TRUE(matchAndVerifyResultTrue( + "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), + new VerifyMatchOnNode<clang::Stmt>("if", + stmt(hasDescendant(forStmt()))))); + EXPECT_TRUE(matchAndVerifyResultFalse( + "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), + new VerifyMatchOnNode<clang::Stmt>("if", + stmt(hasDescendant(declStmt()))))); +} + +TEST(MatchFinder, CanMatchSingleNodesRecursively) { + EXPECT_TRUE(matchAndVerifyResultTrue( + "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), + new VerifyMatchOnNode<clang::Decl>( + "X", recordDecl(has(recordDecl(hasName("X::Y"))))))); + EXPECT_TRUE(matchAndVerifyResultFalse( + "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), + new VerifyMatchOnNode<clang::Decl>( + "X", recordDecl(has(recordDecl(hasName("X::Z"))))))); } class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback { |