[clang] Remove redundant integer values in template type diffing
[llvm-project.git] / llvm / tools / llvm-cvtres / llvm-cvtres.cpp
blob0c10769a9488eab298d93a40cfe5d28aacdd7726
1 //===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
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 // Serialize .res files into .obj files. This is intended to be a
10 // platform-independent port of Microsoft's cvtres.exe.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/BinaryFormat/Magic.h"
15 #include "llvm/Object/Binary.h"
16 #include "llvm/Object/WindowsMachineFlag.h"
17 #include "llvm/Object/WindowsResource.h"
18 #include "llvm/Option/Arg.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Option/Option.h"
21 #include "llvm/Support/BinaryStreamError.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Process.h"
27 #include "llvm/Support/ScopedPrinter.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
31 #include <system_error>
33 using namespace llvm;
34 using namespace object;
36 namespace {
38 enum ID {
39 OPT_INVALID = 0, // This is not an option ID.
40 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
41 #include "Opts.inc"
42 #undef OPTION
45 #define PREFIX(NAME, VALUE) \
46 static constexpr StringLiteral NAME##_init[] = VALUE; \
47 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
48 std::size(NAME##_init) - 1);
49 #include "Opts.inc"
50 #undef PREFIX
52 using namespace llvm::opt;
53 static constexpr opt::OptTable::Info InfoTable[] = {
54 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
55 #include "Opts.inc"
56 #undef OPTION
59 class CvtResOptTable : public opt::GenericOptTable {
60 public:
61 CvtResOptTable() : opt::GenericOptTable(InfoTable, true) {}
65 [[noreturn]] static void reportError(Twine Msg) {
66 errs() << Msg;
67 exit(1);
70 static void reportError(StringRef Input, std::error_code EC) {
71 reportError(Twine(Input) + ": " + EC.message() + ".\n");
74 static void error(StringRef Input, Error EC) {
75 if (!EC)
76 return;
77 handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
78 reportError(Twine(Input) + ": " + EI.message() + ".\n");
79 });
82 static void error(Error EC) {
83 if (!EC)
84 return;
85 handleAllErrors(std::move(EC),
86 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
89 static uint32_t getTime() {
90 std::time_t Now = time(nullptr);
91 if (Now < 0 || !isUInt<32>(Now))
92 return UINT32_MAX;
93 return static_cast<uint32_t>(Now);
96 template <typename T> T error(Expected<T> EC) {
97 if (!EC)
98 error(EC.takeError());
99 return std::move(EC.get());
102 template <typename T> T error(StringRef Input, Expected<T> EC) {
103 if (!EC)
104 error(Input, EC.takeError());
105 return std::move(EC.get());
108 template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
109 return error(Input, errorOrToExpected(std::move(EC)));
112 int main(int Argc, const char **Argv) {
113 InitLLVM X(Argc, Argv);
115 CvtResOptTable T;
116 unsigned MAI, MAC;
117 ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, Argc - 1);
118 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
120 if (InputArgs.hasArg(OPT_HELP)) {
121 T.printHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
122 return 0;
125 bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
127 COFF::MachineTypes MachineType;
129 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) {
130 MachineType = getMachineType(Arg->getValue());
131 if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
132 reportError(Twine("Unsupported machine architecture ") + Arg->getValue() +
133 "\n");
135 } else {
136 if (Verbose)
137 outs() << "Machine architecture not specified; assumed X64.\n";
138 MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
141 std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
143 if (InputFiles.size() == 0) {
144 reportError("No input file specified.\n");
147 SmallString<128> OutputFile;
149 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) {
150 OutputFile = Arg->getValue();
151 } else {
152 OutputFile = sys::path::filename(StringRef(InputFiles[0]));
153 sys::path::replace_extension(OutputFile, ".obj");
156 uint32_t DateTimeStamp;
157 if (llvm::opt::Arg *Arg = InputArgs.getLastArg(OPT_TIMESTAMP)) {
158 StringRef Value(Arg->getValue());
159 if (Value.getAsInteger(0, DateTimeStamp))
160 reportError(Twine("invalid timestamp: ") + Value +
161 ". Expected 32-bit integer\n");
162 } else {
163 DateTimeStamp = getTime();
166 if (Verbose)
167 outs() << "Machine: " << machineToStr(MachineType) << '\n';
169 WindowsResourceParser Parser;
171 for (const auto &File : InputFiles) {
172 std::unique_ptr<MemoryBuffer> Buffer = error(
173 File, MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
174 /*RequiresNullTerminator=*/false));
175 file_magic Type = identify_magic(Buffer->getMemBufferRef().getBuffer());
176 if (Type != file_magic::windows_resource)
177 reportError(File + ": unrecognized file format.\n");
178 std::unique_ptr<WindowsResource> Binary = error(
179 File,
180 WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
182 WindowsResource *RF = Binary.get();
184 if (Verbose) {
185 int EntryNumber = 0;
186 ResourceEntryRef Entry = error(RF->getHeadEntry());
187 bool End = false;
188 while (!End) {
189 error(Entry.moveNext(End));
190 EntryNumber++;
192 outs() << "Number of resources: " << EntryNumber << "\n";
195 std::vector<std::string> Duplicates;
196 error(Parser.parse(RF, Duplicates));
197 for (const auto& DupeDiag : Duplicates)
198 reportError(DupeDiag);
201 if (Verbose) {
202 Parser.printTree(outs());
205 std::unique_ptr<MemoryBuffer> OutputBuffer =
206 error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser,
207 DateTimeStamp));
208 auto FileOrErr =
209 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
210 if (!FileOrErr)
211 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
212 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
213 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
214 FileBuffer->getBufferStart());
215 error(FileBuffer->commit());
217 if (Verbose) {
218 std::unique_ptr<MemoryBuffer> Buffer =
219 error(OutputFile,
220 MemoryBuffer::getFileOrSTDIN(OutputFile, /*IsText=*/false,
221 /*RequiresNullTerminator=*/false));
223 ScopedPrinter W(errs());
224 W.printBinaryBlock("Output File Raw Data",
225 Buffer->getMemBufferRef().getBuffer());
228 return 0;