[PATCH] D140794: Add coroutineBodyStmt matcher
Chris Cotter via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 30 20:14:07 PST 2022
ccotter created this revision.
Herald added a subscriber: ChuanqiXu.
Herald added a project: All.
ccotter requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The coroutineBodyStmt matcher matches CoroutineBodyStmt AST nodes.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D140794
Files:
clang/docs/ReleaseNotes.rst
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -678,6 +678,26 @@
EXPECT_TRUE(matchesConditionally(CoYieldCode,
coyieldExpr(isExpansionInMainFile()),
true, {"-std=c++20", "-I/"}, M));
+
+ StringRef NonCoroCode = R"cpp(
+#include <coro_header>
+void non_coro_function() {
+}
+)cpp";
+
+ EXPECT_TRUE(matchesConditionally(CoReturnCode,
+ coroutineBodyStmt(),
+ true, {"-std=c++20", "-I/"}, M));
+ EXPECT_TRUE(matchesConditionally(CoAwaitCode,
+ coroutineBodyStmt(),
+ true, {"-std=c++20", "-I/"}, M));
+ EXPECT_TRUE(matchesConditionally(CoYieldCode,
+ coroutineBodyStmt(),
+ true, {"-std=c++20", "-I/"}, M));
+
+ EXPECT_FALSE(matchesConditionally(NonCoroCode,
+ coroutineBodyStmt(),
+ true, {"-std=c++20", "-I/"}, M));
}
TEST(Matcher, isClassMessage) {
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -168,6 +168,7 @@
REGISTER_MATCHER(complexType);
REGISTER_MATCHER(compoundLiteralExpr);
REGISTER_MATCHER(compoundStmt);
+ REGISTER_MATCHER(coroutineBodyStmt);
REGISTER_MATCHER(coawaitExpr);
REGISTER_MATCHER(conditionalOperator);
REGISTER_MATCHER(constantArrayType);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===================================================================
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -909,6 +909,7 @@
const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt;
+const internal::VariadicDynCastAllOfMatcher<Stmt, CoroutineBodyStmt> coroutineBodyStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> cxxCatchStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> cxxThrowExpr;
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -2449,6 +2449,17 @@
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoyieldExpr>
coyieldExpr;
+/// Matches coroutine body statements.
+///
+/// coroutineBodyStmt() matches the coroutine below
+/// \code
+/// generator<int> gen() {
+/// co_return;
+/// }
+/// \endcode
+extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoroutineBodyStmt>
+ coroutineBodyStmt;
+
/// Matches nullptr literal.
extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr>
cxxNullPtrLiteralExpr;
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -855,6 +855,8 @@
AST Matchers
------------
+- Add ``coroutineBodyStmt`` matcher.
+
clang-format
------------
- Add ``RemoveSemicolon`` option for removing ``;`` after a non-empty function definition.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140794.485742.patch
Type: text/x-patch
Size: 3745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221231/1081efac/attachment.bin>
More information about the cfe-commits
mailing list