summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2013-08-12 07:49:36 +0000
committerAlexey Samsonov <samsonov@google.com>2013-08-12 07:49:36 +0000
commit9e4fdd26566072ff99cbcb02934848406bdbeb58 (patch)
tree93405cbd260d8986508152610814e007da16b25c /llvm/lib/Transforms/Utils/SpecialCaseList.cpp
parentfeb34713d531d1f48c37116e8cab84814202d057 (diff)
downloadbcm5719-llvm-9e4fdd26566072ff99cbcb02934848406bdbeb58.tar.gz
bcm5719-llvm-9e4fdd26566072ff99cbcb02934848406bdbeb58.zip
Introduce factory methods for SpecialCaseList
Summary: Doing work in constructors is bad: this change suggests to call SpecialCaseList::create(Path, Error) instead of "new SpecialCaseList(Path)". Currently the latter may crash with report_fatal_error, which is undesirable - sometimes we want to report the error to user gracefully - for example, if he provides an incorrect file as an argument of Clang's -fsanitize-blacklist flag. Reviewers: pcc Reviewed By: pcc CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1327 llvm-svn: 188156
Diffstat (limited to 'llvm/lib/Transforms/Utils/SpecialCaseList.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/SpecialCaseList.cpp53
1 files changed, 43 insertions, 10 deletions
diff --git a/llvm/lib/Transforms/Utils/SpecialCaseList.cpp b/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
index b98cb5b6894..5a3b192bf6a 100644
--- a/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
+++ b/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
@@ -49,29 +49,58 @@ struct SpecialCaseList::Entry {
}
};
+SpecialCaseList::SpecialCaseList() : Entries() {}
+
SpecialCaseList::SpecialCaseList(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 + ": " +
+ report_fatal_error("Can't open file '" + Path + "': " +
EC.message());
}
- init(File.get());
+ std::string Error;
+ if (!parse(File.get(), Error))
+ report_fatal_error(Error);
}
SpecialCaseList::SpecialCaseList(const MemoryBuffer *MB) {
- init(MB);
+ std::string Error;
+ if (!parse(MB, Error))
+ report_fatal_error(Error);
}
-void SpecialCaseList::init(const MemoryBuffer *MB) {
+SpecialCaseList *SpecialCaseList::create(
+ const StringRef Path, std::string &Error) {
+ if (Path.empty())
+ return new SpecialCaseList();
+ OwningPtr<MemoryBuffer> File;
+ if (error_code EC = MemoryBuffer::getFile(Path, File)) {
+ Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
+ return 0;
+ }
+ return create(File.get(), Error);
+}
+
+SpecialCaseList *SpecialCaseList::create(
+ const MemoryBuffer *MB, std::string &Error) {
+ OwningPtr<SpecialCaseList> SCL(new SpecialCaseList());
+ if (!SCL->parse(MB, Error))
+ return 0;
+ return SCL.take();
+}
+
+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) {
+ I != E; ++I, ++LineNo) {
// Ignore empty lines and lines starting with "#"
if (I->empty() || I->startswith("#"))
continue;
@@ -80,7 +109,9 @@ void SpecialCaseList::init(const MemoryBuffer *MB) {
StringRef Prefix = SplitLine.first;
if (SplitLine.second.empty()) {
// Missing ':' in the line.
- report_fatal_error("malformed blacklist line: " + SplitLine.first);
+ Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
+ SplitLine.first + "'").str();
+ return false;
}
std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
@@ -113,10 +144,11 @@ void SpecialCaseList::init(const MemoryBuffer *MB) {
// 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);
+ 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.
@@ -135,6 +167,7 @@ void SpecialCaseList::init(const MemoryBuffer *MB) {
Entries[I->getKey()][II->getKey()].RegEx = new Regex(II->getValue());
}
}
+ return true;
}
SpecialCaseList::~SpecialCaseList() {
OpenPOWER on IntegriCloud