32#include <system_error>
41using Match = std::pair<StringRef, unsigned>;
42static constexpr Match NotMatched = {
"", 0};
47 Error insert(StringRef Pattern,
unsigned LineNumber);
48 void preprocess(
bool BySize);
50 Match
match(StringRef Query)
const;
53 Reg(StringRef Name,
unsigned LineNo, Regex &&Rg)
54 : Name(Name), LineNo(LineNo), Rg(std::
move(Rg)) {}
60 std::vector<Reg> RegExes;
65 Error insert(StringRef Pattern,
unsigned LineNumber);
66 void preprocess(
bool BySize);
68 Match
match(StringRef Query)
const;
71 Glob(StringRef Name,
unsigned LineNo, GlobPattern &&Pattern)
72 : Name(Name), LineNo(LineNo), Pattern(std::
move(Pattern)) {}
78 std::vector<GlobMatcher::Glob> Globs;
80 RadixTree<iterator_range<StringRef::const_iterator>,
81 RadixTree<iterator_range<StringRef::const_reverse_iterator>,
92 Matcher(
bool UseGlobs,
bool RemoveDotSlash);
94 Error insert(StringRef Pattern,
unsigned LineNumber);
95 void preprocess(
bool BySize);
96 Match
match(StringRef Query)
const;
98 bool matchAny(StringRef Query)
const {
return match(Query).second > 0; }
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();
128void RegexMatcher::preprocess(
bool BySize) {
131 return A.Name.size() <
B.Name.size();
136Match RegexMatcher::match(StringRef Query)
const {
137 for (
const auto &R :
reverse(RegExes))
138 if (
R.Rg.match(Query))
139 return {
R.Name,
R.LineNo};
143Error GlobMatcher::insert(StringRef Pattern,
unsigned LineNumber) {
147 auto Res = GlobPattern::create(Pattern, 1024);
148 if (
auto Err = Res.takeError())
150 Globs.emplace_back(Pattern, LineNumber, std::move(Res.get()));
151 return Error::success();
154void GlobMatcher::preprocess(
bool BySize) {
157 return A.Name.size() <
B.Name.size();
161 for (
const auto &[Idx,
G] :
enumerate(Globs)) {
162 StringRef
Prefix =
G.Pattern.prefix();
163 StringRef Suffix =
G.Pattern.suffix();
165 if (Suffix.empty() &&
Prefix.empty()) {
168 StringRef Substr =
G.Pattern.longest_substr();
169 if (!Substr.empty()) {
172 auto &
V = SubstrToGlob.emplace(Substr).first->second;
178 auto &SToGlob = PrefixSuffixToGlob.emplace(Prefix).first->second;
179 auto &
V = SToGlob.emplace(
reverse(Suffix)).first->second;
184Match GlobMatcher::match(StringRef Query)
const {
186 if (!PrefixSuffixToGlob.empty()) {
187 for (
const auto &[
_, SToGlob] : PrefixSuffixToGlob.find_prefixes(Query)) {
188 for (
const auto &[
_, V] : SToGlob.find_prefixes(
reverse(Query))) {
192 const GlobMatcher::Glob &
G = Globs[Idx];
193 if (
G.Pattern.match(Query)) {
206 if (!SubstrToGlob.empty()) {
209 for (StringRef Q = Query; !Q.empty(); Q = Q.drop_front()) {
210 for (
const auto &[
_, V] : SubstrToGlob.find_prefixes(Q)) {
214 const GlobMatcher::Glob &
G = Globs[Idx];
215 if (
G.Pattern.match(Query)) {
229 return {Globs[Best].Name, Globs[Best].LineNo};
232Matcher::Matcher(
bool UseGlobs,
bool RemoveDotSlash)
233 : RemoveDotSlash(RemoveDotSlash) {
235 M.emplace<GlobMatcher>();
237 M.emplace<RegexMatcher>();
240Error Matcher::insert(StringRef Pattern,
unsigned LineNumber) {
241 return std::visit([&](
auto &V) {
return V.insert(Pattern, LineNumber); }, M);
244void Matcher::preprocess(
bool BySize) {
245 return std::visit([&](
auto &V) {
return V.preprocess(BySize); }, M);
248Match Matcher::match(StringRef Query)
const {
251 return std::visit([&](
auto &V) -> Match {
return V.match(Query); }, M);
270std::unique_ptr<SpecialCaseList>
274 if (SCL->createInternal(Paths, FS,
Error))
280 std::string &
Error) {
282 if (SCL->createInternal(MB,
Error))
287std::unique_ptr<SpecialCaseList>
298 for (
size_t i = 0; i < Paths.size(); ++i) {
299 const auto &Path = Paths[i];
302 if (std::error_code EC = FileOrErr.
getError()) {
303 Error = (
Twine(
"can't open file '") + Path +
"': " + EC.message()).str();
306 std::string ParseError;
307 if (!
parse(i, FileOrErr.
get().get(), ParseError,
false)) {
308 Error = (
Twine(
"error parsing file '") + Path +
"': " + ParseError).str();
323SpecialCaseList::addSection(
StringRef SectionStr,
unsigned FileNo,
324 unsigned LineNo,
bool UseGlobs) {
325 SectionStr = SectionStr.
copy(StrAlloc);
326 Sections.emplace_back(SectionStr, FileNo, UseGlobs);
327 auto &Section = Sections.back();
329 if (
auto Err = Section.Impl->SectionMatcher.insert(SectionStr, LineNo)) {
331 "malformed section at line " +
Twine(LineNo) +
339bool SpecialCaseList::parse(
unsigned FileIdx,
const MemoryBuffer *MB,
340 std::string &Error,
bool OrderBySize) {
341 unsigned long long Version = 2;
343 StringRef Header = MB->getBuffer();
344 if (Header.consume_front(
"#!special-case-list-v"))
352 bool UseGlobs = Version > 1;
354 bool RemoveDotSlash = Version > 2;
356 auto ErrOrSection =
addSection(
"*", FileIdx, 1,
true);
357 if (
auto Err = ErrOrSection.takeError()) {
361 Section::SectionImpl *CurrentImpl = ErrOrSection.get()->Impl.get();
365 constexpr StringRef PathPrefixes[] = {
"src",
"!src",
"mainfile",
"source"};
367 for (line_iterator LineIt(*MB,
true,
'#');
368 !LineIt.is_at_eof(); LineIt++) {
369 unsigned LineNo = LineIt.line_number();
370 StringRef Line = LineIt->trim();
375 if (Line.starts_with(
"[")) {
376 if (!Line.ends_with(
"]")) {
378 (
"malformed section header on line " + Twine(LineNo) +
": " + Line)
384 addSection(Line.drop_front().drop_back(), FileIdx, LineNo, UseGlobs);
385 if (
auto Err = ErrOrSection.takeError()) {
389 CurrentImpl = ErrOrSection.get()->Impl.get();
394 auto [
Prefix, Postfix] = Line.split(
":");
395 if (Postfix.empty()) {
397 Error = (
"malformed line " + Twine(LineNo) +
": '" + Line +
"'").str();
401 auto [Pattern, Category] = Postfix.split(
"=");
402 auto [It,
_] = CurrentImpl->Entries[
Prefix].try_emplace(
405 Pattern = Pattern.copy(StrAlloc);
406 if (
auto Err = It->second.insert(Pattern, LineNo)) {
408 (Twine(
"malformed ") + (UseGlobs ?
"glob" :
"regex") +
" in line " +
409 Twine(LineNo) +
": '" + Pattern +
"': " +
toString(std::move(Err)))
415 for (Section &S : Sections)
416 S.Impl->preprocess(OrderBySize);
421SpecialCaseList::~SpecialCaseList() =
default;
429std::pair<unsigned, unsigned>
432 for (
const auto &S :
reverse(Sections)) {
433 if (S.Impl->SectionMatcher.matchAny(
Section)) {
434 unsigned Blame = S.getLastMatch(Prefix, Query, Category);
436 return {S.FileIdx, Blame};
444 : Name(Str), FileIdx(FileIdx),
452 return Impl->SectionMatcher.matchAny(Name);
462 if (
II ==
I->second.end())
471 for (
auto &[K2, M] :
E)
472 M.preprocess(OrderBySize);
478 if (
const Matcher *M = Impl->findMatcher(Prefix, Category))
479 return M->match(Query).second;
486 if (
const Matcher *M = Impl->findMatcher(Prefix, Category))
487 return M->match(Query).first;
492 return Impl->Entries.find(Prefix) != Impl->Entries.end();
This file defines the StringMap class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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
void preprocess(bool OrderBySize)
SectionImpl(bool UseGlobs)
LLVM_ABI Section(StringRef Name, unsigned FileIdx, bool UseGlobs)
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query, StringRef Category) const
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.
void stable_sort(R &&Range)
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.