Skip to content

Commit c083fa1

Browse files
authored
[NFC][SpecialCaseList] Use BumpPtrAllocator to own strings (#162304)
This simplifies structure of GlobMatcher, and avoids "no copy/move" requirement.
1 parent 941492b commit c083fa1

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/ADT/ArrayRef.h"
1616
#include "llvm/ADT/StringMap.h"
17+
#include "llvm/Support/Allocator.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include "llvm/Support/GlobPattern.h"
1920
#include "llvm/Support/Regex.h"
@@ -132,14 +133,12 @@ class SpecialCaseList {
132133
struct Reg {
133134
Reg(StringRef Name, unsigned LineNo, Regex &&Rg)
134135
: Name(Name), LineNo(LineNo), Rg(std::move(Rg)) {}
135-
std::string Name;
136+
StringRef Name;
136137
unsigned LineNo;
137138
Regex Rg;
138-
Reg(Reg &&) = delete;
139-
Reg() = default;
140139
};
141140

142-
std::vector<std::unique_ptr<Reg>> RegExes;
141+
std::vector<Reg> RegExes;
143142
};
144143

145144
class GlobMatcher {
@@ -150,17 +149,14 @@ class SpecialCaseList {
150149
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const;
151150

152151
struct Glob {
153-
Glob(StringRef Name, unsigned LineNo) : Name(Name), LineNo(LineNo) {}
154-
std::string Name;
152+
Glob(StringRef Name, unsigned LineNo, GlobPattern &&Pattern)
153+
: Name(Name), LineNo(LineNo), Pattern(std::move(Pattern)) {}
154+
StringRef Name;
155155
unsigned LineNo;
156156
GlobPattern Pattern;
157-
// neither copyable nor movable because GlobPattern contains
158-
// Glob::StringRef that points to Glob::Name.
159-
Glob(Glob &&) = delete;
160-
Glob() = default;
161157
};
162158

163-
std::vector<std::unique_ptr<Glob>> Globs;
159+
std::vector<GlobMatcher::Glob> Globs;
164160
};
165161

166162
/// Represents a set of patterns and their line numbers
@@ -217,6 +213,7 @@ class SpecialCaseList {
217213
ArrayRef<const Section> sections() const { return Sections; }
218214

219215
private:
216+
BumpPtrAllocator StrAlloc;
220217
std::vector<Section> Sections;
221218

222219
LLVM_ABI Expected<Section *> addSection(StringRef SectionStr,

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,36 @@ Error SpecialCaseList::RegexMatcher::insert(StringRef Pattern,
5151
if (!CheckRE.isValid(REError))
5252
return createStringError(errc::invalid_argument, REError);
5353

54-
auto Rg = std::make_unique<Reg>(Pattern, LineNumber, std::move(CheckRE));
55-
RegExes.emplace_back(std::move(Rg));
56-
54+
RegExes.emplace_back(Pattern, LineNumber, std::move(CheckRE));
5755
return Error::success();
5856
}
5957

6058
void SpecialCaseList::RegexMatcher::match(
6159
StringRef Query,
6260
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
63-
for (const auto &Regex : reverse(RegExes))
64-
if (Regex->Rg.match(Query))
65-
Cb(Regex->Name, Regex->LineNo);
61+
for (const auto &R : reverse(RegExes))
62+
if (R.Rg.match(Query))
63+
Cb(R.Name, R.LineNo);
6664
}
6765

6866
Error SpecialCaseList::GlobMatcher::insert(StringRef Pattern,
6967
unsigned LineNumber) {
7068
if (Pattern.empty())
7169
return createStringError(errc::invalid_argument, "Supplied glob was blank");
7270

73-
auto G = std::make_unique<Glob>(Pattern, LineNumber);
74-
// We must be sure to use the string in `Glob` rather than the provided
75-
// reference which could be destroyed before match() is called
76-
if (auto Err = GlobPattern::create(G->Name, /*MaxSubPatterns=*/1024)
77-
.moveInto(G->Pattern))
71+
auto Res = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024);
72+
if (auto Err = Res.takeError())
7873
return Err;
79-
Globs.emplace_back(std::move(G));
74+
Globs.emplace_back(Pattern, LineNumber, std::move(Res.get()));
8075
return Error::success();
8176
}
8277

8378
void SpecialCaseList::GlobMatcher::match(
8479
StringRef Query,
8580
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
86-
for (const auto &Glob : reverse(Globs))
87-
if (Glob->Pattern.match(Query))
88-
Cb(Glob->Name, Glob->LineNo);
81+
for (const auto &G : reverse(Globs))
82+
if (G.Pattern.match(Query))
83+
Cb(G.Name, G.LineNo);
8984
}
9085

9186
SpecialCaseList::Matcher::Matcher(bool UseGlobs, bool RemoveDotSlash)
@@ -167,6 +162,7 @@ SpecialCaseList::addSection(StringRef SectionStr, unsigned FileNo,
167162
Sections.emplace_back(SectionStr, FileNo, UseGlobs);
168163
auto &Section = Sections.back();
169164

165+
SectionStr = SectionStr.copy(StrAlloc);
170166
if (auto Err = Section.SectionMatcher.insert(SectionStr, LineNo)) {
171167
return createStringError(errc::invalid_argument,
172168
"malformed section at line " + Twine(LineNo) +
@@ -241,6 +237,7 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
241237
auto [It, _] = CurrentSection->Entries[Prefix].try_emplace(
242238
Category, UseGlobs,
243239
RemoveDotSlash && llvm::is_contained(PathPrefixes, Prefix));
240+
Pattern = Pattern.copy(StrAlloc);
244241
if (auto Err = It->second.insert(Pattern, LineNo)) {
245242
Error =
246243
(Twine("malformed ") + (UseGlobs ? "glob" : "regex") + " in line " +

0 commit comments

Comments
 (0)