diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index 5145cccc91e3b..2d84f7f22cd7b 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -137,18 +137,24 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB, Expected 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 d6d5621116d72..16da11e2d7835 100644 --- a/llvm/unittests/Support/SpecialCaseListTest.cpp +++ b/llvm/unittests/Support/SpecialCaseListTest.cpp @@ -216,8 +216,9 @@ TEST_F(SpecialCaseListTest, NoTrigramsInARule) { } TEST_F(SpecialCaseListTest, RepetitiveRule) { - std::unique_ptr SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" - "fun:bar*\n"); + std::unique_ptr SCL = + makeSpecialCaseList("fun:*bar*bar*bar*bar*\n" + "fun:bar*\n"); EXPECT_TRUE(SCL->inSection("", "fun", "bara")); EXPECT_FALSE(SCL->inSection("", "fun", "abara")); EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar")); @@ -226,7 +227,8 @@ TEST_F(SpecialCaseListTest, RepetitiveRule) { } TEST_F(SpecialCaseListTest, SpecialSymbolRule) { - std::unique_ptr SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"); + std::unique_ptr SCL = + makeSpecialCaseList("src:*c\\+\\+abi*\n"); EXPECT_TRUE(SCL->inSection("", "src", "c++abi")); EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); } @@ -242,8 +244,9 @@ TEST_F(SpecialCaseListTest, PopularTrigram) { } TEST_F(SpecialCaseListTest, EscapedSymbols) { - std::unique_ptr SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n" - "src:*hello\\\\world*\n"); + std::unique_ptr SCL = + makeSpecialCaseList("src:*c\\+\\+abi*\n" + "src:*hello\\\\world*\n"); EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi")); EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi")); EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi")); @@ -315,7 +318,9 @@ TEST_F(SpecialCaseListTest, Version3) { "src:def\n" "[sect2]\n" "src:def\n" - "src:def\n"); + "src:def\n" + "[sect1]\n" + "src:foo*\n"); EXPECT_TRUE(SCL->inSection("sect1", "src", "fooz")); EXPECT_TRUE(SCL->inSection("sect1", "src", "barz")); EXPECT_FALSE(SCL->inSection("sect2", "src", "fooz")); @@ -323,9 +328,9 @@ TEST_F(SpecialCaseListTest, Version3) { EXPECT_TRUE(SCL->inSection("sect2", "src", "def")); EXPECT_TRUE(SCL->inSection("sect1", "src", "def")); - EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "src", "fooz")); EXPECT_EQ(4u, SCL->inSectionBlame("sect1", "src", "barz")); EXPECT_EQ(5u, SCL->inSectionBlame("sect1", "src", "def")); EXPECT_EQ(8u, SCL->inSectionBlame("sect2", "src", "def")); + EXPECT_EQ(10u, SCL->inSectionBlame("sect1", "src", "fooz")); } -} +} // namespace