[lld] [llvm] [GlobPattern] add AsLiteral to help lld determine if a symbol is literal (PR #215854)
Ellis Hoag via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 11:56:50 PDT 2026
================
@@ -39,6 +39,63 @@ TEST_F(GlobPatternTest, Wildcard) {
EXPECT_FALSE(Pat1->match(""));
}
+TEST_F(GlobPatternTest, AsLiteral) {
+ SmallVector<char, 64> Storage;
+
+ // Plain literals, including characters that are only special in context.
+ // These need no unescaping, so the result aliases the pattern.
+ for (StringRef P : {"abc", "", "a]c", "a}c", "a,c"}) {
+ Expected<GlobPattern> Pat = GlobPattern::create(P);
+ ASSERT_TRUE((bool)Pat) << P;
+ std::optional<StringRef> Lit = Pat->asLiteral(Storage);
+ ASSERT_TRUE(Lit.has_value()) << P;
+ EXPECT_EQ(*Lit, P);
+ EXPECT_EQ(Lit->data(), P.data()) << P;
+ }
+
+ // Real metacharacters are not literals.
+ for (StringRef P : {"a*c", "a?c", "a[bc]d"}) {
+ Expected<GlobPattern> Pat = GlobPattern::create(P);
+ ASSERT_TRUE((bool)Pat) << P;
+ EXPECT_FALSE(Pat->asLiteral(Storage).has_value()) << P;
+ }
+
+ // Escaped metacharacters denote a single string, with escapes resolved.
+ Expected<GlobPattern> Star = GlobPattern::create("a\\*c");
+ ASSERT_TRUE((bool)Star);
+ ASSERT_TRUE(Star->asLiteral(Storage).has_value());
+ EXPECT_EQ(*Star->asLiteral(Storage), "a*c");
----------------
ellishg wrote:
```suggestion
auto Lit = Star->asLiteral(Storage);
ASSERT_TRUE(LIT.has_value());
EXPECT_EQ(*LIT, "a*c");
```
Same for a few checks below
https://github.com/llvm/llvm-project/pull/215854
More information about the llvm-commits
mailing list