[PATCH] D117846: [ASTMatchers] Add `isConstinit` matcher
Evgeny Shulgin via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 20 16:30:36 PST 2022
Izaron created this revision.
Izaron added reviewers: aaron.ballman, cor3ntin.
Izaron requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Support C++20 constinit variables for AST Matchers.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D117846
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
@@ -1841,6 +1841,23 @@
notMatches("void baz() { if (1 > 0) {} }", ifStmt(isConstexpr())));
}
+TEST_P(ASTMatchersTest, IsConstinit) {
+ if (!GetParam().isCXX20OrLater()) {
+ return;
+ }
+
+ EXPECT_TRUE(matches("constinit int foo = 1;",
+ varDecl(hasName("foo"), isConstinit())));
+ EXPECT_TRUE(matches("extern constinit int foo;",
+ varDecl(hasName("foo"), isConstinit())));
+ EXPECT_TRUE(matches("constinit const char* foo = \"bar\";",
+ varDecl(hasName("foo"), isConstinit())));
+ EXPECT_TRUE(notMatches("constexpr int foo = 1;",
+ varDecl(hasName("foo"), isConstinit())));
+ EXPECT_TRUE(notMatches("static inline int foo = 1;",
+ varDecl(hasName("foo"), isConstinit())));
+}
+
TEST_P(ASTMatchersTest, HasInitStatement_MatchesSelectionInitializers) {
EXPECT_TRUE(notMatches("void baz() { if (1 > 0) {} }",
ifStmt(hasInitStatement(anything()))));
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -406,6 +406,7 @@
REGISTER_MATCHER(isConstQualified);
REGISTER_MATCHER(isConsteval);
REGISTER_MATCHER(isConstexpr);
+ REGISTER_MATCHER(isConstinit);
REGISTER_MATCHER(isCopyAssignmentOperator);
REGISTER_MATCHER(isCopyConstructor);
REGISTER_MATCHER(isDefaultConstructor);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5211,6 +5211,19 @@
return Node.isConstexpr();
}
+/// Matches constinit variable declarations.
+///
+/// Given:
+/// \code
+/// constinit int foo = 42;
+/// constinit const char* bar = "baz";
+/// \endcode
+/// varDecl(isConstinit())
+/// matches the declaration of foo and bar.
+AST_MATCHER(VarDecl, isConstinit) {
+ return Node.hasAttr<ConstInitAttr>();
+}
+
/// Matches selection statements with initializer.
///
/// Given:
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -323,6 +323,8 @@
underlying type.
- Added the ``isConsteval`` matcher to match ``consteval`` function
declarations as well as `if consteval` and `if ! consteval` statements.
+- Added the ``isConstinit`` matcher to match ``constinit`` variable
+ declarations.
clang-format
------------
Index: clang/docs/LibASTMatchersReference.html
===================================================================
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -5628,6 +5628,17 @@
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>></td><td class="name" onclick="toggle('isConstinit0')"><a name="isConstinit0Anchor">isConstinit</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isConstinit0"><pre>Matches constinit variable declarations.
+
+Given:
+ constinit int foo = 42;
+ constinit const char* bar = "baz";
+varDecl(isConstinit())
+ matches the declaration of foo and bar.
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>></td><td class="name" onclick="toggle('isDefinition1')"><a name="isDefinition1Anchor">isDefinition</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isDefinition1"><pre>Matches if a declaration has a body attached.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117846.401815.patch
Type: text/x-patch
Size: 3960 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220121/35af4204/attachment.bin>
More information about the cfe-commits
mailing list