diff options
author | Etienne Bergeron <etienneb@google.com> | 2016-05-30 15:25:25 +0000 |
---|---|---|
committer | Etienne Bergeron <etienneb@google.com> | 2016-05-30 15:25:25 +0000 |
commit | c33ca9eb5710ee67a91bfde52cf2deaa1e5b512e (patch) | |
tree | fa127171190fc18ec458b0c698536d660e067bac /unittests/ASTMatchers/ASTMatchersTraversalTest.cpp | |
parent | 4cc451f6796c778574e92e245fb657ffa778c9be (diff) |
[ASTMatchers] Add support of hasCondition for SwitchStmt.
Summary:
The switch statement could be added to the hasCondition matcher.
Example:
```
clang-query> match switchStmt(hasCondition(ignoringImpCasts(declRefExpr())))
```
Output:
```
Match #1:
Binding for "root":
SwitchStmt 0x2f9b528 </usr/local/google/home/etienneb/examples/enum.cc:35:3, line:38:3>
|-<<<NULL>>>
|-ImplicitCastExpr 0x2f9b510 <line:35:11> 'int' <IntegralCast>
| `-ImplicitCastExpr 0x2f9b4f8 <col:11> 'enum Color' <LValueToRValue>
| `-DeclRefExpr 0x2f9b4d0 <col:11> 'enum Color' lvalue Var 0x2f9a118 'C' 'enum Color'
`-CompoundStmt 0x2f9b610 <col:14, line:38:3>
|-CaseStmt 0x2f9b578 <line:36:3, col:22>
| |-ImplicitCastExpr 0x2f9b638 <col:8> 'int' <IntegralCast>
| | `-DeclRefExpr 0x2f9b550 <col:8> 'enum Size' EnumConstant 0x2f99e40 'Small' 'enum Size'
| |-<<<NULL>>>
| `-ReturnStmt 0x2f9b5d0 <col:15, col:22>
| `-IntegerLiteral 0x2f9b5b0 <col:22> 'int' 1
`-DefaultStmt 0x2f9b5f0 <line:37:3, col:12>
`-BreakStmt 0x2f9b5e8 <col:12>
1 match.
```
Reviewers: aaron.ballman, sbenza, klimek
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D20767
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@271208 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ASTMatchers/ASTMatchersTraversalTest.cpp')
-rw-r--r-- | unittests/ASTMatchers/ASTMatchersTraversalTest.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index a2e1dee261..481b7e3041 100644 --- a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -958,6 +958,28 @@ TEST(Matcher, VisitsTemplateInstantiations) { callee(cxxMethodDecl(hasName("x")))))))); } +TEST(Matcher, HasCondition) { + StatementMatcher IfStmt = + ifStmt(hasCondition(cxxBoolLiteral(equals(true)))); + EXPECT_TRUE(matches("void x() { if (true) {} }", IfStmt)); + EXPECT_TRUE(notMatches("void x() { if (false) {} }", IfStmt)); + + StatementMatcher ForStmt = + forStmt(hasCondition(cxxBoolLiteral(equals(true)))); + EXPECT_TRUE(matches("void x() { for (;true;) {} }", ForStmt)); + EXPECT_TRUE(notMatches("void x() { for (;false;) {} }", ForStmt)); + + StatementMatcher WhileStmt = + whileStmt(hasCondition(cxxBoolLiteral(equals(true)))); + EXPECT_TRUE(matches("void x() { while (true) {} }", WhileStmt)); + EXPECT_TRUE(notMatches("void x() { while (false) {} }", WhileStmt)); + + StatementMatcher SwitchStmt = + switchStmt(hasCondition(integerLiteral(equals(42)))); + EXPECT_TRUE(matches("void x() { switch (42) {case 42:;} }", SwitchStmt)); + EXPECT_TRUE(notMatches("void x() { switch (43) {case 43:;} }", SwitchStmt)); +} + TEST(For, ForLoopInternals) { EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", forStmt(hasCondition(anything())))); |