[llvm-branch-commits] [NFCI] Avoid adding duplicated SpecialCaseList::Sections. (PR #140821)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue May 20 16:53:42 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Qinkun Bao (qinkunbao)
<details>
<summary>Changes</summary>
https://github.com/llvm/llvm-project/pull/140127 converts SpecialCaseList::Sections
from StringMap to vector. However, the previous StringMap ensures that only a new
section is created when the SectionStr is different. We should keep the same behavior.
---
Full diff: https://github.com/llvm/llvm-project/pull/140821.diff
2 Files Affected:
- (modified) llvm/include/llvm/Support/SpecialCaseList.h (+1)
- (modified) llvm/lib/Support/SpecialCaseList.cpp (+13-6)
``````````diff
diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h
index fc6dc93651f38..baa5c917220e3 100644
--- a/llvm/include/llvm/Support/SpecialCaseList.h
+++ b/llvm/include/llvm/Support/SpecialCaseList.h
@@ -138,6 +138,7 @@ class SpecialCaseList {
std::unique_ptr<Matcher> SectionMatcher;
SectionEntries Entries;
std::string SectionStr;
+ unsigned LineNo;
};
std::vector<Section> Sections;
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index 5145cccc91e3b..e8dac4680f96f 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -137,18 +137,25 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
Expected<SpecialCaseList::Section *>
SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
bool UseGlobs) {
- Sections.emplace_back();
- auto &Section = Sections.back();
- Section.SectionStr = SectionStr;
-
- if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
+ auto it =
+ std::find_if(Sections.begin(), Sections.end(), [&](const Section &s) {
+ return s.SectionStr == SectionStr && s.LineNo == LineNo;
+ });
+ if (it == Sections.end()) {
+ Sections.emplace_back();
+ auto &sec = Sections.back();
+ sec.SectionStr = SectionStr;
+ sec.LineNo = LineNo;
+ it = std::prev(Sections.end());
+ }
+ if (auto Err = it->SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
return createStringError(errc::invalid_argument,
"malformed section at line " + Twine(LineNo) +
": '" + SectionStr +
"': " + toString(std::move(Err)));
}
- return &Section;
+ return &(*it);
}
bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/140821
More information about the llvm-branch-commits
mailing list