[llvm] [NFCI] Avoid adding duplicated SpecialCaseList::Sections. (PR #140478)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 18 13:52:40 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, SpecialCaseList::addSection may be invoked several times with
the same SectionStr.


---
Full diff: https://github.com/llvm/llvm-project/pull/140478.diff


2 Files Affected:

- (modified) llvm/lib/Support/SpecialCaseList.cpp (+12-6) 
- (modified) llvm/unittests/Support/SpecialCaseListTest.cpp (+20) 


``````````diff
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index dddf84cbb1ced..926aadb65d493 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -132,18 +132,24 @@ 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;
+      });
+  if (it == Sections.end()) {
+    Sections.emplace_back();
+    auto &sec = Sections.back();
+    sec.SectionStr = SectionStr;
+  }
+  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) {
diff --git a/llvm/unittests/Support/SpecialCaseListTest.cpp b/llvm/unittests/Support/SpecialCaseListTest.cpp
index 4289a5e702155..15dc0222a57c3 100644
--- a/llvm/unittests/Support/SpecialCaseListTest.cpp
+++ b/llvm/unittests/Support/SpecialCaseListTest.cpp
@@ -306,4 +306,24 @@ TEST_F(SpecialCaseListTest, Version2) {
   EXPECT_TRUE(SCL->inSection("sect2", "fun", "bar"));
   EXPECT_FALSE(SCL->inSection("sect3", "fun", "bar"));
 }
+
+TEST_F(SpecialCaseListTest, Version3) {
+  std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("[sect1]\n"
+                                                             "fun:foo*\n"
+                                                             "[sect1]\n"
+                                                             "fun:bar*\n"
+                                                             "[sect2]\n"
+                                                             "fun:def\n");
+  EXPECT_TRUE(SCL->inSection("sect1", "fun", "fooz"));
+  EXPECT_TRUE(SCL->inSection("sect1", "fun", "barz"));
+  EXPECT_FALSE(SCL->inSection("sect2", "fun", "fooz"));
+
+  EXPECT_TRUE(SCL->inSection("sect2", "fun", "def"));
+  EXPECT_FALSE(SCL->inSection("sect1", "fun", "def"));
+
+  EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "fun", "fooz"));
+  EXPECT_EQ(4u, SCL->inSectionBlame("sect1", "fun", "barz"));
+  EXPECT_EQ(6u, SCL->inSectionBlame("sect2", "fun", "def"));
+}
+
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/140478


More information about the llvm-commits mailing list