LLVM  13.0.0git
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, uint64_t MaxSize) {
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.Arch)
36  return yaml2archive(*Doc.Arch, Out, ErrHandler);
37  if (Doc.Elf)
38  return yaml2elf(*Doc.Elf, Out, ErrHandler, MaxSize);
39  if (Doc.Coff)
40  return yaml2coff(*Doc.Coff, Out, ErrHandler);
41  if (Doc.MachO || Doc.FatMachO)
42  return yaml2macho(Doc, Out, ErrHandler);
43  if (Doc.Minidump)
44  return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
45  if (Doc.Wasm)
46  return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
47  if (Doc.Xcoff)
48  return yaml2xcoff(*Doc.Xcoff, Out, ErrHandler);
49 
50  ErrHandler("unknown document type");
51  return false;
52 
53  } while (YIn.nextDocument());
54 
55  ErrHandler("cannot find the " + Twine(DocNum) +
56  getOrdinalSuffix(DocNum).data() + " document");
57  return false;
58 }
59 
60 std::unique_ptr<object::ObjectFile>
62  ErrorHandler ErrHandler) {
63  Storage.clear();
64  raw_svector_ostream OS(Storage);
65 
66  yaml::Input YIn(Yaml);
67  if (!convertYAML(YIn, OS, ErrHandler))
68  return {};
69 
72  MemoryBufferRef(OS.str(), "YamlObject"));
73  if (ObjOrErr)
74  return std::move(*ObjOrErr);
75 
76  ErrHandler(toString(ObjOrErr.takeError()));
77  return {};
78 }
79 
80 } // namespace yaml
81 } // namespace llvm
llvm
Definition: AllocatorList.h:23
ObjectYAML.h
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:991
llvm::yaml::yaml2xcoff
bool yaml2xcoff(XCOFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: XCOFFEmitter.cpp:310
llvm::yaml::YamlObjectFile
Definition: ObjectYAML.h:27
Errc.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::yaml::yaml2macho
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: MachOEmitter.cpp:635
llvm::yaml::yaml2archive
bool yaml2archive(ArchYAML::Archive &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: ArchiveEmitter.cpp:20
Twine.h
llvm::yaml::yaml2coff
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: COFFEmitter.cpp:603
llvm::yaml::yaml2elf
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH, uint64_t MaxSize)
Definition: ELFEmitter.cpp:1942
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
StringExtras.h
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:184
ObjectFile.h
YAMLTraits.h
yaml2obj.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::raw_svector_ostream::str
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:672
llvm::yaml::yaml2wasm
bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: WasmEmitter.cpp:682
WithColor.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::yaml::yaml2minidump
bool yaml2minidump(MinidumpYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
Definition: MinidumpEmitter.cpp:229
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:584
llvm::yaml::yaml2ObjectFile
std::unique_ptr< object::ObjectFile > yaml2ObjectFile(SmallVectorImpl< char > &Storage, StringRef Yaml, ErrorHandler ErrHandler)
Convenience function for tests.
Definition: yaml2obj.cpp:61
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
llvm::yaml::convertYAML
bool convertYAML(Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler, unsigned DocNum=1, uint64_t MaxSize=UINT64_MAX)
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
llvm::SmallVectorImpl< char >