Skip to content

Commit e9dbf31

Browse files
authored
[NFCI][Sanitizer] Convert Matcher::Globs from StringMap to vector. (#140964)
As discussed in #139772 and #140529, Matcher::Globs can keep the order when parsing the case list.
1 parent b208016 commit e9dbf31

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

clang/lib/Basic/Diagnostic.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,12 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
553553
// Each section has a matcher with that section's name, attached to that
554554
// line.
555555
const auto &DiagSectionMatcher = Entry.SectionMatcher;
556-
unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
556+
unsigned DiagLine = 0;
557+
for (const auto &Glob : DiagSectionMatcher->Globs)
558+
if (Glob->Name == DiagName) {
559+
DiagLine = Glob->LineNo;
560+
break;
561+
}
557562
LineAndSectionEntry.emplace_back(DiagLine, &Entry);
558563
}
559564
llvm::sort(LineAndSectionEntry);
@@ -625,12 +630,12 @@ bool WarningsSpecialCaseList::globsMatches(
625630
StringRef Category = Entry.getKey();
626631
const llvm::SpecialCaseList::Matcher &Matcher = Entry.getValue();
627632
bool IsPositive = Category != "emit";
628-
for (const auto &[Pattern, Glob] : Matcher.Globs) {
629-
if (Pattern.size() < LongestMatch.size())
633+
for (const auto &Glob : Matcher.Globs) {
634+
if (Glob->Name.size() < LongestMatch.size())
630635
continue;
631-
if (!Glob.first.match(FilePath))
636+
if (!Glob->Pattern.match(FilePath))
632637
continue;
633-
LongestMatch = Pattern;
638+
LongestMatch = Glob->Name;
634639
LongestIsPositive = IsPositive;
635640
}
636641
}

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,17 @@ class SpecialCaseList {
125125
// Returns zero if no match is found.
126126
LLVM_ABI unsigned match(StringRef Query) const;
127127

128-
StringMap<std::pair<GlobPattern, unsigned>> Globs;
128+
struct Glob {
129+
std::string Name;
130+
unsigned LineNo;
131+
GlobPattern Pattern;
132+
// neither copyable nor movable because GlobPattern contains
133+
// Glob::StringRef that points to Glob::Name.
134+
Glob(Glob &&) = delete;
135+
Glob() = default;
136+
};
137+
138+
std::vector<std::unique_ptr<Matcher::Glob>> Globs;
129139
std::vector<std::pair<std::unique_ptr<Regex>, unsigned>> RegExes;
130140
};
131141

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,22 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
5353
return Error::success();
5454
}
5555

56-
auto [It, DidEmplace] = Globs.try_emplace(Pattern);
57-
if (DidEmplace) {
58-
// We must be sure to use the string in the map rather than the provided
59-
// reference which could be destroyed before match() is called
60-
Pattern = It->getKey();
61-
auto &Pair = It->getValue();
62-
if (auto Err = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024)
63-
.moveInto(Pair.first))
64-
return Err;
65-
Pair.second = LineNumber;
66-
}
56+
auto Glob = std::make_unique<Matcher::Glob>();
57+
Glob->Name = Pattern.str();
58+
Glob->LineNo = LineNumber;
59+
// We must be sure to use the string in `Glob` rather than the provided
60+
// reference which could be destroyed before match() is called
61+
if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024)
62+
.moveInto(Glob->Pattern))
63+
return Err;
64+
Globs.push_back(std::move(Glob));
6765
return Error::success();
6866
}
6967

7068
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
71-
for (const auto &[Pattern, Pair] : Globs)
72-
if (Pair.first.match(Query))
73-
return Pair.second;
69+
for (const auto &Glob : Globs)
70+
if (Glob->Pattern.match(Query))
71+
return Glob->LineNo;
7472
for (const auto &[Regex, LineNumber] : RegExes)
7573
if (Regex->match(Query))
7674
return LineNumber;

0 commit comments

Comments
 (0)