LLVM 19.0.0git
Go to the documentation of this file.
1//===-- yaml2obj.cpp ------------------------------------------------------===//
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
11#include "llvm/ADT/Twine.h"
14#include "llvm/Support/Errc.h"
18namespace llvm {
19namespace yaml {
21bool 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;
29 YIn >> Doc;
30 if (std::error_code EC = YIn.error()) {
31 ErrHandler("failed to parse YAML input: " + EC.message());
32 return false;
33 }
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.Goff)
42 return yaml2goff(*Doc.Goff, Out, ErrHandler);
43 if (Doc.MachO || Doc.FatMachO)
44 return yaml2macho(Doc, Out, ErrHandler);
45 if (Doc.Minidump)
46 return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
47 if (Doc.Offload)
48 return yaml2offload(*Doc.Offload, Out, ErrHandler);
49 if (Doc.Wasm)
50 return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
51 if (Doc.Xcoff)
52 return yaml2xcoff(*Doc.Xcoff, Out, ErrHandler);
53 if (Doc.DXContainer)
54 return yaml2dxcontainer(*Doc.DXContainer, Out, ErrHandler);
56 ErrHandler("unknown document type");
57 return false;
59 } while (YIn.nextDocument());
61 ErrHandler("cannot find the " + Twine(DocNum) +
62 getOrdinalSuffix(DocNum).data() + " document");
63 return false;
68 ErrorHandler ErrHandler) {
69 Storage.clear();
70 raw_svector_ostream OS(Storage);
72 yaml::Input YIn(Yaml);
73 if (!convertYAML(YIn, OS, ErrHandler))
74 return {};
78 MemoryBufferRef(OS.str(), "YamlObject"));
79 if (ObjOrErr)
80 return std::move(*ObjOrErr);
82 ErrHandler(toString(ObjOrErr.takeError()));
83 return {};
86} // namespace yaml
87} // namespace llvm
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
An efficient, type-erasing, non-owning reference to a callable.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:209
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
bool yaml2dxcontainer(DXContainerYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH, uint64_t MaxSize)
bool yaml2goff(GOFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool convertYAML(Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler, unsigned DocNum=1, uint64_t MaxSize=UINT64_MAX)
bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2offload(OffloadYAML::Binary &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:67
bool yaml2xcoff(XCOFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2archive(ArchYAML::Archive &Doc, raw_ostream &Out, ErrorHandler EH)
bool yaml2minidump(MinidumpYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MachOYAML::UniversalBinary > FatMachO
Definition: ObjectYAML.h:36
std::unique_ptr< XCOFFYAML::Object > Xcoff
Definition: ObjectYAML.h:40
std::unique_ptr< OffloadYAML::Binary > Offload
Definition: ObjectYAML.h:38
std::unique_ptr< MachOYAML::Object > MachO
Definition: ObjectYAML.h:35
std::unique_ptr< ArchYAML::Archive > Arch
Definition: ObjectYAML.h:31
std::unique_ptr< COFFYAML::Object > Coff
Definition: ObjectYAML.h:33
std::unique_ptr< ELFYAML::Object > Elf
Definition: ObjectYAML.h:32
std::unique_ptr< GOFFYAML::Object > Goff
Definition: ObjectYAML.h:34
std::unique_ptr< WasmYAML::Object > Wasm
Definition: ObjectYAML.h:39
std::unique_ptr< DXContainerYAML::Object > DXContainer
Definition: ObjectYAML.h:41
std::unique_ptr< MinidumpYAML::Object > Minidump
Definition: ObjectYAML.h:37
Common declarations for yaml2obj.