[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
Thu Aug 8 08:20:51 PDT 2024
================
@@ -2503,6 +2503,25 @@ 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 prefix
+ if (!GetParam().isCXX20OrLater()) {
+ return;
+ }
+ EXPECT_TRUE(matches("const wchar_t* a = L\"foo\";", Literal));
+ EXPECT_TRUE(matches("const char16_t* a = u\"foo\";", Literal));
+ EXPECT_TRUE(matches("const char32_t* a = U\"foo\";", Literal));
----------------
AaronBallman wrote:
I think this code needs to use `matchesConditionally()` instead to ensure it gets run in the correct language mode. e.g.,
```
TEST(IsExpandedFromMacro, MatchesFromCommandLine) {
StringRef input = R"cc(
void Test() { FOUR_PLUS_FOUR; }
)cc";
EXPECT_TRUE(matchesConditionally(
input, binaryOperator(isExpandedFromMacro("FOUR_PLUS_FOUR")), true,
{"-std=c++11", "-DFOUR_PLUS_FOUR=4+4"}));
}
```
but that said, these all exist in C++11 and later (though `L` was C++98), and C11 and later (though `L` was C99), so we shouldn't need this check at all, right?
https://github.com/llvm/llvm-project/pull/102152
More information about the cfe-commits
mailing list