From: Peter Collingbourne Date: Fri, 10 May 2013 11:52:02 +0000 (+0000) Subject: Add caseStmt(), defaultStmt(), eachCase() and hasCaseConstant() matchers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=acf02715ab1aa1e5a791f20e0d1ca217af256823;p=clang Add caseStmt(), defaultStmt(), eachCase() and hasCaseConstant() matchers. Differential Revision: http://llvm-reviews.chandlerc.com/D744 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181588 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 2b04f8c2e4..61f11060d8 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -894,6 +894,26 @@ const internal::VariadicDynCastAllOfMatcher switchStmt; /// matches 'case 42: break;' and 'default: break;'. const internal::VariadicDynCastAllOfMatcher switchCase; +/// \brief Matches case statements inside switch statements. +/// +/// Given +/// \code +/// switch(a) { case 42: break; default: break; } +/// \endcode +/// caseStmt() +/// matches 'case 42: break;'. +const internal::VariadicDynCastAllOfMatcher caseStmt; + +/// \brief Matches default statements inside switch statements. +/// +/// Given +/// \code +/// switch(a) { case 42: break; default: break; } +/// \endcode +/// defaultStmt() +/// matches 'default: break;'. +const internal::VariadicDynCastAllOfMatcher defaultStmt; + /// \brief Matches compound statements. /// /// Example matches '{}' and '{{}}'in 'for (;;) {{}}' @@ -3348,6 +3368,54 @@ AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, Stmt*, Other, 1) { /// @} +/// \brief Matches each case or default statement belonging to the given switch +/// statement. This matcher may produce multiple matches. +/// +/// Given +/// \code +/// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } } +/// \endcode +/// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s") +/// matches four times, with "c" binding each of "case 1:", "case 2:", +/// "case 3:" and "case 4:", and "s" respectively binding "switch (1)", +/// "switch (1)", "switch (2)" and "switch (2)". +AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher, + InnerMatcher) { + // FIXME: getSwitchCaseList() does not necessarily guarantee a stable + // iteration order. We should use the more general iterating matchers once + // they are capable of expressing this matcher (for example, it should ignore + // case statements belonging to nested switch statements). + bool Matched = false; + for (const SwitchCase *SC = Node.getSwitchCaseList(); SC; + SC = SC->getNextSwitchCase()) { + BoundNodesTreeBuilder CaseBuilder; + bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder); + if (CaseMatched) { + Matched = true; + Builder->addMatch(CaseBuilder.build()); + } + } + + return Matched; +} + +/// \brief If the given case statement does not use the GNU case range +/// extension, matches the constant given in the statement. +/// +/// Given +/// \code +/// switch (1) { case 1: case 1+1: case 3 ... 4: ; } +/// \endcode +/// caseStmt(hasCaseConstant(integerLiteral())) +/// matches "case 1:" +AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher, + InnerMatcher) { + if (Node.getRHS()) + return false; + + return InnerMatcher.matches(*Node.getLHS(), Finder, Builder); +} + } // end namespace ast_matchers } // end namespace clang diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 50aa583f94..8dd3b70d21 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2924,6 +2924,31 @@ TEST(SwitchCase, MatchesSwitch) { EXPECT_TRUE(notMatches("void x() {}", switchStmt())); } +TEST(SwitchCase, MatchesEachCase) { + EXPECT_TRUE(notMatches("void x() { switch(42); }", + switchStmt(forEachSwitchCase(caseStmt())))); + EXPECT_TRUE(matches("void x() { switch(42) case 42:; }", + switchStmt(forEachSwitchCase(caseStmt())))); + EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", + switchStmt(forEachSwitchCase(caseStmt())))); + EXPECT_TRUE(notMatches( + "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }", + ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt())))))); + EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }", + switchStmt(forEachSwitchCase( + caseStmt(hasCaseConstant(integerLiteral())))))); + EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }", + switchStmt(forEachSwitchCase( + caseStmt(hasCaseConstant(integerLiteral())))))); + EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }", + switchStmt(forEachSwitchCase( + caseStmt(hasCaseConstant(integerLiteral())))))); + EXPECT_TRUE(matchAndVerifyResultTrue( + "void x() { switch (42) { case 1: case 2: case 3: default:; } }", + switchStmt(forEachSwitchCase(caseStmt().bind("x"))), + new VerifyIdIsBoundTo("x", 3))); +} + TEST(ExceptionHandling, SimpleCases) { EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt())); EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));