diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 27e934ccacc5b597d1a61b2af5e99df6c5257faa..433ba7843fd6448f83c81446adeb8550c85ee123 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -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().bind("c"))).bind("s") </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) {} diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 7b1a7e77d5a8ad14f7987f696a41bb7d34546f7e..c445152c7c4f2fced12595cd65e97ee02372de75 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -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 && diff --git a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index a2e1dee261a45cce5f214307af1bb96b76636fbe..481b7e3041dba251537ae17e9c641429d10d9ae7 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()))));