[clang] 589a939 - Add `isConstinit` matcher
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 24 05:36:55 PST 2022
Author: Evgeny Shulgin
Date: 2022-01-24T08:35:42-05:00
New Revision: 589a93907222cf2380198ca2172ff6697dd43d87
URL: https://github.com/llvm/llvm-project/commit/589a93907222cf2380198ca2172ff6697dd43d87
DIFF: https://github.com/llvm/llvm-project/commit/589a93907222cf2380198ca2172ff6697dd43d87.diff
LOG: Add `isConstinit` matcher
Support C++20 constinit variables 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 fb856de0936dc..4c3916c0325c9 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -5628,6 +5628,19 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</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";
+ int baz = 42;
+ [[clang::require_constant_initialization]] int xyz = 42;
+varDecl(isConstinit())
+ matches the declaration of `foo` and `bar`, but not `baz` and `xyz`.
+</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.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4fe037741256f..5cd2896de54d5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -331,6 +331,8 @@ AST Matchers
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
------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index c934b708cb96c..86bd44091b593 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -5211,6 +5211,23 @@ AST_POLYMORPHIC_MATCHER(isConstexpr,
return Node.isConstexpr();
}
+/// Matches constinit variable declarations.
+///
+/// Given:
+/// \code
+/// constinit int foo = 42;
+/// constinit const char* bar = "bar";
+/// int baz = 42;
+/// [[clang::require_constant_initialization]] int xyz = 42;
+/// \endcode
+/// varDecl(isConstinit())
+/// matches the declaration of `foo` and `bar`, but not `baz` and `xyz`.
+AST_MATCHER(VarDecl, isConstinit) {
+ if (const auto *CIA = Node.getAttr<ConstInitAttr>())
+ return CIA->isConstinit();
+ return false;
+}
+
/// Matches selection statements with initializer.
///
/// Given:
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 2210c5413cc5a..47db6b51966ac 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -406,6 +406,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(isConstQualified);
REGISTER_MATCHER(isConsteval);
REGISTER_MATCHER(isConstexpr);
+ REGISTER_MATCHER(isConstinit);
REGISTER_MATCHER(isCopyAssignmentOperator);
REGISTER_MATCHER(isCopyConstructor);
REGISTER_MATCHER(isDefaultConstructor);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 51946e1430cf6..d1c9790401f02 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1841,6 +1841,25 @@ TEST_P(ASTMatchersTest, IsConstexpr_MatchesIfConstexpr) {
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("[[clang::require_constant_initialization]] int foo = 1;",
+ 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()))));
More information about the cfe-commits
mailing list