bool SpecialCaseList::Matcher::insert(std::string Regexp, unsigned LineNumber, std::string &REError) { if (Regexp.empty()) { REError = "Supplied regexp was blank"; return false; } if (Regex::isLiteralERE(Regexp)) { Strings[Regexp] = LineNumber; return true; } Trigrams.insert(Regexp); // Replace * with .* for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos; pos += strlen(".*")) { Regexp.replace(pos, strlen("*"), ".*"); } Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str(); // Check that the regexp is valid. Regex CheckRE(Regexp); if (!CheckRE.isValid(REError)) return false; RegExes.emplace_back( std::make_pair(make_unique<Regex>(std::move(CheckRE)), LineNumber)); return true; }
BlackList::BlackList(const StringRef Path) { // Validate and open blacklist file. if (Path.empty()) return; OwningPtr<MemoryBuffer> File; if (error_code EC = MemoryBuffer::getFile(Path, File)) { report_fatal_error("Can't open blacklist file: " + Path + ": " + EC.message()); } // Iterate through each line in the blacklist file. SmallVector<StringRef, 16> Lines; SplitString(File.take()->getBuffer(), Lines, "\n\r"); StringMap<std::string> Regexps; for (SmallVector<StringRef, 16>::iterator I = Lines.begin(), E = Lines.end(); I != E; ++I) { // Ignore empty lines and lines starting with "#" if (I->empty() || I->startswith("#")) continue; // Get our prefix and unparsed regexp. std::pair<StringRef, StringRef> SplitLine = I->split(":"); StringRef Prefix = SplitLine.first; std::string Regexp = SplitLine.second; if (Regexp.empty()) { // Missing ':' in the line. report_fatal_error("malformed blacklist line: " + SplitLine.first); } // Replace * with .* for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos; pos += strlen(".*")) { Regexp.replace(pos, strlen("*"), ".*"); } // Check that the regexp is valid. Regex CheckRE(Regexp); std::string Error; if (!CheckRE.isValid(Error)) { report_fatal_error("malformed blacklist regex: " + SplitLine.second + ": " + Error); } // Add this regexp into the proper group by its prefix. if (!Regexps[Prefix].empty()) Regexps[Prefix] += "|"; Regexps[Prefix] += Regexp; } // Iterate through each of the prefixes, and create Regexs for them. for (StringMap<std::string>::iterator I = Regexps.begin(), E = Regexps.end(); I != E; ++I) { Entries[I->getKey()] = new Regex(I->getValue()); } }
bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) { // Iterate through each line in the blacklist file. SmallVector<StringRef, 16> Lines; SplitString(MB->getBuffer(), Lines, "\n\r"); int LineNo = 1; for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { // Ignore empty lines and lines starting with "#" if (I->empty() || I->startswith("#")) continue; // Get our prefix and unparsed regexp. std::pair<StringRef, StringRef> SplitLine = I->split(":"); StringRef Prefix = SplitLine.first; if (SplitLine.second.empty()) { // Missing ':' in the line. Error = (Twine("malformed line ") + Twine(LineNo) + ": '" + SplitLine.first + "'").str(); return false; } std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("="); std::string Regexp = SplitRegexp.first; StringRef Category = SplitRegexp.second; // See if we can store Regexp in Strings. auto &Entry = Entries[Prefix][Category]; if (Regex::isLiteralERE(Regexp)) { Entry.Strings.insert(Regexp); continue; } Entry.Trigrams.insert(Regexp); // Replace * with .* for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos; pos += strlen(".*")) { Regexp.replace(pos, strlen("*"), ".*"); } // Check that the regexp is valid. Regex CheckRE(Regexp); std::string REError; if (!CheckRE.isValid(REError)) { Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" + SplitLine.second + "': " + REError).str(); return false; } // Add this regexp into the proper group by its prefix. if (!Regexps[Prefix][Category].empty()) Regexps[Prefix][Category] += "|"; Regexps[Prefix][Category] += "^" + Regexp + "$"; } return true; }
bool SpecialCaseList::Matcher::insert(std::string Regexp, std::string &REError) { if (Regex::isLiteralERE(Regexp)) { Strings.insert(Regexp); return true; } Trigrams.insert(Regexp); // Replace * with .* for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos; pos += strlen(".*")) { Regexp.replace(pos, strlen("*"), ".*"); } // Check that the regexp is valid. Regex CheckRE(Regexp); if (!CheckRE.isValid(REError)) return false; if (!UncompiledRegEx.empty()) UncompiledRegEx += "|"; UncompiledRegEx += "^(" + Regexp + ")$"; return true; }
bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) { // Iterate through each line in the blacklist file. SmallVector<StringRef, 16> Lines; SplitString(MB->getBuffer(), Lines, "\n\r"); StringMap<StringMap<std::string> > Regexps; assert(Entries.empty() && "parse() should be called on an empty SpecialCaseList"); int LineNo = 1; for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { // Ignore empty lines and lines starting with "#" if (I->empty() || I->startswith("#")) continue; // Get our prefix and unparsed regexp. std::pair<StringRef, StringRef> SplitLine = I->split(":"); StringRef Prefix = SplitLine.first; if (SplitLine.second.empty()) { // Missing ':' in the line. Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" + SplitLine.first + "'").str(); return false; } std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("="); std::string Regexp = SplitRegexp.first; StringRef Category = SplitRegexp.second; // Backwards compatibility. if (Prefix == "global-init") { Prefix = "global"; Category = "init"; } else if (Prefix == "global-init-type") { Prefix = "type"; Category = "init"; } else if (Prefix == "global-init-src") { Prefix = "src"; Category = "init"; } // See if we can store Regexp in Strings. if (Regex::isLiteralERE(Regexp)) { Entries[Prefix][Category].Strings.insert(Regexp); continue; } // Replace * with .* for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos; pos += strlen(".*")) { Regexp.replace(pos, strlen("*"), ".*"); } // Check that the regexp is valid. Regex CheckRE(Regexp); std::string REError; if (!CheckRE.isValid(REError)) { Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" + SplitLine.second + "': " + REError).str(); return false; } // Add this regexp into the proper group by its prefix. if (!Regexps[Prefix][Category].empty()) Regexps[Prefix][Category] += "|"; Regexps[Prefix][Category] += "^" + Regexp + "$"; } // Iterate through each of the prefixes, and create Regexs for them. for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(), E = Regexps.end(); I != E; ++I) { for (StringMap<std::string>::const_iterator II = I->second.begin(), IE = I->second.end(); II != IE; ++II) { Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue()); } } return true; }
bool SpecialCaseList::parse(const MemoryBuffer *MB, StringMap<size_t> &SectionsMap, std::string &Error) { // Iterate through each line in the blacklist file. SmallVector<StringRef, 16> Lines; MB->getBuffer().split(Lines, '\n'); unsigned LineNo = 1; StringRef Section = "*"; for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) { *I = I->trim(); // Ignore empty lines and lines starting with "#" if (I->empty() || I->startswith("#")) continue; // Save section names if (I->startswith("[")) { if (!I->endswith("]")) { Error = (Twine("malformed section header on line ") + Twine(LineNo) + ": " + *I).str(); return false; } Section = I->slice(1, I->size() - 1); std::string REError; Regex CheckRE(Section); if (!CheckRE.isValid(REError)) { Error = (Twine("malformed regex for section ") + Section + ": '" + REError) .str(); return false; } continue; } // Get our prefix and unparsed regexp. std::pair<StringRef, StringRef> SplitLine = I->split(":"); StringRef Prefix = SplitLine.first; if (SplitLine.second.empty()) { // Missing ':' in the line. Error = (Twine("malformed line ") + Twine(LineNo) + ": '" + SplitLine.first + "'").str(); return false; } std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("="); std::string Regexp = SplitRegexp.first; StringRef Category = SplitRegexp.second; // Create this section if it has not been seen before. if (SectionsMap.find(Section) == SectionsMap.end()) { std::unique_ptr<Matcher> M = make_unique<Matcher>(); std::string REError; if (!M->insert(Section, LineNo, REError)) { Error = (Twine("malformed section ") + Section + ": '" + REError).str(); return false; } SectionsMap[Section] = Sections.size(); Sections.emplace_back(std::move(M)); } auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category]; std::string REError; if (!Entry.insert(std::move(Regexp), LineNo, REError)) { Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" + SplitLine.second + "': " + REError).str(); return false; } } return true; }