Skip to content

[NFCI][Sanitizer] Convert SpecialCaseList::Sections from StringMap to vector. #140127

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

Conversation

qinkunbao
Copy link
Member

@qinkunbao qinkunbao commented May 15, 2025

As discussed in #139772, SpecialCaseList::Sections can keep the order of Sections when parsing the case list.

Created using spr 1.3.6
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" llvm:support labels May 15, 2025
@llvmbot
Copy link
Member

llvmbot commented May 15, 2025

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-clang

Author: Qinkun Bao (qinkunbao)

Changes

In this way, SpecialCaseList::Sections can keep the order of Sections
when parsing the case list.


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

5 Files Affected:

  • (modified) clang/lib/Basic/Diagnostic.cpp (+11-7)
  • (modified) clang/lib/Basic/ProfileList.cpp (+1-1)
  • (modified) clang/lib/Basic/SanitizerSpecialCaseList.cpp (+2-3)
  • (modified) llvm/include/llvm/Support/SpecialCaseList.h (+6-4)
  • (modified) llvm/lib/Support/SpecialCaseList.cpp (+16-13)
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index 538c1d18a8ac1..586273ab88bd3 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -533,16 +533,20 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
   // Drop the default section introduced by special case list, we only support
   // exact diagnostic group names.
   // FIXME: We should make this configurable in the parser instead.
-  Sections.erase("*");
+  // FIXME: C++20 can use std::erase_if(Sections, [](Section &sec) { return
+  // sec.SectionStr == "*"; });
+  Sections.erase(
+      std::remove_if(Sections.begin(), Sections.end(),
+                     [](Section &sec) { return sec.SectionStr == "*"; }),
+      Sections.end());
   // Make sure we iterate sections by their line numbers.
-  std::vector<std::pair<unsigned, const llvm::StringMapEntry<Section> *>>
-      LineAndSectionEntry;
+  std::vector<std::pair<unsigned, const Section *>> LineAndSectionEntry;
   LineAndSectionEntry.reserve(Sections.size());
   for (const auto &Entry : Sections) {
-    StringRef DiagName = Entry.getKey();
+    StringRef DiagName = Entry.SectionStr;
     // Each section has a matcher with that section's name, attached to that
     // line.
-    const auto &DiagSectionMatcher = Entry.getValue().SectionMatcher;
+    const auto &DiagSectionMatcher = Entry.SectionMatcher;
     unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
     LineAndSectionEntry.emplace_back(DiagLine, &Entry);
   }
@@ -550,7 +554,7 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
   static constexpr auto WarningFlavor = clang::diag::Flavor::WarningOrError;
   for (const auto &[_, SectionEntry] : LineAndSectionEntry) {
     SmallVector<diag::kind> GroupDiags;
-    StringRef DiagGroup = SectionEntry->getKey();
+    StringRef DiagGroup = SectionEntry->SectionStr;
     if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
             WarningFlavor, DiagGroup, GroupDiags)) {
       StringRef Suggestion =
@@ -563,7 +567,7 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
     for (diag::kind Diag : GroupDiags)
       // We're intentionally overwriting any previous mappings here to make sure
       // latest one takes precedence.
-      DiagToSection[Diag] = &SectionEntry->getValue();
+      DiagToSection[Diag] = SectionEntry;
   }
 }
 
diff --git a/clang/lib/Basic/ProfileList.cpp b/clang/lib/Basic/ProfileList.cpp
index 01b8d7a073432..f2383c76853ec 100644
--- a/clang/lib/Basic/ProfileList.cpp
+++ b/clang/lib/Basic/ProfileList.cpp
@@ -37,7 +37,7 @@ class ProfileSpecialCaseList : public llvm::SpecialCaseList {
 
   bool hasPrefix(StringRef Prefix) const {
     for (const auto &It : Sections)
-      if (It.second.Entries.count(Prefix) > 0)
+      if (It.Entries.count(Prefix) > 0)
         return true;
     return false;
   }
diff --git a/clang/lib/Basic/SanitizerSpecialCaseList.cpp b/clang/lib/Basic/SanitizerSpecialCaseList.cpp
index b02e868cdaa44..8af46c74535ae 100644
--- a/clang/lib/Basic/SanitizerSpecialCaseList.cpp
+++ b/clang/lib/Basic/SanitizerSpecialCaseList.cpp
@@ -38,11 +38,10 @@ SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
 
 void SanitizerSpecialCaseList::createSanitizerSections() {
   for (auto &It : Sections) {
-    auto &S = It.second;
     SanitizerMask Mask;
 
 #define SANITIZER(NAME, ID)                                                    \
-  if (S.SectionMatcher->match(NAME))                                           \
+  if (It.SectionMatcher->match(NAME))                                          \
     Mask |= SanitizerKind::ID;
 #define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)
 
@@ -50,7 +49,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() {
 #undef SANITIZER
 #undef SANITIZER_GROUP
 
-    SanitizerSections.emplace_back(Mask, S.Entries);
+    SanitizerSections.emplace_back(Mask, It.Entries);
   }
 }
 
diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h
index ca2030abdc1f5..4f4c097c7162a 100644
--- a/llvm/include/llvm/Support/SpecialCaseList.h
+++ b/llvm/include/llvm/Support/SpecialCaseList.h
@@ -132,14 +132,16 @@ class SpecialCaseList {
   using SectionEntries = StringMap<StringMap<Matcher>>;
 
   struct Section {
-    Section(std::unique_ptr<Matcher> M) : SectionMatcher(std::move(M)){};
-    Section() : Section(std::make_unique<Matcher>()) {}
+    Section(std::unique_ptr<Matcher> M, StringRef str)
+        : SectionMatcher(std::move(M)), SectionStr(SectionStr) {};
+    Section(StringRef str) : Section(std::make_unique<Matcher>(), str) {};
 
     std::unique_ptr<Matcher> SectionMatcher;
     SectionEntries Entries;
+    std::string SectionStr;
   };
 
-  StringMap<Section> Sections;
+  std::vector<Section> Sections;
 
   LLVM_ABI Expected<Section *> addSection(StringRef SectionStr, unsigned LineNo,
                                           bool UseGlobs = true);
@@ -154,6 +156,6 @@ class SpecialCaseList {
                                    StringRef Category) const;
 };
 
-}  // namespace llvm
+} // namespace llvm
 
 #endif // LLVM_SUPPORT_SPECIALCASELIST_H
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index 7a23421eaeb89..76c705c097aaa 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -132,14 +132,16 @@ bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
 Expected<SpecialCaseList::Section *>
 SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
                             bool UseGlobs) {
-  auto [It, DidEmplace] = Sections.try_emplace(SectionStr);
-  auto &Section = It->getValue();
-  if (DidEmplace)
-    if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs))
-      return createStringError(errc::invalid_argument,
-                               "malformed section at line " + Twine(LineNo) +
-                                   ": '" + SectionStr +
-                                   "': " + toString(std::move(Err)));
+  Sections.emplace_back(SectionStr);
+  auto &Section = Sections.back();
+
+  if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
+    return createStringError(errc::invalid_argument,
+                             "malformed section at line " + Twine(LineNo) +
+                                 ": '" + SectionStr +
+                                 "': " + toString(std::move(Err)));
+  }
+
   return &Section;
 }
 
@@ -213,9 +215,8 @@ unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
                                          StringRef Query,
                                          StringRef Category) const {
   for (const auto &It : Sections) {
-    const auto &S = It.getValue();
-    if (S.SectionMatcher->match(Section)) {
-      unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
+    if (It.SectionMatcher->match(Section)) {
+      unsigned Blame = inSectionBlame(It.Entries, Prefix, Query, Category);
       if (Blame)
         return Blame;
     }
@@ -227,9 +228,11 @@ unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
                                          StringRef Prefix, StringRef Query,
                                          StringRef Category) const {
   SectionEntries::const_iterator I = Entries.find(Prefix);
-  if (I == Entries.end()) return 0;
+  if (I == Entries.end())
+    return 0;
   StringMap<Matcher>::const_iterator II = I->second.find(Category);
-  if (II == I->second.end()) return 0;
+  if (II == I->second.end())
+    return 0;
 
   return II->getValue().match(Query);
 }

@qinkunbao qinkunbao requested review from vitalybuka and thurstond May 15, 2025 19:32
@qinkunbao
Copy link
Member Author

Looks like the unit tests have not been fixed. Will work on fixing them

@qinkunbao qinkunbao changed the title Convert SpecialCaseList::Sections from StringMap to vector. [Sanitizer] Convert SpecialCaseList::Sections from StringMap to vector. May 16, 2025
Copy link
Collaborator

@vitalybuka vitalybuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tag [NFC] or [NFCI] if the patch does not change any functionality.

@qinkunbao qinkunbao changed the title [Sanitizer] Convert SpecialCaseList::Sections from StringMap to vector. [NFCI][Sanitizer] Convert SpecialCaseList::Sections from StringMap to vector. May 16, 2025
qinkunbao added 2 commits May 16, 2025 15:22
Created using spr 1.3.6
@qinkunbao qinkunbao merged commit dd4a730 into main May 16, 2025
11 checks passed
@qinkunbao qinkunbao deleted the users/qinkunbao/spr/convert-specialcaselistsections-from-stringmap-to-vector-1 branch May 16, 2025 19:32
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 16, 2025
…tringMap to vector.

As discussed in llvm/llvm-project#139772, SpecialCaseList::Sections can keep the order of Sections when parsing the case list.

Reviewers: thurstond, vitalybuka

Reviewed By: vitalybuka

Pull Request: llvm/llvm-project#140127
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants