unique_ptrify the result of SpecialCaseList::create

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216925 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie 2014-09-02 18:13:54 +00:00
parent fb9a3fb12d
commit 7b96c4919a
4 changed files with 32 additions and 28 deletions

View File

@ -60,13 +60,15 @@ class SpecialCaseList {
/// Parses the special case list from a file. If Path is empty, returns
/// an empty special case list. On failure, returns 0 and writes an error
/// message to string.
static SpecialCaseList *create(StringRef Path, std::string &Error);
static std::unique_ptr<SpecialCaseList> create(StringRef Path,
std::string &Error);
/// Parses the special case list from a memory buffer. On failure, returns
/// 0 and writes an error message to string.
static SpecialCaseList *create(const MemoryBuffer *MB, std::string &Error);
static std::unique_ptr<SpecialCaseList> create(const MemoryBuffer *MB,
std::string &Error);
/// Parses the special case list from a file. On failure, reports a fatal
/// error.
static SpecialCaseList *createOrDie(StringRef Path);
static std::unique_ptr<SpecialCaseList> createOrDie(StringRef Path);
~SpecialCaseList();

View File

@ -48,9 +48,10 @@ struct SpecialCaseList::Entry {
SpecialCaseList::SpecialCaseList() : Entries() {}
SpecialCaseList *SpecialCaseList::create(StringRef Path, std::string &Error) {
std::unique_ptr<SpecialCaseList> SpecialCaseList::create(StringRef Path,
std::string &Error) {
if (Path.empty())
return new SpecialCaseList();
return std::unique_ptr<SpecialCaseList>(new SpecialCaseList());
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFile(Path);
if (std::error_code EC = FileOrErr.getError()) {
@ -60,17 +61,17 @@ SpecialCaseList *SpecialCaseList::create(StringRef Path, std::string &Error) {
return create(FileOrErr.get().get(), Error);
}
SpecialCaseList *SpecialCaseList::create(
const MemoryBuffer *MB, std::string &Error) {
std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
std::string &Error) {
std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
if (!SCL->parse(MB, Error))
return nullptr;
return SCL.release();
return SCL;
}
SpecialCaseList *SpecialCaseList::createOrDie(StringRef Path) {
std::unique_ptr<SpecialCaseList> SpecialCaseList::createOrDie(StringRef Path) {
std::string Error;
if (SpecialCaseList *SCL = create(Path, Error))
if (auto SCL = create(Path, Error))
return SCL;
report_fatal_error(Error);
}

View File

@ -139,7 +139,7 @@ class DFSanABIList {
std::unique_ptr<SpecialCaseList> SCL;
public:
DFSanABIList(SpecialCaseList *SCL) : SCL(SCL) {}
DFSanABIList(std::unique_ptr<SpecialCaseList> SCL) : SCL(std::move(SCL)) {}
/// Returns whether either this function or its source file are listed in the
/// given category.

View File

@ -17,14 +17,15 @@ namespace {
class SpecialCaseListTest : public ::testing::Test {
protected:
SpecialCaseList *makeSpecialCaseList(StringRef List, std::string &Error) {
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
std::string &Error) {
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
return SpecialCaseList::create(MB.get(), Error);
}
SpecialCaseList *makeSpecialCaseList(StringRef List) {
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
std::string Error;
SpecialCaseList *SCL = makeSpecialCaseList(List, Error);
auto SCL = makeSpecialCaseList(List, Error);
assert(SCL);
assert(Error == "");
return SCL;
@ -32,13 +33,13 @@ protected:
};
TEST_F(SpecialCaseListTest, Basic) {
std::unique_ptr<SpecialCaseList> SCL(
std::unique_ptr<SpecialCaseList> SCL =
makeSpecialCaseList("# This is a comment.\n"
"\n"
"src:hello\n"
"src:bye\n"
"src:hi=category\n"
"src:z*=category\n"));
"src:z*=category\n");
EXPECT_TRUE(SCL->inSection("src", "hello"));
EXPECT_TRUE(SCL->inSection("src", "bye"));
EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
@ -49,38 +50,38 @@ TEST_F(SpecialCaseListTest, Basic) {
}
TEST_F(SpecialCaseListTest, GlobalInitCompat) {
std::unique_ptr<SpecialCaseList> SCL(
makeSpecialCaseList("global:foo=init\n"));
std::unique_ptr<SpecialCaseList> SCL =
makeSpecialCaseList("global:foo=init\n");
EXPECT_FALSE(SCL->inSection("global", "foo"));
EXPECT_FALSE(SCL->inSection("global", "bar"));
EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
SCL.reset(makeSpecialCaseList("global-init:foo\n"));
SCL = makeSpecialCaseList("global-init:foo\n");
EXPECT_FALSE(SCL->inSection("global", "foo"));
EXPECT_FALSE(SCL->inSection("global", "bar"));
EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
SCL.reset(makeSpecialCaseList("type:t2=init\n"));
SCL = makeSpecialCaseList("type:t2=init\n");
EXPECT_FALSE(SCL->inSection("type", "t1"));
EXPECT_FALSE(SCL->inSection("type", "t2"));
EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
SCL.reset(makeSpecialCaseList("global-init-type:t2\n"));
SCL = makeSpecialCaseList("global-init-type:t2\n");
EXPECT_FALSE(SCL->inSection("type", "t1"));
EXPECT_FALSE(SCL->inSection("type", "t2"));
EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
SCL.reset(makeSpecialCaseList("src:hello=init\n"));
SCL = makeSpecialCaseList("src:hello=init\n");
EXPECT_FALSE(SCL->inSection("src", "hello"));
EXPECT_FALSE(SCL->inSection("src", "bye"));
EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
SCL.reset(makeSpecialCaseList("global-init-src:hello\n"));
SCL = makeSpecialCaseList("global-init-src:hello\n");
EXPECT_FALSE(SCL->inSection("src", "hello"));
EXPECT_FALSE(SCL->inSection("src", "bye"));
EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
@ -88,14 +89,14 @@ TEST_F(SpecialCaseListTest, GlobalInitCompat) {
}
TEST_F(SpecialCaseListTest, Substring) {
std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList("src:hello\n"
"fun:foo\n"
"global:bar\n"));
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
"fun:foo\n"
"global:bar\n");
EXPECT_FALSE(SCL->inSection("src", "othello"));
EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
EXPECT_FALSE(SCL->inSection("global", "bartender"));
SCL.reset(makeSpecialCaseList("fun:*foo*\n"));
SCL = makeSpecialCaseList("fun:*foo*\n");
EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
EXPECT_TRUE(SCL->inSection("fun", "foobar"));
}
@ -117,7 +118,7 @@ TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
}
TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList(""));
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
EXPECT_FALSE(SCL->inSection("foo", "bar"));
}