forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineParser.h
309 lines (266 loc) · 10.4 KB
/
CommandLineParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Validates and parses command-line options into an internal representation.
*/
#pragma once
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/FileReader.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libyul/YulStack.h>
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/EVMVersion.h>
#include <libsolutil/JSON.h>
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <vector>
namespace solidity::frontend
{
enum class InputMode
{
Help,
License,
Version,
Compiler,
CompilerWithASTImport,
StandardJson,
Linker,
Assembler,
LanguageServer,
EVMAssemblerJSON
};
struct CompilerOutputs
{
bool operator!=(CompilerOutputs const& _other) const noexcept { return !(*this == _other); }
bool operator==(CompilerOutputs const& _other) const noexcept;
friend std::ostream& operator<<(std::ostream& _out, CompilerOutputs const& _requests);
static std::string const& componentName(bool CompilerOutputs::* _component);
static auto const& componentMap()
{
static std::map<std::string, bool CompilerOutputs::*> const components = {
{"ast-compact-json", &CompilerOutputs::astCompactJson},
{"asm", &CompilerOutputs::asm_},
{"asm-json", &CompilerOutputs::asmJson},
{"opcodes", &CompilerOutputs::opcodes},
{"bin", &CompilerOutputs::binary},
{"bin-runtime", &CompilerOutputs::binaryRuntime},
{"abi", &CompilerOutputs::abi},
{"ir", &CompilerOutputs::ir},
{"ir-ast-json", &CompilerOutputs::irAstJson},
{"ir-optimized", &CompilerOutputs::irOptimized},
{"ir-optimized-ast-json", &CompilerOutputs::irOptimizedAstJson},
{"hashes", &CompilerOutputs::signatureHashes},
{"userdoc", &CompilerOutputs::natspecUser},
{"devdoc", &CompilerOutputs::natspecDev},
{"metadata", &CompilerOutputs::metadata},
{"storage-layout", &CompilerOutputs::storageLayout},
{"transient-storage-layout", &CompilerOutputs::transientStorageLayout},
{"yul-cfg-json", &CompilerOutputs::yulCFGJson},
{"ethdebug", &CompilerOutputs::ethdebug},
{"ethdebug-runtime", &CompilerOutputs::ethdebugRuntime},
};
return components;
}
bool astCompactJson = false;
bool asm_ = false;
bool asmJson = false;
bool opcodes = false;
bool binary = false;
bool binaryRuntime = false;
bool abi = false;
bool ir = false;
bool irAstJson = false;
bool yulCFGJson = false;
bool irOptimized = false;
bool irOptimizedAstJson = false;
bool signatureHashes = false;
bool natspecUser = false;
bool natspecDev = false;
bool metadata = false;
bool storageLayout = false;
bool transientStorageLayout = false;
bool ethdebug = false;
bool ethdebugRuntime = false;
};
struct CombinedJsonRequests
{
bool operator!=(CombinedJsonRequests const& _other) const noexcept { return !(*this == _other); }
bool operator==(CombinedJsonRequests const& _other) const noexcept;
friend std::ostream& operator<<(std::ostream& _out, CombinedJsonRequests const& _requests);
static std::string const& componentName(bool CombinedJsonRequests::* _component);
static auto const& componentMap()
{
static std::map<std::string, bool CombinedJsonRequests::*> const components = {
{"abi", &CombinedJsonRequests::abi},
{"metadata", &CombinedJsonRequests::metadata},
{"bin", &CombinedJsonRequests::binary},
{"bin-runtime", &CombinedJsonRequests::binaryRuntime},
{"opcodes", &CombinedJsonRequests::opcodes},
{"asm", &CombinedJsonRequests::asm_},
{"storage-layout", &CombinedJsonRequests::storageLayout},
{"transient-storage-layout", &CombinedJsonRequests::transientStorageLayout},
{"generated-sources", &CombinedJsonRequests::generatedSources},
{"generated-sources-runtime", &CombinedJsonRequests::generatedSourcesRuntime},
{"srcmap", &CombinedJsonRequests::srcMap},
{"srcmap-runtime", &CombinedJsonRequests::srcMapRuntime},
{"function-debug", &CombinedJsonRequests::funDebug},
{"function-debug-runtime", &CombinedJsonRequests::funDebugRuntime},
{"hashes", &CombinedJsonRequests::signatureHashes},
{"devdoc", &CombinedJsonRequests::natspecDev},
{"userdoc", &CombinedJsonRequests::natspecUser},
{"ast", &CombinedJsonRequests::ast},
};
return components;
}
bool abi = false;
bool metadata = false;
bool binary = false;
bool binaryRuntime = false;
bool opcodes = false;
bool asm_ = false;
bool storageLayout = false;
bool transientStorageLayout = false;
bool generatedSources = false;
bool generatedSourcesRuntime = false;
bool srcMap = false;
bool srcMapRuntime = false;
bool funDebug = false;
bool funDebugRuntime = false;
bool signatureHashes = false;
bool natspecDev = false;
bool natspecUser = false;
bool ast = false;
};
struct CommandLineOptions
{
bool operator==(CommandLineOptions const& _other) const noexcept;
bool operator!=(CommandLineOptions const& _other) const noexcept { return !(*this == _other); }
OptimiserSettings optimiserSettings() const;
struct
{
InputMode mode = InputMode::Compiler;
std::set<boost::filesystem::path> paths;
std::vector<ImportRemapper::Remapping> remappings;
bool addStdin = false;
boost::filesystem::path basePath = "";
std::vector<boost::filesystem::path> includePaths;
FileReader::FileSystemPathSet allowedDirectories;
bool ignoreMissingFiles = false;
bool noImportCallback = false;
} input;
struct
{
boost::filesystem::path dir;
bool overwriteFiles = false;
langutil::EVMVersion evmVersion;
bool viaIR = false;
RevertStrings revertStrings = RevertStrings::Default;
std::optional<langutil::DebugInfoSelection> debugInfoSelection;
CompilerStack::State stopAfter = CompilerStack::State::CompilationSuccessful;
std::optional<uint8_t> eofVersion;
} output;
struct
{
yul::YulStack::Machine targetMachine = yul::YulStack::Machine::EVM;
yul::YulStack::Language inputLanguage = yul::YulStack::Language::StrictAssembly;
} assembly;
struct
{
std::map<std::string, util::h160> libraries; // library name -> address
} linker;
struct
{
util::JsonFormat json;
std::optional<bool> coloredOutput;
bool withErrorIds = false;
} formatting;
struct
{
CompilerOutputs outputs;
bool estimateGas = false;
std::optional<CombinedJsonRequests> combinedJsonRequests;
} compiler;
struct
{
CompilerStack::MetadataFormat format = CompilerStack::defaultMetadataFormat();
CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS;
bool literalSources = false;
} metadata;
struct
{
bool optimizeEvmasm = false;
bool optimizeYul = false;
std::optional<unsigned> expectedExecutionsPerDeployment;
std::optional<std::string> yulSteps;
} optimizer;
struct
{
bool initialize = false;
ModelCheckerSettings settings;
} modelChecker;
};
/// Parses the command-line arguments and produces a filled-out CommandLineOptions structure.
/// Validates provided values and reports errors by throwing @p CommandLineValidationErrors.
class CommandLineParser
{
public:
/// Parses the command-line arguments and fills out the internal CommandLineOptions structure.
/// @throws CommandLineValidationError if the arguments cannot be properly parsed or are invalid.
/// When an exception is thrown, the @p CommandLineOptions may be only partially filled out.
void parse(int _argc, char const* const* _argv);
CommandLineOptions const& options() const { return m_options; }
static void printHelp(std::ostream& _out) { _out << optionsDescription(true /* _forHelp */); }
private:
/// @returns a specification of all named command-line options accepted by the compiler.
/// The object can be used to parse command-line arguments or to generate the help screen.
static boost::program_options::options_description optionsDescription(bool _forHelp = false);
/// @returns a specification of all positional command-line arguments accepted by the compiler.
/// The object can be used to parse command-line arguments or to generate the help screen.
static boost::program_options::positional_options_description positionalOptionsDescription();
/// Uses boost::program_options to parse the command-line arguments and leaves the result in @a m_args.
/// Also handles the arguments that result in information being printed followed by immediate exit.
/// @returns false if parsing fails due to syntactical errors or the arguments not matching the description.
void parseArgs(int _argc, char const* const* _argv);
/// Validates parsed arguments stored in @a m_args and fills out the internal CommandLineOptions
/// structure.
/// @throws CommandLineValidationError in case of validation errors.
void processArgs();
/// Parses the value supplied to --combined-json.
/// @throws CommandLineValidationError in case of validation errors.
void parseCombinedJsonOption();
/// Parses the names of the input files, remappings. Does not check if the files actually exist.
/// @throws CommandLineValidationError in case of validation errors.
void parseInputPathsAndRemappings();
/// Tries to read from the file @a _input or interprets @a _input literally if that fails.
/// It then tries to parse the contents and appends to @a m_options.libraries.
/// @throws CommandLineValidationError in case of validation errors.
void parseLibraryOption(std::string const& _input);
void parseOutputSelection();
void checkMutuallyExclusive(std::vector<std::string> const& _optionNames);
size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const;
static std::string joinOptionNames(std::vector<std::string> const& _optionNames, std::string _separator = ", ");
CommandLineOptions m_options;
/// Map of command-line arguments produced by boost::program_options.
/// Serves as input for filling out m_options.
boost::program_options::variables_map m_args;
};
} // namespace solidity::frontend