33#include <system_error>
45 Error insert(StringRef Pattern,
unsigned LineNumber);
46 unsigned match(StringRef Query)
const;
50 Reg(StringRef Name,
unsigned LineNo, Regex &&Rg)
51 : Name(Name), LineNo(LineNo), Rg(std::
move(Rg)) {}
57 std::vector<Reg> RegExes;
62 Error insert(StringRef Pattern,
unsigned LineNumber);
63 unsigned match(StringRef Query)
const;
67 Glob(StringRef Name,
unsigned LineNo, GlobPattern &&Pattern)
68 : Name(Name), LineNo(LineNo), Pattern(std::
move(Pattern)) {}
74 void LazyInit()
const;
76 std::vector<GlobMatcher::Glob> Globs;
78 mutable RadixTree<iterator_range<StringRef::const_iterator>,
79 RadixTree<iterator_range<StringRef::const_reverse_iterator>,
83 mutable RadixTree<iterator_range<StringRef::const_iterator>,
87 mutable bool Initialized =
false;
93 Matcher(
bool UseGlobs,
bool RemoveDotSlash);
95 Error insert(StringRef Pattern,
unsigned LineNumber);
96 unsigned match(StringRef Query)
const;
98 bool matchAny(StringRef Query)
const {
return match(Query); }
100 std::variant<RegexMatcher, GlobMatcher> M;
107 "Supplied regex was blank");
110 auto Regexp = Pattern.str();
111 for (
size_t pos = 0; (pos = Regexp.find(
'*', pos)) != std::string::npos;
112 pos += strlen(
".*")) {
113 Regexp.replace(pos, strlen(
"*"),
".*");
116 Regexp = (Twine(
"^(") + StringRef(Regexp) +
")$").str();
119 Regex CheckRE(Regexp);
121 if (!CheckRE.isValid(REError))
124 RegExes.emplace_back(Pattern, LineNumber, std::move(CheckRE));
125 return Error::success();
128unsigned RegexMatcher::match(StringRef Query)
const {
129 for (
const auto &R :
reverse(RegExes))
130 if (
R.Rg.match(Query))
135Error GlobMatcher::insert(StringRef Pattern,
unsigned LineNumber) {
139 auto Res = GlobPattern::create(Pattern, 1024);
140 if (
auto Err = Res.takeError())
142 Globs.emplace_back(Pattern, LineNumber, std::move(Res.get()));
143 return Error::success();
146void GlobMatcher::LazyInit()
const {
150 for (
const auto &[Idx,
G] :
enumerate(Globs)) {
151 StringRef
Prefix =
G.Pattern.prefix();
152 StringRef Suffix =
G.Pattern.suffix();
154 if (Suffix.empty() &&
Prefix.empty()) {
157 StringRef Substr =
G.Pattern.longest_substr();
158 if (!Substr.empty()) {
161 auto &
V = SubstrToGlob.emplace(Substr).first->second;
167 auto &SToGlob = PrefixSuffixToGlob.emplace(Prefix).first->second;
168 auto &
V = SToGlob.emplace(
reverse(Suffix)).first->second;
173unsigned GlobMatcher::match(StringRef Query)
const {
177 if (!PrefixSuffixToGlob.empty()) {
178 for (
const auto &[
_, SToGlob] : PrefixSuffixToGlob.find_prefixes(Query)) {
179 for (
const auto &[
_, V] : SToGlob.find_prefixes(
reverse(Query))) {
183 const GlobMatcher::Glob &
G = Globs[Idx];
184 if (
G.Pattern.match(Query)) {
197 if (!SubstrToGlob.empty()) {
200 for (StringRef Q = Query; !Q.empty(); Q = Q.drop_front()) {
201 for (
const auto &[
_, V] : SubstrToGlob.find_prefixes(Q)) {
205 const GlobMatcher::Glob &
G = Globs[Idx];
206 if (
G.Pattern.match(Query)) {
218 return Best < 0 ? 0 : Globs[Best].LineNo;
221Matcher::Matcher(
bool UseGlobs,
bool RemoveDotSlash)
222 : RemoveDotSlash(RemoveDotSlash) {
224 M.emplace<GlobMatcher>();
226 M.emplace<RegexMatcher>();
229Error Matcher::insert(StringRef Pattern,
unsigned LineNumber) {
230 return std::visit([&](
auto &V) {
return V.insert(Pattern, LineNumber); }, M);
233unsigned Matcher::match(StringRef Query)
const {
236 return std::visit([&](
auto &V) ->
unsigned {
return V.match(Query); }, M);
254std::unique_ptr<SpecialCaseList>
258 if (SCL->createInternal(Paths, FS,
Error))
264 std::string &
Error) {
266 if (SCL->createInternal(MB,
Error))
271std::unique_ptr<SpecialCaseList>
282 for (
size_t i = 0; i < Paths.size(); ++i) {
283 const auto &Path = Paths[i];
286 if (std::error_code EC = FileOrErr.
getError()) {
287 Error = (
Twine(
"can't open file '") + Path +
"': " + EC.message()).str();
290 std::string ParseError;
291 if (!
parse(i, FileOrErr.
get().get(), ParseError)) {
292 Error = (
Twine(
"error parsing file '") + Path +
"': " + ParseError).str();
300 std::string &
Error) {
307SpecialCaseList::addSection(
StringRef SectionStr,
unsigned FileNo,
308 unsigned LineNo,
bool UseGlobs) {
309 SectionStr = SectionStr.
copy(StrAlloc);
310 Sections.emplace_back(SectionStr, FileNo, UseGlobs);
311 auto &Section = Sections.back();
313 if (
auto Err = Section.Impl->SectionMatcher.insert(SectionStr, LineNo)) {
315 "malformed section at line " +
Twine(LineNo) +
323bool SpecialCaseList::parse(
unsigned FileIdx,
const MemoryBuffer *MB,
324 std::string &Error) {
325 unsigned long long Version = 2;
327 StringRef Header = MB->getBuffer();
328 if (Header.consume_front(
"#!special-case-list-v"))
336 bool UseGlobs = Version > 1;
338 bool RemoveDotSlash = Version > 2;
340 auto ErrOrSection =
addSection(
"*", FileIdx, 1,
true);
341 if (
auto Err = ErrOrSection.takeError()) {
345 Section::SectionImpl *CurrentImpl = ErrOrSection.get()->Impl.get();
349 constexpr StringRef PathPrefixes[] = {
"src",
"!src",
"mainfile",
"source"};
351 for (line_iterator LineIt(*MB,
true,
'#');
352 !LineIt.is_at_eof(); LineIt++) {
353 unsigned LineNo = LineIt.line_number();
354 StringRef Line = LineIt->trim();
359 if (Line.starts_with(
"[")) {
360 if (!Line.ends_with(
"]")) {
362 (
"malformed section header on line " + Twine(LineNo) +
": " + Line)
368 addSection(Line.drop_front().drop_back(), FileIdx, LineNo, UseGlobs);
369 if (
auto Err = ErrOrSection.takeError()) {
373 CurrentImpl = ErrOrSection.get()->Impl.get();
378 auto [
Prefix, Postfix] = Line.split(
":");
379 if (Postfix.empty()) {
381 Error = (
"malformed line " + Twine(LineNo) +
": '" + Line +
"'").str();
385 auto [Pattern, Category] = Postfix.split(
"=");
386 auto [It,
_] = CurrentImpl->Entries[
Prefix].try_emplace(
389 Pattern = Pattern.copy(StrAlloc);
390 if (
auto Err = It->second.insert(Pattern, LineNo)) {
392 (Twine(
"malformed ") + (UseGlobs ?
"glob" :
"regex") +
" in line " +
393 Twine(LineNo) +
": '" + Pattern +
"': " +
toString(std::move(Err)))
402SpecialCaseList::~SpecialCaseList() =
default;
410std::pair<unsigned, unsigned>
413 for (
const auto &S :
reverse(Sections)) {
414 if (S.Impl->SectionMatcher.matchAny(
Section)) {
415 unsigned Blame = S.getLastMatch(Prefix, Query, Category);
417 return {S.FileIdx, Blame};
425 : Name(Str), FileIdx(FileIdx),
433 return Impl->SectionMatcher.matchAny(Name);
443 if (
II ==
I->second.end())
452 if (
const Matcher *M = Impl->findMatcher(Prefix, Category))
453 return M->match(Query);
458 return Impl->Entries.contains(Prefix);
This file defines the StringMap class.
#define LLVM_LIKELY(EXPR)
static llvm::Error parse(DataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
static const char * toString(MIToken::TokenKind TokenKind)
static Error addSection(const NewSectionInfo &NewSection, Object &Obj)
uint64_t IntrinsicInst * II
This file defines the SmallVector class.
Defines the virtual file system interface vfs::FileSystem.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
StringMap< StringMap< Matcher > > SectionEntries
const Matcher * findMatcher(StringRef Prefix, StringRef Category) const
SectionImpl(bool UseGlobs)
LLVM_ABI Section(StringRef Name, unsigned FileIdx, bool UseGlobs)
LLVM_ABI bool hasPrefix(StringRef Prefix) const
Returns true if the section has any entries for the given prefix.
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query, StringRef Category) const
LLVM_ABI bool matchName(StringRef Name) const
static constexpr std::pair< unsigned, unsigned > NotFound
LLVM_ABI std::pair< unsigned, unsigned > inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns the file index and the line number <FileIdx, LineNo> corresponding to the special case list e...
LLVM_ABI bool createInternal(const std::vector< std::string > &Paths, vfs::FileSystem &VFS, std::string &Error)
static LLVM_ABI std::unique_ptr< SpecialCaseList > createOrDie(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS)
Parses the special case list entries from files.
static LLVM_ABI std::unique_ptr< SpecialCaseList > create(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS, std::string &Error)
Parses the special case list entries from files.
SpecialCaseList()=default
LLVM_ABI bool inSection(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category=StringRef()) const
Returns true, if special case list contains a line.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringMapIterBase< StringMap< Matcher >, true > const_iterator
StringRef - Represent a constant reference to a string, i.e.
StringRef copy(Allocator &A) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The virtual file system interface.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false, bool IsText=true)
This is a convenience method that opens a file, gets its content and then closes the file.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
bool match(Val *V, const Pattern &P)
LLVM_ABI StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
auto reverse(ContainerTy &&C)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Implement std::hash so that hash_code can be used in STL containers.