[PATCH] Add caseStmt(), defaultStmt(), eachCase() and hasCaseConstant() matchers.

Daniel Jasper djasper at google.com
Tue May 7 22:01:26 PDT 2013



================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:3356
@@ +3355,3 @@
+/// statement.
+AST_MATCHER_P(SwitchStmt, eachCase, internal::Matcher<SwitchCase>,
+              InnerMatcher) {
----------------
I am not very happy with the naming here:
1) This should follow the conventions we already have. Meaning, this should be called forEach.. Ideally, we would then also add a corresponding has...
2) It is slightly weird, that this is called ..Case, when in fact it matches on SwitchCases. I'd vote for either calling this ..SwitchCase or only matching on CaseStmts.

================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:3379
@@ +3378,3 @@
+/// statement.
+AST_MATCHER_P(SwitchCase, hasCaseConstant, internal::Matcher<Expr>,
+              InnerMatcher) {
----------------
I think this should just be a matcher on CaseStmt (making the dyn_cast) unnecessary. Or am I missing something?

================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:3355
@@ +3354,3 @@
+/// \brief Matches each case or default statement belonging to the given switch
+/// statement.
+AST_MATCHER_P(SwitchStmt, eachCase, internal::Matcher<SwitchCase>,
----------------
Please add a usage example (like all the other matchers have).

================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:3378
@@ +3377,3 @@
+/// the GNU case range extension, matches the constant given in the case
+/// statement.
+AST_MATCHER_P(SwitchCase, hasCaseConstant, internal::Matcher<Expr>,
----------------
Please add a usage example.

================
Comment at: unittests/ASTMatchers/ASTMatchersTest.cpp:2922
@@ +2921,3 @@
+  EXPECT_TRUE(
+      notMatches("void x() { switch(42); }", switchStmt(eachCase(caseStmt()))));
+  EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
----------------
There are no tests that verify that "eachCase" binds all matches. We need something like:

  EXPECT_TRUE(matchAndVerifyResultTrue(
        "void x() { switch (42) { case 1: case 2: break; case 3: default: break } }",
        switchStmt(eachCase(caseStmt().bind("x"))),
        new VerifyIdIsBoundTo<CaseStmt>("x", 3)));


http://llvm-reviews.chandlerc.com/D744



More information about the cfe-commits mailing list