diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -3775,6 +3775,16 @@ +Matcher<CompoundStmt>hasSubstatementSequenceMatcher<Stmt> InnerMatcher1, Matcher<Stmt> InnerMatcher2 +
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; }'
+
+ + Matcher<CompoundStmt>statementCountIsunsigned N
Checks that a compound statement contains a specific number of
 child statements.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -277,6 +277,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
 ------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5432,6 +5432,32 @@
                                           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);
+    if (It == CS->body_end())
+      return false;
+    ++It;
+    if (It == CS->body_end())
+      return false;
+    return InnerMatcher2.matches(**It, Finder, Builder);
+  }
+  return false;
+}
+
 /// Checks that a compound statement contains a specific number of
 /// child statements.
 ///
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/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);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2604,6 +2604,19 @@
   EXPECT_TRUE(matchesObjC("void f() { ^{}(); }", blockExpr()));
 }
 
+TEST_P(ASTMatchersTest, HasSubstatementSequence) {
+  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,
        StatementCountIs_FindsNoStatementsInAnEmptyCompoundStatement) {
   EXPECT_TRUE(matches("void f() { }", compoundStmt(statementCountIs(0))));