summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ObjectYAML
diff options
context:
space:
mode:
authorGeorge Rimar <grimar@accesssoftek.com>2019-09-13 16:00:16 +0000
committerGeorge Rimar <grimar@accesssoftek.com>2019-09-13 16:00:16 +0000
commit850110272783e85bc79d9fad64add5d303a23ad1 (patch)
treeaa900898c68a57f1ccb06d2aeb297b10373e6622 /llvm/unittests/ObjectYAML
parent7b81a13bfcd1d92f478109f1c87971dafba639d2 (diff)
downloadbcm5719-llvm-850110272783e85bc79d9fad64add5d303a23ad1.tar.gz
bcm5719-llvm-850110272783e85bc79d9fad64add5d303a23ad1.zip
[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
Diffstat (limited to 'llvm/unittests/ObjectYAML')
-rw-r--r--llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp5
-rw-r--r--llvm/unittests/ObjectYAML/YAML2ObjTest.cpp57
2 files changed, 54 insertions, 8 deletions
diff --git a/llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp b/llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp
index 6df9536e9d3..576d8a09dfd 100644
--- a/llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp
+++ b/llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp
@@ -20,8 +20,9 @@ toBinary(SmallVectorImpl<char> &Storage, StringRef Yaml) {
Storage.clear();
raw_svector_ostream OS(Storage);
yaml::Input YIn(Yaml);
- if (Error E = yaml::convertYAML(YIn, OS))
- return std::move(E);
+ if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
+ return createStringError(std::errc::invalid_argument,
+ "unable to convert YAML");
return object::MinidumpFile::create(MemoryBufferRef(OS.str(), "Binary"));
}
diff --git a/llvm/unittests/ObjectYAML/YAML2ObjTest.cpp b/llvm/unittests/ObjectYAML/YAML2ObjTest.cpp
index 0a383881687..8fafca4f908 100644
--- a/llvm/unittests/ObjectYAML/YAML2ObjTest.cpp
+++ b/llvm/unittests/ObjectYAML/YAML2ObjTest.cpp
@@ -10,6 +10,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Error.h"
+#include "llvm/Support/YAMLTraits.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
@@ -18,19 +19,63 @@ using namespace object;
using namespace yaml;
TEST(yaml2ObjectFile, ELF) {
+ bool ErrorReported = false;
+ auto ErrHandler = [&](const Twine &Msg) { ErrorReported = true; };
+
+ SmallString<0> Storage;
+ std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, R"(
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64)", ErrHandler);
+
+ ASSERT_FALSE(ErrorReported);
+ ASSERT_TRUE(Obj);
+ ASSERT_TRUE(Obj->isELF());
+ ASSERT_TRUE(Obj->isRelocatableObject());
+}
+
+TEST(yaml2ObjectFile, Errors) {
+ std::vector<std::string> Errors;
+ auto ErrHandler = [&](const Twine &Msg) {
+ Errors.push_back("ObjectYAML: " + Msg.str());
+ };
+
SmallString<0> Storage;
- Expected<std::unique_ptr<ObjectFile>> ErrOrObj = yaml2ObjectFile(Storage, R"(
+ StringRef Yaml = R"(
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
- Machine: EM_X86_64)");
+ Machine: EM_X86_64
+Symbols:
+ - Name: foo
+ - Name: foo
+ - Name: foo
+)";
+
+ // 1. Test yaml2ObjectFile().
+
+ std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, Yaml, ErrHandler);
+
+ ASSERT_FALSE(Obj);
+ ASSERT_TRUE(Errors.size() == 2);
+ ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
+ ASSERT_TRUE(Errors[1] == Errors[0]);
- ASSERT_THAT_EXPECTED(ErrOrObj, Succeeded());
+ // 2. Test convertYAML().
- std::unique_ptr<ObjectFile> ObjFile = std::move(ErrOrObj.get());
+ Errors.clear();
+ Storage.clear();
+ raw_svector_ostream OS(Storage);
- ASSERT_TRUE(ObjFile->isELF());
- ASSERT_TRUE(ObjFile->isRelocatableObject());
+ yaml::Input YIn(Yaml);
+ bool Res = convertYAML(YIn, OS, ErrHandler);
+ ASSERT_FALSE(Res);
+ ASSERT_TRUE(Errors.size() == 2);
+ ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
+ ASSERT_TRUE(Errors[1] == Errors[0]);
}
OpenPOWER on IntegriCloud