diff options
author | Manuel Klimek <klimek@google.com> | 2013-02-07 12:42:10 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-02-07 12:42:10 +0000 |
commit | bee085762bce7f552afa0ebc43085102f96e41a4 (patch) | |
tree | 1a21b2cab625c934a2832dd378ae751414c42e0c /clang/unittests/ASTMatchers/ASTMatchersTest.cpp | |
parent | 07c740e2130057967b36e1b2c150e98a663e3099 (diff) | |
download | bcm5719-llvm-bee085762bce7f552afa0ebc43085102f96e41a4.tar.gz bcm5719-llvm-bee085762bce7f552afa0ebc43085102f96e41a4.zip |
Implements equalsNode for Decl and Stmt.
This is a powerful tool when doing iterative refined matches,
where another match is started inside the match callback of the first
one; this allows for example to find out whether the node was in
the condition or body of its parent if-statement.
llvm-svn: 174605
Diffstat (limited to 'clang/unittests/ASTMatchers/ASTMatchersTest.cpp')
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 60a79e86de3..618ac6ec51e 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3546,6 +3546,37 @@ TEST(MatchFinder, CanMatchSingleNodesRecursively) { "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z"))); } +template <typename T> +class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { +public: + virtual bool run(const BoundNodes *Nodes) { return false; } + + virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { + const T *Node = Nodes->getNodeAs<T>(""); + return verify(*Nodes, *Context, Node); + } + + bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { + return selectFirst<const T>( + "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))), + *Node, Context)) != NULL; + } + bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { + return selectFirst<const T>( + "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))), + *Node, Context)) != NULL; + } +}; + +TEST(IsEqualTo, MatchesNodesByIdentity) { + EXPECT_TRUE(matchAndVerifyResultTrue( + "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""), + new VerifyAncestorHasChildIsEqual<Decl>())); + EXPECT_TRUE( + matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""), + new VerifyAncestorHasChildIsEqual<Stmt>())); +} + class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback { public: VerifyStartOfTranslationUnit() : Called(false) {} |