[PATCH] D141925: [ASTMatchers] Add `isDirectInit` matcher.
Clement Courbet via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 17 05:55:25 PST 2023
courbet created this revision.
courbet added reviewers: hokein, alexfh.
Herald added a project: All.
courbet requested review of this revision.
Herald added a project: clang.
To match variables that are direct-initialized.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D141925
Files:
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1439,6 +1439,20 @@
EXPECT_TRUE(notMatches("int X;", M));
}
+TEST_P(ASTMatchersTest, IsDirectInit) {
+ auto M = varDecl(isDirectInit());
+ EXPECT_TRUE(notMatches("int i1 = 3;", M));
+ if (!GetParam().isCXX()) {
+ return;
+ }
+ EXPECT_TRUE(matches("int i2(3);", M));
+ if (!GetParam().isCXX11OrLater()) {
+ return;
+ }
+ EXPECT_TRUE(matches("int i3{3};", M));
+ EXPECT_TRUE(notMatches("int i4;", M));
+}
+
TEST_P(ASTMatchersTest, StorageDuration) {
StringRef T =
"void f() { int x; static int y; } int a;static int b;extern int c;";
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4114,6 +4114,22 @@
InnerMatcher.matches(*Initializer, Finder, Builder));
}
+/// \brief Matches a variable initializer is a direct-initializer.
+///
+/// Example matches i1, but not i2, i3 or i4
+/// (matcher = varDecl(isStaticLocal()))
+/// \code
+/// void f() {
+/// int i1 = 3;
+/// int i2(3);
+/// int i3{3};
+/// int i4;
+/// }
+/// \endcode
+AST_MATCHER(VarDecl, isDirectInit) {
+ return Node.isDirectInit();
+}
+
/// \brief Matches a static variable with local scope.
///
/// Example matches y (matcher = varDecl(isStaticLocal()))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141925.489788.patch
Type: text/x-patch
Size: 1609 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230117/9aaea383/attachment.bin>
More information about the cfe-commits
mailing list