Support C++20 consteval functions and C++2b if consteval for AST Matchers.
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('isConsteval1')"><a name="isConsteval1Anchor">isConsteval</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isConsteval1"><pre>Matches consteval function declarations and if consteval/if ! consteval
+statements.
+
+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()".
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('isConstexpr1')"><a name="isConstexpr1Anchor">isConstexpr</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isConstexpr1"><pre>Matches constexpr variable and function declarations,
and if constexpr.
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1IfStmt.html">IfStmt</a>></td><td class="name" onclick="toggle('isConsteval2')"><a name="isConsteval2Anchor">isConsteval</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isConsteval2"><pre>Matches consteval function declarations and if consteval/if ! consteval
+statements.
+
+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()".
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1IfStmt.html">IfStmt</a>></td><td class="name" onclick="toggle('isConstexpr2')"><a name="isConstexpr2Anchor">isConstexpr</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isConstexpr2"><pre>Matches constexpr variable and function declarations,
and if constexpr.
and the underlying ``Type`` with ``hasUnderlyingType``.
``hasDeclaration`` continues to see through the alias and apply to the
underlying type.
+- Added the ``isConsteval`` matcher to match ``consteval`` function
+ declarations as well as `if consteval` and `if ! consteval` statements.
clang-format
------------
return FnTy->isNothrow();
}
+/// Matches consteval function declarations and if consteval/if ! consteval
+/// statements.
+///
+/// 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.
///
REGISTER_MATCHER(isComparisonOperator);
REGISTER_MATCHER(isConst);
REGISTER_MATCHER(isConstQualified);
+ REGISTER_MATCHER(isConsteval);
REGISTER_MATCHER(isConstexpr);
REGISTER_MATCHER(isCopyAssignmentOperator);
REGISTER_MATCHER(isCopyConstructor);
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;