annotate llvm/lib/ObjectYAML/yaml2obj.cpp @ 220:42394fc6a535

Added tag llvm12 for changeset 0572611fdcc8
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:13:43 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===-- yaml2obj.cpp ------------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "llvm/ObjectYAML/yaml2obj.h"
anatofuz
parents:
diff changeset
10 #include "llvm/ADT/StringExtras.h"
anatofuz
parents:
diff changeset
11 #include "llvm/ADT/Twine.h"
anatofuz
parents:
diff changeset
12 #include "llvm/Object/ObjectFile.h"
anatofuz
parents:
diff changeset
13 #include "llvm/ObjectYAML/ObjectYAML.h"
anatofuz
parents:
diff changeset
14 #include "llvm/Support/Errc.h"
anatofuz
parents:
diff changeset
15 #include "llvm/Support/WithColor.h"
anatofuz
parents:
diff changeset
16 #include "llvm/Support/YAMLTraits.h"
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 namespace llvm {
anatofuz
parents:
diff changeset
19 namespace yaml {
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
anatofuz
parents:
diff changeset
22 unsigned DocNum) {
anatofuz
parents:
diff changeset
23 unsigned CurDocNum = 0;
anatofuz
parents:
diff changeset
24 do {
anatofuz
parents:
diff changeset
25 if (++CurDocNum != DocNum)
anatofuz
parents:
diff changeset
26 continue;
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 yaml::YamlObjectFile Doc;
anatofuz
parents:
diff changeset
29 YIn >> Doc;
anatofuz
parents:
diff changeset
30 if (std::error_code EC = YIn.error()) {
anatofuz
parents:
diff changeset
31 ErrHandler("failed to parse YAML input: " + EC.message());
anatofuz
parents:
diff changeset
32 return false;
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 if (Doc.Elf)
anatofuz
parents:
diff changeset
36 return yaml2elf(*Doc.Elf, Out, ErrHandler);
anatofuz
parents:
diff changeset
37 if (Doc.Coff)
anatofuz
parents:
diff changeset
38 return yaml2coff(*Doc.Coff, Out, ErrHandler);
anatofuz
parents:
diff changeset
39 if (Doc.MachO || Doc.FatMachO)
anatofuz
parents:
diff changeset
40 return yaml2macho(Doc, Out, ErrHandler);
anatofuz
parents:
diff changeset
41 if (Doc.Minidump)
anatofuz
parents:
diff changeset
42 return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
anatofuz
parents:
diff changeset
43 if (Doc.Wasm)
anatofuz
parents:
diff changeset
44 return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 ErrHandler("unknown document type");
anatofuz
parents:
diff changeset
47 return false;
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 } while (YIn.nextDocument());
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 ErrHandler("cannot find the " + Twine(DocNum) +
anatofuz
parents:
diff changeset
52 getOrdinalSuffix(DocNum).data() + " document");
anatofuz
parents:
diff changeset
53 return false;
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 std::unique_ptr<object::ObjectFile>
anatofuz
parents:
diff changeset
57 yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml,
anatofuz
parents:
diff changeset
58 ErrorHandler ErrHandler) {
anatofuz
parents:
diff changeset
59 Storage.clear();
anatofuz
parents:
diff changeset
60 raw_svector_ostream OS(Storage);
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 yaml::Input YIn(Yaml);
anatofuz
parents:
diff changeset
63 if (!convertYAML(YIn, OS, ErrHandler))
anatofuz
parents:
diff changeset
64 return {};
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 Expected<std::unique_ptr<object::ObjectFile>> ObjOrErr =
anatofuz
parents:
diff changeset
67 object::ObjectFile::createObjectFile(
anatofuz
parents:
diff changeset
68 MemoryBufferRef(OS.str(), "YamlObject"));
anatofuz
parents:
diff changeset
69 if (ObjOrErr)
anatofuz
parents:
diff changeset
70 return std::move(*ObjOrErr);
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 ErrHandler(toString(ObjOrErr.takeError()));
anatofuz
parents:
diff changeset
73 return {};
anatofuz
parents:
diff changeset
74 }
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 } // namespace yaml
anatofuz
parents:
diff changeset
77 } // namespace llvm