LegalizeAdulthood created this revision.
LegalizeAdulthood added a reviewer: klimek.
LegalizeAdulthood added a project: clang.
LegalizeAdulthood requested review of this revision.
This allows the matching of two specific statements in sequence within
a compound statement. For instance `if (x) return true; return false;`
can be matched as
compoundStmt(hasSubstatementSequence(ifStmt(), returnStmt()))
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D116518
Files:
clang/docs/LibASTMatchersReference.html
clang/docs/ReleaseNotes.rst
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ 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))));
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/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ 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<Stmt>, InnerMatcher1,
+ internal::Matcher<Stmt>, InnerMatcher2) {
+ if (const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::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.
///
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ 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
------------
Index: clang/docs/LibASTMatchersReference.html
===================================================================
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -3775,6 +3775,16 @@
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1CompoundStmt.html">CompoundStmt</a>></td><td class="name" onclick="toggle('hasSubstatementSequence0')"><a name="hasSubstatementSequence0Anchor">hasSubstatementSequence</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>> InnerMatcher1, Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>> InnerMatcher2</td></tr>
+<tr><td colspan="4" class="doc" id="hasSubstatementSequence0"><pre>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; }'
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1CompoundStmt.html">CompoundStmt</a>></td><td class="name" onclick="toggle('statementCountIs0')"><a name="statementCountIs0Anchor">statementCountIs</a></td><td>unsigned N</td></tr>
<tr><td colspan="4" class="doc" id="statementCountIs0"><pre>Checks that a compound statement contains a specific number of
child statements.
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits