mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 04:02:41 +01:00
[TextAPI][elfabi] Make TBE handlers functions that return Errors
Since TBEHandler doesn't maintain state or otherwise have any need to be a class right now, the read and write functions have been moved out and turned into standalone functions. Additionally, the TBE read function has been updated to return an Expected value for better error handling. Tests have been updated to reflect these changes. Differential Revision: https://reviews.llvm.org/D55450 llvm-svn: 348735
This commit is contained in:
parent
b33546a190
commit
ace43195c3
@ -17,6 +17,7 @@
|
||||
#define LLVM_TEXTAPI_ELF_TBEHANDLER_H
|
||||
|
||||
#include "llvm/Support/VersionTuple.h"
|
||||
#include "llvm/Support/Error.h"
|
||||
#include <memory>
|
||||
|
||||
namespace llvm {
|
||||
@ -32,14 +33,12 @@ class ELFStub;
|
||||
|
||||
const VersionTuple TBEVersionCurrent(1, 0);
|
||||
|
||||
class TBEHandler {
|
||||
public:
|
||||
/// Attempts to read an ELF interface file from a StringRef buffer.
|
||||
std::unique_ptr<ELFStub> readFile(StringRef Buf);
|
||||
/// Attempts to read an ELF interface file from a StringRef buffer.
|
||||
Expected<std::unique_ptr<ELFStub>> readTBEFromBuffer(StringRef Buf);
|
||||
|
||||
/// Attempts to write an ELF interface file to a raw_ostream.
|
||||
Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub);
|
||||
|
||||
/// Attempts to write an ELF interface file to a raw_ostream.
|
||||
Error writeFile(raw_ostream &OS, const ELFStub &Stub);
|
||||
};
|
||||
} // end namespace elfabi
|
||||
} // end namespace llvm
|
||||
|
||||
|
@ -142,16 +142,17 @@ template <> struct MappingTraits<ELFStub> {
|
||||
} // end namespace yaml
|
||||
} // end namespace llvm
|
||||
|
||||
std::unique_ptr<ELFStub> TBEHandler::readFile(StringRef Buf) {
|
||||
Expected<std::unique_ptr<ELFStub>> elfabi::readTBEFromBuffer(StringRef Buf) {
|
||||
yaml::Input YamlIn(Buf);
|
||||
std::unique_ptr<ELFStub> 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<ELFStub &>(Stub);
|
||||
|
@ -1,4 +1,5 @@
|
||||
set(LLVM_LINK_COMPONENTS
|
||||
TestingSupport
|
||||
TextAPI
|
||||
)
|
||||
|
||||
|
@ -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 <string>
|
||||
|
||||
@ -18,16 +19,6 @@ using namespace llvm;
|
||||
using namespace llvm::ELF;
|
||||
using namespace llvm::elfabi;
|
||||
|
||||
std::unique_ptr<ELFStub> readFromBuffer(const char Data[]) {
|
||||
TBEHandler Handler;
|
||||
|
||||
StringRef Buf(Data);
|
||||
|
||||
std::unique_ptr<ELFStub> 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 @@ TEST(ElfYamlTextAPI, YAMLReadableTBE) {
|
||||
"Symbols:\n"
|
||||
" foo: { Type: Func, Undefined: true }\n"
|
||||
"...\n";
|
||||
StringRef Buf = StringRef(Data);
|
||||
TBEHandler Handler;
|
||||
std::unique_ptr<ELFStub> Stub = Handler.readFile(Buf);
|
||||
Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
|
||||
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
||||
std::unique_ptr<ELFStub> 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 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
|
||||
" not: { Type: File, Undefined: true, Size: 111, "
|
||||
"Warning: \'All fields populated!\' }\n"
|
||||
"...\n";
|
||||
std::unique_ptr<ELFStub> Stub = readFromBuffer(Data);
|
||||
Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
|
||||
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
||||
std::unique_ptr<ELFStub> 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 @@ TEST(ElfYamlTextAPI, YAMLReadsNoTBESyms) {
|
||||
"Arch: x86_64\n"
|
||||
"Symbols: {}\n"
|
||||
"...\n";
|
||||
std::unique_ptr<ELFStub> Stub = readFromBuffer(Data);
|
||||
Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
|
||||
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
||||
std::unique_ptr<ELFStub> 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 @@ TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
|
||||
"Arch: x86_64\n"
|
||||
"Symbols:\n"
|
||||
" foo: { Type: Func, Undefined: true }\n";
|
||||
StringRef Buf = StringRef(Data);
|
||||
std::unique_ptr<ELFStub> Stub = Handler.readFile(Buf);
|
||||
EXPECT_EQ(Stub.get(), nullptr);
|
||||
Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
|
||||
ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
|
||||
}
|
||||
|
||||
TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
|
||||
@ -185,8 +180,7 @@ TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
|
||||
|
||||
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 @@ TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user