[clang] [clang][ASTMatcher] Add `matchesString` for `StringLiteral` which matches literals on given `RegExp` (PR #102152)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 06:27:49 PDT 2024


================
@@ -2503,6 +2503,28 @@ TEST_P(ASTMatchersTest, IsDelegatingConstructor) {
       cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(1))));
 }
 
+TEST_P(ASTMatchersTest, MatchesString) {
+  StatementMatcher Literal = stringLiteral(matchesString("foo.*"));
+  EXPECT_TRUE(matches("const char* a = \"foo\";", Literal));
+  EXPECT_TRUE(matches("const char* b = \"foobar\";", Literal));
+  EXPECT_TRUE(matches("const char* b = \"fo\"\"obar\";", Literal));
+  EXPECT_TRUE(notMatches("const char* c = \"bar\";", Literal));
+  // test embedded nulls
+  StatementMatcher Literal2 = stringLiteral(matchesString("bar"));
+  EXPECT_TRUE(matches("const char* b = \"foo\\0bar\";", Literal2));
+  EXPECT_TRUE(notMatches("const char* b = \"foo\\0b\\0ar\";", Literal2));
+}
+
+TEST(MatchesString, MatchesStringPrefixed) {
+  StatementMatcher Literal = stringLiteral(matchesString("foo.*"));
+  EXPECT_TRUE(matchesConditionally("const char16_t* a = u\"foo\";", Literal,
+                                   true, {"-std=c++11"}));
+  EXPECT_TRUE(matchesConditionally("const char32_t* a = U\"foo\";", Literal,
+                                   true, {"-std=c++11"}));
+  EXPECT_TRUE(matchesConditionally("const wchar_t* a = L\"foo\";", Literal,
+                                   true, {"-std=c++11"}));
----------------
AaronBallman wrote:

CC @cor3ntin @tahonermann 

Should these actually match? I'm not certain how we should handle these in general given that the regex is a string literal with no encoding information and it's matching against source string literals which do have a runtime encoding. Because this is AST matching, I suppose the idea is that the regex is UTF-8 and the source is treated as UTF-8, so the encoding prefixes shouldn't matter. Do I have that right?

https://github.com/llvm/llvm-project/pull/102152


More information about the cfe-commits mailing list