Skip to content

[NFCI][Sanitizer] Convert Matcher::Globs from StringMap to vector. #140964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15 changes: 10 additions & 5 deletions clang/lib/Basic/Diagnostic.cpp
Original file line number Diff line number Diff line change
@@ -553,7 +553,12 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
// Each section has a matcher with that section's name, attached to that
// line.
const auto &DiagSectionMatcher = Entry.SectionMatcher;
unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
unsigned DiagLine = 0;
for (const auto &Glob : DiagSectionMatcher->Globs)
if (Glob->Name == DiagName) {
DiagLine = Glob->LineNo;
break;
}
LineAndSectionEntry.emplace_back(DiagLine, &Entry);
}
llvm::sort(LineAndSectionEntry);
@@ -625,12 +630,12 @@ bool WarningsSpecialCaseList::globsMatches(
StringRef Category = Entry.getKey();
const llvm::SpecialCaseList::Matcher &Matcher = Entry.getValue();
bool IsPositive = Category != "emit";
for (const auto &[Pattern, Glob] : Matcher.Globs) {
if (Pattern.size() < LongestMatch.size())
for (const auto &Glob : Matcher.Globs) {
if (Glob->Name.size() < LongestMatch.size())
continue;
if (!Glob.first.match(FilePath))
if (!Glob->Pattern.match(FilePath))
continue;
LongestMatch = Pattern;
LongestMatch = Glob->Name;
LongestIsPositive = IsPositive;
}
}
12 changes: 11 additions & 1 deletion llvm/include/llvm/Support/SpecialCaseList.h
Original file line number Diff line number Diff line change
@@ -125,7 +125,17 @@ class SpecialCaseList {
// Returns zero if no match is found.
LLVM_ABI unsigned match(StringRef Query) const;

StringMap<std::pair<GlobPattern, unsigned>> Globs;
struct Glob {
std::string Name;
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<Matcher::Glob>> Globs;
std::vector<std::pair<std::unique_ptr<Regex>, unsigned>> RegExes;
};

26 changes: 12 additions & 14 deletions llvm/lib/Support/SpecialCaseList.cpp
Original file line number Diff line number Diff line change
@@ -53,24 +53,22 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
return Error::success();
}

auto [It, DidEmplace] = Globs.try_emplace(Pattern);
if (DidEmplace) {
// We must be sure to use the string in the map rather than the provided
// reference which could be destroyed before match() is called
Pattern = It->getKey();
auto &Pair = It->getValue();
if (auto Err = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024)
.moveInto(Pair.first))
return Err;
Pair.second = LineNumber;
}
auto Glob = std::make_unique<Matcher::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))
return Err;
Globs.push_back(std::move(Glob));
return Error::success();
}

unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
for (const auto &[Pattern, Pair] : Globs)
if (Pair.first.match(Query))
return Pair.second;
for (const auto &Glob : Globs)
if (Glob->Pattern.match(Query))
return Glob->LineNo;
for (const auto &[Regex, LineNumber] : RegExes)
if (Regex->match(Query))
return LineNumber;
Loading
Oops, something went wrong.