Skip to content

Conversation

vitalybuka
Copy link
Collaborator

This simplifies structure of GlobMatcher, and
avoids "no copy/move" requirement.

@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2025

@llvm/pr-subscribers-llvm-support

Author: Vitaly Buka (vitalybuka)

Changes

This simplifies structure of GlobMatcher, and
avoids "no copy/move" requirement.


Full diff: https://github.com/llvm/llvm-project/pull/162304.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Support/SpecialCaseList.h (+5-6)
  • (modified) llvm/lib/Support/SpecialCaseList.cpp (+8-12)
diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h
index eff36569fcab7..9b56cd8902e39 100644
--- a/llvm/include/llvm/Support/SpecialCaseList.h
+++ b/llvm/include/llvm/Support/SpecialCaseList.h
@@ -13,6 +13,7 @@
 #define LLVM_SUPPORT_SPECIALCASELIST_H
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/Support/Allocator.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/GlobPattern.h"
 #include "llvm/Support/Regex.h"
@@ -134,16 +135,13 @@ class SpecialCaseList {
     LLVM_ABI unsigned match(StringRef Query) const;
     LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber);
     struct Glob {
-      std::string Name;
+      Glob(unsigned LineNo, GlobPattern &&Pattern)
+          : LineNo(LineNo), Pattern(std::move(Pattern)) {}
       unsigned LineNo;
       GlobPattern Pattern;
-      // neither copyable nor movable because GlobPattern contains
-      // Glob::StringRef that points to Glob::Name.
-      Glob(Glob &&) = delete;
-      Glob() = default;
     };
 
-    std::vector<std::unique_ptr<GlobMatcher::Glob>> Globs;
+    std::vector<GlobMatcher::Glob> Globs;
   };
 
   /// Represents a set of globs and their line numbers
@@ -175,6 +173,7 @@ class SpecialCaseList {
     unsigned FileIdx;
   };
 
+  BumpPtrAllocator StrAlloc;
   std::vector<Section> Sections;
 
   LLVM_ABI Expected<Section *> addSection(StringRef SectionStr,
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index 71f7b9aa65796..cd9b2f9b0cb28 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -62,26 +62,20 @@ unsigned SpecialCaseList::RegexMatcher::match(StringRef Query) const {
 
 Error SpecialCaseList::GlobMatcher::insert(StringRef Pattern,
                                            unsigned LineNumber) {
-  if (Pattern.empty()) {
+  if (Pattern.empty())
     return createStringError(errc::invalid_argument, "Supplied glob was blank");
-  }
 
-  auto Glob = std::make_unique<GlobMatcher::Glob>();
-  Glob->Name = Pattern.str();
-  Glob->LineNo = LineNumber;
-  // We must be sure to use the string in `Glob` rather than the provided
-  // reference which could be destroyed before match() is called
-  if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024)
-                     .moveInto(Glob->Pattern))
+  auto Res = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024);
+  if (auto Err = Res.takeError())
     return Err;
-  Globs.push_back(std::move(Glob));
+  Globs.emplace_back(LineNumber, std::move(Res.get()));
   return Error::success();
 }
 
 unsigned SpecialCaseList::GlobMatcher::match(StringRef Query) const {
   for (const auto &Glob : reverse(Globs))
-    if (Glob->Pattern.match(Query))
-      return Glob->LineNo;
+    if (Glob.Pattern.match(Query))
+      return Glob.LineNo;
   return 0;
 }
 
@@ -159,6 +153,7 @@ SpecialCaseList::addSection(StringRef SectionStr, unsigned FileNo,
   Sections.emplace_back(SectionStr, FileNo, UseGlobs);
   auto &Section = Sections.back();
 
+  SectionStr = SectionStr.copy(StrAlloc);
   if (auto Err = Section.SectionMatcher.insert(SectionStr, LineNo)) {
     return createStringError(errc::invalid_argument,
                              "malformed section at line " + Twine(LineNo) +
@@ -220,6 +215,7 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
     auto [Pattern, Category] = Postfix.split("=");
     auto [It, _] =
         CurrentSection->Entries[Prefix].try_emplace(Category, UseGlobs);
+    Pattern = Pattern.copy(StrAlloc);
     if (auto Err = It->second.insert(Pattern, LineNo)) {
       Error =
           (Twine("malformed ") + (UseGlobs ? "glob" : "regex") + " in line " +

Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Created using spr 1.3.6

[skip ci]
Created using spr 1.3.6
Created using spr 1.3.6

[skip ci]
@vitalybuka vitalybuka changed the base branch from users/vitalybuka/spr/main.nfcspecialcaselist-use-bumpptrallocator-to-own-strings to main October 8, 2025 22:27
Created using spr 1.3.6
@vitalybuka vitalybuka changed the title [NFC][SpecialCaseList] Use BumpPtrAllocator to own strings [NFC][SpecialCaseList] Use BumpPtrAllocator to allocate patterns Oct 8, 2025
@vitalybuka vitalybuka enabled auto-merge (squash) October 8, 2025 22:27
@vitalybuka vitalybuka merged commit c083fa1 into main Oct 8, 2025
7 of 8 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/nfcspecialcaselist-use-bumpptrallocator-to-own-strings branch October 8, 2025 22:56
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
This simplifies structure of GlobMatcher, and
avoids "no copy/move" requirement.
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
This simplifies structure of GlobMatcher, and
avoids "no copy/move" requirement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants