[clang] e9dbf31 - [NFCI][Sanitizer] Convert Matcher::Globs from StringMap to vector. (#140964)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 17:23:17 PDT 2025
Author: Qinkun Bao
Date: 2025-05-23T20:23:13-04:00
New Revision: e9dbf31be5475a691fca26e56ba8b27337ba31b6
URL: https://github.com/llvm/llvm-project/commit/e9dbf31be5475a691fca26e56ba8b27337ba31b6
DIFF: https://github.com/llvm/llvm-project/commit/e9dbf31be5475a691fca26e56ba8b27337ba31b6.diff
LOG: [NFCI][Sanitizer] Convert Matcher::Globs from StringMap to vector. (#140964)
As discussed in https://github.com/llvm/llvm-project/pull/139772 and
https://github.com/llvm/llvm-project/pull/140529, Matcher::Globs can
keep the order when parsing the case list.
Added:
Modified:
clang/lib/Basic/Diagnostic.cpp
llvm/include/llvm/Support/SpecialCaseList.h
llvm/lib/Support/SpecialCaseList.cpp
Removed:
################################################################################
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index 22821418c7078..5dc5991a294e3 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -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;
}
}
diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h
index fc6dc93651f38..653a3b14ebf03 100644
--- a/llvm/include/llvm/Support/SpecialCaseList.h
+++ b/llvm/include/llvm/Support/SpecialCaseList.h
@@ -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;
};
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index dddf84cbb1ced..47ff3e24706a4 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -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;
More information about the cfe-commits
mailing list