28#include <system_error>
34 unsigned LineNumber) {
37 "Supplied regex was blank");
40 auto Regexp = Pattern.str();
41 for (
size_t pos = 0; (pos = Regexp.find(
'*', pos)) != std::string::npos;
42 pos += strlen(
".*")) {
43 Regexp.replace(pos, strlen(
"*"),
".*");
46 Regexp = (Twine(
"^(") + StringRef(Regexp) +
")$").str();
49 Regex CheckRE(Regexp);
51 if (!CheckRE.isValid(REError))
54 RegExes.emplace_back(Pattern, LineNumber, std::move(CheckRE));
58void SpecialCaseList::RegexMatcher::preprocess(
bool BySize) {
61 return A.Name.size() <
B.Name.size();
66void SpecialCaseList::RegexMatcher::match(
68 llvm::function_ref<
void(StringRef Rule,
unsigned LineNo)> Cb)
const {
69 for (
const auto &R :
reverse(RegExes))
70 if (
R.Rg.match(Query))
71 return Cb(
R.Name,
R.LineNo);
74Error SpecialCaseList::GlobMatcher::insert(StringRef Pattern,
75 unsigned LineNumber) {
80 if (
auto Err = Res.takeError())
82 Globs.emplace_back(Pattern, LineNumber, std::move(Res.get()));
86void SpecialCaseList::GlobMatcher::preprocess(
bool BySize) {
89 return A.Name.size() <
B.Name.size();
93 for (
const auto &
G :
reverse(Globs)) {
94 StringRef
Prefix =
G.Pattern.prefix();
95 StringRef Suffix =
G.Pattern.suffix();
97 if (Suffix.empty() &&
Prefix.empty()) {
100 StringRef Substr =
G.Pattern.longest_substr();
101 if (!Substr.empty()) {
104 auto &
V = SubstrToGlob.emplace(Substr).first->second;
110 auto &SToGlob = PrefixSuffixToGlob.emplace(Prefix).first->second;
111 auto &
V = SToGlob.emplace(
reverse(Suffix)).first->second;
116void SpecialCaseList::GlobMatcher::match(
118 llvm::function_ref<
void(StringRef Rule,
unsigned LineNo)> Cb)
const {
119 if (!PrefixSuffixToGlob.empty()) {
120 for (
const auto &[
_, SToGlob] : PrefixSuffixToGlob.find_prefixes(Query)) {
121 for (
const auto &[
_, V] : SToGlob.find_prefixes(
reverse(Query))) {
122 for (
const auto *
G : V) {
123 if (
G->Pattern.match(Query)) {
124 Cb(
G->Name,
G->LineNo);
136 if (!SubstrToGlob.empty()) {
139 for (StringRef Q = Query; !Q.empty(); Q = Q.drop_front()) {
140 for (
const auto &[
_, V] : SubstrToGlob.find_prefixes(Q)) {
141 for (
const auto *
G : V) {
142 if (
G->Pattern.match(Query)) {
143 Cb(
G->Name,
G->LineNo);
156SpecialCaseList::Matcher::Matcher(
bool UseGlobs,
bool RemoveDotSlash)
157 : RemoveDotSlash(RemoveDotSlash) {
159 M.emplace<GlobMatcher>();
161 M.emplace<RegexMatcher>();
164Error SpecialCaseList::Matcher::insert(StringRef Pattern,
unsigned LineNumber) {
165 return std::visit([&](
auto &V) {
return V.insert(Pattern, LineNumber); }, M);
168void SpecialCaseList::Matcher::preprocess(
bool BySize) {
169 return std::visit([&](
auto &V) {
return V.preprocess(BySize); }, M);
172void SpecialCaseList::Matcher::match(
177 return std::visit([&](
auto &V) {
return V.match(Query, Cb); }, M);
181std::unique_ptr<SpecialCaseList>
185 if (SCL->createInternal(Paths, FS,
Error))
191 std::string &
Error) {
193 if (SCL->createInternal(MB,
Error))
198std::unique_ptr<SpecialCaseList>
209 for (
size_t i = 0; i < Paths.size(); ++i) {
210 const auto &Path = Paths[i];
213 if (std::error_code EC = FileOrErr.
getError()) {
214 Error = (
Twine(
"can't open file '") + Path +
"': " + EC.message()).str();
217 std::string ParseError;
218 if (!
parse(i, FileOrErr.
get().get(), ParseError,
false)) {
219 Error = (
Twine(
"error parsing file '") + Path +
"': " + ParseError).str();
234SpecialCaseList::addSection(
StringRef SectionStr,
unsigned FileNo,
235 unsigned LineNo,
bool UseGlobs) {
236 Sections.emplace_back(SectionStr, FileNo, UseGlobs);
237 auto &Section = Sections.back();
239 SectionStr = SectionStr.
copy(StrAlloc);
240 if (
auto Err = Section.SectionMatcher.insert(SectionStr, LineNo)) {
242 "malformed section at line " +
Twine(LineNo) +
250bool SpecialCaseList::parse(
unsigned FileIdx,
const MemoryBuffer *MB,
251 std::string &Error,
bool OrderBySize) {
252 unsigned long long Version = 2;
254 StringRef Header = MB->getBuffer();
255 if (Header.consume_front(
"#!special-case-list-v"))
263 bool UseGlobs = Version > 1;
265 bool RemoveDotSlash = Version > 2;
268 if (
auto Err =
addSection(
"*", FileIdx, 1,
true).moveInto(CurrentSection)) {
275 constexpr StringRef PathPrefixes[] = {
"src",
"!src",
"mainfile",
"source"};
277 for (line_iterator LineIt(*MB,
true,
'#');
278 !LineIt.is_at_eof(); LineIt++) {
279 unsigned LineNo = LineIt.line_number();
280 StringRef Line = LineIt->trim();
285 if (Line.starts_with(
"[")) {
286 if (!Line.ends_with(
"]")) {
288 (
"malformed section header on line " + Twine(LineNo) +
": " + Line)
293 if (
auto Err =
addSection(Line.drop_front().drop_back(), FileIdx, LineNo,
295 .moveInto(CurrentSection)) {
303 auto [
Prefix, Postfix] = Line.split(
":");
304 if (Postfix.empty()) {
306 Error = (
"malformed line " + Twine(LineNo) +
": '" + Line +
"'").str();
310 auto [Pattern, Category] = Postfix.split(
"=");
311 auto [It,
_] = CurrentSection->Entries[
Prefix].try_emplace(
314 Pattern = Pattern.copy(StrAlloc);
315 if (
auto Err = It->second.insert(Pattern, LineNo)) {
317 (Twine(
"malformed ") + (UseGlobs ?
"glob" :
"regex") +
" in line " +
318 Twine(LineNo) +
": '" + Pattern +
"': " +
toString(std::move(Err)))
324 for (Section &S : Sections)
325 S.preprocess(OrderBySize);
330SpecialCaseList::~SpecialCaseList() =
default;
338std::pair<unsigned, unsigned>
341 for (
const auto &S :
reverse(Sections)) {
342 if (S.SectionMatcher.matchAny(
Section)) {
343 unsigned Blame = S.getLastMatch(Prefix, Query, Category);
345 return {S.FileIdx, Blame};
351const SpecialCaseList::Matcher *
352SpecialCaseList::Section::findMatcher(
StringRef Prefix,
354 SectionEntries::const_iterator
I = Entries.find(Prefix);
355 if (
I == Entries.end())
358 if (
II ==
I->second.end())
364LLVM_ABI void SpecialCaseList::Section::preprocess(
bool OrderBySize) {
365 SectionMatcher.preprocess(
false);
366 for (
auto &[K1,
E] : Entries)
367 for (
auto &[K2, M] :
E)
368 M.preprocess(OrderBySize);
374 unsigned LastLine = 0;
375 if (
const Matcher *M = findMatcher(Prefix, Category)) {
376 M->match(Query, [&](
StringRef,
unsigned LineNo) {
377 LastLine = std::max(LastLine, LineNo);
387 if (
const Matcher *M = findMatcher(Prefix, Category)) {
388 M->match(Query, [&](
StringRef Rule,
unsigned) {
389 if (LongestRule.
size() < Rule.
size())
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
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.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
This interface provides simple read-only access to a block of memory, and provides simple methods for...
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.
StringMapIterBase< ValueTy, true > const_iterator
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
StringRef copy(Allocator &A) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
An efficient, type-erasing, non-owning reference to a callable.
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.
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)
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)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query, StringRef Category) const
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query, StringRef Category) const