Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit c33ca9e

Browse files
committed
[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
1 parent 4cc451f commit c33ca9e

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

docs/LibASTMatchersReference.html

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3536,9 +3536,9 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
35363536
</pre></td></tr>
35373537

35383538

3539-
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1AbstractConditionalOperator.html">AbstractConditionalOperator</a>&gt;</td><td class="name" onclick="toggle('hasCondition4')"><a name="hasCondition4Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
3540-
<tr><td colspan="4" class="doc" id="hasCondition4"><pre>Matches the condition expression of an if statement, for loop,
3541-
or conditional operator.
3539+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1AbstractConditionalOperator.html">AbstractConditionalOperator</a>&gt;</td><td class="name" onclick="toggle('hasCondition5')"><a name="hasCondition5Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
3540+
<tr><td colspan="4" class="doc" id="hasCondition5"><pre>Matches the condition expression of an if statement, for loop,
3541+
switch statement or conditional operator.
35423542

35433543
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
35443544
if (true) {}
@@ -4293,7 +4293,7 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
42934293

42944294
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1DoStmt.html">DoStmt</a>&gt;</td><td class="name" onclick="toggle('hasCondition3')"><a name="hasCondition3Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
42954295
<tr><td colspan="4" class="doc" id="hasCondition3"><pre>Matches the condition expression of an if statement, for loop,
4296-
or conditional operator.
4296+
switch statement or conditional operator.
42974297

42984298
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
42994299
if (true) {}
@@ -4476,7 +4476,7 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
44764476

44774477
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1ForStmt.html">ForStmt</a>&gt;</td><td class="name" onclick="toggle('hasCondition1')"><a name="hasCondition1Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
44784478
<tr><td colspan="4" class="doc" id="hasCondition1"><pre>Matches the condition expression of an if statement, for loop,
4479-
or conditional operator.
4479+
switch statement or conditional operator.
44804480

44814481
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
44824482
if (true) {}
@@ -4554,7 +4554,7 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
45544554

45554555
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1IfStmt.html">IfStmt</a>&gt;</td><td class="name" onclick="toggle('hasCondition0')"><a name="hasCondition0Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
45564556
<tr><td colspan="4" class="doc" id="hasCondition0"><pre>Matches the condition expression of an if statement, for loop,
4557-
or conditional operator.
4557+
switch statement or conditional operator.
45584558

45594559
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
45604560
if (true) {}
@@ -5052,6 +5052,15 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
50525052
</pre></td></tr>
50535053

50545054

5055+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchStmt.html">SwitchStmt</a>&gt;</td><td class="name" onclick="toggle('hasCondition4')"><a name="hasCondition4Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
5056+
<tr><td colspan="4" class="doc" id="hasCondition4"><pre>Matches the condition expression of an if statement, for loop,
5057+
switch statement or conditional operator.
5058+
5059+
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
5060+
if (true) {}
5061+
</pre></td></tr>
5062+
5063+
50555064
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TagType.html">TagType</a>&gt;</td><td class="name" onclick="toggle('hasDeclaration4')"><a name="hasDeclaration4Anchor">hasDeclaration</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt; InnerMatcher</td></tr>
50565065
<tr><td colspan="4" class="doc" id="hasDeclaration4"><pre>Matches a node if the declaration associated with that node
50575066
matches the given matcher.
@@ -5385,7 +5394,7 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
53855394

53865395
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1WhileStmt.html">WhileStmt</a>&gt;</td><td class="name" onclick="toggle('hasCondition2')"><a name="hasCondition2Anchor">hasCondition</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; InnerMatcher</td></tr>
53875396
<tr><td colspan="4" class="doc" id="hasCondition2"><pre>Matches the condition expression of an if statement, for loop,
5388-
or conditional operator.
5397+
switch statement or conditional operator.
53895398

53905399
Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
53915400
if (true) {}

include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,7 +3299,7 @@ AST_POLYMORPHIC_MATCHER(isConstexpr,
32993299
}
33003300

33013301
/// \brief Matches the condition expression of an if statement, for loop,
3302-
/// or conditional operator.
3302+
/// switch statement or conditional operator.
33033303
///
33043304
/// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
33053305
/// \code
@@ -3308,7 +3308,7 @@ AST_POLYMORPHIC_MATCHER(isConstexpr,
33083308
AST_POLYMORPHIC_MATCHER_P(
33093309
hasCondition,
33103310
AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, ForStmt, WhileStmt, DoStmt,
3311-
AbstractConditionalOperator),
3311+
SwitchStmt, AbstractConditionalOperator),
33123312
internal::Matcher<Expr>, InnerMatcher) {
33133313
const Expr *const Condition = Node.getCond();
33143314
return (Condition != nullptr &&

unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,28 @@ TEST(Matcher, VisitsTemplateInstantiations) {
958958
callee(cxxMethodDecl(hasName("x"))))))));
959959
}
960960

961+
TEST(Matcher, HasCondition) {
962+
StatementMatcher IfStmt =
963+
ifStmt(hasCondition(cxxBoolLiteral(equals(true))));
964+
EXPECT_TRUE(matches("void x() { if (true) {} }", IfStmt));
965+
EXPECT_TRUE(notMatches("void x() { if (false) {} }", IfStmt));
966+
967+
StatementMatcher ForStmt =
968+
forStmt(hasCondition(cxxBoolLiteral(equals(true))));
969+
EXPECT_TRUE(matches("void x() { for (;true;) {} }", ForStmt));
970+
EXPECT_TRUE(notMatches("void x() { for (;false;) {} }", ForStmt));
971+
972+
StatementMatcher WhileStmt =
973+
whileStmt(hasCondition(cxxBoolLiteral(equals(true))));
974+
EXPECT_TRUE(matches("void x() { while (true) {} }", WhileStmt));
975+
EXPECT_TRUE(notMatches("void x() { while (false) {} }", WhileStmt));
976+
977+
StatementMatcher SwitchStmt =
978+
switchStmt(hasCondition(integerLiteral(equals(42))));
979+
EXPECT_TRUE(matches("void x() { switch (42) {case 42:;} }", SwitchStmt));
980+
EXPECT_TRUE(notMatches("void x() { switch (43) {case 43:;} }", SwitchStmt));
981+
}
982+
961983
TEST(For, ForLoopInternals) {
962984
EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
963985
forStmt(hasCondition(anything()))));

0 commit comments

Comments
 (0)