-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NFCI] Avoid adding duplicated SpecialCaseList::Sections. #140478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
qinkunbao
wants to merge
6
commits into
main
from
users/qinkunbao/spr/nfci-avoid-adding-duplicated-specialcaselistsections
Closed
[NFCI] Avoid adding duplicated SpecialCaseList::Sections. #140478
qinkunbao
wants to merge
6
commits into
main
from
users/qinkunbao/spr/nfci-avoid-adding-duplicated-specialcaselistsections
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.6
Member
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-llvm-support Author: Qinkun Bao (qinkunbao) Changes#140127 converts SpecialCaseList::Sections from StringMap to vector. Full diff: https://github.com/llvm/llvm-project/pull/140478.diff 2 Files Affected:
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"));
+}
+
}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Created using spr 1.3.6
Created using spr 1.3.6 [skip ci]
Created using spr 1.3.6 [skip ci]
Created using spr 1.3.6
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
llvm:support
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#140127 converts SpecialCaseList::Sections from StringMap to vector.
However, SpecialCaseList::addSection may be invoked several times with
the same SectionStr and LineNo