Author: Evgeny Shulgin Date: 2022-01-20T13:35:10-05:00 New Revision: b80db150cdba17b3d4970389025f95b1c93482b8
URL: https://github.com/llvm/llvm-project/commit/b80db150cdba17b3d4970389025f95b1c93482b8 DIFF: https://github.com/llvm/llvm-project/commit/b80db150cdba17b3d4970389025f95b1c93482b8.diff LOG: Add `isConsteval` matcher Support C++20 consteval functions and C++2b if consteval for AST Matchers. Added: Modified: 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 Removed: ################################################################################ diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 9e71608a86250..fb856de0936dc 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -4201,6 +4201,22 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2> </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. @@ -4473,6 +4489,22 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2> </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. diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 08e4d75299d2b..c787d355a3148 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -316,6 +316,8 @@ AST Matchers 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 ------------ diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 599ab407c442b..c934b708cb96c 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -5170,6 +5170,25 @@ AST_POLYMORPHIC_MATCHER(isNoThrow, 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. /// diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp index 4f3efdb0a6630..2210c5413cc5a 100644 --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -404,6 +404,7 @@ RegistryMaps::RegistryMaps() { 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 index f604d0a19e18f..51946e1430cf6 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -1790,6 +1790,35 @@ TEST_P(ASTMatchersTest, IsNoThrow_CXX11) { 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; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits