[llvm] [NFCI] Avoid adding duplicated SpecialCaseList::Sections. (PR #140478)
Qinkun Bao via llvm-commits
llvm-commits at lists.llvm.org
Sun May 18 13:58:05 PDT 2025
https://github.com/qinkunbao updated https://github.com/llvm/llvm-project/pull/140478
>From c51ac31bd7ec778fcbb9faf74d9645384cf67206 Mon Sep 17 00:00:00 2001
From: Qinkun Bao <qinkun at google.com>
Date: Sun, 18 May 2025 20:51:54 +0000
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.6
---
llvm/lib/Support/SpecialCaseList.cpp | 18 +++++++++++------
.../unittests/Support/SpecialCaseListTest.cpp | 20 +++++++++++++++++++
2 files changed, 32 insertions(+), 6 deletions(-)
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"));
+}
+
}
More information about the llvm-commits
mailing list