Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / yaml2obj / yaml2obj.cpp
blobba1f54d2ecf7c1c441471cb0a40bdc4c5b02c601
1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
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 // This program takes a YAML description of an object file and outputs the
10 // binary equivalent.
12 // This is used for writing tests that require binary files.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ObjectYAML/yaml2obj.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ObjectYAML/ObjectYAML.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/InitLLVM.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/ToolOutputFile.h"
24 #include "llvm/Support/WithColor.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <optional>
28 #include <system_error>
30 using namespace llvm;
32 namespace {
33 cl::OptionCategory Cat("yaml2obj Options");
35 cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),
36 cl::init("-"), cl::cat(Cat));
38 cl::list<std::string>
39 D("D", cl::Prefix,
40 cl::desc("Defined the specified macros to their specified "
41 "definition. The syntax is <macro>=<definition>"),
42 cl::cat(Cat));
44 cl::opt<bool> PreprocessOnly("E", cl::desc("Just print the preprocessed file"),
45 cl::cat(Cat));
47 cl::opt<unsigned>
48 DocNum("docnum", cl::init(1),
49 cl::desc("Read specified document from input (default = 1)"),
50 cl::cat(Cat));
52 static cl::opt<uint64_t> MaxSize(
53 "max-size", cl::init(10 * 1024 * 1024),
54 cl::desc(
55 "Sets the maximum allowed output size (0 means no limit) [ELF only]"),
56 cl::cat(Cat));
58 cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
59 cl::value_desc("filename"), cl::init("-"),
60 cl::Prefix, cl::cat(Cat));
61 } // namespace
63 static std::optional<std::string> preprocess(StringRef Buf,
64 yaml::ErrorHandler ErrHandler) {
65 DenseMap<StringRef, StringRef> Defines;
66 for (StringRef Define : D) {
67 StringRef Macro, Definition;
68 std::tie(Macro, Definition) = Define.split('=');
69 if (!Define.count('=') || Macro.empty()) {
70 ErrHandler("invalid syntax for -D: " + Define);
71 return {};
73 if (!Defines.try_emplace(Macro, Definition).second) {
74 ErrHandler("'" + Macro + "'" + " redefined");
75 return {};
79 std::string Preprocessed;
80 while (!Buf.empty()) {
81 if (Buf.startswith("[[")) {
82 size_t I = Buf.find_first_of("[]", 2);
83 if (Buf.substr(I).startswith("]]")) {
84 StringRef MacroExpr = Buf.substr(2, I - 2);
85 StringRef Macro;
86 StringRef Default;
87 std::tie(Macro, Default) = MacroExpr.split('=');
89 // When the -D option is requested, we use the provided value.
90 // Otherwise we use a default macro value if present.
91 auto It = Defines.find(Macro);
92 std::optional<StringRef> Value;
93 if (It != Defines.end())
94 Value = It->second;
95 else if (!Default.empty() || MacroExpr.endswith("="))
96 Value = Default;
98 if (Value) {
99 Preprocessed += *Value;
100 Buf = Buf.substr(I + 2);
101 continue;
106 Preprocessed += Buf[0];
107 Buf = Buf.substr(1);
110 return Preprocessed;
113 int main(int argc, char **argv) {
114 InitLLVM X(argc, argv);
115 cl::HideUnrelatedOptions(Cat);
116 cl::ParseCommandLineOptions(
117 argc, argv, "Create an object file from a YAML description", nullptr,
118 nullptr, /*LongOptionsUseDoubleDash=*/true);
120 auto ErrHandler = [](const Twine &Msg) {
121 WithColor::error(errs(), "yaml2obj") << Msg << "\n";
124 std::error_code EC;
125 std::unique_ptr<ToolOutputFile> Out(
126 new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
127 if (EC) {
128 ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());
129 return 1;
132 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
133 MemoryBuffer::getFileOrSTDIN(Input);
134 if (!Buf)
135 return 1;
137 std::optional<std::string> Buffer =
138 preprocess(Buf.get()->getBuffer(), ErrHandler);
139 if (!Buffer)
140 return 1;
142 if (PreprocessOnly) {
143 Out->os() << Buffer;
144 } else {
145 yaml::Input YIn(*Buffer);
147 if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,
148 MaxSize == 0 ? UINT64_MAX : MaxSize))
149 return 1;
152 Out->keep();
153 Out->os().flush();
154 return 0;