Skip to content

Commit dd4a730

Browse files
authored
[NFCI][Sanitizer] Convert SpecialCaseList::Sections from StringMap to vector.
As discussed in #139772, SpecialCaseList::Sections can keep the order of Sections when parsing the case list. Reviewers: thurstond, vitalybuka Reviewed By: vitalybuka Pull Request: #140127
1 parent efa2833 commit dd4a730

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

clang/lib/Basic/Diagnostic.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,24 +533,28 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
533533
// Drop the default section introduced by special case list, we only support
534534
// exact diagnostic group names.
535535
// FIXME: We should make this configurable in the parser instead.
536-
Sections.erase("*");
536+
// FIXME: C++20 can use std::erase_if(Sections, [](Section &sec) { return
537+
// sec.SectionStr == "*"; });
538+
Sections.erase(
539+
std::remove_if(Sections.begin(), Sections.end(),
540+
[](Section &sec) { return sec.SectionStr == "*"; }),
541+
Sections.end());
537542
// Make sure we iterate sections by their line numbers.
538-
std::vector<std::pair<unsigned, const llvm::StringMapEntry<Section> *>>
539-
LineAndSectionEntry;
543+
std::vector<std::pair<unsigned, const Section *>> LineAndSectionEntry;
540544
LineAndSectionEntry.reserve(Sections.size());
541545
for (const auto &Entry : Sections) {
542-
StringRef DiagName = Entry.getKey();
546+
StringRef DiagName = Entry.SectionStr;
543547
// Each section has a matcher with that section's name, attached to that
544548
// line.
545-
const auto &DiagSectionMatcher = Entry.getValue().SectionMatcher;
549+
const auto &DiagSectionMatcher = Entry.SectionMatcher;
546550
unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
547551
LineAndSectionEntry.emplace_back(DiagLine, &Entry);
548552
}
549553
llvm::sort(LineAndSectionEntry);
550554
static constexpr auto WarningFlavor = clang::diag::Flavor::WarningOrError;
551555
for (const auto &[_, SectionEntry] : LineAndSectionEntry) {
552556
SmallVector<diag::kind> GroupDiags;
553-
StringRef DiagGroup = SectionEntry->getKey();
557+
StringRef DiagGroup = SectionEntry->SectionStr;
554558
if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
555559
WarningFlavor, DiagGroup, GroupDiags)) {
556560
StringRef Suggestion =
@@ -563,7 +567,7 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
563567
for (diag::kind Diag : GroupDiags)
564568
// We're intentionally overwriting any previous mappings here to make sure
565569
// latest one takes precedence.
566-
DiagToSection[Diag] = &SectionEntry->getValue();
570+
DiagToSection[Diag] = SectionEntry;
567571
}
568572
}
569573

clang/lib/Basic/ProfileList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ProfileSpecialCaseList : public llvm::SpecialCaseList {
3737

3838
bool hasPrefix(StringRef Prefix) const {
3939
for (const auto &It : Sections)
40-
if (It.second.Entries.count(Prefix) > 0)
40+
if (It.Entries.count(Prefix) > 0)
4141
return true;
4242
return false;
4343
}

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
3737
}
3838

3939
void SanitizerSpecialCaseList::createSanitizerSections() {
40-
for (auto &It : Sections) {
41-
auto &S = It.second;
40+
for (auto &S : Sections) {
4241
SanitizerMask Mask;
4342

4443
#define SANITIZER(NAME, ID) \

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ class SpecialCaseList {
132132
using SectionEntries = StringMap<StringMap<Matcher>>;
133133

134134
struct Section {
135-
Section(std::unique_ptr<Matcher> M) : SectionMatcher(std::move(M)){};
136-
Section() : Section(std::make_unique<Matcher>()) {}
135+
Section(std::unique_ptr<Matcher> M) : SectionMatcher(std::move(M)) {};
136+
Section() : Section(std::make_unique<Matcher>()) {};
137137

138138
std::unique_ptr<Matcher> SectionMatcher;
139139
SectionEntries Entries;
140+
std::string SectionStr;
140141
};
141142

142-
StringMap<Section> Sections;
143+
std::vector<Section> Sections;
143144

144145
LLVM_ABI Expected<Section *> addSection(StringRef SectionStr, unsigned LineNo,
145146
bool UseGlobs = true);
@@ -154,6 +155,6 @@ class SpecialCaseList {
154155
StringRef Category) const;
155156
};
156157

157-
} // namespace llvm
158+
} // namespace llvm
158159

159160
#endif // LLVM_SUPPORT_SPECIALCASELIST_H

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,17 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
132132
Expected<SpecialCaseList::Section *>
133133
SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
134134
bool UseGlobs) {
135-
auto [It, DidEmplace] = Sections.try_emplace(SectionStr);
136-
auto &Section = It->getValue();
137-
if (DidEmplace)
138-
if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs))
139-
return createStringError(errc::invalid_argument,
140-
"malformed section at line " + Twine(LineNo) +
141-
": '" + SectionStr +
142-
"': " + toString(std::move(Err)));
135+
Sections.emplace_back();
136+
auto &Section = Sections.back();
137+
Section.SectionStr = SectionStr;
138+
139+
if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
140+
return createStringError(errc::invalid_argument,
141+
"malformed section at line " + Twine(LineNo) +
142+
": '" + SectionStr +
143+
"': " + toString(std::move(Err)));
144+
}
145+
143146
return &Section;
144147
}
145148

@@ -212,8 +215,7 @@ bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
212215
unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
213216
StringRef Query,
214217
StringRef Category) const {
215-
for (const auto &It : Sections) {
216-
const auto &S = It.getValue();
218+
for (const auto &S : Sections) {
217219
if (S.SectionMatcher->match(Section)) {
218220
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
219221
if (Blame)

0 commit comments

Comments
 (0)