[lib/ObjectYAML] - Change interface to return `bool` instead of `int`. NFCI

It was suggested in comments for D67445 to split this part.

Differential revision: https://reviews.llvm.org/D67488

llvm-svn: 371828
This commit is contained in:
George Rimar 2019-09-13 09:12:38 +00:00
parent 1572b68509
commit 7da559f2f6
7 changed files with 34 additions and 36 deletions

View File

@ -44,11 +44,11 @@ namespace yaml {
class Input;
struct YamlObjectFile;
int yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out);
int yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out);
int yaml2macho(YamlObjectFile &Doc, raw_ostream &Out);
int yaml2minidump(MinidumpYAML::Object &Doc, raw_ostream &Out);
int yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out);
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out);
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out);
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out);
bool yaml2minidump(MinidumpYAML::Object &Doc, raw_ostream &Out);
bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out);
Error convertYAML(Input &YIn, raw_ostream &Out, unsigned DocNum = 1);

View File

@ -592,27 +592,27 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
namespace llvm {
namespace yaml {
int yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
bool yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
COFFParser CP(Doc);
if (!CP.parse()) {
errs() << "yaml2obj: Failed to parse YAML file!\n";
return 1;
return false;
}
if (!layoutOptionalHeader(CP)) {
errs() << "yaml2obj: Failed to layout optional header for COFF file!\n";
return 1;
return false;
}
if (!layoutCOFF(CP)) {
errs() << "yaml2obj: Failed to layout COFF file!\n";
return 1;
return false;
}
if (!writeCOFF(CP, Out)) {
errs() << "yaml2obj: Failed to write COFF file!\n";
return 1;
return false;
}
return 0;
return true;
}
} // namespace yaml

View File

@ -168,7 +168,7 @@ template <class ELFT> class ELFState {
ContiguousBlobAccumulator &CBA);
ELFState(ELFYAML::Object &D);
public:
static int writeELF(raw_ostream &OS, ELFYAML::Object &Doc);
static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc);
};
} // end anonymous namespace
@ -983,7 +983,7 @@ template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
}
template <class ELFT>
int ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
ELFState<ELFT> State(Doc);
// Finalize .strtab and .dynstr sections. We do that early because want to
@ -1010,19 +1010,19 @@ int ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
State.setProgramHeaderLayout(PHeaders, SHeaders);
if (State.HasError)
return 1;
return false;
State.writeELFHeader(CBA, OS);
writeArrayData(OS, makeArrayRef(PHeaders));
CBA.writeBlobToStream(OS);
writeArrayData(OS, makeArrayRef(SHeaders));
return 0;
return true;
}
namespace llvm {
namespace yaml {
int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
if (Is64Bit) {

View File

@ -596,13 +596,13 @@ void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
namespace llvm {
namespace yaml {
int yaml2macho(YamlObjectFile &Doc, raw_ostream &Out) {
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out) {
UniversalWriter Writer(Doc);
if (auto Err = Writer.writeMachO(Out)) {
errs() << toString(std::move(Err));
return 1;
return false;
}
return 0;
return true;
}
} // namespace yaml

View File

@ -198,7 +198,7 @@ static Directory layout(BlobAllocator &File, Stream &S) {
namespace llvm {
namespace yaml {
int yaml2minidump(MinidumpYAML::Object &Obj, raw_ostream &Out) {
bool yaml2minidump(MinidumpYAML::Object &Obj, raw_ostream &Out) {
BlobAllocator File;
File.allocateObject(Obj.Header);
@ -211,7 +211,7 @@ int yaml2minidump(MinidumpYAML::Object &Obj, raw_ostream &Out) {
StreamDirectory[Stream.index()] = layout(File, *Stream.value());
File.writeTo(Out);
return 0;
return true;
}
} // namespace yaml

View File

@ -26,7 +26,7 @@ namespace {
class WasmWriter {
public:
WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {}
int writeWasm(raw_ostream &OS);
bool writeWasm(raw_ostream &OS);
private:
int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
@ -563,7 +563,7 @@ int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
return 0;
}
int WasmWriter::writeWasm(raw_ostream &OS) {
bool WasmWriter::writeWasm(raw_ostream &OS) {
// Write headers
OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
writeUint32(OS, Obj.Header.Version);
@ -576,7 +576,7 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
SecName = S->Name;
if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
errs() << "Out of order section type: " << Sec->Type << "\n";
return 1;
return false;
}
encodeULEB128(Sec->Type, OS);
std::string OutString;
@ -625,7 +625,7 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
return Err;
} else {
errs() << "Unknown section type: " << Sec->Type << "\n";
return 1;
return false;
}
StringStream.flush();
@ -652,15 +652,14 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
OS << OutString;
}
return 0;
return true;
}
namespace llvm {
namespace yaml {
int yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out) {
bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out) {
WasmWriter Writer(Doc);
return Writer.writeWasm(Out);
}

View File

@ -17,9 +17,8 @@ namespace llvm {
namespace yaml {
Error convertYAML(yaml::Input &YIn, raw_ostream &Out, unsigned DocNum) {
// TODO: make yaml2* functions return Error instead of int.
auto IntToErr = [](int Ret) -> Error {
if (Ret)
auto BoolToErr = [](bool Ret) -> Error {
if (!Ret)
return createStringError(errc::invalid_argument, "yaml2obj failed");
return Error::success();
};
@ -32,15 +31,15 @@ Error convertYAML(yaml::Input &YIn, raw_ostream &Out, unsigned DocNum) {
if (std::error_code EC = YIn.error())
return createStringError(EC, "Failed to parse YAML input!");
if (Doc.Elf)
return IntToErr(yaml2elf(*Doc.Elf, Out));
return BoolToErr(yaml2elf(*Doc.Elf, Out));
if (Doc.Coff)
return IntToErr(yaml2coff(*Doc.Coff, Out));
return BoolToErr(yaml2coff(*Doc.Coff, Out));
if (Doc.MachO || Doc.FatMachO)
return IntToErr(yaml2macho(Doc, Out));
return BoolToErr(yaml2macho(Doc, Out));
if (Doc.Minidump)
return IntToErr(yaml2minidump(*Doc.Minidump, Out));
return BoolToErr(yaml2minidump(*Doc.Minidump, Out));
if (Doc.Wasm)
return IntToErr(yaml2wasm(*Doc.Wasm, Out));
return BoolToErr(yaml2wasm(*Doc.Wasm, Out));
return createStringError(errc::invalid_argument,
"Unknown document type!");
}