Skip to content

Commit 4f6cb06

Browse files
authored
[NFC][SpecialCaseList] Move "LongestMatch" logic from WarningsSpecialCaseList into SpecialCaseList (#162409)
This way we can be more flexible optimizing SpecialCaseList internals.
1 parent 774ffe5 commit 4f6cb06

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

clang/lib/Basic/Diagnostic.cpp

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,6 @@ class WarningsSpecialCaseList : public llvm::SpecialCaseList {
517517
const SourceManager &SM) const;
518518

519519
private:
520-
// Find the longest glob pattern that matches FilePath amongst
521-
// CategoriesToMatchers, return true iff the match exists and belongs to a
522-
// positive category.
523-
bool globsMatches(const llvm::StringMap<Matcher> &CategoriesToMatchers,
524-
StringRef FilePath) const;
525-
526520
llvm::DenseMap<diag::kind, const Section *> DiagToSection;
527521
};
528522
} // namespace
@@ -584,43 +578,24 @@ void DiagnosticsEngine::setDiagSuppressionMapping(llvm::MemoryBuffer &Input) {
584578
bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId,
585579
SourceLocation DiagLoc,
586580
const SourceManager &SM) const {
581+
PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc);
582+
if (!PLoc.isValid())
583+
return false;
587584
const Section *DiagSection = DiagToSection.lookup(DiagId);
588585
if (!DiagSection)
589586
return false;
590-
const SectionEntries &EntityTypeToCategories = DiagSection->Entries;
591-
auto SrcEntriesIt = EntityTypeToCategories.find("src");
592-
if (SrcEntriesIt == EntityTypeToCategories.end())
587+
588+
StringRef F = llvm::sys::path::remove_leading_dotslash(PLoc.getFilename());
589+
590+
StringRef LongestSup = DiagSection->getLongestMatch("src", F, "");
591+
if (LongestSup.empty())
593592
return false;
594-
const llvm::StringMap<llvm::SpecialCaseList::Matcher> &CategoriesToMatchers =
595-
SrcEntriesIt->getValue();
596-
// We also use presumed locations here to improve reproducibility for
597-
// preprocessed inputs.
598-
if (PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc); PLoc.isValid())
599-
return globsMatches(
600-
CategoriesToMatchers,
601-
llvm::sys::path::remove_leading_dotslash(PLoc.getFilename()));
602-
return false;
603-
}
604593

605-
bool WarningsSpecialCaseList::globsMatches(
606-
const llvm::StringMap<Matcher> &CategoriesToMatchers,
607-
StringRef FilePath) const {
608-
StringRef LongestMatch;
609-
bool LongestIsPositive = false;
610-
for (const auto &Entry : CategoriesToMatchers) {
611-
StringRef Category = Entry.getKey();
612-
const llvm::SpecialCaseList::Matcher &Matcher = Entry.getValue();
613-
bool IsPositive = Category != "emit";
614-
for (const auto &Glob : Matcher.Globs) {
615-
if (Glob->Name.size() < LongestMatch.size())
616-
continue;
617-
if (!Glob->Pattern.match(FilePath))
618-
continue;
619-
LongestMatch = Glob->Name;
620-
LongestIsPositive = IsPositive;
621-
}
622-
}
623-
return LongestIsPositive;
594+
StringRef LongestEmit = DiagSection->getLongestMatch("src", F, "emit");
595+
if (LongestEmit.empty())
596+
return true;
597+
598+
return LongestSup.size() > LongestEmit.size();
624599
}
625600

626601
bool DiagnosticsEngine::isSuppressedViaMapping(diag::kind DiagId,

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,17 @@ unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
273273
return LastLine;
274274
}
275275

276+
StringRef SpecialCaseList::Section::getLongestMatch(StringRef Prefix,
277+
StringRef Query,
278+
StringRef Category) const {
279+
StringRef LongestRule;
280+
if (const Matcher *M = findMatcher(Prefix, Category)) {
281+
M->match(Query, [&](StringRef Rule, unsigned) {
282+
if (LongestRule.size() < Rule.size())
283+
LongestRule = Rule;
284+
});
285+
}
286+
return LongestRule;
287+
}
288+
276289
} // namespace llvm

0 commit comments

Comments
 (0)