[clang] [clang][ASTMatcher] Add matcher for 'matches' (PR #102152)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 6 07:22:55 PDT 2024
https://github.com/Gitspike created https://github.com/llvm/llvm-project/pull/102152
Add Matcher matches.
>From ece7952421b0b565a653f3ada8d9bb0b8f366b69 Mon Sep 17 00:00:00 2001
From: hehouhua <hehouhua at feysh.com>
Date: Tue, 6 Aug 2024 22:17:00 +0800
Subject: [PATCH] [clang][ASTMatcher] Add matcher for 'matches'
Add Matcher matches.
---
clang/docs/LibASTMatchersReference.html | 14 +++++++++++
clang/docs/ReleaseNotes.rst | 2 ++
clang/include/clang/ASTMatchers/ASTMatchers.h | 24 +++++++++++++++++++
clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 +
.../ASTMatchers/ASTMatchersNarrowingTest.cpp | 6 +++++
5 files changed, 47 insertions(+)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index a16b9c44ef0ea..c8781c7ec36f5 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -5582,6 +5582,20 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1StringLiteral.html">StringLiteral</a>></td><td class="name" onclick="toggle('matches0')"><a name="matchesName0A">matches</a></td><td>StringRef RegExp, Regex::RegexFlags Flags = NoFlags</td></tr>
+<tr><td colspan="4" class="doc" id="matches0"><pre>Matches string literals that contain a substring matched by the given RegExp
+
+Example matches "foo" and "foobar" but not "bar"
+ (matcher = stringLiteral(matches("foo.*")))
+ const char* a = "foo";
+ const char* b = "foobar";
+ const char* c = "bar";
+
+Usable as: Matcher<StringLiteral>
+</pre></td></tr>
+
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1StringLiteral.html">StringLiteral</a>></td><td class="name" onclick="toggle('hasSize1')"><a name="hasSize1Anchor">hasSize</a></td><td>unsigned N</td></tr>
<tr><td colspan="4" class="doc" id="hasSize1"><pre>Matches nodes that have the specified size.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 54decb5dbf230..22f84f8fe8837 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -284,6 +284,8 @@ AST Matchers
- Fixed an issue with the `hasName` and `hasAnyName` matcher when matching
inline namespaces with an enclosing namespace of the same name.
+- Add ``matches``.
+
clang-format
------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index ca44c3ee08565..53dd9717fc232 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3116,6 +3116,30 @@ AST_MATCHER_REGEX(NamedDecl, matchesName, RegExp) {
return RegExp->match(FullNameString);
}
+/// Matches string literals that contain a substring matched by the given RegExp.
+///
+/// Example matches "foo" and "foobar" but not "bar"
+/// (matcher = stringLiteral(matches("foo.*")))
+/// \code
+/// const char* a = "foo";
+/// const char* b = "foobar";
+/// const char* c = "bar";
+/// \endcode
+///
+/// Usable as: Matcher<StringLiteral>
+AST_MATCHER_REGEX(StringLiteral, matches, RegExp) {
+ constexpr unsigned StringLength = 64;
+ SmallString<StringLength> Str;
+ llvm::raw_svector_ostream OS(Str);
+ Node.outputString(OS);
+ StringRef OSRef = OS.str();
+ if (OSRef.size() < 2U) {
+ return false;
+ }
+ OSRef = OSRef.substr(1, OSRef.size() - 2);
+ return RegExp->match(OSRef);
+}
+
/// Matches overloaded operator names.
///
/// Matches overloaded operator names specified in strings without the
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 2c75e6beb7430..c1bdc47ea8907 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -493,6 +493,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(lambdaCapture);
REGISTER_MATCHER(lambdaExpr);
REGISTER_MATCHER(linkageSpecDecl);
+ REGISTER_MATCHER(matches);
REGISTER_MATCHER(macroQualifiedType);
REGISTER_MATCHER(materializeTemporaryExpr);
REGISTER_MATCHER(member);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 611e1f9ba5327..3a560e6fa45d4 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2503,6 +2503,12 @@ TEST_P(ASTMatchersTest, IsDelegatingConstructor) {
cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(1))));
}
+TEST_P(ASTMatchersTest, matches) {
+ StatementMatcher Literal = stringLiteral(matches("foo.*"));
+ EXPECT_TRUE(matches("const char* a = \"foo\";", Literal));
+ EXPECT_TRUE(matches("const char* b = \"foobar\";", Literal));
+}
+
TEST_P(ASTMatchersTest, HasSize) {
StatementMatcher Literal = stringLiteral(hasSize(4));
EXPECT_TRUE(matches("const char *s = \"abcd\";", Literal));
More information about the cfe-commits
mailing list