Index: llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h =================================================================== --- llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h +++ llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h @@ -17,6 +17,7 @@ #define LLVM_TEXTAPI_ELF_TBEHANDLER_H #include "llvm/Support/VersionTuple.h" +#include "llvm/Support/Error.h" #include namespace llvm { @@ -32,14 +33,12 @@ const VersionTuple TBEVersionCurrent(1, 0); -class TBEHandler { -public: - /// Attempts to read an ELF interface file from a StringRef buffer. - std::unique_ptr readFile(StringRef Buf); - - /// Attempts to write an ELF interface file to a raw_ostream. - Error writeFile(raw_ostream &OS, const ELFStub &Stub); -}; +/// Attempts to read an ELF interface file from a StringRef buffer. +Expected> readTBEFromBuffer(StringRef Buf); + +/// Attempts to write an ELF interface file to a raw_ostream. +Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub); + } // end namespace elfabi } // end namespace llvm Index: llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp =================================================================== --- llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp +++ llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp @@ -142,16 +142,17 @@ } // end namespace yaml } // end namespace llvm -std::unique_ptr TBEHandler::readFile(StringRef Buf) { +Expected> elfabi::readTBEFromBuffer(StringRef Buf) { yaml::Input YamlIn(Buf); std::unique_ptr Stub(new ELFStub()); YamlIn >> *Stub; - if (YamlIn.error()) - return nullptr; + if (std::error_code Err = YamlIn.error()) + return createStringError(Err, "YAML failed reading as TBE"); + return Stub; } -Error TBEHandler::writeFile(raw_ostream &OS, const ELFStub &Stub) { +Error elfabi::writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub) { yaml::Output YamlOut(OS, NULL, /*WrapColumn =*/0); YamlOut << const_cast(Stub); Index: llvm/trunk/unittests/TextAPI/CMakeLists.txt =================================================================== --- llvm/trunk/unittests/TextAPI/CMakeLists.txt +++ llvm/trunk/unittests/TextAPI/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_LINK_COMPONENTS + TestingSupport TextAPI ) Index: llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp =================================================================== --- llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp +++ llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp @@ -11,6 +11,7 @@ #include "llvm/TextAPI/ELF/ELFStub.h" #include "llvm/TextAPI/ELF/TBEHandler.h" #include "llvm/Support/Error.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" #include @@ -18,16 +19,6 @@ using namespace llvm::ELF; using namespace llvm::elfabi; -std::unique_ptr readFromBuffer(const char Data[]) { - TBEHandler Handler; - - StringRef Buf(Data); - - std::unique_ptr Stub = Handler.readFile(Buf); - EXPECT_NE(Stub.get(), nullptr); - return Stub; -} - void compareByLine(StringRef LHS, StringRef RHS) { StringRef Line1; StringRef Line2; @@ -52,9 +43,9 @@ "Symbols:\n" " foo: { Type: Func, Undefined: true }\n" "...\n"; - StringRef Buf = StringRef(Data); - TBEHandler Handler; - std::unique_ptr Stub = Handler.readFile(Buf); + Expected> StubOrErr = readTBEFromBuffer(Data); + ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded()); + std::unique_ptr Stub = std::move(StubOrErr.get()); EXPECT_NE(Stub.get(), nullptr); EXPECT_EQ(Stub->Arch, (uint16_t)llvm::ELF::EM_X86_64); EXPECT_STREQ(Stub->SoName.c_str(), "test.so"); @@ -77,7 +68,10 @@ " not: { Type: File, Undefined: true, Size: 111, " "Warning: \'All fields populated!\' }\n" "...\n"; - std::unique_ptr Stub = readFromBuffer(Data); + Expected> StubOrErr = readTBEFromBuffer(Data); + ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded()); + std::unique_ptr Stub = std::move(StubOrErr.get()); + EXPECT_NE(Stub.get(), nullptr); EXPECT_EQ(Stub->Symbols.size(), 5u); auto Iterator = Stub->Symbols.begin(); @@ -126,12 +120,14 @@ "Arch: x86_64\n" "Symbols: {}\n" "...\n"; - std::unique_ptr Stub = readFromBuffer(Data); + Expected> StubOrErr = readTBEFromBuffer(Data); + ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded()); + std::unique_ptr Stub = std::move(StubOrErr.get()); + EXPECT_NE(Stub.get(), nullptr); EXPECT_EQ(0u, Stub->Symbols.size()); } TEST(ElfYamlTextAPI, YAMLUnreadableTBE) { - TBEHandler Handler; // Can't read: wrong format/version. const char Data[] = "--- !tapi-tbz\n" "TbeVersion: z.3\n" @@ -139,9 +135,8 @@ "Arch: x86_64\n" "Symbols:\n" " foo: { Type: Func, Undefined: true }\n"; - StringRef Buf = StringRef(Data); - std::unique_ptr Stub = Handler.readFile(Buf); - EXPECT_EQ(Stub.get(), nullptr); + Expected> StubOrErr = readTBEFromBuffer(Data); + ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed()); } TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) { @@ -185,8 +180,7 @@ std::string Result; raw_string_ostream OS(Result); - TBEHandler Handler; - EXPECT_FALSE(Handler.writeFile(OS, Moved)); + ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Moved), Succeeded()); Result = OS.str(); compareByLine(Result.c_str(), Expected); } @@ -212,8 +206,7 @@ std::string Result; raw_string_ostream OS(Result); - TBEHandler Handler; - EXPECT_FALSE(Handler.writeFile(OS, Stub)); + ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Stub), Succeeded()); Result = OS.str(); compareByLine(Result.c_str(), Expected); }