r271208 - [ASTMatchers] Add support of hasCondition for SwitchStmt.
Etienne Bergeron via cfe-commits
cfe-commits at lists.llvm.org
Mon May 30 08:25:34 PDT 2016
Author: etienneb
Date: Mon May 30 10:25:25 2016
New Revision: 271208
URL: http://llvm.org/viewvc/llvm-project?rev=271208&view=rev
Log:
[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
Modified:
cfe/trunk/docs/LibASTMatchersReference.html
cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Modified: cfe/trunk/docs/LibASTMatchersReference.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LibASTMatchersReference.html?rev=271208&r1=271207&r2=271208&view=diff
==============================================================================
--- cfe/trunk/docs/LibASTMatchersReference.html (original)
+++ cfe/trunk/docs/LibASTMatchersReference.html Mon May 30 10:25:25 2016
@@ -3536,9 +3536,9 @@ Usable as: Any Matcher
</pre></td></tr>
-<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1AbstractConditionalOperator.html">AbstractConditionalOperator</a>></td><td class="name" onclick="toggle('hasCondition4')"><a name="hasCondition4Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
-<tr><td colspan="4" class="doc" id="hasCondition4"><pre>Matches the condition expression of an if statement, for loop,
-or conditional operator.
+<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1AbstractConditionalOperator.html">AbstractConditionalOperator</a>></td><td class="name" onclick="toggle('hasCondition5')"><a name="hasCondition5Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="hasCondition5"><pre>Matches the condition expression of an if statement, for loop,
+switch statement or conditional operator.
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
if (true) {}
@@ -4293,7 +4293,7 @@ with compoundStmt()
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1DoStmt.html">DoStmt</a>></td><td class="name" onclick="toggle('hasCondition3')"><a name="hasCondition3Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="hasCondition3"><pre>Matches the condition expression of an if statement, for loop,
-or conditional operator.
+switch statement or conditional operator.
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
if (true) {}
@@ -4476,7 +4476,7 @@ with compoundStmt()
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ForStmt.html">ForStmt</a>></td><td class="name" onclick="toggle('hasCondition1')"><a name="hasCondition1Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="hasCondition1"><pre>Matches the condition expression of an if statement, for loop,
-or conditional operator.
+switch statement or conditional operator.
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
if (true) {}
@@ -4554,7 +4554,7 @@ cxxMethodDecl(returns(asString("int")))
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1IfStmt.html">IfStmt</a>></td><td class="name" onclick="toggle('hasCondition0')"><a name="hasCondition0Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="hasCondition0"><pre>Matches the condition expression of an if statement, for loop,
-or conditional operator.
+switch statement or conditional operator.
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
if (true) {}
@@ -5052,6 +5052,15 @@ switchStmt(forEachSwitchCase(caseStmt().
</pre></td></tr>
+<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchStmt.html">SwitchStmt</a>></td><td class="name" onclick="toggle('hasCondition4')"><a name="hasCondition4Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="hasCondition4"><pre>Matches the condition expression of an if statement, for loop,
+switch statement or conditional operator.
+
+Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
+ if (true) {}
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TagType.html">TagType</a>></td><td class="name" onclick="toggle('hasDeclaration4')"><a name="hasDeclaration4Anchor">hasDeclaration</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="hasDeclaration4"><pre>Matches a node if the declaration associated with that node
matches the given matcher.
@@ -5385,7 +5394,7 @@ with compoundStmt()
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1WhileStmt.html">WhileStmt</a>></td><td class="name" onclick="toggle('hasCondition2')"><a name="hasCondition2Anchor">hasCondition</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="hasCondition2"><pre>Matches the condition expression of an if statement, for loop,
-or conditional operator.
+switch statement or conditional operator.
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
if (true) {}
Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=271208&r1=271207&r2=271208&view=diff
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Mon May 30 10:25:25 2016
@@ -3299,7 +3299,7 @@ AST_POLYMORPHIC_MATCHER(isConstexpr,
}
/// \brief Matches the condition expression of an if statement, for loop,
-/// or conditional operator.
+/// switch statement or conditional operator.
///
/// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
/// \code
@@ -3308,7 +3308,7 @@ AST_POLYMORPHIC_MATCHER(isConstexpr,
AST_POLYMORPHIC_MATCHER_P(
hasCondition,
AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, ForStmt, WhileStmt, DoStmt,
- AbstractConditionalOperator),
+ SwitchStmt, AbstractConditionalOperator),
internal::Matcher<Expr>, InnerMatcher) {
const Expr *const Condition = Node.getCond();
return (Condition != nullptr &&
Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp?rev=271208&r1=271207&r2=271208&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp Mon May 30 10:25:25 2016
@@ -958,6 +958,28 @@ TEST(Matcher, VisitsTemplateInstantiatio
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()))));
More information about the cfe-commits
mailing list