1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00
llvm-mirror/lib/ObjectYAML/yaml2obj.cpp
George Rimar 676775660f [yaml2obj/ObjectYAML] - Cleanup the error reporting API, add custom errors handlers.
This is a continuation of the YAML library error reporting
refactoring/improvement and the idea by itself was mentioned
in the following thread:
https://reviews.llvm.org/D67182?id=218714#inline-603404

This performs a cleanup of all object emitters in the library.
It allows using the custom one provided by the caller.

One of the nice things is that each tool can now print its tool name,
e.g: "yaml2obj: error: <text>"

Also, the code became a bit simpler.

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

llvm-svn: 371865
2019-09-13 16:00:16 +00:00

78 lines
2.2 KiB
C++

//===-- yaml2obj.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/ObjectYAML/ObjectYAML.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/YAMLTraits.h"
namespace llvm {
namespace yaml {
bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
unsigned DocNum) {
unsigned CurDocNum = 0;
do {
if (++CurDocNum != DocNum)
continue;
yaml::YamlObjectFile Doc;
YIn >> Doc;
if (std::error_code EC = YIn.error()) {
ErrHandler("failed to parse YAML input: " + EC.message());
return false;
}
if (Doc.Elf)
return yaml2elf(*Doc.Elf, Out, ErrHandler);
if (Doc.Coff)
return yaml2coff(*Doc.Coff, Out, ErrHandler);
if (Doc.MachO || Doc.FatMachO)
return yaml2macho(Doc, Out, ErrHandler);
if (Doc.Minidump)
return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
if (Doc.Wasm)
return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
ErrHandler("unknown document type");
return false;
} while (YIn.nextDocument());
ErrHandler("cannot find the " + Twine(DocNum) +
getOrdinalSuffix(DocNum).data() + " document");
return false;
}
std::unique_ptr<object::ObjectFile>
yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml,
ErrorHandler ErrHandler) {
Storage.clear();
raw_svector_ostream OS(Storage);
yaml::Input YIn(Yaml);
if (!convertYAML(YIn, OS, ErrHandler))
return {};
Expected<std::unique_ptr<object::ObjectFile>> ObjOrErr =
object::ObjectFile::createObjectFile(
MemoryBufferRef(OS.str(), "YamlObject"));
if (ObjOrErr)
return std::move(*ObjOrErr);
ErrHandler(toString(ObjOrErr.takeError()));
return {};
}
} // namespace yaml
} // namespace llvm