[NFC][SpecialCaseList] Extract findMatcher and ::match with callback (#162397)

There are some users who rely on more than line
number. It would be easy to move some logic from
users side here with extracted methods.
This commit is contained in:
Vitaly Buka
2025-10-07 17:42:09 -07:00
committed by GitHub
parent bdef80f7a9
commit aed53d19f9
3 changed files with 46 additions and 17 deletions

View File

@@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() {
SanitizerMask Mask;
#define SANITIZER(NAME, ID) \
if (S.SectionMatcher.match(NAME)) \
if (S.SectionMatcher.matchAny(NAME)) \
Mask |= SanitizerKind::ID;
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)

View File

@@ -123,9 +123,15 @@ protected:
public:
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
bool UseRegex);
// Returns the line number in the source file that this query matches to.
// Returns zero if no match is found.
LLVM_ABI unsigned match(StringRef Query) const;
LLVM_ABI void
match(StringRef Query,
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const;
LLVM_ABI bool matchAny(StringRef Query) const {
bool R = false;
match(Query, [&](StringRef, unsigned) { R = true; });
return R;
}
struct Glob {
std::string Name;
@@ -158,6 +164,15 @@ protected:
// 1-based line number on which rule is defined, or 0 if there is no match.
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query,
StringRef Category) const;
// Helper method to search by Prefix, Query, and Category. Returns
// matching rule, or empty string if there is no match.
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query,
StringRef Category) const;
private:
LLVM_ABI const SpecialCaseList::Matcher *
findMatcher(StringRef Prefix, StringRef Category) const;
};
std::vector<Section> Sections;

View File

@@ -20,6 +20,7 @@
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <algorithm>
#include <limits>
#include <stdio.h>
#include <string>
@@ -69,14 +70,15 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
return Error::success();
}
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
void SpecialCaseList::Matcher::match(
StringRef Query,
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
for (const auto &Glob : reverse(Globs))
if (Glob->Pattern.match(Query))
return Glob->LineNo;
Cb(Glob->Name, Glob->LineNo);
for (const auto &[Regex, LineNumber] : reverse(RegExes))
if (Regex->match(Query))
return LineNumber;
return 0;
Cb(/*FIXME: there is no users of this param yet */ "", LineNumber);
}
// TODO: Refactor this to return Expected<...>
@@ -227,7 +229,7 @@ std::pair<unsigned, unsigned>
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
StringRef Query, StringRef Category) const {
for (const auto &S : reverse(Sections)) {
if (S.SectionMatcher.match(Section)) {
if (S.SectionMatcher.matchAny(Section)) {
unsigned Blame = S.getLastMatch(Prefix, Query, Category);
if (Blame)
return {S.FileIdx, Blame};
@@ -236,17 +238,29 @@ SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
return NotFound;
}
const SpecialCaseList::Matcher *
SpecialCaseList::Section::findMatcher(StringRef Prefix,
StringRef Category) const {
SectionEntries::const_iterator I = Entries.find(Prefix);
if (I == Entries.end())
return nullptr;
StringMap<Matcher>::const_iterator II = I->second.find(Category);
if (II == I->second.end())
return nullptr;
return &II->second;
}
unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
StringRef Query,
StringRef Category) const {
SectionEntries::const_iterator I = Entries.find(Prefix);
if (I == Entries.end())
return 0;
StringMap<Matcher>::const_iterator II = I->second.find(Category);
if (II == I->second.end())
return 0;
return II->getValue().match(Query);
unsigned LastLine = 0;
if (const Matcher *M = findMatcher(Prefix, Category)) {
M->match(Query, [&](StringRef, unsigned LineNo) {
LastLine = std::max(LastLine, LineNo);
});
}
return LastLine;
}
} // namespace llvm