[llvm] [NFC][Support] Implement slash-agnostic path matching in GlobPattern (PR #202854)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 23:57:54 PDT 2026


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/202854

>From 1672852e9a2bd51386af66e4c52188c3adde70a7 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 9 Jun 2026 22:08:25 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
 =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7

[skip ci]
---
 llvm/lib/Support/GlobPattern.cpp | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp
index 2715229c65be1..1aaddbb8408a3 100644
--- a/llvm/lib/Support/GlobPattern.cpp
+++ b/llvm/lib/Support/GlobPattern.cpp
@@ -16,6 +16,9 @@
 
 using namespace llvm;
 
+static constexpr char PrefixMetacharacters[] = "?*[{\\";
+static constexpr char SuffixMetacharacters[] = "?*[]{}\\";
+
 // Expands character ranges and returns a bitmap.
 // For example, "a-cf-hz" is expanded to "abcfghz".
 static Expected<BitVector> expand(StringRef S, StringRef Original) {
@@ -135,7 +138,7 @@ parseBraceExpansions(StringRef S, std::optional<size_t> MaxSubPatterns) {
 static StringRef maxPlainSubstring(StringRef S) {
   StringRef Best;
   while (!S.empty()) {
-    size_t PrefixSize = S.find_first_of("?*[{\\");
+    size_t PrefixSize = S.find_first_of(PrefixMetacharacters);
     if (PrefixSize == std::string::npos)
       PrefixSize = S.size();
 
@@ -181,7 +184,7 @@ GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
   Pat.Pattern = S;
 
   // Store the prefix that does not contain any metacharacter.
-  Pat.PrefixSize = S.find_first_of("?*[{\\");
+  Pat.PrefixSize = S.find_first_of(PrefixMetacharacters);
   if (Pat.PrefixSize == std::string::npos) {
     Pat.PrefixSize = S.size();
     return Pat;
@@ -189,7 +192,7 @@ GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
   S = S.substr(Pat.PrefixSize);
 
   // Just in case we stop on unmatched opening brackets.
-  size_t SuffixStart = S.find_last_of("?*[]{}\\");
+  size_t SuffixStart = S.find_last_of(SuffixMetacharacters);
   assert(SuffixStart != std::string::npos);
   if (S[SuffixStart] == '\\')
     ++SuffixStart;
@@ -228,13 +231,15 @@ GlobPattern::SubGlobPattern::create(StringRef S) {
                                        errc::invalid_argument);
       StringRef Chars = S.substr(I, J - I);
       bool Invert = S[I] == '^' || S[I] == '!';
-      Expected<BitVector> BV =
-          Invert ? expand(Chars.substr(1), S) : expand(Chars, S);
-      if (!BV)
-        return BV.takeError();
       if (Invert)
-        BV->flip();
-      Pat.Brackets.push_back(Bracket{J + 1, std::move(*BV)});
+        Chars = Chars.drop_front();
+      Expected<BitVector> BVOrErr = expand(Chars, S);
+      if (!BVOrErr)
+        return BVOrErr.takeError();
+      BitVector &BV = *BVOrErr;
+      if (Invert)
+        BV.flip();
+      Pat.Brackets.push_back(Bracket{J + 1, std::move(BV)});
       I = J;
     } else if (S[I] == '\\') {
       if (++I == E)

>From 4ff8cc4902b20476389df5e5eb4904014861a7a2 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 10 Jun 2026 23:57:43 -0700
Subject: [PATCH 2/2] no default

Created using spr 1.3.7
---
 llvm/include/llvm/Support/GlobPattern.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h
index ec3631ade518a..ff9aa7dcb9e74 100644
--- a/llvm/include/llvm/Support/GlobPattern.h
+++ b/llvm/include/llvm/Support/GlobPattern.h
@@ -93,7 +93,7 @@ class GlobPattern {
   struct SubGlobPattern {
     /// \param Pat the pattern to match against
     LLVM_ABI static Expected<SubGlobPattern> create(StringRef Pat,
-                                                    bool SlashAgnostic = false);
+                                                    bool SlashAgnostic);
     /// \returns \p true if \p S matches this glob pattern
     LLVM_ABI bool match(StringRef S, bool SlashAgnostic) const;
     StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); }



More information about the llvm-commits mailing list