Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -3775,6 +3775,16 @@ +
Checks that a compound statement contains two sequential statements as matched by the two matchers.
+
+Example: Given
+ { if (x > 10) return false; return true; }
+compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))
+ matches '{ if (x > 10) return false; return true; }'
+Checks that a compound statement contains a specific number of
child statements.
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -283,6 +283,8 @@
and the underlying ``Type`` with ``hasUnderlyingType``.
``hasDeclaration`` continues to see through the alias and apply to the
underlying type.
+- The ``hasSubstatementSequence`` matcher has been added to match two statements
+ appearing sequentially within a compound statement.
clang-format
------------
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5431,6 +5431,35 @@
Builder) != CS->body_end();
}
+/// Matches two consecutive statements within a compound statement.
+///
+/// Given
+/// \code
+/// { if (x > 0) return true; return false; }
+/// \endcode
+/// compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))
+/// matches '{ if (x > 0) return true; return false; }'
+AST_POLYMORPHIC_MATCHER_P2(hasSubstatementSequence,
+ AST_POLYMORPHIC_SUPPORTED_TYPES(CompoundStmt,
+ StmtExpr),
+ internal::Matcher, InnerMatcher1,
+ internal::Matcher, InnerMatcher2) {
+ if (const CompoundStmt *CS = CompoundStmtMatcher::get(Node)) {
+ auto It = matchesFirstInPointerRange(InnerMatcher1, CS->body_begin(),
+ CS->body_end(), Finder, Builder);
+ while (It != CS->body_end()) {
+ ++It;
+ if (It == CS->body_end())
+ return false;
+ if (InnerMatcher2.matches(**It, Finder, Builder))
+ return true;
+ It = matchesFirstInPointerRange(InnerMatcher1, It, CS->body_end(), Finder,
+ Builder);
+ }
+ }
+ return false;
+}
+
/// Checks that a compound statement contains a specific number of
/// child statements.
///
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -355,6 +355,7 @@
REGISTER_MATCHER(hasSpecializedTemplate);
REGISTER_MATCHER(hasStaticStorageDuration);
REGISTER_MATCHER(hasStructuredBlock);
+ REGISTER_MATCHER(hasSubstatementSequence);
REGISTER_MATCHER(hasSyntacticForm);
REGISTER_MATCHER(hasTargetDecl);
REGISTER_MATCHER(hasTemplateArgument);
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2604,6 +2604,73 @@
EXPECT_TRUE(matchesObjC("void f() { ^{}(); }", blockExpr()));
}
+TEST_P(ASTMatchersTest, HasSubstatementSequenceSimple) {
+ const char *Text = "int f() { int x = 5; if (x < 0) return 1; return 0; }";
+ EXPECT_TRUE(matches(
+ Text, compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))));
+ EXPECT_FALSE(matches(
+ Text, compoundStmt(hasSubstatementSequence(ifStmt(), labelStmt()))));
+ EXPECT_FALSE(matches(
+ Text, compoundStmt(hasSubstatementSequence(returnStmt(), ifStmt()))));
+ EXPECT_FALSE(matches(
+ Text, compoundStmt(hasSubstatementSequence(switchStmt(), labelStmt()))));
+}
+
+TEST_P(ASTMatchersTest, HasSubstatementSequenceAlmost) {
+ const char *Text = R"code(
+int f() {
+ int x = 5;
+ if (x < 10)
+ ;
+ if (x < 0)
+ return 1;
+ return 0;
+}
+)code";
+ EXPECT_TRUE(matches(
+ Text, compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))));
+ EXPECT_TRUE(
+ matches(Text, compoundStmt(hasSubstatementSequence(ifStmt(), ifStmt()))));
+}
+
+TEST_P(ASTMatchersTest, HasSubstatementSequenceComplex) {
+ const char *Text = R"code(
+int f() {
+ int x = 5;
+ if (x < 10)
+ x -= 10;
+ if (x < 0)
+ return 1;
+ return 0;
+}
+)code";
+ EXPECT_TRUE(matches(
+ Text, compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))));
+ EXPECT_FALSE(
+ matches(Text, compoundStmt(hasSubstatementSequence(ifStmt(), expr()))));
+}
+
+TEST_P(ASTMatchersTest, HasSubstatementSequenceExpression) {
+ const char *Text = R"code(
+int f() {
+ return ({ int x = 5;
+ int result;
+ if (x < 10)
+ x -= 10;
+ if (x < 0)
+ result = 1;
+ else
+ result = 0;
+ result;
+ });
+ }
+)code";
+ EXPECT_TRUE(
+ matches(Text, stmtExpr(hasSubstatementSequence(ifStmt(), expr()))));
+ EXPECT_FALSE(
+ matches(Text, stmtExpr(hasSubstatementSequence(ifStmt(), returnStmt()))));
+}
+
TEST_P(ASTMatchersTest,
StatementCountIs_FindsNoStatementsInAnEmptyCompoundStatement) {
EXPECT_TRUE(matches("void f() { }", compoundStmt(statementCountIs(0))));