1 | //===- llvm/Support/CommandLine.h - Command line handler --------*- 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 | // This class implements a command line argument processor that is useful when |
10 | // creating a tool. It provides a simple, minimalistic interface that is easily |
11 | // extensible and supports nonlocal (library) command line options. |
12 | // |
13 | // Note that rather than trying to figure out what this code does, you should |
14 | // read the library documentation located in docs/CommandLine.html or looks at |
15 | // the many example usages in tools/*/*.cpp |
16 | // |
17 | //===----------------------------------------------------------------------===// |
18 | |
19 | #ifndef LLVM_SUPPORT_COMMANDLINE_H |
20 | #define LLVM_SUPPORT_COMMANDLINE_H |
21 | |
22 | #include "llvm/ADT/ArrayRef.h" |
23 | #include "llvm/ADT/STLExtras.h" |
24 | #include "llvm/ADT/SmallPtrSet.h" |
25 | #include "llvm/ADT/SmallVector.h" |
26 | #include "llvm/ADT/StringMap.h" |
27 | #include "llvm/ADT/StringRef.h" |
28 | #include "llvm/ADT/Twine.h" |
29 | #include "llvm/ADT/iterator_range.h" |
30 | #include "llvm/Support/ErrorHandling.h" |
31 | #include "llvm/Support/ManagedStatic.h" |
32 | #include "llvm/Support/StringSaver.h" |
33 | #include "llvm/Support/raw_ostream.h" |
34 | #include <cassert> |
35 | #include <climits> |
36 | #include <cstddef> |
37 | #include <functional> |
38 | #include <initializer_list> |
39 | #include <string> |
40 | #include <type_traits> |
41 | #include <vector> |
42 | |
43 | namespace llvm { |
44 | |
45 | namespace vfs { |
46 | class FileSystem; |
47 | } |
48 | |
49 | class StringSaver; |
50 | |
51 | /// This namespace contains all of the command line option processing machinery. |
52 | /// It is intentionally a short name to make qualified usage concise. |
53 | namespace cl { |
54 | |
55 | //===----------------------------------------------------------------------===// |
56 | // Command line option processing entry point. |
57 | // |
58 | // Returns true on success. Otherwise, this will print the error message to |
59 | // stderr and exit if \p Errs is not set (nullptr by default), or print the |
60 | // error message to \p Errs and return false if \p Errs is provided. |
61 | // |
62 | // If EnvVar is not nullptr, command-line options are also parsed from the |
63 | // environment variable named by EnvVar. Precedence is given to occurrences |
64 | // from argv. This precedence is currently implemented by parsing argv after |
65 | // the environment variable, so it is only implemented correctly for options |
66 | // that give precedence to later occurrences. If your program supports options |
67 | // that give precedence to earlier occurrences, you will need to extend this |
68 | // function to support it correctly. |
69 | bool ParseCommandLineOptions(int argc, const char *const *argv, |
70 | StringRef Overview = "" , |
71 | raw_ostream *Errs = nullptr, |
72 | const char *EnvVar = nullptr, |
73 | bool LongOptionsUseDoubleDash = false); |
74 | |
75 | // Function pointer type for printing version information. |
76 | using VersionPrinterTy = std::function<void(raw_ostream &)>; |
77 | |
78 | ///===---------------------------------------------------------------------===// |
79 | /// Override the default (LLVM specific) version printer used to print out the |
80 | /// version when --version is given on the command line. This allows other |
81 | /// systems using the CommandLine utilities to print their own version string. |
82 | void SetVersionPrinter(VersionPrinterTy func); |
83 | |
84 | ///===---------------------------------------------------------------------===// |
85 | /// Add an extra printer to use in addition to the default one. This can be |
86 | /// called multiple times, and each time it adds a new function to the list |
87 | /// which will be called after the basic LLVM version printing is complete. |
88 | /// Each can then add additional information specific to the tool. |
89 | void (VersionPrinterTy func); |
90 | |
91 | // Print option values. |
92 | // With -print-options print the difference between option values and defaults. |
93 | // With -print-all-options print all option values. |
94 | // (Currently not perfect, but best-effort.) |
95 | void PrintOptionValues(); |
96 | |
97 | // Forward declaration - AddLiteralOption needs to be up here to make gcc happy. |
98 | class Option; |
99 | |
100 | /// Adds a new option for parsing and provides the option it refers to. |
101 | /// |
102 | /// \param O pointer to the option |
103 | /// \param Name the string name for the option to handle during parsing |
104 | /// |
105 | /// Literal options are used by some parsers to register special option values. |
106 | /// This is how the PassNameParser registers pass names for opt. |
107 | void AddLiteralOption(Option &O, StringRef Name); |
108 | |
109 | //===----------------------------------------------------------------------===// |
110 | // Flags permitted to be passed to command line arguments |
111 | // |
112 | |
113 | enum NumOccurrencesFlag { // Flags for the number of occurrences allowed |
114 | Optional = 0x00, // Zero or One occurrence |
115 | ZeroOrMore = 0x01, // Zero or more occurrences allowed |
116 | Required = 0x02, // One occurrence required |
117 | OneOrMore = 0x03, // One or more occurrences required |
118 | |
119 | // Indicates that this option is fed anything that follows the last positional |
120 | // argument required by the application (it is an error if there are zero |
121 | // positional arguments, and a ConsumeAfter option is used). |
122 | // Thus, for example, all arguments to LLI are processed until a filename is |
123 | // found. Once a filename is found, all of the succeeding arguments are |
124 | // passed, unprocessed, to the ConsumeAfter option. |
125 | // |
126 | ConsumeAfter = 0x04 |
127 | }; |
128 | |
129 | enum ValueExpected { // Is a value required for the option? |
130 | // zero reserved for the unspecified value |
131 | ValueOptional = 0x01, // The value can appear... or not |
132 | ValueRequired = 0x02, // The value is required to appear! |
133 | ValueDisallowed = 0x03 // A value may not be specified (for flags) |
134 | }; |
135 | |
136 | enum OptionHidden { // Control whether -help shows this option |
137 | NotHidden = 0x00, // Option included in -help & -help-hidden |
138 | Hidden = 0x01, // -help doesn't, but -help-hidden does |
139 | ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg |
140 | }; |
141 | |
142 | // This controls special features that the option might have that cause it to be |
143 | // parsed differently... |
144 | // |
145 | // Prefix - This option allows arguments that are otherwise unrecognized to be |
146 | // matched by options that are a prefix of the actual value. This is useful for |
147 | // cases like a linker, where options are typically of the form '-lfoo' or |
148 | // '-L../../include' where -l or -L are the actual flags. When prefix is |
149 | // enabled, and used, the value for the flag comes from the suffix of the |
150 | // argument. |
151 | // |
152 | // AlwaysPrefix - Only allow the behavior enabled by the Prefix flag and reject |
153 | // the Option=Value form. |
154 | // |
155 | |
156 | enum FormattingFlags { |
157 | NormalFormatting = 0x00, // Nothing special |
158 | Positional = 0x01, // Is a positional argument, no '-' required |
159 | Prefix = 0x02, // Can this option directly prefix its value? |
160 | AlwaysPrefix = 0x03 // Can this option only directly prefix its value? |
161 | }; |
162 | |
163 | enum MiscFlags { // Miscellaneous flags to adjust argument |
164 | CommaSeparated = 0x01, // Should this cl::list split between commas? |
165 | PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args? |
166 | Sink = 0x04, // Should this cl::list eat all unknown options? |
167 | |
168 | // Can this option group with other options? |
169 | // If this is enabled, multiple letter options are allowed to bunch together |
170 | // with only a single hyphen for the whole group. This allows emulation |
171 | // of the behavior that ls uses for example: ls -la === ls -l -a |
172 | Grouping = 0x08, |
173 | |
174 | // Default option |
175 | DefaultOption = 0x10 |
176 | }; |
177 | |
178 | //===----------------------------------------------------------------------===// |
179 | // |
180 | class OptionCategory { |
181 | private: |
182 | StringRef const Name; |
183 | StringRef const Description; |
184 | |
185 | void registerCategory(); |
186 | |
187 | public: |
188 | OptionCategory(StringRef const Name, |
189 | StringRef const Description = "" ) |
190 | : Name(Name), Description(Description) { |
191 | registerCategory(); |
192 | } |
193 | |
194 | StringRef getName() const { return Name; } |
195 | StringRef getDescription() const { return Description; } |
196 | }; |
197 | |
198 | // The general Option Category (used as default category). |
199 | OptionCategory &getGeneralCategory(); |
200 | |
201 | //===----------------------------------------------------------------------===// |
202 | // |
203 | class SubCommand { |
204 | private: |
205 | StringRef Name; |
206 | StringRef Description; |
207 | |
208 | protected: |
209 | void registerSubCommand(); |
210 | void unregisterSubCommand(); |
211 | |
212 | public: |
213 | SubCommand(StringRef Name, StringRef Description = "" ) |
214 | : Name(Name), Description(Description) { |
215 | registerSubCommand(); |
216 | } |
217 | SubCommand() = default; |
218 | |
219 | // Get the special subcommand representing no subcommand. |
220 | static SubCommand &getTopLevel(); |
221 | |
222 | // Get the special subcommand that can be used to put an option into all |
223 | // subcommands. |
224 | static SubCommand &getAll(); |
225 | |
226 | void reset(); |
227 | |
228 | explicit operator bool() const; |
229 | |
230 | StringRef getName() const { return Name; } |
231 | StringRef getDescription() const { return Description; } |
232 | |
233 | SmallVector<Option *, 4> PositionalOpts; |
234 | SmallVector<Option *, 4> SinkOpts; |
235 | StringMap<Option *> OptionsMap; |
236 | |
237 | Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists. |
238 | }; |
239 | |
240 | // A special subcommand representing no subcommand |
241 | extern ManagedStatic<SubCommand> TopLevelSubCommand; |
242 | |
243 | // A special subcommand that can be used to put an option into all subcommands. |
244 | extern ManagedStatic<SubCommand> AllSubCommands; |
245 | |
246 | //===----------------------------------------------------------------------===// |
247 | // |
248 | class Option { |
249 | friend class alias; |
250 | |
251 | // Overriden by subclasses to handle the value passed into an argument. Should |
252 | // return true if there was an error processing the argument and the program |
253 | // should exit. |
254 | // |
255 | virtual bool handleOccurrence(unsigned pos, StringRef ArgName, |
256 | StringRef Arg) = 0; |
257 | |
258 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
259 | return ValueOptional; |
260 | } |
261 | |
262 | // Out of line virtual function to provide home for the class. |
263 | virtual void anchor(); |
264 | |
265 | uint16_t NumOccurrences; // The number of times specified |
266 | // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid |
267 | // problems with signed enums in bitfields. |
268 | uint16_t Occurrences : 3; // enum NumOccurrencesFlag |
269 | // not using the enum type for 'Value' because zero is an implementation |
270 | // detail representing the non-value |
271 | uint16_t Value : 2; |
272 | uint16_t HiddenFlag : 2; // enum OptionHidden |
273 | uint16_t Formatting : 2; // enum FormattingFlags |
274 | uint16_t Misc : 5; |
275 | uint16_t FullyInitialized : 1; // Has addArgument been called? |
276 | uint16_t Position; // Position of last occurrence of the option |
277 | uint16_t AdditionalVals; // Greater than 0 for multi-valued option. |
278 | |
279 | public: |
280 | StringRef ArgStr; // The argument string itself (ex: "help", "o") |
281 | StringRef HelpStr; // The descriptive text message for -help |
282 | StringRef ValueStr; // String describing what the value of this option is |
283 | SmallVector<OptionCategory *, 1> |
284 | Categories; // The Categories this option belongs to |
285 | SmallPtrSet<SubCommand *, 1> Subs; // The subcommands this option belongs to. |
286 | |
287 | inline enum NumOccurrencesFlag getNumOccurrencesFlag() const { |
288 | return (enum NumOccurrencesFlag)Occurrences; |
289 | } |
290 | |
291 | inline enum ValueExpected getValueExpectedFlag() const { |
292 | return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault(); |
293 | } |
294 | |
295 | inline enum OptionHidden getOptionHiddenFlag() const { |
296 | return (enum OptionHidden)HiddenFlag; |
297 | } |
298 | |
299 | inline enum FormattingFlags getFormattingFlag() const { |
300 | return (enum FormattingFlags)Formatting; |
301 | } |
302 | |
303 | inline unsigned getMiscFlags() const { return Misc; } |
304 | inline unsigned getPosition() const { return Position; } |
305 | inline unsigned getNumAdditionalVals() const { return AdditionalVals; } |
306 | |
307 | // Return true if the argstr != "" |
308 | bool hasArgStr() const { return !ArgStr.empty(); } |
309 | bool isPositional() const { return getFormattingFlag() == cl::Positional; } |
310 | bool isSink() const { return getMiscFlags() & cl::Sink; } |
311 | bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; } |
312 | |
313 | bool isConsumeAfter() const { |
314 | return getNumOccurrencesFlag() == cl::ConsumeAfter; |
315 | } |
316 | |
317 | bool isInAllSubCommands() const { |
318 | return Subs.contains(Ptr: &SubCommand::getAll()); |
319 | } |
320 | |
321 | //-------------------------------------------------------------------------=== |
322 | // Accessor functions set by OptionModifiers |
323 | // |
324 | void setArgStr(StringRef S); |
325 | void setDescription(StringRef S) { HelpStr = S; } |
326 | void setValueStr(StringRef S) { ValueStr = S; } |
327 | void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; } |
328 | void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; } |
329 | void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; } |
330 | void setFormattingFlag(enum FormattingFlags V) { Formatting = V; } |
331 | void setMiscFlag(enum MiscFlags M) { Misc |= M; } |
332 | void setPosition(unsigned pos) { Position = pos; } |
333 | void addCategory(OptionCategory &C); |
334 | void addSubCommand(SubCommand &S) { Subs.insert(Ptr: &S); } |
335 | |
336 | protected: |
337 | explicit Option(enum NumOccurrencesFlag OccurrencesFlag, |
338 | enum OptionHidden Hidden) |
339 | : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0), |
340 | HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), |
341 | FullyInitialized(false), Position(0), AdditionalVals(0) { |
342 | Categories.push_back(Elt: &getGeneralCategory()); |
343 | } |
344 | |
345 | inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } |
346 | |
347 | public: |
348 | virtual ~Option() = default; |
349 | |
350 | // Register this argument with the commandline system. |
351 | // |
352 | void addArgument(); |
353 | |
354 | /// Unregisters this option from the CommandLine system. |
355 | /// |
356 | /// This option must have been the last option registered. |
357 | /// For testing purposes only. |
358 | void removeArgument(); |
359 | |
360 | // Return the width of the option tag for printing... |
361 | virtual size_t getOptionWidth() const = 0; |
362 | |
363 | // Print out information about this option. The to-be-maintained width is |
364 | // specified. |
365 | // |
366 | virtual void printOptionInfo(size_t GlobalWidth) const = 0; |
367 | |
368 | virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0; |
369 | |
370 | virtual void setDefault() = 0; |
371 | |
372 | // Prints the help string for an option. |
373 | // |
374 | // This maintains the Indent for multi-line descriptions. |
375 | // FirstLineIndentedBy is the count of chars of the first line |
376 | // i.e. the one containing the --<option name>. |
377 | static void printHelpStr(StringRef HelpStr, size_t Indent, |
378 | size_t FirstLineIndentedBy); |
379 | |
380 | // Prints the help string for an enum value. |
381 | // |
382 | // This maintains the Indent for multi-line descriptions. |
383 | // FirstLineIndentedBy is the count of chars of the first line |
384 | // i.e. the one containing the =<value>. |
385 | static void printEnumValHelpStr(StringRef HelpStr, size_t Indent, |
386 | size_t FirstLineIndentedBy); |
387 | |
388 | virtual void (SmallVectorImpl<StringRef> &) {} |
389 | |
390 | // Wrapper around handleOccurrence that enforces Flags. |
391 | // |
392 | virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value, |
393 | bool MultiArg = false); |
394 | |
395 | // Prints option name followed by message. Always returns true. |
396 | bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs()); |
397 | bool error(const Twine &Message, raw_ostream &Errs) { |
398 | return error(Message, ArgName: StringRef(), Errs); |
399 | } |
400 | |
401 | inline int getNumOccurrences() const { return NumOccurrences; } |
402 | void reset(); |
403 | }; |
404 | |
405 | //===----------------------------------------------------------------------===// |
406 | // Command line option modifiers that can be used to modify the behavior of |
407 | // command line option parsers... |
408 | // |
409 | |
410 | // Modifier to set the description shown in the -help output... |
411 | struct desc { |
412 | StringRef Desc; |
413 | |
414 | desc(StringRef Str) : Desc(Str) {} |
415 | |
416 | void apply(Option &O) const { O.setDescription(Desc); } |
417 | }; |
418 | |
419 | // Modifier to set the value description shown in the -help output... |
420 | struct value_desc { |
421 | StringRef Desc; |
422 | |
423 | value_desc(StringRef Str) : Desc(Str) {} |
424 | |
425 | void apply(Option &O) const { O.setValueStr(Desc); } |
426 | }; |
427 | |
428 | // Specify a default (initial) value for the command line argument, if the |
429 | // default constructor for the argument type does not give you what you want. |
430 | // This is only valid on "opt" arguments, not on "list" arguments. |
431 | template <class Ty> struct initializer { |
432 | const Ty &Init; |
433 | initializer(const Ty &Val) : Init(Val) {} |
434 | |
435 | template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); } |
436 | }; |
437 | |
438 | template <class Ty> struct list_initializer { |
439 | ArrayRef<Ty> Inits; |
440 | list_initializer(ArrayRef<Ty> Vals) : Inits(Vals) {} |
441 | |
442 | template <class Opt> void apply(Opt &O) const { O.setInitialValues(Inits); } |
443 | }; |
444 | |
445 | template <class Ty> initializer<Ty> init(const Ty &Val) { |
446 | return initializer<Ty>(Val); |
447 | } |
448 | |
449 | template <class Ty> |
450 | list_initializer<Ty> list_init(ArrayRef<Ty> Vals) { |
451 | return list_initializer<Ty>(Vals); |
452 | } |
453 | |
454 | // Allow the user to specify which external variable they want to store the |
455 | // results of the command line argument processing into, if they don't want to |
456 | // store it in the option itself. |
457 | template <class Ty> struct LocationClass { |
458 | Ty &Loc; |
459 | |
460 | LocationClass(Ty &L) : Loc(L) {} |
461 | |
462 | template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); } |
463 | }; |
464 | |
465 | template <class Ty> LocationClass<Ty> location(Ty &L) { |
466 | return LocationClass<Ty>(L); |
467 | } |
468 | |
469 | // Specify the Option category for the command line argument to belong to. |
470 | struct cat { |
471 | OptionCategory &Category; |
472 | |
473 | cat(OptionCategory &c) : Category(c) {} |
474 | |
475 | template <class Opt> void apply(Opt &O) const { O.addCategory(Category); } |
476 | }; |
477 | |
478 | // Specify the subcommand that this option belongs to. |
479 | struct sub { |
480 | SubCommand ⋐ |
481 | |
482 | sub(SubCommand &S) : Sub(S) {} |
483 | |
484 | template <class Opt> void apply(Opt &O) const { O.addSubCommand(Sub); } |
485 | }; |
486 | |
487 | // Specify a callback function to be called when an option is seen. |
488 | // Can be used to set other options automatically. |
489 | template <typename R, typename Ty> struct cb { |
490 | std::function<R(Ty)> CB; |
491 | |
492 | cb(std::function<R(Ty)> CB) : CB(CB) {} |
493 | |
494 | template <typename Opt> void apply(Opt &O) const { O.setCallback(CB); } |
495 | }; |
496 | |
497 | namespace detail { |
498 | template <typename F> |
499 | struct callback_traits : public callback_traits<decltype(&F::operator())> {}; |
500 | |
501 | template <typename R, typename C, typename... Args> |
502 | struct callback_traits<R (C::*)(Args...) const> { |
503 | using result_type = R; |
504 | using arg_type = std::tuple_element_t<0, std::tuple<Args...>>; |
505 | static_assert(sizeof...(Args) == 1, "callback function must have one and only one parameter" ); |
506 | static_assert(std::is_same_v<result_type, void>, |
507 | "callback return type must be void" ); |
508 | static_assert(std::is_lvalue_reference_v<arg_type> && |
509 | std::is_const_v<std::remove_reference_t<arg_type>>, |
510 | "callback arg_type must be a const lvalue reference" ); |
511 | }; |
512 | } // namespace detail |
513 | |
514 | template <typename F> |
515 | cb<typename detail::callback_traits<F>::result_type, |
516 | typename detail::callback_traits<F>::arg_type> |
517 | callback(F CB) { |
518 | using result_type = typename detail::callback_traits<F>::result_type; |
519 | using arg_type = typename detail::callback_traits<F>::arg_type; |
520 | return cb<result_type, arg_type>(CB); |
521 | } |
522 | |
523 | //===----------------------------------------------------------------------===// |
524 | |
525 | // Support value comparison outside the template. |
526 | struct GenericOptionValue { |
527 | virtual bool compare(const GenericOptionValue &V) const = 0; |
528 | |
529 | protected: |
530 | GenericOptionValue() = default; |
531 | GenericOptionValue(const GenericOptionValue&) = default; |
532 | GenericOptionValue &operator=(const GenericOptionValue &) = default; |
533 | ~GenericOptionValue() = default; |
534 | |
535 | private: |
536 | virtual void anchor(); |
537 | }; |
538 | |
539 | template <class DataType> struct OptionValue; |
540 | |
541 | // The default value safely does nothing. Option value printing is only |
542 | // best-effort. |
543 | template <class DataType, bool isClass> |
544 | struct OptionValueBase : public GenericOptionValue { |
545 | // Temporary storage for argument passing. |
546 | using WrapperType = OptionValue<DataType>; |
547 | |
548 | bool hasValue() const { return false; } |
549 | |
550 | const DataType &getValue() const { llvm_unreachable("no default value" ); } |
551 | |
552 | // Some options may take their value from a different data type. |
553 | template <class DT> void setValue(const DT & /*V*/) {} |
554 | |
555 | bool compare(const DataType & /*V*/) const { return false; } |
556 | |
557 | bool compare(const GenericOptionValue & /*V*/) const override { |
558 | return false; |
559 | } |
560 | |
561 | protected: |
562 | ~OptionValueBase() = default; |
563 | }; |
564 | |
565 | // Simple copy of the option value. |
566 | template <class DataType> class OptionValueCopy : public GenericOptionValue { |
567 | DataType Value; |
568 | bool Valid = false; |
569 | |
570 | protected: |
571 | OptionValueCopy(const OptionValueCopy&) = default; |
572 | OptionValueCopy &operator=(const OptionValueCopy &) = default; |
573 | ~OptionValueCopy() = default; |
574 | |
575 | public: |
576 | OptionValueCopy() = default; |
577 | |
578 | bool hasValue() const { return Valid; } |
579 | |
580 | const DataType &getValue() const { |
581 | assert(Valid && "invalid option value" ); |
582 | return Value; |
583 | } |
584 | |
585 | void setValue(const DataType &V) { |
586 | Valid = true; |
587 | Value = V; |
588 | } |
589 | |
590 | bool compare(const DataType &V) const { return Valid && (Value != V); } |
591 | |
592 | bool compare(const GenericOptionValue &V) const override { |
593 | const OptionValueCopy<DataType> &VC = |
594 | static_cast<const OptionValueCopy<DataType> &>(V); |
595 | if (!VC.hasValue()) |
596 | return false; |
597 | return compare(VC.getValue()); |
598 | } |
599 | }; |
600 | |
601 | // Non-class option values. |
602 | template <class DataType> |
603 | struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> { |
604 | using WrapperType = DataType; |
605 | |
606 | protected: |
607 | OptionValueBase() = default; |
608 | OptionValueBase(const OptionValueBase&) = default; |
609 | OptionValueBase &operator=(const OptionValueBase &) = default; |
610 | ~OptionValueBase() = default; |
611 | }; |
612 | |
613 | // Top-level option class. |
614 | template <class DataType> |
615 | struct OptionValue final |
616 | : OptionValueBase<DataType, std::is_class_v<DataType>> { |
617 | OptionValue() = default; |
618 | |
619 | OptionValue(const DataType &V) { this->setValue(V); } |
620 | |
621 | // Some options may take their value from a different data type. |
622 | template <class DT> OptionValue<DataType> &operator=(const DT &V) { |
623 | this->setValue(V); |
624 | return *this; |
625 | } |
626 | }; |
627 | |
628 | // Other safe-to-copy-by-value common option types. |
629 | enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE }; |
630 | template <> |
631 | struct OptionValue<cl::boolOrDefault> final |
632 | : OptionValueCopy<cl::boolOrDefault> { |
633 | using WrapperType = cl::boolOrDefault; |
634 | |
635 | OptionValue() = default; |
636 | |
637 | OptionValue(const cl::boolOrDefault &V) { this->setValue(V); } |
638 | |
639 | OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) { |
640 | setValue(V); |
641 | return *this; |
642 | } |
643 | |
644 | private: |
645 | void anchor() override; |
646 | }; |
647 | |
648 | template <> |
649 | struct OptionValue<std::string> final : OptionValueCopy<std::string> { |
650 | using WrapperType = StringRef; |
651 | |
652 | OptionValue() = default; |
653 | |
654 | OptionValue(const std::string &V) { this->setValue(V); } |
655 | |
656 | OptionValue<std::string> &operator=(const std::string &V) { |
657 | setValue(V); |
658 | return *this; |
659 | } |
660 | |
661 | private: |
662 | void anchor() override; |
663 | }; |
664 | |
665 | //===----------------------------------------------------------------------===// |
666 | // Enum valued command line option |
667 | // |
668 | |
669 | // This represents a single enum value, using "int" as the underlying type. |
670 | struct OptionEnumValue { |
671 | StringRef Name; |
672 | int Value; |
673 | StringRef Description; |
674 | }; |
675 | |
676 | #define clEnumVal(ENUMVAL, DESC) \ |
677 | llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC } |
678 | #define clEnumValN(ENUMVAL, FLAGNAME, DESC) \ |
679 | llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC } |
680 | |
681 | // For custom data types, allow specifying a group of values together as the |
682 | // values that go into the mapping that the option handler uses. |
683 | // |
684 | class ValuesClass { |
685 | // Use a vector instead of a map, because the lists should be short, |
686 | // the overhead is less, and most importantly, it keeps them in the order |
687 | // inserted so we can print our option out nicely. |
688 | SmallVector<OptionEnumValue, 4> Values; |
689 | |
690 | public: |
691 | ValuesClass(std::initializer_list<OptionEnumValue> Options) |
692 | : Values(Options) {} |
693 | |
694 | template <class Opt> void apply(Opt &O) const { |
695 | for (const auto &Value : Values) |
696 | O.getParser().addLiteralOption(Value.Name, Value.Value, |
697 | Value.Description); |
698 | } |
699 | }; |
700 | |
701 | /// Helper to build a ValuesClass by forwarding a variable number of arguments |
702 | /// as an initializer list to the ValuesClass constructor. |
703 | template <typename... OptsTy> ValuesClass values(OptsTy... Options) { |
704 | return ValuesClass({Options...}); |
705 | } |
706 | |
707 | //===----------------------------------------------------------------------===// |
708 | // Parameterizable parser for different data types. By default, known data types |
709 | // (string, int, bool) have specialized parsers, that do what you would expect. |
710 | // The default parser, used for data types that are not built-in, uses a mapping |
711 | // table to map specific options to values, which is used, among other things, |
712 | // to handle enum types. |
713 | |
714 | //-------------------------------------------------- |
715 | // This class holds all the non-generic code that we do not need replicated for |
716 | // every instance of the generic parser. This also allows us to put stuff into |
717 | // CommandLine.cpp |
718 | // |
719 | class generic_parser_base { |
720 | protected: |
721 | class GenericOptionInfo { |
722 | public: |
723 | GenericOptionInfo(StringRef name, StringRef helpStr) |
724 | : Name(name), HelpStr(helpStr) {} |
725 | StringRef Name; |
726 | StringRef HelpStr; |
727 | }; |
728 | |
729 | public: |
730 | generic_parser_base(Option &O) : Owner(O) {} |
731 | |
732 | virtual ~generic_parser_base() = default; |
733 | // Base class should have virtual-destructor |
734 | |
735 | // Virtual function implemented by generic subclass to indicate how many |
736 | // entries are in Values. |
737 | // |
738 | virtual unsigned getNumOptions() const = 0; |
739 | |
740 | // Return option name N. |
741 | virtual StringRef getOption(unsigned N) const = 0; |
742 | |
743 | // Return description N |
744 | virtual StringRef getDescription(unsigned N) const = 0; |
745 | |
746 | // Return the width of the option tag for printing... |
747 | virtual size_t getOptionWidth(const Option &O) const; |
748 | |
749 | virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0; |
750 | |
751 | // Print out information about this option. The to-be-maintained width is |
752 | // specified. |
753 | // |
754 | virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const; |
755 | |
756 | void printGenericOptionDiff(const Option &O, const GenericOptionValue &V, |
757 | const GenericOptionValue &Default, |
758 | size_t GlobalWidth) const; |
759 | |
760 | // Print the value of an option and it's default. |
761 | // |
762 | // Template definition ensures that the option and default have the same |
763 | // DataType (via the same AnyOptionValue). |
764 | template <class AnyOptionValue> |
765 | void printOptionDiff(const Option &O, const AnyOptionValue &V, |
766 | const AnyOptionValue &Default, |
767 | size_t GlobalWidth) const { |
768 | printGenericOptionDiff(O, V, Default, GlobalWidth); |
769 | } |
770 | |
771 | void initialize() {} |
772 | |
773 | void (SmallVectorImpl<StringRef> &OptionNames) { |
774 | // If there has been no argstr specified, that means that we need to add an |
775 | // argument for every possible option. This ensures that our options are |
776 | // vectored to us. |
777 | if (!Owner.hasArgStr()) |
778 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
779 | OptionNames.push_back(Elt: getOption(N: i)); |
780 | } |
781 | |
782 | enum ValueExpected getValueExpectedFlagDefault() const { |
783 | // If there is an ArgStr specified, then we are of the form: |
784 | // |
785 | // -opt=O2 or -opt O2 or -optO2 |
786 | // |
787 | // In which case, the value is required. Otherwise if an arg str has not |
788 | // been specified, we are of the form: |
789 | // |
790 | // -O2 or O2 or -la (where -l and -a are separate options) |
791 | // |
792 | // If this is the case, we cannot allow a value. |
793 | // |
794 | if (Owner.hasArgStr()) |
795 | return ValueRequired; |
796 | else |
797 | return ValueDisallowed; |
798 | } |
799 | |
800 | // Return the option number corresponding to the specified |
801 | // argument string. If the option is not found, getNumOptions() is returned. |
802 | // |
803 | unsigned findOption(StringRef Name); |
804 | |
805 | protected: |
806 | Option &Owner; |
807 | }; |
808 | |
809 | // Default parser implementation - This implementation depends on having a |
810 | // mapping of recognized options to values of some sort. In addition to this, |
811 | // each entry in the mapping also tracks a help message that is printed with the |
812 | // command line option for -help. Because this is a simple mapping parser, the |
813 | // data type can be any unsupported type. |
814 | // |
815 | template <class DataType> class parser : public generic_parser_base { |
816 | protected: |
817 | class OptionInfo : public GenericOptionInfo { |
818 | public: |
819 | OptionInfo(StringRef name, DataType v, StringRef helpStr) |
820 | : GenericOptionInfo(name, helpStr), V(v) {} |
821 | |
822 | OptionValue<DataType> V; |
823 | }; |
824 | SmallVector<OptionInfo, 8> Values; |
825 | |
826 | public: |
827 | parser(Option &O) : generic_parser_base(O) {} |
828 | |
829 | using parser_data_type = DataType; |
830 | |
831 | // Implement virtual functions needed by generic_parser_base |
832 | unsigned getNumOptions() const override { return unsigned(Values.size()); } |
833 | StringRef getOption(unsigned N) const override { return Values[N].Name; } |
834 | StringRef getDescription(unsigned N) const override { |
835 | return Values[N].HelpStr; |
836 | } |
837 | |
838 | // Return the value of option name N. |
839 | const GenericOptionValue &getOptionValue(unsigned N) const override { |
840 | return Values[N].V; |
841 | } |
842 | |
843 | // Return true on error. |
844 | bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) { |
845 | StringRef ArgVal; |
846 | if (Owner.hasArgStr()) |
847 | ArgVal = Arg; |
848 | else |
849 | ArgVal = ArgName; |
850 | |
851 | for (size_t i = 0, e = Values.size(); i != e; ++i) |
852 | if (Values[i].Name == ArgVal) { |
853 | V = Values[i].V.getValue(); |
854 | return false; |
855 | } |
856 | |
857 | return O.error(Message: "Cannot find option named '" + ArgVal + "'!" ); |
858 | } |
859 | |
860 | /// Add an entry to the mapping table. |
861 | /// |
862 | template <class DT> |
863 | void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) { |
864 | assert(findOption(Name) == Values.size() && "Option already exists!" ); |
865 | OptionInfo X(Name, static_cast<DataType>(V), HelpStr); |
866 | Values.push_back(X); |
867 | AddLiteralOption(Owner, Name); |
868 | } |
869 | |
870 | /// Remove the specified option. |
871 | /// |
872 | void removeLiteralOption(StringRef Name) { |
873 | unsigned N = findOption(Name); |
874 | assert(N != Values.size() && "Option not found!" ); |
875 | Values.erase(Values.begin() + N); |
876 | } |
877 | }; |
878 | |
879 | //-------------------------------------------------- |
880 | // Super class of parsers to provide boilerplate code |
881 | // |
882 | class basic_parser_impl { // non-template implementation of basic_parser<t> |
883 | public: |
884 | basic_parser_impl(Option &) {} |
885 | |
886 | virtual ~basic_parser_impl() = default; |
887 | |
888 | enum ValueExpected getValueExpectedFlagDefault() const { |
889 | return ValueRequired; |
890 | } |
891 | |
892 | void (SmallVectorImpl<StringRef> &) {} |
893 | |
894 | void initialize() {} |
895 | |
896 | // Return the width of the option tag for printing... |
897 | size_t getOptionWidth(const Option &O) const; |
898 | |
899 | // Print out information about this option. The to-be-maintained width is |
900 | // specified. |
901 | // |
902 | void printOptionInfo(const Option &O, size_t GlobalWidth) const; |
903 | |
904 | // Print a placeholder for options that don't yet support printOptionDiff(). |
905 | void printOptionNoValue(const Option &O, size_t GlobalWidth) const; |
906 | |
907 | // Overload in subclass to provide a better default value. |
908 | virtual StringRef getValueName() const { return "value" ; } |
909 | |
910 | // An out-of-line virtual method to provide a 'home' for this class. |
911 | virtual void anchor(); |
912 | |
913 | protected: |
914 | // A helper for basic_parser::printOptionDiff. |
915 | void printOptionName(const Option &O, size_t GlobalWidth) const; |
916 | }; |
917 | |
918 | // The real basic parser is just a template wrapper that provides a typedef for |
919 | // the provided data type. |
920 | // |
921 | template <class DataType> class basic_parser : public basic_parser_impl { |
922 | public: |
923 | using parser_data_type = DataType; |
924 | using OptVal = OptionValue<DataType>; |
925 | |
926 | basic_parser(Option &O) : basic_parser_impl(O) {} |
927 | }; |
928 | |
929 | //-------------------------------------------------- |
930 | |
931 | extern template class basic_parser<bool>; |
932 | |
933 | template <> class parser<bool> : public basic_parser<bool> { |
934 | public: |
935 | parser(Option &O) : basic_parser(O) {} |
936 | |
937 | // Return true on error. |
938 | bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val); |
939 | |
940 | void initialize() {} |
941 | |
942 | enum ValueExpected getValueExpectedFlagDefault() const { |
943 | return ValueOptional; |
944 | } |
945 | |
946 | // Do not print =<value> at all. |
947 | StringRef getValueName() const override { return StringRef(); } |
948 | |
949 | void printOptionDiff(const Option &O, bool V, OptVal Default, |
950 | size_t GlobalWidth) const; |
951 | |
952 | // An out-of-line virtual method to provide a 'home' for this class. |
953 | void anchor() override; |
954 | }; |
955 | |
956 | //-------------------------------------------------- |
957 | |
958 | extern template class basic_parser<boolOrDefault>; |
959 | |
960 | template <> class parser<boolOrDefault> : public basic_parser<boolOrDefault> { |
961 | public: |
962 | parser(Option &O) : basic_parser(O) {} |
963 | |
964 | // Return true on error. |
965 | bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val); |
966 | |
967 | enum ValueExpected getValueExpectedFlagDefault() const { |
968 | return ValueOptional; |
969 | } |
970 | |
971 | // Do not print =<value> at all. |
972 | StringRef getValueName() const override { return StringRef(); } |
973 | |
974 | void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default, |
975 | size_t GlobalWidth) const; |
976 | |
977 | // An out-of-line virtual method to provide a 'home' for this class. |
978 | void anchor() override; |
979 | }; |
980 | |
981 | //-------------------------------------------------- |
982 | |
983 | extern template class basic_parser<int>; |
984 | |
985 | template <> class parser<int> : public basic_parser<int> { |
986 | public: |
987 | parser(Option &O) : basic_parser(O) {} |
988 | |
989 | // Return true on error. |
990 | bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val); |
991 | |
992 | // Overload in subclass to provide a better default value. |
993 | StringRef getValueName() const override { return "int" ; } |
994 | |
995 | void printOptionDiff(const Option &O, int V, OptVal Default, |
996 | size_t GlobalWidth) const; |
997 | |
998 | // An out-of-line virtual method to provide a 'home' for this class. |
999 | void anchor() override; |
1000 | }; |
1001 | |
1002 | //-------------------------------------------------- |
1003 | |
1004 | extern template class basic_parser<long>; |
1005 | |
1006 | template <> class parser<long> final : public basic_parser<long> { |
1007 | public: |
1008 | parser(Option &O) : basic_parser(O) {} |
1009 | |
1010 | // Return true on error. |
1011 | bool parse(Option &O, StringRef ArgName, StringRef Arg, long &Val); |
1012 | |
1013 | // Overload in subclass to provide a better default value. |
1014 | StringRef getValueName() const override { return "long" ; } |
1015 | |
1016 | void printOptionDiff(const Option &O, long V, OptVal Default, |
1017 | size_t GlobalWidth) const; |
1018 | |
1019 | // An out-of-line virtual method to provide a 'home' for this class. |
1020 | void anchor() override; |
1021 | }; |
1022 | |
1023 | //-------------------------------------------------- |
1024 | |
1025 | extern template class basic_parser<long long>; |
1026 | |
1027 | template <> class parser<long long> : public basic_parser<long long> { |
1028 | public: |
1029 | parser(Option &O) : basic_parser(O) {} |
1030 | |
1031 | // Return true on error. |
1032 | bool parse(Option &O, StringRef ArgName, StringRef Arg, long long &Val); |
1033 | |
1034 | // Overload in subclass to provide a better default value. |
1035 | StringRef getValueName() const override { return "long" ; } |
1036 | |
1037 | void printOptionDiff(const Option &O, long long V, OptVal Default, |
1038 | size_t GlobalWidth) const; |
1039 | |
1040 | // An out-of-line virtual method to provide a 'home' for this class. |
1041 | void anchor() override; |
1042 | }; |
1043 | |
1044 | //-------------------------------------------------- |
1045 | |
1046 | extern template class basic_parser<unsigned>; |
1047 | |
1048 | template <> class parser<unsigned> : public basic_parser<unsigned> { |
1049 | public: |
1050 | parser(Option &O) : basic_parser(O) {} |
1051 | |
1052 | // Return true on error. |
1053 | bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val); |
1054 | |
1055 | // Overload in subclass to provide a better default value. |
1056 | StringRef getValueName() const override { return "uint" ; } |
1057 | |
1058 | void printOptionDiff(const Option &O, unsigned V, OptVal Default, |
1059 | size_t GlobalWidth) const; |
1060 | |
1061 | // An out-of-line virtual method to provide a 'home' for this class. |
1062 | void anchor() override; |
1063 | }; |
1064 | |
1065 | //-------------------------------------------------- |
1066 | |
1067 | extern template class basic_parser<unsigned long>; |
1068 | |
1069 | template <> |
1070 | class parser<unsigned long> final : public basic_parser<unsigned long> { |
1071 | public: |
1072 | parser(Option &O) : basic_parser(O) {} |
1073 | |
1074 | // Return true on error. |
1075 | bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned long &Val); |
1076 | |
1077 | // Overload in subclass to provide a better default value. |
1078 | StringRef getValueName() const override { return "ulong" ; } |
1079 | |
1080 | void printOptionDiff(const Option &O, unsigned long V, OptVal Default, |
1081 | size_t GlobalWidth) const; |
1082 | |
1083 | // An out-of-line virtual method to provide a 'home' for this class. |
1084 | void anchor() override; |
1085 | }; |
1086 | |
1087 | //-------------------------------------------------- |
1088 | |
1089 | extern template class basic_parser<unsigned long long>; |
1090 | |
1091 | template <> |
1092 | class parser<unsigned long long> : public basic_parser<unsigned long long> { |
1093 | public: |
1094 | parser(Option &O) : basic_parser(O) {} |
1095 | |
1096 | // Return true on error. |
1097 | bool parse(Option &O, StringRef ArgName, StringRef Arg, |
1098 | unsigned long long &Val); |
1099 | |
1100 | // Overload in subclass to provide a better default value. |
1101 | StringRef getValueName() const override { return "ulong" ; } |
1102 | |
1103 | void printOptionDiff(const Option &O, unsigned long long V, OptVal Default, |
1104 | size_t GlobalWidth) const; |
1105 | |
1106 | // An out-of-line virtual method to provide a 'home' for this class. |
1107 | void anchor() override; |
1108 | }; |
1109 | |
1110 | //-------------------------------------------------- |
1111 | |
1112 | extern template class basic_parser<double>; |
1113 | |
1114 | template <> class parser<double> : public basic_parser<double> { |
1115 | public: |
1116 | parser(Option &O) : basic_parser(O) {} |
1117 | |
1118 | // Return true on error. |
1119 | bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val); |
1120 | |
1121 | // Overload in subclass to provide a better default value. |
1122 | StringRef getValueName() const override { return "number" ; } |
1123 | |
1124 | void printOptionDiff(const Option &O, double V, OptVal Default, |
1125 | size_t GlobalWidth) const; |
1126 | |
1127 | // An out-of-line virtual method to provide a 'home' for this class. |
1128 | void anchor() override; |
1129 | }; |
1130 | |
1131 | //-------------------------------------------------- |
1132 | |
1133 | extern template class basic_parser<float>; |
1134 | |
1135 | template <> class parser<float> : public basic_parser<float> { |
1136 | public: |
1137 | parser(Option &O) : basic_parser(O) {} |
1138 | |
1139 | // Return true on error. |
1140 | bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val); |
1141 | |
1142 | // Overload in subclass to provide a better default value. |
1143 | StringRef getValueName() const override { return "number" ; } |
1144 | |
1145 | void printOptionDiff(const Option &O, float V, OptVal Default, |
1146 | size_t GlobalWidth) const; |
1147 | |
1148 | // An out-of-line virtual method to provide a 'home' for this class. |
1149 | void anchor() override; |
1150 | }; |
1151 | |
1152 | //-------------------------------------------------- |
1153 | |
1154 | extern template class basic_parser<std::string>; |
1155 | |
1156 | template <> class parser<std::string> : public basic_parser<std::string> { |
1157 | public: |
1158 | parser(Option &O) : basic_parser(O) {} |
1159 | |
1160 | // Return true on error. |
1161 | bool parse(Option &, StringRef, StringRef Arg, std::string &Value) { |
1162 | Value = Arg.str(); |
1163 | return false; |
1164 | } |
1165 | |
1166 | // Overload in subclass to provide a better default value. |
1167 | StringRef getValueName() const override { return "string" ; } |
1168 | |
1169 | void printOptionDiff(const Option &O, StringRef V, const OptVal &Default, |
1170 | size_t GlobalWidth) const; |
1171 | |
1172 | // An out-of-line virtual method to provide a 'home' for this class. |
1173 | void anchor() override; |
1174 | }; |
1175 | |
1176 | //-------------------------------------------------- |
1177 | |
1178 | extern template class basic_parser<char>; |
1179 | |
1180 | template <> class parser<char> : public basic_parser<char> { |
1181 | public: |
1182 | parser(Option &O) : basic_parser(O) {} |
1183 | |
1184 | // Return true on error. |
1185 | bool parse(Option &, StringRef, StringRef Arg, char &Value) { |
1186 | Value = Arg[0]; |
1187 | return false; |
1188 | } |
1189 | |
1190 | // Overload in subclass to provide a better default value. |
1191 | StringRef getValueName() const override { return "char" ; } |
1192 | |
1193 | void printOptionDiff(const Option &O, char V, OptVal Default, |
1194 | size_t GlobalWidth) const; |
1195 | |
1196 | // An out-of-line virtual method to provide a 'home' for this class. |
1197 | void anchor() override; |
1198 | }; |
1199 | |
1200 | //-------------------------------------------------- |
1201 | // This collection of wrappers is the intermediary between class opt and class |
1202 | // parser to handle all the template nastiness. |
1203 | |
1204 | // This overloaded function is selected by the generic parser. |
1205 | template <class ParserClass, class DT> |
1206 | void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V, |
1207 | const OptionValue<DT> &Default, size_t GlobalWidth) { |
1208 | OptionValue<DT> OV = V; |
1209 | P.printOptionDiff(O, OV, Default, GlobalWidth); |
1210 | } |
1211 | |
1212 | // This is instantiated for basic parsers when the parsed value has a different |
1213 | // type than the option value. e.g. HelpPrinter. |
1214 | template <class ParserDT, class ValDT> struct OptionDiffPrinter { |
1215 | void print(const Option &O, const parser<ParserDT> &P, const ValDT & /*V*/, |
1216 | const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) { |
1217 | P.printOptionNoValue(O, GlobalWidth); |
1218 | } |
1219 | }; |
1220 | |
1221 | // This is instantiated for basic parsers when the parsed value has the same |
1222 | // type as the option value. |
1223 | template <class DT> struct OptionDiffPrinter<DT, DT> { |
1224 | void print(const Option &O, const parser<DT> &P, const DT &V, |
1225 | const OptionValue<DT> &Default, size_t GlobalWidth) { |
1226 | P.printOptionDiff(O, V, Default, GlobalWidth); |
1227 | } |
1228 | }; |
1229 | |
1230 | // This overloaded function is selected by the basic parser, which may parse a |
1231 | // different type than the option type. |
1232 | template <class ParserClass, class ValDT> |
1233 | void printOptionDiff( |
1234 | const Option &O, |
1235 | const basic_parser<typename ParserClass::parser_data_type> &P, |
1236 | const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) { |
1237 | |
1238 | OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer; |
1239 | printer.print(O, static_cast<const ParserClass &>(P), V, Default, |
1240 | GlobalWidth); |
1241 | } |
1242 | |
1243 | //===----------------------------------------------------------------------===// |
1244 | // This class is used because we must use partial specialization to handle |
1245 | // literal string arguments specially (const char* does not correctly respond to |
1246 | // the apply method). Because the syntax to use this is a pain, we have the |
1247 | // 'apply' method below to handle the nastiness... |
1248 | // |
1249 | template <class Mod> struct applicator { |
1250 | template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); } |
1251 | }; |
1252 | |
1253 | // Handle const char* as a special case... |
1254 | template <unsigned n> struct applicator<char[n]> { |
1255 | template <class Opt> static void opt(StringRef Str, Opt &O) { |
1256 | O.setArgStr(Str); |
1257 | } |
1258 | }; |
1259 | template <unsigned n> struct applicator<const char[n]> { |
1260 | template <class Opt> static void opt(StringRef Str, Opt &O) { |
1261 | O.setArgStr(Str); |
1262 | } |
1263 | }; |
1264 | template <> struct applicator<StringRef > { |
1265 | template <class Opt> static void opt(StringRef Str, Opt &O) { |
1266 | O.setArgStr(Str); |
1267 | } |
1268 | }; |
1269 | |
1270 | template <> struct applicator<NumOccurrencesFlag> { |
1271 | static void opt(NumOccurrencesFlag N, Option &O) { |
1272 | O.setNumOccurrencesFlag(N); |
1273 | } |
1274 | }; |
1275 | |
1276 | template <> struct applicator<ValueExpected> { |
1277 | static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } |
1278 | }; |
1279 | |
1280 | template <> struct applicator<OptionHidden> { |
1281 | static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } |
1282 | }; |
1283 | |
1284 | template <> struct applicator<FormattingFlags> { |
1285 | static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } |
1286 | }; |
1287 | |
1288 | template <> struct applicator<MiscFlags> { |
1289 | static void opt(MiscFlags MF, Option &O) { |
1290 | assert((MF != Grouping || O.ArgStr.size() == 1) && |
1291 | "cl::Grouping can only apply to single character Options." ); |
1292 | O.setMiscFlag(MF); |
1293 | } |
1294 | }; |
1295 | |
1296 | // Apply modifiers to an option in a type safe way. |
1297 | template <class Opt, class Mod, class... Mods> |
1298 | void apply(Opt *O, const Mod &M, const Mods &... Ms) { |
1299 | applicator<Mod>::opt(M, *O); |
1300 | apply(O, Ms...); |
1301 | } |
1302 | |
1303 | template <class Opt, class Mod> void apply(Opt *O, const Mod &M) { |
1304 | applicator<Mod>::opt(M, *O); |
1305 | } |
1306 | |
1307 | //===----------------------------------------------------------------------===// |
1308 | // Default storage class definition: external storage. This implementation |
1309 | // assumes the user will specify a variable to store the data into with the |
1310 | // cl::location(x) modifier. |
1311 | // |
1312 | template <class DataType, bool ExternalStorage, bool isClass> |
1313 | class opt_storage { |
1314 | DataType *Location = nullptr; // Where to store the object... |
1315 | OptionValue<DataType> Default; |
1316 | |
1317 | void check_location() const { |
1318 | assert(Location && "cl::location(...) not specified for a command " |
1319 | "line option with external storage, " |
1320 | "or cl::init specified before cl::location()!!" ); |
1321 | } |
1322 | |
1323 | public: |
1324 | opt_storage() = default; |
1325 | |
1326 | bool setLocation(Option &O, DataType &L) { |
1327 | if (Location) |
1328 | return O.error(Message: "cl::location(x) specified more than once!" ); |
1329 | Location = &L; |
1330 | Default = L; |
1331 | return false; |
1332 | } |
1333 | |
1334 | template <class T> void setValue(const T &V, bool initial = false) { |
1335 | check_location(); |
1336 | *Location = V; |
1337 | if (initial) |
1338 | Default = V; |
1339 | } |
1340 | |
1341 | DataType &getValue() { |
1342 | check_location(); |
1343 | return *Location; |
1344 | } |
1345 | const DataType &getValue() const { |
1346 | check_location(); |
1347 | return *Location; |
1348 | } |
1349 | |
1350 | operator DataType() const { return this->getValue(); } |
1351 | |
1352 | const OptionValue<DataType> &getDefault() const { return Default; } |
1353 | }; |
1354 | |
1355 | // Define how to hold a class type object, such as a string. Since we can |
1356 | // inherit from a class, we do so. This makes us exactly compatible with the |
1357 | // object in all cases that it is used. |
1358 | // |
1359 | template <class DataType> |
1360 | class opt_storage<DataType, false, true> : public DataType { |
1361 | public: |
1362 | OptionValue<DataType> Default; |
1363 | |
1364 | template <class T> void setValue(const T &V, bool initial = false) { |
1365 | DataType::operator=(V); |
1366 | if (initial) |
1367 | Default = V; |
1368 | } |
1369 | |
1370 | DataType &getValue() { return *this; } |
1371 | const DataType &getValue() const { return *this; } |
1372 | |
1373 | const OptionValue<DataType> &getDefault() const { return Default; } |
1374 | }; |
1375 | |
1376 | // Define a partial specialization to handle things we cannot inherit from. In |
1377 | // this case, we store an instance through containment, and overload operators |
1378 | // to get at the value. |
1379 | // |
1380 | template <class DataType> class opt_storage<DataType, false, false> { |
1381 | public: |
1382 | DataType Value; |
1383 | OptionValue<DataType> Default; |
1384 | |
1385 | // Make sure we initialize the value with the default constructor for the |
1386 | // type. |
1387 | opt_storage() : Value(DataType()), Default() {} |
1388 | |
1389 | template <class T> void setValue(const T &V, bool initial = false) { |
1390 | Value = V; |
1391 | if (initial) |
1392 | Default = V; |
1393 | } |
1394 | DataType &getValue() { return Value; } |
1395 | DataType getValue() const { return Value; } |
1396 | |
1397 | const OptionValue<DataType> &getDefault() const { return Default; } |
1398 | |
1399 | operator DataType() const { return getValue(); } |
1400 | |
1401 | // If the datatype is a pointer, support -> on it. |
1402 | DataType operator->() const { return Value; } |
1403 | }; |
1404 | |
1405 | //===----------------------------------------------------------------------===// |
1406 | // A scalar command line option. |
1407 | // |
1408 | template <class DataType, bool ExternalStorage = false, |
1409 | class ParserClass = parser<DataType>> |
1410 | class opt |
1411 | : public Option, |
1412 | public opt_storage<DataType, ExternalStorage, std::is_class_v<DataType>> { |
1413 | ParserClass Parser; |
1414 | |
1415 | bool handleOccurrence(unsigned pos, StringRef ArgName, |
1416 | StringRef Arg) override { |
1417 | typename ParserClass::parser_data_type Val = |
1418 | typename ParserClass::parser_data_type(); |
1419 | if (Parser.parse(*this, ArgName, Arg, Val)) |
1420 | return true; // Parse error! |
1421 | this->setValue(Val); |
1422 | this->setPosition(pos); |
1423 | Callback(Val); |
1424 | return false; |
1425 | } |
1426 | |
1427 | enum ValueExpected getValueExpectedFlagDefault() const override { |
1428 | return Parser.getValueExpectedFlagDefault(); |
1429 | } |
1430 | |
1431 | void (SmallVectorImpl<StringRef> &OptionNames) override { |
1432 | return Parser.getExtraOptionNames(OptionNames); |
1433 | } |
1434 | |
1435 | // Forward printing stuff to the parser... |
1436 | size_t getOptionWidth() const override { |
1437 | return Parser.getOptionWidth(*this); |
1438 | } |
1439 | |
1440 | void printOptionInfo(size_t GlobalWidth) const override { |
1441 | Parser.printOptionInfo(*this, GlobalWidth); |
1442 | } |
1443 | |
1444 | void printOptionValue(size_t GlobalWidth, bool Force) const override { |
1445 | if (Force || this->getDefault().compare(this->getValue())) { |
1446 | cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(), |
1447 | this->getDefault(), GlobalWidth); |
1448 | } |
1449 | } |
1450 | |
1451 | template <class T, class = std::enable_if_t<std::is_assignable_v<T &, T>>> |
1452 | void setDefaultImpl() { |
1453 | const OptionValue<DataType> &V = this->getDefault(); |
1454 | if (V.hasValue()) |
1455 | this->setValue(V.getValue()); |
1456 | else |
1457 | this->setValue(T()); |
1458 | } |
1459 | |
1460 | template <class T, class = std::enable_if_t<!std::is_assignable_v<T &, T>>> |
1461 | void setDefaultImpl(...) {} |
1462 | |
1463 | void setDefault() override { setDefaultImpl<DataType>(); } |
1464 | |
1465 | void done() { |
1466 | addArgument(); |
1467 | Parser.initialize(); |
1468 | } |
1469 | |
1470 | public: |
1471 | // Command line options should not be copyable |
1472 | opt(const opt &) = delete; |
1473 | opt &operator=(const opt &) = delete; |
1474 | |
1475 | // setInitialValue - Used by the cl::init modifier... |
1476 | void setInitialValue(const DataType &V) { this->setValue(V, true); } |
1477 | |
1478 | ParserClass &getParser() { return Parser; } |
1479 | |
1480 | template <class T> DataType &operator=(const T &Val) { |
1481 | this->setValue(Val); |
1482 | Callback(Val); |
1483 | return this->getValue(); |
1484 | } |
1485 | |
1486 | template <class... Mods> |
1487 | explicit opt(const Mods &... Ms) |
1488 | : Option(llvm::cl::Optional, NotHidden), Parser(*this) { |
1489 | apply(this, Ms...); |
1490 | done(); |
1491 | } |
1492 | |
1493 | void setCallback( |
1494 | std::function<void(const typename ParserClass::parser_data_type &)> CB) { |
1495 | Callback = CB; |
1496 | } |
1497 | |
1498 | std::function<void(const typename ParserClass::parser_data_type &)> Callback = |
1499 | [](const typename ParserClass::parser_data_type &) {}; |
1500 | }; |
1501 | |
1502 | extern template class opt<unsigned>; |
1503 | extern template class opt<int>; |
1504 | extern template class opt<std::string>; |
1505 | extern template class opt<char>; |
1506 | extern template class opt<bool>; |
1507 | |
1508 | //===----------------------------------------------------------------------===// |
1509 | // Default storage class definition: external storage. This implementation |
1510 | // assumes the user will specify a variable to store the data into with the |
1511 | // cl::location(x) modifier. |
1512 | // |
1513 | template <class DataType, class StorageClass> class list_storage { |
1514 | StorageClass *Location = nullptr; // Where to store the object... |
1515 | std::vector<OptionValue<DataType>> Default = |
1516 | std::vector<OptionValue<DataType>>(); |
1517 | bool DefaultAssigned = false; |
1518 | |
1519 | public: |
1520 | list_storage() = default; |
1521 | |
1522 | void clear() {} |
1523 | |
1524 | bool setLocation(Option &O, StorageClass &L) { |
1525 | if (Location) |
1526 | return O.error(Message: "cl::location(x) specified more than once!" ); |
1527 | Location = &L; |
1528 | return false; |
1529 | } |
1530 | |
1531 | template <class T> void addValue(const T &V, bool initial = false) { |
1532 | assert(Location != nullptr && |
1533 | "cl::location(...) not specified for a command " |
1534 | "line option with external storage!" ); |
1535 | Location->push_back(V); |
1536 | if (initial) |
1537 | Default.push_back(V); |
1538 | } |
1539 | |
1540 | const std::vector<OptionValue<DataType>> &getDefault() const { |
1541 | return Default; |
1542 | } |
1543 | |
1544 | void assignDefault() { DefaultAssigned = true; } |
1545 | void overwriteDefault() { DefaultAssigned = false; } |
1546 | bool isDefaultAssigned() { return DefaultAssigned; } |
1547 | }; |
1548 | |
1549 | // Define how to hold a class type object, such as a string. |
1550 | // Originally this code inherited from std::vector. In transitioning to a new |
1551 | // API for command line options we should change this. The new implementation |
1552 | // of this list_storage specialization implements the minimum subset of the |
1553 | // std::vector API required for all the current clients. |
1554 | // |
1555 | // FIXME: Reduce this API to a more narrow subset of std::vector |
1556 | // |
1557 | template <class DataType> class list_storage<DataType, bool> { |
1558 | std::vector<DataType> Storage; |
1559 | std::vector<OptionValue<DataType>> Default; |
1560 | bool DefaultAssigned = false; |
1561 | |
1562 | public: |
1563 | using iterator = typename std::vector<DataType>::iterator; |
1564 | |
1565 | iterator begin() { return Storage.begin(); } |
1566 | iterator end() { return Storage.end(); } |
1567 | |
1568 | using const_iterator = typename std::vector<DataType>::const_iterator; |
1569 | |
1570 | const_iterator begin() const { return Storage.begin(); } |
1571 | const_iterator end() const { return Storage.end(); } |
1572 | |
1573 | using size_type = typename std::vector<DataType>::size_type; |
1574 | |
1575 | size_type size() const { return Storage.size(); } |
1576 | |
1577 | bool empty() const { return Storage.empty(); } |
1578 | |
1579 | void push_back(const DataType &value) { Storage.push_back(value); } |
1580 | void push_back(DataType &&value) { Storage.push_back(value); } |
1581 | |
1582 | using reference = typename std::vector<DataType>::reference; |
1583 | using const_reference = typename std::vector<DataType>::const_reference; |
1584 | |
1585 | reference operator[](size_type pos) { return Storage[pos]; } |
1586 | const_reference operator[](size_type pos) const { return Storage[pos]; } |
1587 | |
1588 | void clear() { |
1589 | Storage.clear(); |
1590 | } |
1591 | |
1592 | iterator erase(const_iterator pos) { return Storage.erase(pos); } |
1593 | iterator erase(const_iterator first, const_iterator last) { |
1594 | return Storage.erase(first, last); |
1595 | } |
1596 | |
1597 | iterator erase(iterator pos) { return Storage.erase(pos); } |
1598 | iterator erase(iterator first, iterator last) { |
1599 | return Storage.erase(first, last); |
1600 | } |
1601 | |
1602 | iterator insert(const_iterator pos, const DataType &value) { |
1603 | return Storage.insert(pos, value); |
1604 | } |
1605 | iterator insert(const_iterator pos, DataType &&value) { |
1606 | return Storage.insert(pos, value); |
1607 | } |
1608 | |
1609 | iterator insert(iterator pos, const DataType &value) { |
1610 | return Storage.insert(pos, value); |
1611 | } |
1612 | iterator insert(iterator pos, DataType &&value) { |
1613 | return Storage.insert(pos, value); |
1614 | } |
1615 | |
1616 | reference front() { return Storage.front(); } |
1617 | const_reference front() const { return Storage.front(); } |
1618 | |
1619 | operator std::vector<DataType> &() { return Storage; } |
1620 | operator ArrayRef<DataType>() const { return Storage; } |
1621 | std::vector<DataType> *operator&() { return &Storage; } |
1622 | const std::vector<DataType> *operator&() const { return &Storage; } |
1623 | |
1624 | template <class T> void addValue(const T &V, bool initial = false) { |
1625 | Storage.push_back(V); |
1626 | if (initial) |
1627 | Default.push_back(OptionValue<DataType>(V)); |
1628 | } |
1629 | |
1630 | const std::vector<OptionValue<DataType>> &getDefault() const { |
1631 | return Default; |
1632 | } |
1633 | |
1634 | void assignDefault() { DefaultAssigned = true; } |
1635 | void overwriteDefault() { DefaultAssigned = false; } |
1636 | bool isDefaultAssigned() { return DefaultAssigned; } |
1637 | }; |
1638 | |
1639 | //===----------------------------------------------------------------------===// |
1640 | // A list of command line options. |
1641 | // |
1642 | template <class DataType, class StorageClass = bool, |
1643 | class ParserClass = parser<DataType>> |
1644 | class list : public Option, public list_storage<DataType, StorageClass> { |
1645 | std::vector<unsigned> Positions; |
1646 | ParserClass Parser; |
1647 | |
1648 | enum ValueExpected getValueExpectedFlagDefault() const override { |
1649 | return Parser.getValueExpectedFlagDefault(); |
1650 | } |
1651 | |
1652 | void (SmallVectorImpl<StringRef> &OptionNames) override { |
1653 | return Parser.getExtraOptionNames(OptionNames); |
1654 | } |
1655 | |
1656 | bool handleOccurrence(unsigned pos, StringRef ArgName, |
1657 | StringRef Arg) override { |
1658 | typename ParserClass::parser_data_type Val = |
1659 | typename ParserClass::parser_data_type(); |
1660 | if (list_storage<DataType, StorageClass>::isDefaultAssigned()) { |
1661 | clear(); |
1662 | list_storage<DataType, StorageClass>::overwriteDefault(); |
1663 | } |
1664 | if (Parser.parse(*this, ArgName, Arg, Val)) |
1665 | return true; // Parse Error! |
1666 | list_storage<DataType, StorageClass>::addValue(Val); |
1667 | setPosition(pos); |
1668 | Positions.push_back(x: pos); |
1669 | Callback(Val); |
1670 | return false; |
1671 | } |
1672 | |
1673 | // Forward printing stuff to the parser... |
1674 | size_t getOptionWidth() const override { |
1675 | return Parser.getOptionWidth(*this); |
1676 | } |
1677 | |
1678 | void printOptionInfo(size_t GlobalWidth) const override { |
1679 | Parser.printOptionInfo(*this, GlobalWidth); |
1680 | } |
1681 | |
1682 | // Unimplemented: list options don't currently store their default value. |
1683 | void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { |
1684 | } |
1685 | |
1686 | void setDefault() override { |
1687 | Positions.clear(); |
1688 | list_storage<DataType, StorageClass>::clear(); |
1689 | for (auto &Val : list_storage<DataType, StorageClass>::getDefault()) |
1690 | list_storage<DataType, StorageClass>::addValue(Val.getValue()); |
1691 | } |
1692 | |
1693 | void done() { |
1694 | addArgument(); |
1695 | Parser.initialize(); |
1696 | } |
1697 | |
1698 | public: |
1699 | // Command line options should not be copyable |
1700 | list(const list &) = delete; |
1701 | list &operator=(const list &) = delete; |
1702 | |
1703 | ParserClass &getParser() { return Parser; } |
1704 | |
1705 | unsigned getPosition(unsigned optnum) const { |
1706 | assert(optnum < this->size() && "Invalid option index" ); |
1707 | return Positions[optnum]; |
1708 | } |
1709 | |
1710 | void clear() { |
1711 | Positions.clear(); |
1712 | list_storage<DataType, StorageClass>::clear(); |
1713 | } |
1714 | |
1715 | // setInitialValues - Used by the cl::list_init modifier... |
1716 | void setInitialValues(ArrayRef<DataType> Vs) { |
1717 | assert(!(list_storage<DataType, StorageClass>::isDefaultAssigned()) && |
1718 | "Cannot have two default values" ); |
1719 | list_storage<DataType, StorageClass>::assignDefault(); |
1720 | for (auto &Val : Vs) |
1721 | list_storage<DataType, StorageClass>::addValue(Val, true); |
1722 | } |
1723 | |
1724 | void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); } |
1725 | |
1726 | template <class... Mods> |
1727 | explicit list(const Mods &... Ms) |
1728 | : Option(ZeroOrMore, NotHidden), Parser(*this) { |
1729 | apply(this, Ms...); |
1730 | done(); |
1731 | } |
1732 | |
1733 | void setCallback( |
1734 | std::function<void(const typename ParserClass::parser_data_type &)> CB) { |
1735 | Callback = CB; |
1736 | } |
1737 | |
1738 | std::function<void(const typename ParserClass::parser_data_type &)> Callback = |
1739 | [](const typename ParserClass::parser_data_type &) {}; |
1740 | }; |
1741 | |
1742 | // Modifier to set the number of additional values. |
1743 | struct multi_val { |
1744 | unsigned AdditionalVals; |
1745 | explicit multi_val(unsigned N) : AdditionalVals(N) {} |
1746 | |
1747 | template <typename D, typename S, typename P> |
1748 | void apply(list<D, S, P> &L) const { |
1749 | L.setNumAdditionalVals(AdditionalVals); |
1750 | } |
1751 | }; |
1752 | |
1753 | //===----------------------------------------------------------------------===// |
1754 | // Default storage class definition: external storage. This implementation |
1755 | // assumes the user will specify a variable to store the data into with the |
1756 | // cl::location(x) modifier. |
1757 | // |
1758 | template <class DataType, class StorageClass> class bits_storage { |
1759 | unsigned *Location = nullptr; // Where to store the bits... |
1760 | |
1761 | template <class T> static unsigned Bit(const T &V) { |
1762 | unsigned BitPos = static_cast<unsigned>(V); |
1763 | assert(BitPos < sizeof(unsigned) * CHAR_BIT && |
1764 | "enum exceeds width of bit vector!" ); |
1765 | return 1 << BitPos; |
1766 | } |
1767 | |
1768 | public: |
1769 | bits_storage() = default; |
1770 | |
1771 | bool setLocation(Option &O, unsigned &L) { |
1772 | if (Location) |
1773 | return O.error(Message: "cl::location(x) specified more than once!" ); |
1774 | Location = &L; |
1775 | return false; |
1776 | } |
1777 | |
1778 | template <class T> void addValue(const T &V) { |
1779 | assert(Location != nullptr && |
1780 | "cl::location(...) not specified for a command " |
1781 | "line option with external storage!" ); |
1782 | *Location |= Bit(V); |
1783 | } |
1784 | |
1785 | unsigned getBits() { return *Location; } |
1786 | |
1787 | void clear() { |
1788 | if (Location) |
1789 | *Location = 0; |
1790 | } |
1791 | |
1792 | template <class T> bool isSet(const T &V) { |
1793 | return (*Location & Bit(V)) != 0; |
1794 | } |
1795 | }; |
1796 | |
1797 | // Define how to hold bits. Since we can inherit from a class, we do so. |
1798 | // This makes us exactly compatible with the bits in all cases that it is used. |
1799 | // |
1800 | template <class DataType> class bits_storage<DataType, bool> { |
1801 | unsigned Bits{0}; // Where to store the bits... |
1802 | |
1803 | template <class T> static unsigned Bit(const T &V) { |
1804 | unsigned BitPos = static_cast<unsigned>(V); |
1805 | assert(BitPos < sizeof(unsigned) * CHAR_BIT && |
1806 | "enum exceeds width of bit vector!" ); |
1807 | return 1 << BitPos; |
1808 | } |
1809 | |
1810 | public: |
1811 | template <class T> void addValue(const T &V) { Bits |= Bit(V); } |
1812 | |
1813 | unsigned getBits() { return Bits; } |
1814 | |
1815 | void clear() { Bits = 0; } |
1816 | |
1817 | template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; } |
1818 | }; |
1819 | |
1820 | //===----------------------------------------------------------------------===// |
1821 | // A bit vector of command options. |
1822 | // |
1823 | template <class DataType, class Storage = bool, |
1824 | class ParserClass = parser<DataType>> |
1825 | class bits : public Option, public bits_storage<DataType, Storage> { |
1826 | std::vector<unsigned> Positions; |
1827 | ParserClass Parser; |
1828 | |
1829 | enum ValueExpected getValueExpectedFlagDefault() const override { |
1830 | return Parser.getValueExpectedFlagDefault(); |
1831 | } |
1832 | |
1833 | void (SmallVectorImpl<StringRef> &OptionNames) override { |
1834 | return Parser.getExtraOptionNames(OptionNames); |
1835 | } |
1836 | |
1837 | bool handleOccurrence(unsigned pos, StringRef ArgName, |
1838 | StringRef Arg) override { |
1839 | typename ParserClass::parser_data_type Val = |
1840 | typename ParserClass::parser_data_type(); |
1841 | if (Parser.parse(*this, ArgName, Arg, Val)) |
1842 | return true; // Parse Error! |
1843 | this->addValue(Val); |
1844 | setPosition(pos); |
1845 | Positions.push_back(x: pos); |
1846 | Callback(Val); |
1847 | return false; |
1848 | } |
1849 | |
1850 | // Forward printing stuff to the parser... |
1851 | size_t getOptionWidth() const override { |
1852 | return Parser.getOptionWidth(*this); |
1853 | } |
1854 | |
1855 | void printOptionInfo(size_t GlobalWidth) const override { |
1856 | Parser.printOptionInfo(*this, GlobalWidth); |
1857 | } |
1858 | |
1859 | // Unimplemented: bits options don't currently store their default values. |
1860 | void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { |
1861 | } |
1862 | |
1863 | void setDefault() override { bits_storage<DataType, Storage>::clear(); } |
1864 | |
1865 | void done() { |
1866 | addArgument(); |
1867 | Parser.initialize(); |
1868 | } |
1869 | |
1870 | public: |
1871 | // Command line options should not be copyable |
1872 | bits(const bits &) = delete; |
1873 | bits &operator=(const bits &) = delete; |
1874 | |
1875 | ParserClass &getParser() { return Parser; } |
1876 | |
1877 | unsigned getPosition(unsigned optnum) const { |
1878 | assert(optnum < this->size() && "Invalid option index" ); |
1879 | return Positions[optnum]; |
1880 | } |
1881 | |
1882 | template <class... Mods> |
1883 | explicit bits(const Mods &... Ms) |
1884 | : Option(ZeroOrMore, NotHidden), Parser(*this) { |
1885 | apply(this, Ms...); |
1886 | done(); |
1887 | } |
1888 | |
1889 | void setCallback( |
1890 | std::function<void(const typename ParserClass::parser_data_type &)> CB) { |
1891 | Callback = CB; |
1892 | } |
1893 | |
1894 | std::function<void(const typename ParserClass::parser_data_type &)> Callback = |
1895 | [](const typename ParserClass::parser_data_type &) {}; |
1896 | }; |
1897 | |
1898 | //===----------------------------------------------------------------------===// |
1899 | // Aliased command line option (alias this name to a preexisting name) |
1900 | // |
1901 | |
1902 | class alias : public Option { |
1903 | Option *AliasFor; |
1904 | |
1905 | bool handleOccurrence(unsigned pos, StringRef /*ArgName*/, |
1906 | StringRef Arg) override { |
1907 | return AliasFor->handleOccurrence(pos, ArgName: AliasFor->ArgStr, Arg); |
1908 | } |
1909 | |
1910 | bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value, |
1911 | bool MultiArg = false) override { |
1912 | return AliasFor->addOccurrence(pos, ArgName: AliasFor->ArgStr, Value, MultiArg); |
1913 | } |
1914 | |
1915 | // Handle printing stuff... |
1916 | size_t getOptionWidth() const override; |
1917 | void printOptionInfo(size_t GlobalWidth) const override; |
1918 | |
1919 | // Aliases do not need to print their values. |
1920 | void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { |
1921 | } |
1922 | |
1923 | void setDefault() override { AliasFor->setDefault(); } |
1924 | |
1925 | ValueExpected getValueExpectedFlagDefault() const override { |
1926 | return AliasFor->getValueExpectedFlag(); |
1927 | } |
1928 | |
1929 | void done() { |
1930 | if (!hasArgStr()) |
1931 | error(Message: "cl::alias must have argument name specified!" ); |
1932 | if (!AliasFor) |
1933 | error(Message: "cl::alias must have an cl::aliasopt(option) specified!" ); |
1934 | if (!Subs.empty()) |
1935 | error(Message: "cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!" ); |
1936 | Subs = AliasFor->Subs; |
1937 | Categories = AliasFor->Categories; |
1938 | addArgument(); |
1939 | } |
1940 | |
1941 | public: |
1942 | // Command line options should not be copyable |
1943 | alias(const alias &) = delete; |
1944 | alias &operator=(const alias &) = delete; |
1945 | |
1946 | void setAliasFor(Option &O) { |
1947 | if (AliasFor) |
1948 | error(Message: "cl::alias must only have one cl::aliasopt(...) specified!" ); |
1949 | AliasFor = &O; |
1950 | } |
1951 | |
1952 | template <class... Mods> |
1953 | explicit alias(const Mods &... Ms) |
1954 | : Option(Optional, Hidden), AliasFor(nullptr) { |
1955 | apply(this, Ms...); |
1956 | done(); |
1957 | } |
1958 | }; |
1959 | |
1960 | // Modifier to set the option an alias aliases. |
1961 | struct aliasopt { |
1962 | Option &Opt; |
1963 | |
1964 | explicit aliasopt(Option &O) : Opt(O) {} |
1965 | |
1966 | void apply(alias &A) const { A.setAliasFor(Opt); } |
1967 | }; |
1968 | |
1969 | // Provide additional help at the end of the normal help output. All occurrences |
1970 | // of cl::extrahelp will be accumulated and printed to stderr at the end of the |
1971 | // regular help, just before exit is called. |
1972 | struct { |
1973 | StringRef ; |
1974 | |
1975 | explicit (StringRef help); |
1976 | }; |
1977 | |
1978 | void PrintVersionMessage(); |
1979 | |
1980 | /// This function just prints the help message, exactly the same way as if the |
1981 | /// -help or -help-hidden option had been given on the command line. |
1982 | /// |
1983 | /// \param Hidden if true will print hidden options |
1984 | /// \param Categorized if true print options in categories |
1985 | void PrintHelpMessage(bool Hidden = false, bool Categorized = false); |
1986 | |
1987 | //===----------------------------------------------------------------------===// |
1988 | // Public interface for accessing registered options. |
1989 | // |
1990 | |
1991 | /// Use this to get a StringMap to all registered named options |
1992 | /// (e.g. -help). |
1993 | /// |
1994 | /// \return A reference to the StringMap used by the cl APIs to parse options. |
1995 | /// |
1996 | /// Access to unnamed arguments (i.e. positional) are not provided because |
1997 | /// it is expected that the client already has access to these. |
1998 | /// |
1999 | /// Typical usage: |
2000 | /// \code |
2001 | /// main(int argc,char* argv[]) { |
2002 | /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions(); |
2003 | /// assert(opts.count("help") == 1) |
2004 | /// opts["help"]->setDescription("Show alphabetical help information") |
2005 | /// // More code |
2006 | /// llvm::cl::ParseCommandLineOptions(argc,argv); |
2007 | /// //More code |
2008 | /// } |
2009 | /// \endcode |
2010 | /// |
2011 | /// This interface is useful for modifying options in libraries that are out of |
2012 | /// the control of the client. The options should be modified before calling |
2013 | /// llvm::cl::ParseCommandLineOptions(). |
2014 | /// |
2015 | /// Hopefully this API can be deprecated soon. Any situation where options need |
2016 | /// to be modified by tools or libraries should be handled by sane APIs rather |
2017 | /// than just handing around a global list. |
2018 | StringMap<Option *> & |
2019 | getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel()); |
2020 | |
2021 | /// Use this to get all registered SubCommands from the provided parser. |
2022 | /// |
2023 | /// \return A range of all SubCommand pointers registered with the parser. |
2024 | /// |
2025 | /// Typical usage: |
2026 | /// \code |
2027 | /// main(int argc, char* argv[]) { |
2028 | /// llvm::cl::ParseCommandLineOptions(argc, argv); |
2029 | /// for (auto* S : llvm::cl::getRegisteredSubcommands()) { |
2030 | /// if (*S) { |
2031 | /// std::cout << "Executing subcommand: " << S->getName() << std::endl; |
2032 | /// // Execute some function based on the name... |
2033 | /// } |
2034 | /// } |
2035 | /// } |
2036 | /// \endcode |
2037 | /// |
2038 | /// This interface is useful for defining subcommands in libraries and |
2039 | /// the dispatch from a single point (like in the main function). |
2040 | iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator> |
2041 | getRegisteredSubcommands(); |
2042 | |
2043 | //===----------------------------------------------------------------------===// |
2044 | // Standalone command line processing utilities. |
2045 | // |
2046 | |
2047 | /// Tokenizes a command line that can contain escapes and quotes. |
2048 | // |
2049 | /// The quoting rules match those used by GCC and other tools that use |
2050 | /// libiberty's buildargv() or expandargv() utilities, and do not match bash. |
2051 | /// They differ from buildargv() on treatment of backslashes that do not escape |
2052 | /// a special character to make it possible to accept most Windows file paths. |
2053 | /// |
2054 | /// \param [in] Source The string to be split on whitespace with quotes. |
2055 | /// \param [in] Saver Delegates back to the caller for saving parsed strings. |
2056 | /// \param [in] MarkEOLs true if tokenizing a response file and you want end of |
2057 | /// lines and end of the response file to be marked with a nullptr string. |
2058 | /// \param [out] NewArgv All parsed strings are appended to NewArgv. |
2059 | void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver, |
2060 | SmallVectorImpl<const char *> &NewArgv, |
2061 | bool MarkEOLs = false); |
2062 | |
2063 | /// Tokenizes a string of Windows command line arguments, which may contain |
2064 | /// quotes and escaped quotes. |
2065 | /// |
2066 | /// See MSDN docs for CommandLineToArgvW for information on the quoting rules. |
2067 | /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx |
2068 | /// |
2069 | /// For handling a full Windows command line including the executable name at |
2070 | /// the start, see TokenizeWindowsCommandLineFull below. |
2071 | /// |
2072 | /// \param [in] Source The string to be split on whitespace with quotes. |
2073 | /// \param [in] Saver Delegates back to the caller for saving parsed strings. |
2074 | /// \param [in] MarkEOLs true if tokenizing a response file and you want end of |
2075 | /// lines and end of the response file to be marked with a nullptr string. |
2076 | /// \param [out] NewArgv All parsed strings are appended to NewArgv. |
2077 | void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, |
2078 | SmallVectorImpl<const char *> &NewArgv, |
2079 | bool MarkEOLs = false); |
2080 | |
2081 | /// Tokenizes a Windows command line while attempting to avoid copies. If no |
2082 | /// quoting or escaping was used, this produces substrings of the original |
2083 | /// string. If a token requires unquoting, it will be allocated with the |
2084 | /// StringSaver. |
2085 | void TokenizeWindowsCommandLineNoCopy(StringRef Source, StringSaver &Saver, |
2086 | SmallVectorImpl<StringRef> &NewArgv); |
2087 | |
2088 | /// Tokenizes a Windows full command line, including command name at the start. |
2089 | /// |
2090 | /// This uses the same syntax rules as TokenizeWindowsCommandLine for all but |
2091 | /// the first token. But the first token is expected to be parsed as the |
2092 | /// executable file name in the way CreateProcess would do it, rather than the |
2093 | /// way the C library startup code would do it: CreateProcess does not consider |
2094 | /// that \ is ever an escape character (because " is not a valid filename char, |
2095 | /// hence there's never a need to escape it to be used literally). |
2096 | /// |
2097 | /// Parameters are the same as for TokenizeWindowsCommandLine. In particular, |
2098 | /// if you set MarkEOLs = true, then the first word of every line will be |
2099 | /// parsed using the special rules for command names, making this function |
2100 | /// suitable for parsing a file full of commands to execute. |
2101 | void TokenizeWindowsCommandLineFull(StringRef Source, StringSaver &Saver, |
2102 | SmallVectorImpl<const char *> &NewArgv, |
2103 | bool MarkEOLs = false); |
2104 | |
2105 | /// String tokenization function type. Should be compatible with either |
2106 | /// Windows or Unix command line tokenizers. |
2107 | using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver, |
2108 | SmallVectorImpl<const char *> &NewArgv, |
2109 | bool MarkEOLs); |
2110 | |
2111 | /// Tokenizes content of configuration file. |
2112 | /// |
2113 | /// \param [in] Source The string representing content of config file. |
2114 | /// \param [in] Saver Delegates back to the caller for saving parsed strings. |
2115 | /// \param [out] NewArgv All parsed strings are appended to NewArgv. |
2116 | /// \param [in] MarkEOLs Added for compatibility with TokenizerCallback. |
2117 | /// |
2118 | /// It works like TokenizeGNUCommandLine with ability to skip comment lines. |
2119 | /// |
2120 | void tokenizeConfigFile(StringRef Source, StringSaver &Saver, |
2121 | SmallVectorImpl<const char *> &NewArgv, |
2122 | bool MarkEOLs = false); |
2123 | |
2124 | /// Contains options that control response file expansion. |
2125 | class ExpansionContext { |
2126 | /// Provides persistent storage for parsed strings. |
2127 | StringSaver Saver; |
2128 | |
2129 | /// Tokenization strategy. Typically Unix or Windows. |
2130 | TokenizerCallback Tokenizer; |
2131 | |
2132 | /// File system used for all file access when running the expansion. |
2133 | vfs::FileSystem *FS; |
2134 | |
2135 | /// Path used to resolve relative rsp files. If empty, the file system |
2136 | /// current directory is used instead. |
2137 | StringRef CurrentDir; |
2138 | |
2139 | /// Directories used for search of config files. |
2140 | ArrayRef<StringRef> SearchDirs; |
2141 | |
2142 | /// True if names of nested response files must be resolved relative to |
2143 | /// including file. |
2144 | bool RelativeNames = false; |
2145 | |
2146 | /// If true, mark end of lines and the end of the response file with nullptrs |
2147 | /// in the Argv vector. |
2148 | bool MarkEOLs = false; |
2149 | |
2150 | /// If true, body of config file is expanded. |
2151 | bool InConfigFile = false; |
2152 | |
2153 | llvm::Error expandResponseFile(StringRef FName, |
2154 | SmallVectorImpl<const char *> &NewArgv); |
2155 | |
2156 | public: |
2157 | ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T); |
2158 | |
2159 | ExpansionContext &setMarkEOLs(bool X) { |
2160 | MarkEOLs = X; |
2161 | return *this; |
2162 | } |
2163 | |
2164 | ExpansionContext &setRelativeNames(bool X) { |
2165 | RelativeNames = X; |
2166 | return *this; |
2167 | } |
2168 | |
2169 | ExpansionContext &setCurrentDir(StringRef X) { |
2170 | CurrentDir = X; |
2171 | return *this; |
2172 | } |
2173 | |
2174 | ExpansionContext &setSearchDirs(ArrayRef<StringRef> X) { |
2175 | SearchDirs = X; |
2176 | return *this; |
2177 | } |
2178 | |
2179 | ExpansionContext &setVFS(vfs::FileSystem *X) { |
2180 | FS = X; |
2181 | return *this; |
2182 | } |
2183 | |
2184 | /// Looks for the specified configuration file. |
2185 | /// |
2186 | /// \param[in] FileName Name of the file to search for. |
2187 | /// \param[out] FilePath File absolute path, if it was found. |
2188 | /// \return True if file was found. |
2189 | /// |
2190 | /// If the specified file name contains a directory separator, it is searched |
2191 | /// for by its absolute path. Otherwise looks for file sequentially in |
2192 | /// directories specified by SearchDirs field. |
2193 | bool findConfigFile(StringRef FileName, SmallVectorImpl<char> &FilePath); |
2194 | |
2195 | /// Reads command line options from the given configuration file. |
2196 | /// |
2197 | /// \param [in] CfgFile Path to configuration file. |
2198 | /// \param [out] Argv Array to which the read options are added. |
2199 | /// \return true if the file was successfully read. |
2200 | /// |
2201 | /// It reads content of the specified file, tokenizes it and expands "@file" |
2202 | /// commands resolving file names in them relative to the directory where |
2203 | /// CfgFilename resides. It also expands "<CFGDIR>" to the base path of the |
2204 | /// current config file. |
2205 | Error readConfigFile(StringRef CfgFile, SmallVectorImpl<const char *> &Argv); |
2206 | |
2207 | /// Expands constructs "@file" in the provided array of arguments recursively. |
2208 | Error expandResponseFiles(SmallVectorImpl<const char *> &Argv); |
2209 | }; |
2210 | |
2211 | /// A convenience helper which concatenates the options specified by the |
2212 | /// environment variable EnvVar and command line options, then expands |
2213 | /// response files recursively. |
2214 | /// \return true if all @files were expanded successfully or there were none. |
2215 | bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, |
2216 | SmallVectorImpl<const char *> &NewArgv); |
2217 | |
2218 | /// A convenience helper which supports the typical use case of expansion |
2219 | /// function call. |
2220 | bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, |
2221 | SmallVectorImpl<const char *> &Argv); |
2222 | |
2223 | /// A convenience helper which concatenates the options specified by the |
2224 | /// environment variable EnvVar and command line options, then expands response |
2225 | /// files recursively. The tokenizer is a predefined GNU or Windows one. |
2226 | /// \return true if all @files were expanded successfully or there were none. |
2227 | bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, |
2228 | StringSaver &Saver, |
2229 | SmallVectorImpl<const char *> &NewArgv); |
2230 | |
2231 | /// Mark all options not part of this category as cl::ReallyHidden. |
2232 | /// |
2233 | /// \param Category the category of options to keep displaying |
2234 | /// |
2235 | /// Some tools (like clang-format) like to be able to hide all options that are |
2236 | /// not specific to the tool. This function allows a tool to specify a single |
2237 | /// option category to display in the -help output. |
2238 | void HideUnrelatedOptions(cl::OptionCategory &Category, |
2239 | SubCommand &Sub = SubCommand::getTopLevel()); |
2240 | |
2241 | /// Mark all options not part of the categories as cl::ReallyHidden. |
2242 | /// |
2243 | /// \param Categories the categories of options to keep displaying. |
2244 | /// |
2245 | /// Some tools (like clang-format) like to be able to hide all options that are |
2246 | /// not specific to the tool. This function allows a tool to specify a single |
2247 | /// option category to display in the -help output. |
2248 | void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories, |
2249 | SubCommand &Sub = SubCommand::getTopLevel()); |
2250 | |
2251 | /// Reset all command line options to a state that looks as if they have |
2252 | /// never appeared on the command line. This is useful for being able to parse |
2253 | /// a command line multiple times (especially useful for writing tests). |
2254 | void ResetAllOptionOccurrences(); |
2255 | |
2256 | /// Reset the command line parser back to its initial state. This |
2257 | /// removes |
2258 | /// all options, categories, and subcommands and returns the parser to a state |
2259 | /// where no options are supported. |
2260 | void ResetCommandLineParser(); |
2261 | |
2262 | /// Parses `Arg` into the option handler `Handler`. |
2263 | bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i); |
2264 | |
2265 | } // end namespace cl |
2266 | |
2267 | } // end namespace llvm |
2268 | |
2269 | #endif // LLVM_SUPPORT_COMMANDLINE_H |
2270 | |