1 //===- YAML2ObjTest.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ObjectYAML/yaml2obj.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Object/ObjectFile.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/YAMLTraits.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18 using namespace object;
19 using namespace yaml;
20
TEST(yaml2ObjectFile,ELF)21 TEST(yaml2ObjectFile, ELF) {
22 bool ErrorReported = false;
23 auto ErrHandler = [&](const Twine &Msg) { ErrorReported = true; };
24
25 SmallString<0> Storage;
26 std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, R"(
27 --- !ELF
28 FileHeader:
29 Class: ELFCLASS64
30 Data: ELFDATA2LSB
31 Type: ET_REL
32 Machine: EM_X86_64)", ErrHandler);
33
34 ASSERT_FALSE(ErrorReported);
35 ASSERT_TRUE(Obj);
36 ASSERT_TRUE(Obj->isELF());
37 ASSERT_TRUE(Obj->isRelocatableObject());
38 }
39
TEST(yaml2ObjectFile,Errors)40 TEST(yaml2ObjectFile, Errors) {
41 std::vector<std::string> Errors;
42 auto ErrHandler = [&](const Twine &Msg) {
43 Errors.push_back("ObjectYAML: " + Msg.str());
44 };
45
46 SmallString<0> Storage;
47 StringRef Yaml = R"(
48 --- !ELF
49 FileHeader:
50 Class: ELFCLASS64
51 Data: ELFDATA2LSB
52 Type: ET_REL
53 Machine: EM_X86_64
54 Symbols:
55 - Name: foo
56 - Name: foo
57 - Name: foo
58 )";
59
60 // 1. Test yaml2ObjectFile().
61
62 std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, Yaml, ErrHandler);
63
64 ASSERT_FALSE(Obj);
65 ASSERT_TRUE(Errors.size() == 2);
66 ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
67 ASSERT_TRUE(Errors[1] == Errors[0]);
68
69 // 2. Test convertYAML().
70
71 Errors.clear();
72 Storage.clear();
73 raw_svector_ostream OS(Storage);
74
75 yaml::Input YIn(Yaml);
76 bool Res = convertYAML(YIn, OS, ErrHandler);
77 ASSERT_FALSE(Res);
78 ASSERT_TRUE(Errors.size() == 2);
79 ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
80 ASSERT_TRUE(Errors[1] == Errors[0]);
81 }
82