LegalizeAdulthood updated this revision to Diff 397004.
LegalizeAdulthood added a comment.
Improve matching when the first matcher matches multiple times,
but the 2nd matcher doesn't match at first.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D116518/new/
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,52 @@
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,
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,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<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);
+ 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/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