diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -4201,6 +4201,21 @@ +
Matches consteval function declarations and if consteval. + +Given: + consteval int a(); + void b() { if consteval {} } + void c() { if ! consteval {} } + void d() { if ! consteval {} else {} } +functionDecl(isConsteval()) + matches the declaration of "int a()". +ifStmt(isConsteval()) + matches the if statement in "void b()", "void c()", "void d()". +
Matches constexpr variable and function declarations, and if constexpr. @@ -4473,6 +4488,21 @@
Matches consteval function declarations and if consteval. + +Given: + consteval int a(); + void b() { if consteval {} } + void c() { if ! consteval {} } + void d() { if ! consteval {} else {} } +functionDecl(isConsteval()) + matches the declaration of "int a()". +ifStmt(isConsteval()) + matches the if statement in "void b()", "void c()", "void d()". +
Matches constexpr variable and function declarations, and if constexpr. 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 @@ -5170,6 +5170,24 @@ return FnTy->isNothrow(); } +/// Matches consteval function declarations and if consteval. +/// +/// Given: +/// \code +/// consteval int a(); +/// void b() { if consteval {} } +/// void c() { if ! consteval {} } +/// void d() { if ! consteval {} else {} } +/// \endcode +/// functionDecl(isConsteval()) +/// matches the declaration of "int a()". +/// ifStmt(isConsteval()) +/// matches the if statement in "void b()", "void c()", "void d()". +AST_POLYMORPHIC_MATCHER(isConsteval, + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, IfStmt)) { + return Node.isConsteval(); +} + /// Matches constexpr variable and function declarations, /// and if constexpr. /// 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 @@ -404,6 +404,7 @@ REGISTER_MATCHER(isComparisonOperator); REGISTER_MATCHER(isConst); REGISTER_MATCHER(isConstQualified); + REGISTER_MATCHER(isConsteval); REGISTER_MATCHER(isConstexpr); REGISTER_MATCHER(isCopyAssignmentOperator); REGISTER_MATCHER(isCopyConstructor); 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 @@ -1790,6 +1790,35 @@ EXPECT_TRUE(matches("void f() noexcept;", functionProtoType(isNoThrow()))); } +TEST_P(ASTMatchersTest, IsConsteval) { + if (!GetParam().isCXX20OrLater()) + return; + + EXPECT_TRUE(matches("consteval int bar();", + functionDecl(hasName("bar"), isConsteval()))); + EXPECT_TRUE(notMatches("constexpr int bar();", + functionDecl(hasName("bar"), isConsteval()))); + EXPECT_TRUE( + notMatches("int bar();", functionDecl(hasName("bar"), isConsteval()))); +} + +TEST_P(ASTMatchersTest, IsConsteval_MatchesIfConsteval) { + if (!GetParam().isCXX20OrLater()) + return; + + EXPECT_TRUE(matches("void baz() { if consteval {} }", ifStmt(isConsteval()))); + EXPECT_TRUE( + matches("void baz() { if ! consteval {} }", ifStmt(isConsteval()))); + EXPECT_TRUE(matches("void baz() { if ! consteval {} else {} }", + ifStmt(isConsteval()))); + EXPECT_TRUE( + matches("void baz() { if not consteval {} }", ifStmt(isConsteval()))); + EXPECT_TRUE(notMatches("void baz() { if constexpr(1 > 0) {} }", + ifStmt(isConsteval()))); + EXPECT_TRUE( + notMatches("void baz() { if (1 > 0) {} }", ifStmt(isConsteval()))); +} + TEST_P(ASTMatchersTest, IsConstexpr) { if (!GetParam().isCXX11OrLater()) { return;