LLVM  10.0.0svn
yaml2obj.cpp
Go to the documentation of this file.
1 //===-- yaml2obj.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 
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/WithColor.h"
17 
18 namespace llvm {
19 namespace yaml {
20 
21 bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
22  unsigned DocNum) {
23  unsigned CurDocNum = 0;
24  do {
25  if (++CurDocNum != DocNum)
26  continue;
27 
29  YIn >> Doc;
30  if (std::error_code EC = YIn.error()) {
31  ErrHandler("failed to parse YAML input: " + EC.message());
32  return false;
33  }
34 
35  if (Doc.Elf)
36  return yaml2elf(*Doc.Elf, Out, ErrHandler);
37  if (Doc.Coff)
38  return yaml2coff(*Doc.Coff, Out, ErrHandler);
39  if (Doc.MachO || Doc.FatMachO)
40  return yaml2macho(Doc, Out, ErrHandler);
41  if (Doc.Minidump)
42  return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
43  if (Doc.Wasm)
44  return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
45 
46  ErrHandler("unknown document type");
47  return false;
48 
49  } while (YIn.nextDocument());
50 
51  ErrHandler("cannot find the " + Twine(DocNum) +
52  getOrdinalSuffix(DocNum).data() + " document");
53  return false;
54 }
55 
56 std::unique_ptr<object::ObjectFile>
58  ErrorHandler ErrHandler) {
59  Storage.clear();
60  raw_svector_ostream OS(Storage);
61 
62  yaml::Input YIn(Yaml);
63  if (!convertYAML(YIn, OS, ErrHandler))
64  return {};
65 
68  MemoryBufferRef(OS.str(), "YamlObject"));
69  if (ObjOrErr)
70  return std::move(*ObjOrErr);
71 
72  ErrHandler(toString(ObjOrErr.takeError()));
73  return {};
74 }
75 
76 } // namespace yaml
77 } // namespace llvm
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:167
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:104
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
Common declarations for yaml2obj.
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:555
bool yaml2minidump(MinidumpYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool convertYAML(Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler, unsigned DocNum=1)
StringRef getOrdinalSuffix(unsigned Val)
Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
Definition: StringExtras.h:266
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
std::unique_ptr< object::ObjectFile > yaml2ObjectFile(SmallVectorImpl< char > &Storage, StringRef Yaml, ErrorHandler ErrHandler)
Convenience function for tests.
Definition: yaml2obj.cpp:57