[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
diff --git a/llvm/lib/ObjectYAML/COFFEmitter.cpp b/llvm/lib/ObjectYAML/COFFEmitter.cpp
index daa4f6d..efcdc51 100644
--- a/llvm/lib/ObjectYAML/COFFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/COFFEmitter.cpp
@@ -34,8 +34,8 @@
/// This parses a yaml stream that represents a COFF object file.
/// See docs/yaml2obj for the yaml scheema.
struct COFFParser {
- COFFParser(COFFYAML::Object &Obj)
- : Obj(Obj), SectionTableStart(0), SectionTableSize(0) {
+ COFFParser(COFFYAML::Object &Obj, yaml::ErrorHandler EH)
+ : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
// A COFF string table always starts with a 4 byte size field. Offsets into
// it include this size, so allocate it now.
StringTable.append(4, char(0));
@@ -81,7 +81,7 @@
unsigned Index = getStringIndex(Name);
std::string str = utostr(Index);
if (str.size() > 7) {
- errs() << "String table got too large\n";
+ ErrHandler("string table got too large");
return false;
}
Sec.Header.Name[0] = '/';
@@ -90,11 +90,11 @@
if (Sec.Alignment) {
if (Sec.Alignment > 8192) {
- errs() << "Section alignment is too large\n";
+ ErrHandler("section alignment is too large");
return false;
}
if (!isPowerOf2_32(Sec.Alignment)) {
- errs() << "Section alignment is not a power of 2\n";
+ ErrHandler("section alignment is not a power of 2");
return false;
}
Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
@@ -155,6 +155,8 @@
std::string StringTable;
uint32_t SectionTableStart;
uint32_t SectionTableSize;
+
+ yaml::ErrorHandler ErrHandler;
};
enum { DOSStubSize = 128 };
@@ -592,24 +594,25 @@
namespace llvm {
namespace yaml {
-bool yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
- COFFParser CP(Doc);
+bool yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out,
+ ErrorHandler ErrHandler) {
+ COFFParser CP(Doc, ErrHandler);
if (!CP.parse()) {
- errs() << "yaml2obj: Failed to parse YAML file!\n";
+ ErrHandler("failed to parse YAML file");
return false;
}
if (!layoutOptionalHeader(CP)) {
- errs() << "yaml2obj: Failed to layout optional header for COFF file!\n";
+ ErrHandler("failed to layout optional header for COFF file");
return false;
}
if (!layoutCOFF(CP)) {
- errs() << "yaml2obj: Failed to layout COFF file!\n";
+ ErrHandler("failed to layout COFF file");
return false;
}
if (!writeCOFF(CP, Out)) {
- errs() << "yaml2obj: Failed to write COFF file!\n";
+ ErrHandler("failed to write COFF file");
return false;
}
return true;