aboutsummaryrefslogtreecommitdiff
path: root/unittests/ASTMatchers/ASTMatchersTest.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2012-10-24 14:47:44 +0000
committerManuel Klimek <klimek@google.com>2012-10-24 14:47:44 +0000
commit3e2aa99e992b89bd421ac2a6bf79307114d257fc (patch)
tree3e4707ed91c371667f38b5b14b2a6a05e36d14d5 /unittests/ASTMatchers/ASTMatchersTest.cpp
parent403f2c68046eba1a9d5e3f7b52b8481573e56d40 (diff)
Adds the possibility to run ASTMatchFinder over arbitrary AST nodes.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@166567 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ASTMatchers/ASTMatchersTest.cpp')
-rw-r--r--unittests/ASTMatchers/ASTMatchersTest.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 5060e807c6..689c91f476 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -3171,5 +3171,42 @@ TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
}
+template <typename T>
+class VerifyRecursiveMatch : public BoundNodesCallback {
+public:
+ explicit VerifyRecursiveMatch(StringRef Id,
+ const internal::Matcher<T> &InnerMatcher)
+ : Id(Id), InnerMatcher(InnerMatcher) {}
+ 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;
+ }
+private:
+ std::string Id;
+ internal::Matcher<T> InnerMatcher;
+};
+
+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")))));
+}
+
+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())));
+}
+
} // end namespace ast_matchers
} // end namespace clang