Line data Source code
1 : //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This class implements a command line argument processor that is useful when
11 : // creating a tool. It provides a simple, minimalistic interface that is easily
12 : // extensible and supports nonlocal (library) command line options.
13 : //
14 : // Note that rather than trying to figure out what this code does, you should
15 : // read the library documentation located in docs/CommandLine.html or looks at
16 : // the many example usages in tools/*/*.cpp
17 : //
18 : //===----------------------------------------------------------------------===//
19 :
20 : #ifndef LLVM_SUPPORT_COMMANDLINE_H
21 : #define LLVM_SUPPORT_COMMANDLINE_H
22 :
23 : #include "llvm/ADT/ArrayRef.h"
24 : #include "llvm/ADT/STLExtras.h"
25 : #include "llvm/ADT/SmallPtrSet.h"
26 : #include "llvm/ADT/SmallVector.h"
27 : #include "llvm/ADT/StringMap.h"
28 : #include "llvm/ADT/StringRef.h"
29 : #include "llvm/ADT/Twine.h"
30 : #include "llvm/ADT/iterator_range.h"
31 : #include "llvm/Support/ErrorHandling.h"
32 : #include "llvm/Support/ManagedStatic.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 : class StringSaver;
46 : class raw_ostream;
47 :
48 : /// cl Namespace - This namespace contains all of the command line option
49 : /// processing machinery. It is intentionally a short name to make qualified
50 : /// usage concise.
51 : namespace cl {
52 :
53 : //===----------------------------------------------------------------------===//
54 : // ParseCommandLineOptions - Command line option processing entry point.
55 : //
56 : // Returns true on success. Otherwise, this will print the error message to
57 : // stderr and exit if \p Errs is not set (nullptr by default), or print the
58 : // error message to \p Errs and return false if \p Errs is provided.
59 : bool ParseCommandLineOptions(int argc, const char *const *argv,
60 : StringRef Overview = "",
61 : raw_ostream *Errs = nullptr);
62 :
63 : //===----------------------------------------------------------------------===//
64 : // ParseEnvironmentOptions - Environment variable option processing alternate
65 : // entry point.
66 : //
67 : void ParseEnvironmentOptions(const char *progName, const char *envvar,
68 : const char *Overview = "");
69 :
70 : // Function pointer type for printing version information.
71 : using VersionPrinterTy = std::function<void(raw_ostream &)>;
72 :
73 : ///===---------------------------------------------------------------------===//
74 : /// SetVersionPrinter - Override the default (LLVM specific) version printer
75 : /// used to print out the version when --version is given
76 : /// on the command line. This allows other systems using the
77 : /// CommandLine utilities to print their own version string.
78 : void SetVersionPrinter(VersionPrinterTy func);
79 :
80 : ///===---------------------------------------------------------------------===//
81 : /// AddExtraVersionPrinter - Add an extra printer to use in addition to the
82 : /// default one. This can be called multiple times,
83 : /// and each time it adds a new function to the list
84 : /// which will be called after the basic LLVM version
85 : /// printing is complete. Each can then add additional
86 : /// information specific to the tool.
87 : void AddExtraVersionPrinter(VersionPrinterTy func);
88 :
89 : // PrintOptionValues - Print option values.
90 : // With -print-options print the difference between option values and defaults.
91 : // With -print-all-options print all option values.
92 : // (Currently not perfect, but best-effort.)
93 : void PrintOptionValues();
94 :
95 : // Forward declaration - AddLiteralOption needs to be up here to make gcc happy.
96 : class Option;
97 :
98 : /// Adds a new option for parsing and provides the option it refers to.
99 : ///
100 : /// \param O pointer to the option
101 : /// \param Name the string name for the option to handle during parsing
102 : ///
103 : /// Literal options are used by some parsers to register special option values.
104 : /// This is how the PassNameParser registers pass names for opt.
105 : void AddLiteralOption(Option &O, StringRef Name);
106 :
107 : //===----------------------------------------------------------------------===//
108 : // Flags permitted to be passed to command line arguments
109 : //
110 :
111 : enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
112 : Optional = 0x00, // Zero or One occurrence
113 : ZeroOrMore = 0x01, // Zero or more occurrences allowed
114 : Required = 0x02, // One occurrence required
115 : OneOrMore = 0x03, // One or more occurrences required
116 :
117 : // ConsumeAfter - Indicates that this option is fed anything that follows the
118 : // last positional argument required by the application (it is an error if
119 : // there are zero positional arguments, and a ConsumeAfter option is used).
120 : // Thus, for example, all arguments to LLI are processed until a filename is
121 : // found. Once a filename is found, all of the succeeding arguments are
122 : // passed, unprocessed, to the ConsumeAfter option.
123 : //
124 : ConsumeAfter = 0x04
125 : };
126 :
127 : enum ValueExpected { // Is a value required for the option?
128 : // zero reserved for the unspecified value
129 : ValueOptional = 0x01, // The value can appear... or not
130 : ValueRequired = 0x02, // The value is required to appear!
131 : ValueDisallowed = 0x03 // A value may not be specified (for flags)
132 : };
133 :
134 : enum OptionHidden { // Control whether -help shows this option
135 : NotHidden = 0x00, // Option included in -help & -help-hidden
136 : Hidden = 0x01, // -help doesn't, but -help-hidden does
137 : ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
138 : };
139 :
140 : // Formatting flags - This controls special features that the option might have
141 : // that cause it to be parsed differently...
142 : //
143 : // Prefix - This option allows arguments that are otherwise unrecognized to be
144 : // matched by options that are a prefix of the actual value. This is useful for
145 : // cases like a linker, where options are typically of the form '-lfoo' or
146 : // '-L../../include' where -l or -L are the actual flags. When prefix is
147 : // enabled, and used, the value for the flag comes from the suffix of the
148 : // argument.
149 : //
150 : // Grouping - With this option enabled, multiple letter options are allowed to
151 : // bunch together with only a single hyphen for the whole group. This allows
152 : // emulation of the behavior that ls uses for example: ls -la === ls -l -a
153 : //
154 :
155 : enum FormattingFlags {
156 : NormalFormatting = 0x00, // Nothing special
157 : Positional = 0x01, // Is a positional argument, no '-' required
158 : Prefix = 0x02, // Can this option directly prefix its value?
159 : Grouping = 0x03 // Can this option group with other options?
160 : };
161 :
162 : enum MiscFlags { // Miscellaneous flags to adjust argument
163 : CommaSeparated = 0x01, // Should this cl::list split between commas?
164 : PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
165 : Sink = 0x04 // Should this cl::list eat all unknown options?
166 : };
167 :
168 : //===----------------------------------------------------------------------===//
169 : // Option Category class
170 : //
171 : class OptionCategory {
172 : private:
173 : StringRef const Name;
174 : StringRef const Description;
175 :
176 : void registerCategory();
177 :
178 : public:
179 : OptionCategory(StringRef const Name,
180 : StringRef const Description = "")
181 228 : : Name(Name), Description(Description) {
182 228 : registerCategory();
183 : }
184 :
185 0 : StringRef getName() const { return Name; }
186 0 : StringRef getDescription() const { return Description; }
187 : };
188 :
189 : // The general Option Category (used as default category).
190 : extern OptionCategory GeneralCategory;
191 :
192 : //===----------------------------------------------------------------------===//
193 : // SubCommand class
194 : //
195 : class SubCommand {
196 : private:
197 : StringRef Name;
198 : StringRef Description;
199 :
200 : protected:
201 : void registerSubCommand();
202 : void unregisterSubCommand();
203 :
204 : public:
205 : SubCommand(StringRef Name, StringRef Description = "")
206 46 : : Name(Name), Description(Description) {
207 46 : registerSubCommand();
208 : }
209 : SubCommand() = default;
210 :
211 : void reset();
212 :
213 : explicit operator bool() const;
214 :
215 0 : StringRef getName() const { return Name; }
216 0 : StringRef getDescription() const { return Description; }
217 :
218 : SmallVector<Option *, 4> PositionalOpts;
219 : SmallVector<Option *, 4> SinkOpts;
220 : StringMap<Option *> OptionsMap;
221 :
222 : Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists.
223 : };
224 :
225 : // A special subcommand representing no subcommand
226 : extern ManagedStatic<SubCommand> TopLevelSubCommand;
227 :
228 : // A special subcommand that can be used to put an option into all subcommands.
229 : extern ManagedStatic<SubCommand> AllSubCommands;
230 :
231 : //===----------------------------------------------------------------------===//
232 : // Option Base class
233 : //
234 : class Option {
235 : friend class alias;
236 :
237 : // handleOccurrences - Overriden by subclasses to handle the value passed into
238 : // an argument. Should return true if there was an error processing the
239 : // argument and the program should exit.
240 : //
241 : virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
242 : StringRef Arg) = 0;
243 :
244 0 : virtual enum ValueExpected getValueExpectedFlagDefault() const {
245 0 : return ValueOptional;
246 : }
247 :
248 : // Out of line virtual function to provide home for the class.
249 : virtual void anchor();
250 :
251 : int NumOccurrences = 0; // The number of times specified
252 : // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
253 : // problems with signed enums in bitfields.
254 : unsigned Occurrences : 3; // enum NumOccurrencesFlag
255 : // not using the enum type for 'Value' because zero is an implementation
256 : // detail representing the non-value
257 : unsigned Value : 2;
258 : unsigned HiddenFlag : 2; // enum OptionHidden
259 : unsigned Formatting : 2; // enum FormattingFlags
260 : unsigned Misc : 3;
261 : unsigned Position = 0; // Position of last occurrence of the option
262 : unsigned AdditionalVals = 0; // Greater than 0 for multi-valued option.
263 :
264 : public:
265 : StringRef ArgStr; // The argument string itself (ex: "help", "o")
266 : StringRef HelpStr; // The descriptive text message for -help
267 : StringRef ValueStr; // String describing what the value of this option is
268 : OptionCategory *Category; // The Category this option belongs to
269 : SmallPtrSet<SubCommand *, 4> Subs; // The subcommands this option belongs to.
270 : bool FullyInitialized = false; // Has addArgument been called?
271 :
272 : inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
273 263358582 : return (enum NumOccurrencesFlag)Occurrences;
274 : }
275 :
276 : inline enum ValueExpected getValueExpectedFlag() const {
277 299038 : return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
278 : }
279 :
280 : inline enum OptionHidden getOptionHiddenFlag() const {
281 14007 : return (enum OptionHidden)HiddenFlag;
282 : }
283 :
284 : inline enum FormattingFlags getFormattingFlag() const {
285 155702280 : return (enum FormattingFlags)Formatting;
286 : }
287 :
288 155663545 : inline unsigned getMiscFlags() const { return Misc; }
289 0 : inline unsigned getPosition() const { return Position; }
290 0 : inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
291 :
292 : // hasArgStr - Return true if the argstr != ""
293 : bool hasArgStr() const { return !ArgStr.empty(); }
294 : bool isPositional() const { return getFormattingFlag() == cl::Positional; }
295 : bool isSink() const { return getMiscFlags() & cl::Sink; }
296 :
297 : bool isConsumeAfter() const {
298 : return getNumOccurrencesFlag() == cl::ConsumeAfter;
299 : }
300 :
301 : bool isInAllSubCommands() const {
302 17 : return any_of(Subs, [](const SubCommand *SC) {
303 17 : return SC == &*AllSubCommands;
304 : });
305 : }
306 :
307 : //-------------------------------------------------------------------------===
308 : // Accessor functions set by OptionModifiers
309 : //
310 : void setArgStr(StringRef S);
311 141394534 : void setDescription(StringRef S) { HelpStr = S; }
312 4308318 : void setValueStr(StringRef S) { ValueStr = S; }
313 22440469 : void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
314 2061076 : void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
315 142254310 : void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
316 330381 : void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
317 1034722 : void setMiscFlag(enum MiscFlags M) { Misc |= M; }
318 286846 : void setPosition(unsigned pos) { Position = pos; }
319 9235164 : void setCategory(OptionCategory &C) { Category = &C; }
320 2235726 : void addSubCommand(SubCommand &S) { Subs.insert(&S); }
321 :
322 : protected:
323 : explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
324 : enum OptionHidden Hidden)
325 154387091 : : Occurrences(OccurrencesFlag), Value(0), HiddenFlag(Hidden),
326 161312483 : Formatting(NormalFormatting), Misc(0), Category(&GeneralCategory) {}
327 :
328 : inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
329 :
330 : public:
331 1511 : virtual ~Option() = default;
332 :
333 : // addArgument - Register this argument with the commandline system.
334 : //
335 : void addArgument();
336 :
337 : /// Unregisters this option from the CommandLine system.
338 : ///
339 : /// This option must have been the last option registered.
340 : /// For testing purposes only.
341 : void removeArgument();
342 :
343 : // Return the width of the option tag for printing...
344 : virtual size_t getOptionWidth() const = 0;
345 :
346 : // printOptionInfo - Print out information about this option. The
347 : // to-be-maintained width is specified.
348 : //
349 : virtual void printOptionInfo(size_t GlobalWidth) const = 0;
350 :
351 : virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
352 :
353 : virtual void setDefault() = 0;
354 :
355 : static void printHelpStr(StringRef HelpStr, size_t Indent,
356 : size_t FirstLineIndentedBy);
357 :
358 15 : virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
359 :
360 : // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
361 : //
362 : virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
363 : bool MultiArg = false);
364 :
365 : // Prints option name followed by message. Always returns true.
366 : bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs());
367 : bool error(const Twine &Message, raw_ostream &Errs) {
368 2 : return error(Message, StringRef(), Errs);
369 : }
370 :
371 0 : inline int getNumOccurrences() const { return NumOccurrences; }
372 832853 : inline void reset() { NumOccurrences = 0; }
373 : };
374 :
375 : //===----------------------------------------------------------------------===//
376 : // Command line option modifiers that can be used to modify the behavior of
377 : // command line option parsers...
378 : //
379 :
380 : // desc - Modifier to set the description shown in the -help output...
381 : struct desc {
382 : StringRef Desc;
383 :
384 35651 : desc(StringRef Str) : Desc(Str) {}
385 :
386 : void apply(Option &O) const { O.setDescription(Desc); }
387 : };
388 :
389 : // value_desc - Modifier to set the value description shown in the -help
390 : // output...
391 : struct value_desc {
392 : StringRef Desc;
393 :
394 486 : value_desc(StringRef Str) : Desc(Str) {}
395 :
396 : void apply(Option &O) const { O.setValueStr(Desc); }
397 : };
398 :
399 : // init - Specify a default (initial) value for the command line argument, if
400 : // the default constructor for the argument type does not give you what you
401 : // want. This is only valid on "opt" arguments, not on "list" arguments.
402 : //
403 : template <class Ty> struct initializer {
404 : const Ty &Init;
405 : initializer(const Ty &Val) : Init(Val) {}
406 :
407 43809210 : template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
408 : };
409 :
410 : template <class Ty> initializer<Ty> init(const Ty &Val) {
411 : return initializer<Ty>(Val);
412 : }
413 :
414 : // location - Allow the user to specify which external variable they want to
415 : // store the results of the command line argument processing into, if they don't
416 : // want to store it in the option itself.
417 : //
418 : template <class Ty> struct LocationClass {
419 : Ty &Loc;
420 :
421 : LocationClass(Ty &L) : Loc(L) {}
422 :
423 1133978 : template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
424 : };
425 :
426 : template <class Ty> LocationClass<Ty> location(Ty &L) {
427 : return LocationClass<Ty>(L);
428 : }
429 :
430 : // cat - Specifiy the Option category for the command line argument to belong
431 : // to.
432 : struct cat {
433 : OptionCategory &Category;
434 :
435 2887 : cat(OptionCategory &c) : Category(c) {}
436 :
437 0 : template <class Opt> void apply(Opt &O) const { O.setCategory(Category); }
438 : };
439 :
440 : // sub - Specify the subcommand that this option belongs to.
441 : struct sub {
442 : SubCommand ⋐
443 :
444 3350 : sub(SubCommand &S) : Sub(S) {}
445 :
446 0 : template <class Opt> void apply(Opt &O) const { O.addSubCommand(Sub); }
447 : };
448 :
449 : //===----------------------------------------------------------------------===//
450 : // OptionValue class
451 :
452 : // Support value comparison outside the template.
453 0 : struct GenericOptionValue {
454 : virtual bool compare(const GenericOptionValue &V) const = 0;
455 :
456 : protected:
457 : GenericOptionValue() = default;
458 141238449 : GenericOptionValue(const GenericOptionValue&) = default;
459 : GenericOptionValue &operator=(const GenericOptionValue &) = default;
460 : ~GenericOptionValue() = default;
461 :
462 : private:
463 : virtual void anchor();
464 : };
465 :
466 : template <class DataType> struct OptionValue;
467 :
468 : // The default value safely does nothing. Option value printing is only
469 : // best-effort.
470 : template <class DataType, bool isClass>
471 : struct OptionValueBase : public GenericOptionValue {
472 : // Temporary storage for argument passing.
473 : using WrapperType = OptionValue<DataType>;
474 :
475 0 : bool hasValue() const { return false; }
476 :
477 0 : const DataType &getValue() const { llvm_unreachable("no default value"); }
478 :
479 : // Some options may take their value from a different data type.
480 0 : template <class DT> void setValue(const DT & /*V*/) {}
481 :
482 0 : bool compare(const DataType & /*V*/) const { return false; }
483 :
484 0 : bool compare(const GenericOptionValue & /*V*/) const override {
485 0 : return false;
486 : }
487 0 :
488 0 : protected:
489 : ~OptionValueBase() = default;
490 0 : };
491 0 :
492 : // Simple copy of the option value.
493 0 : template <class DataType> class OptionValueCopy : public GenericOptionValue {
494 0 : DataType Value;
495 : bool Valid = false;
496 :
497 : protected:
498 280772738 : OptionValueCopy(const OptionValueCopy&) = default;
499 : OptionValueCopy &operator=(const OptionValueCopy &) = default;
500 : ~OptionValueCopy() = default;
501 :
502 : public:
503 7895233 : OptionValueCopy() = default;
504 1704160 :
505 0 : bool hasValue() const { return Valid; }
506 :
507 0 : const DataType &getValue() const {
508 : assert(Valid && "invalid option value");
509 234322 : return Value;
510 : }
511 0 :
512 0 : void setValue(const DataType &V) {
513 268460514 : Valid = true;
514 229353420 : Value = V;
515 0 : }
516 0 :
517 0 : bool compare(const DataType &V) const { return Valid && (Value != V); }
518 1 :
519 1917180 : bool compare(const GenericOptionValue &V) const override {
520 0 : const OptionValueCopy<DataType> &VC =
521 0 : static_cast<const OptionValueCopy<DataType> &>(V);
522 724884 : if (!VC.hasValue())
523 0 : return false;
524 0 : return compare(VC.getValue());
525 0 : }
526 0 : };
527 0 :
528 0 : // Non-class option values.
529 0 : template <class DataType>
530 0 : struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
531 0 : using WrapperType = DataType;
532 0 :
533 0 : protected:
534 0 : OptionValueBase() = default;
535 0 : OptionValueBase(const OptionValueBase&) = default;
536 0 : OptionValueBase &operator=(const OptionValueBase &) = default;
537 0 : ~OptionValueBase() = default;
538 0 : };
539 0 :
540 0 : // Top-level option class.
541 0 : template <class DataType>
542 123150883 : struct OptionValue final
543 0 : : OptionValueBase<DataType, std::is_class<DataType>::value> {
544 1894301 : OptionValue() = default;
545 0 :
546 49908049 : OptionValue(const DataType &V) { this->setValue(V); }
547 0 :
548 0 : // Some options may take their value from a different data type.
549 0 : template <class DT> OptionValue<DataType> &operator=(const DT &V) {
550 0 : this->setValue(V);
551 0 : return *this;
552 0 : }
553 0 : };
554 0 :
555 0 : // Other safe-to-copy-by-value common option types.
556 21320 : enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
557 0 : template <>
558 0 : struct OptionValue<cl::boolOrDefault> final
559 0 : : OptionValueCopy<cl::boolOrDefault> {
560 1288029 : using WrapperType = cl::boolOrDefault;
561 0 :
562 0 : OptionValue() = default;
563 0 :
564 6791264 : OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
565 0 :
566 856380 : OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) {
567 0 : setValue(V);
568 47170671 : return *this;
569 0 : }
570 0 :
571 728045 : private:
572 0 : void anchor() override;
573 411796 : };
574 0 :
575 6653673 : template <>
576 0 : struct OptionValue<std::string> final : OptionValueCopy<std::string> {
577 0 : using WrapperType = StringRef;
578 0 :
579 15397864 : OptionValue() = default;
580 0 :
581 0 : OptionValue(const std::string &V) { this->setValue(V); }
582 806452 :
583 0 : OptionValue<std::string> &operator=(const std::string &V) {
584 78885 : setValue(V);
585 0 : return *this;
586 8744983 : }
587 0 :
588 0 : private:
589 0 : void anchor() override;
590 2914815 : };
591 0 :
592 0 : //===----------------------------------------------------------------------===//
593 1569122 : // Enum valued command line option
594 0 : //
595 0 :
596 0 : // This represents a single enum value, using "int" as the underlying type.
597 8318720 : struct OptionEnumValue {
598 0 : StringRef Name;
599 0 : int Value;
600 0 : StringRef Description;
601 0 : };
602 0 :
603 0 : #define clEnumVal(ENUMVAL, DESC) \
604 1931 : llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC }
605 0 : #define clEnumValN(ENUMVAL, FLAGNAME, DESC) \
606 0 : llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC }
607 0 :
608 1448546 : // values - For custom data types, allow specifying a group of values together
609 0 : // as the values that go into the mapping that the option handler uses.
610 58828 : //
611 906106 : class ValuesClass {
612 1294216 : // Use a vector instead of a map, because the lists should be short,
613 362442 : // the overhead is less, and most importantly, it keeps them in the order
614 0 : // inserted so we can print our option out nicely.
615 1367232 : SmallVector<OptionEnumValue, 4> Values;
616 522012 :
617 0 : public:
618 0 : ValuesClass(std::initializer_list<OptionEnumValue> Options)
619 3760057 : : Values(Options) {}
620 0 :
621 856381 : template <class Opt> void apply(Opt &O) const {
622 3311587 : for (auto Value : Values)
623 2455206 : O.getParser().addLiteralOption(Value.Name, Value.Value,
624 0 : Value.Description);
625 856381 : }
626 97176 : };
627 867645 :
628 0 : /// Helper to build a ValuesClass by forwarding a variable number of arguments
629 0 : /// as an initializer list to the ValuesClass constructor.
630 830958 : template <typename... OptsTy> ValuesClass values(OptsTy... Options) {
631 629435 : return ValuesClass({Options...});
632 0 : }
633 0 :
634 0 : //===----------------------------------------------------------------------===//
635 363 : // parser class - Parameterizable parser for different data types. By default,
636 11656 : // known data types (string, int, bool) have specialized parsers, that do what
637 11733 : // you would expect. The default parser, used for data types that are not
638 113 : // built-in, uses a mapping table to map specific options to values, which is
639 352 : // used, among other things, to handle enum types.
640 0 :
641 663 : //--------------------------------------------------
642 684300 : // generic_parser_base - This class holds all the non-generic code that we do
643 1429094 : // not need replicated for every instance of the generic parser. This also
644 7080998 : // allows us to put stuff into CommandLine.cpp
645 5651904 : //
646 0 : class generic_parser_base {
647 1429094 : protected:
648 0 : class GenericOptionInfo {
649 0 : public:
650 125823 : GenericOptionInfo(StringRef name, StringRef helpStr)
651 512060 : : Name(name), HelpStr(helpStr) {}
652 1584061 : StringRef Name;
653 1654071 : StringRef HelpStr;
654 125823 : };
655 0 :
656 0 : public:
657 1209235 : generic_parser_base(Option &O) : Owner(O) {}
658 113936 :
659 586555 : virtual ~generic_parser_base() = default;
660 466592 : // Base class should have virtual-destructor
661 0 :
662 228597 : // getNumOptions - Virtual function implemented by generic subclass to
663 474432 : // indicate how many entries are in Values.
664 344336 : //
665 0 : virtual unsigned getNumOptions() const = 0;
666 114661 :
667 114289 : // getOption - Return option name N.
668 685028 : virtual StringRef getOption(unsigned N) const = 0;
669 232681 :
670 1037814 : // getDescription - Return description N
671 920852 : virtual StringRef getDescription(unsigned N) const = 0;
672 114308 :
673 233439 : // Return the width of the option tag for printing...
674 1953 : virtual size_t getOptionWidth(const Option &O) const;
675 479 :
676 499 : virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
677 687451 :
678 3033417 : // printOptionInfo - Print out information about this option. The
679 3775112 : // to-be-maintained width is specified.
680 219 : //
681 688262 : virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
682 173554 :
683 764147 : void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
684 746963 : const GenericOptionValue &Default,
685 1027192 : size_t GlobalWidth) const;
686 1097484 :
687 173159 : // printOptionDiff - print the value of an option and it's default.
688 974929 : //
689 687606 : // Template definition ensures that the option and default have the same
690 570400 : // DataType (via the same AnyOptionValue).
691 515190 : template <class AnyOptionValue>
692 341808 : void printOptionDiff(const Option &O, const AnyOptionValue &V,
693 1481312 : const AnyOptionValue &Default,
694 1253524 : size_t GlobalWidth) const {
695 570477 : printGenericOptionDiff(O, V, Default, GlobalWidth);
696 857322 : }
697 59201 :
698 342055 : void initialize() {}
699 333 :
700 1239 : void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
701 200756 : // If there has been no argstr specified, that means that we need to add an
702 199804 : // argument for every possible option. This ensures that our options are
703 803036 : // vectored to us.
704 342387 : if (!Owner.hasArgStr())
705 356875 : for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
706 114625 : OptionNames.push_back(getOption(i));
707 1487632 : }
708 1086018 :
709 820206 : enum ValueExpected getValueExpectedFlagDefault() const {
710 342027 : // If there is an ArgStr specified, then we are of the form:
711 1319732 : //
712 856380 : // -opt=O2 or -opt O2 or -optO2
713 235385 : //
714 411615 : // In which case, the value is required. Otherwise if an arg str has not
715 114301 : // been specified, we are of the form:
716 514913 : //
717 401260 : // -O2 or O2 or -la (where -l and -a are separate options)
718 125899 : //
719 172764 : // If this is the case, we cannot allow a value.
720 967240 : //
721 628797 : if (Owner.hasArgStr())
722 557428 : return ValueRequired;
723 353182 : else
724 1419499 : return ValueDisallowed;
725 32431 : }
726 188705 :
727 156217 : // findOption - Return the option number corresponding to the specified
728 176634 : // argument string. If the option is not found, getNumOptions() is returned.
729 378073 : //
730 236381 : unsigned findOption(StringRef Name);
731 91372 :
732 197214 : protected:
733 349409 : Option &Owner;
734 754262 : };
735 122340 :
736 453375 : // Default parser implementation - This implementation depends on having a
737 24427 : // mapping of recognized options to values of some sort. In addition to this,
738 272 : // each entry in the mapping also tracks a help message that is printed with the
739 8098 : // command line option for -help. Because this is a simple mapping parser, the
740 29799 : // data type can be any unsupported type.
741 360972 : //
742 591606 : template <class DataType> class parser : public generic_parser_base {
743 976 : protected:
744 67882418 : class OptionInfo : public GenericOptionInfo {
745 8169 : public:
746 40490 : OptionInfo(StringRef name, DataType v, StringRef helpStr)
747 32392 : : GenericOptionInfo(name, helpStr), V(v) {}
748 32 :
749 8098 : OptionValue<DataType> V;
750 401 : };
751 39 : SmallVector<OptionInfo, 8> Values;
752 32 :
753 0 : public:
754 2236101 : parser(Option &O) : generic_parser_base(O) {}
755 56702 :
756 0 : using parser_data_type = DataType;
757 8098 :
758 18114 : // Implement virtual functions needed by generic_parser_base
759 105642764 : unsigned getNumOptions() const override { return unsigned(Values.size()); }
760 17339050936 : StringRef getOption(unsigned N) const override { return Values[N].Name; }
761 49347 : StringRef getDescription(unsigned N) const override {
762 344242 : return Values[N].HelpStr;
763 5733 : }
764 4851 :
765 0 : // getOptionValue - Return the value of option name N.
766 912376 : const GenericOptionValue &getOptionValue(unsigned N) const override {
767 147 : return Values[N].V;
768 295726 : }
769 820 :
770 12 : // parse - Return true on error.
771 330 : bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
772 330 : StringRef ArgVal;
773 1159 : if (Owner.hasArgStr())
774 710 : ArgVal = Arg;
775 2 : else
776 2858396 : ArgVal = ArgName;
777 400 :
778 2110 : for (size_t i = 0, e = Values.size(); i != e; ++i)
779 1617 : if (Values[i].Name == ArgVal) {
780 183 : V = Values[i].V.getValue();
781 500 : return false;
782 861 : }
783 252914 :
784 681 : return O.error("Cannot find option named '" + ArgVal + "'!");
785 911754 : }
786 413 :
787 147076 : /// addLiteralOption - Add an entry to the mapping table.
788 1180 : ///
789 1065 : template <class DT>
790 272 : void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
791 228031 : assert(findOption(Name) == Values.size() && "Option already exists!");
792 132299 : OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
793 1824417 : Values.push_back(X);
794 1597 : AddLiteralOption(Owner, Name);
795 343769 : }
796 936 :
797 0 : /// removeLiteralOption - Remove the specified option.
798 105 : ///
799 1 : void removeLiteralOption(StringRef Name) {
800 1387913 : unsigned N = findOption(Name);
801 15041231 : assert(N != Values.size() && "Option not found!");
802 464863 : Values.erase(Values.begin() + N);
803 2981 : }
804 202 : };
805 277 :
806 630 : //--------------------------------------------------
807 512 : // basic_parser - Super class of parsers to provide boilerplate code
808 300 : //
809 253 : class basic_parser_impl { // non-template implementation of basic_parser<t>
810 1135 : public:
811 52154652 : basic_parser_impl(Option &) {}
812 342 :
813 220 : enum ValueExpected getValueExpectedFlagDefault() const {
814 250 : return ValueRequired;
815 195 : }
816 151 :
817 244 : void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
818 77 :
819 342066 : void initialize() {}
820 235507 :
821 160 : // Return the width of the option tag for printing...
822 95 : size_t getOptionWidth(const Option &O) const;
823 21 :
824 24 : // printOptionInfo - Print out information about this option. The
825 2482101 : // to-be-maintained width is specified.
826 385 : //
827 758 : void printOptionInfo(const Option &O, size_t GlobalWidth) const;
828 305 :
829 89 : // printOptionNoValue - Print a placeholder for options that don't yet support
830 4228 : // printOptionDiff().
831 1139360 : void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
832 114059 :
833 46381202 : // getValueName - Overload in subclass to provide a better default value.
834 42461 : virtual StringRef getValueName() const { return "value"; }
835 413107 :
836 259250 : // An out-of-line virtual method to provide a 'home' for this class.
837 812 : virtual void anchor();
838 39960 :
839 3669 : protected:
840 4075505 : ~basic_parser_impl() = default;
841 1949775 :
842 118415 : // A helper for basic_parser::printOptionDiff.
843 3059100 : void printOptionName(const Option &O, size_t GlobalWidth) const;
844 6016111 : };
845 20028 :
846 140946 : // basic_parser - The real basic parser is just a template wrapper that provides
847 17752 : // a typedef for the provided data type.
848 2212100 : //
849 37706 : template <class DataType> class basic_parser : public basic_parser_impl {
850 14814 : public:
851 28435 : using parser_data_type = DataType;
852 10493625 : using OptVal = OptionValue<DataType>;
853 57326 :
854 46793 : basic_parser(Option &O) : basic_parser_impl(O) {}
855 14775 :
856 22639 : protected:
857 11714 : ~basic_parser() = default;
858 46811 : };
859 7241860 :
860 14738 : //--------------------------------------------------
861 22357 : // parser<bool>
862 11243 : //
863 538821 : template <> class parser<bool> final : public basic_parser<bool> {
864 150006 : public:
865 41176170 : parser(Option &O) : basic_parser(O) {}
866 22506 :
867 1613018 : // parse - Return true on error.
868 1133071 : bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
869 888146 :
870 25591 : void initialize() {}
871 267622 :
872 32601 : enum ValueExpected getValueExpectedFlagDefault() const {
873 132086 : return ValueOptional;
874 8075872 : }
875 13453 :
876 52012 : // getValueName - Do not print =<value> at all.
877 42143 : StringRef getValueName() const override { return StringRef(); }
878 185405 :
879 1442883 : void printOptionDiff(const Option &O, bool V, OptVal Default,
880 11328 : size_t GlobalWidth) const;
881 41320 :
882 33640 : // An out-of-line virtual method to provide a 'home' for this class.
883 123612 : void anchor() override;
884 165833 : };
885 34439 :
886 51834 : extern template class basic_parser<bool>;
887 27885527 :
888 121337 : //--------------------------------------------------
889 90418 : // parser<boolOrDefault>
890 26562 : template <>
891 55143 : class parser<boolOrDefault> final : public basic_parser<boolOrDefault> {
892 33159 : public:
893 381757 : parser(Option &O) : basic_parser(O) {}
894 1427647 :
895 1088 : // parse - Return true on error.
896 34087 : bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
897 33775 :
898 3739388 : enum ValueExpected getValueExpectedFlagDefault() const {
899 176254 : return ValueOptional;
900 940 : }
901 37973 :
902 320827 : // getValueName - Do not print =<value> at all.
903 149496 : StringRef getValueName() const override { return StringRef(); }
904 161504 :
905 34203 : void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
906 6118637 : size_t GlobalWidth) const;
907 197542 :
908 67731 : // An out-of-line virtual method to provide a 'home' for this class.
909 56440 : void anchor() override;
910 1289452 : };
911 29987 :
912 151808 : extern template class basic_parser<boolOrDefault>;
913 5364832 :
914 43737 : //--------------------------------------------------
915 240921 : // parser<int>
916 34140 : //
917 374562 : template <> class parser<int> final : public basic_parser<int> {
918 149693 : public:
919 2753608 : parser(Option &O) : basic_parser(O) {}
920 791 :
921 592064 : // parse - Return true on error.
922 31823 : bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
923 149758 :
924 128790 : // getValueName - Overload in subclass to provide a better default value.
925 5748 : StringRef getValueName() const override { return "int"; }
926 370040 :
927 11906 : void printOptionDiff(const Option &O, int V, OptVal Default,
928 4903833 : size_t GlobalWidth) const;
929 1570 :
930 3148 : // An out-of-line virtual method to provide a 'home' for this class.
931 454718 : void anchor() override;
932 292568 : };
933 363 :
934 27070 : extern template class basic_parser<int>;
935 26856 :
936 1380 : //--------------------------------------------------
937 233037 : // parser<unsigned>
938 193988 : //
939 1882 : template <> class parser<unsigned> final : public basic_parser<unsigned> {
940 22241 : public:
941 11989507 : parser(Option &O) : basic_parser(O) {}
942 308 :
943 42803 : // parse - Return true on error.
944 502256 : bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
945 121843 :
946 330 : // getValueName - Overload in subclass to provide a better default value.
947 398 : StringRef getValueName() const override { return "uint"; }
948 288997 :
949 229340 : void printOptionDiff(const Option &O, unsigned V, OptVal Default,
950 1364 : size_t GlobalWidth) const;
951 198 :
952 228068 : // An out-of-line virtual method to provide a 'home' for this class.
953 1768 : void anchor() override;
954 1766 : };
955 675 :
956 1235593 : extern template class basic_parser<unsigned>;
957 3512 :
958 3311 : //--------------------------------------------------
959 444 : // parser<unsigned long long>
960 1824165 : //
961 778 : template <>
962 23 : class parser<unsigned long long> final
963 13960960 : : public basic_parser<unsigned long long> {
964 1270497 : public:
965 114095 : parser(Option &O) : basic_parser(O) {}
966 194 :
967 346618 : // parse - Return true on error.
968 0 : bool parse(Option &O, StringRef ArgName, StringRef Arg,
969 684004 : unsigned long long &Val);
970 581865 :
971 346167 : // getValueName - Overload in subclass to provide a better default value.
972 8718 : StringRef getValueName() const override { return "uint"; }
973 4359 :
974 227872 : void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
975 227883 : size_t GlobalWidth) const;
976 11760 :
977 12938 : // An out-of-line virtual method to provide a 'home' for this class.
978 113936 : void anchor() override;
979 4359 : };
980 4381 :
981 172 : extern template class basic_parser<unsigned long long>;
982 3190211 :
983 6 : //--------------------------------------------------
984 47 : // parser<double>
985 232691 : //
986 116886 : template <> class parser<double> final : public basic_parser<double> {
987 1280 : public:
988 228830 : parser(Option &O) : basic_parser(O) {}
989 576625 :
990 663 : // parse - Return true on error.
991 3 : bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
992 4363 :
993 374883 : // getValueName - Overload in subclass to provide a better default value.
994 373978 : StringRef getValueName() const override { return "number"; }
995 1488 :
996 2585 : void printOptionDiff(const Option &O, double V, OptVal Default,
997 4785387 : size_t GlobalWidth) const;
998 113938 :
999 7 : // An out-of-line virtual method to provide a 'home' for this class.
1000 6 : void anchor() override;
1001 37 : };
1002 60 :
1003 1 : extern template class basic_parser<double>;
1004 797552 :
1005 1 : //--------------------------------------------------
1006 455769 : // parser<float>
1007 0 : //
1008 6 : template <> class parser<float> final : public basic_parser<float> {
1009 12787 : public:
1010 0 : parser(Option &O) : basic_parser(O) {}
1011 0 :
1012 5 : // parse - Return true on error.
1013 119164 : bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
1014 0 :
1015 195 : // getValueName - Overload in subclass to provide a better default value.
1016 0 : StringRef getValueName() const override { return "number"; }
1017 815 :
1018 0 : void printOptionDiff(const Option &O, float V, OptVal Default,
1019 2 : size_t GlobalWidth) const;
1020 22 :
1021 0 : // An out-of-line virtual method to provide a 'home' for this class.
1022 0 : void anchor() override;
1023 683616 : };
1024 294 :
1025 0 : extern template class basic_parser<float>;
1026 0 :
1027 0 : //--------------------------------------------------
1028 113936 : // parser<std::string>
1029 0 : //
1030 0 : template <> class parser<std::string> final : public basic_parser<std::string> {
1031 0 : public:
1032 1547653 : parser(Option &O) : basic_parser(O) {}
1033 0 :
1034 83589 : // parse - Return true on error.
1035 0 : bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
1036 113936 : Value = Arg.str();
1037 0 : return false;
1038 0 : }
1039 58974 :
1040 455816 : // getValueName - Overload in subclass to provide a better default value.
1041 0 : StringRef getValueName() const override { return "string"; }
1042 236280 :
1043 0 : void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
1044 0 : size_t GlobalWidth) const;
1045 3564 :
1046 960836 : // An out-of-line virtual method to provide a 'home' for this class.
1047 864 : void anchor() override;
1048 0 : };
1049 275332 :
1050 108 : extern template class basic_parser<std::string>;
1051 3532016 :
1052 0 : //--------------------------------------------------
1053 0 : // parser<char>
1054 9012 : //
1055 0 : template <> class parser<char> final : public basic_parser<char> {
1056 0 : public:
1057 1176560 : parser(Option &O) : basic_parser(O) {}
1058 0 :
1059 0 : // parse - Return true on error.
1060 134 : bool parse(Option &, StringRef, StringRef Arg, char &Value) {
1061 869340 : Value = Arg[0];
1062 452 : return false;
1063 0 : }
1064 0 :
1065 0 : // getValueName - Overload in subclass to provide a better default value.
1066 228496 : StringRef getValueName() const override { return "char"; }
1067 468644 :
1068 312 : void printOptionDiff(const Option &O, char V, OptVal Default,
1069 522012 : size_t GlobalWidth) const;
1070 0 :
1071 27 : // An out-of-line virtual method to provide a 'home' for this class.
1072 191 : void anchor() override;
1073 571507 : };
1074 113982 :
1075 86 : extern template class basic_parser<char>;
1076 12 :
1077 8 : //--------------------------------------------------
1078 4 : // PrintOptionDiff
1079 455744 : //
1080 878248 : // This collection of wrappers is the intermediary between class opt and class
1081 36 : // parser to handle all the template nastiness.
1082 0 :
1083 4712 : // This overloaded function is selected by the generic parser.
1084 117825 : template <class ParserClass, class DT>
1085 0 : void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
1086 137 : const OptionValue<DT> &Default, size_t GlobalWidth) {
1087 10 : OptionValue<DT> OV = V;
1088 34 : P.printOptionDiff(O, OV, Default, GlobalWidth);
1089 6 : }
1090 12 :
1091 0 : // This is instantiated for basic parsers when the parsed value has a different
1092 3 : // type than the option value. e.g. HelpPrinter.
1093 5141 : template <class ParserDT, class ValDT> struct OptionDiffPrinter {
1094 0 : void print(const Option &O, const parser<ParserDT> &P, const ValDT & /*V*/,
1095 455747 : const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) {
1096 6 : P.printOptionNoValue(O, GlobalWidth);
1097 0 : }
1098 3 : };
1099 6 :
1100 0 : // This is instantiated for basic parsers when the parsed value has the same
1101 3 : // type as the option value.
1102 6 : template <class DT> struct OptionDiffPrinter<DT, DT> {
1103 153862 : void print(const Option &O, const parser<DT> &P, const DT &V,
1104 4 : const OptionValue<DT> &Default, size_t GlobalWidth) {
1105 113944 : P.printOptionDiff(O, V, Default, GlobalWidth);
1106 1407 : }
1107 3 : };
1108 6 :
1109 0 : // This overloaded function is selected by the basic parser, which may parse a
1110 5 : // different type than the option type.
1111 1058904 : template <class ParserClass, class ValDT>
1112 0 : void printOptionDiff(
1113 0 : const Option &O,
1114 0 : const basic_parser<typename ParserClass::parser_data_type> &P,
1115 30 : const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
1116 0 :
1117 3 : OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
1118 3 : printer.print(O, static_cast<const ParserClass &>(P), V, Default,
1119 6 : GlobalWidth);
1120 393 : }
1121 0 :
1122 0 : //===----------------------------------------------------------------------===//
1123 0 : // applicator class - This class is used because we must use partial
1124 10 : // specialization to handle literal string arguments specially (const char* does
1125 11891 : // not correctly respond to the apply method). Because the syntax to use this
1126 12310 : // is a pain, we have the 'apply' method below to handle the nastiness...
1127 23785 : //
1128 18 : template <class Mod> struct applicator {
1129 47742860 : template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
1130 11888 : };
1131 5 :
1132 3533947 : // Handle const char* as a special case...
1133 0 : template <unsigned n> struct applicator<char[n]> {
1134 11891 : template <class Opt> static void opt(StringRef Str, Opt &O) {
1135 52987700 : O.setArgStr(Str);
1136 0 : }
1137 341885 : };
1138 5 : template <unsigned n> struct applicator<const char[n]> {
1139 0 : template <class Opt> static void opt(StringRef Str, Opt &O) {
1140 379 : O.setArgStr(Str);
1141 0 : }
1142 0 : };
1143 1618607 : template <> struct applicator<StringRef > {
1144 146 : template <class Opt> static void opt(StringRef Str, Opt &O) {
1145 32058 : O.setArgStr(Str);
1146 0 : }
1147 0 : };
1148 0 :
1149 2243816 : template <> struct applicator<NumOccurrencesFlag> {
1150 3685 : static void opt(NumOccurrencesFlag N, Option &O) {
1151 45593474 : O.setNumOccurrencesFlag(N);
1152 7412 : }
1153 3685 : };
1154 0 :
1155 6 : template <> struct applicator<ValueExpected> {
1156 6 : static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
1157 47082849 : };
1158 4056429 :
1159 688274 : template <> struct applicator<OptionHidden> {
1160 3687 : static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
1161 1 : };
1162 3759908 :
1163 0 : template <> struct applicator<FormattingFlags> {
1164 2931151 : static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
1165 911502 : };
1166 3698728 :
1167 9 : template <> struct applicator<MiscFlags> {
1168 4443509 : static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
1169 845 : };
1170 10026501 :
1171 0 : // apply method - Apply modifiers to an option in a type safe way.
1172 2279528 : template <class Opt, class Mod, class... Mods>
1173 53088267 : void apply(Opt *O, const Mod &M, const Mods &... Ms) {
1174 53006540 : applicator<Mod>::opt(M, *O);
1175 49900749 : apply(O, Ms...);
1176 63684359 : }
1177 8738518 :
1178 685599 : template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
1179 351992 : applicator<Mod>::opt(M, *O);
1180 820454 : }
1181 15042137 :
1182 14588106 : //===----------------------------------------------------------------------===//
1183 21376026 : // opt_storage class
1184 14584387 :
1185 15142198 : // Default storage class definition: external storage. This implementation
1186 13774935 : // assumes the user will specify a variable to store the data into with the
1187 16804472 : // cl::location(x) modifier.
1188 16018927 : //
1189 7552921 : template <class DataType, bool ExternalStorage, bool isClass>
1190 8402340 : class opt_storage {
1191 7305811 : DataType *Location = nullptr; // Where to store the object...
1192 13913781 : OptionValue<DataType> Default;
1193 4333662 :
1194 4458336 : void check_location() const {
1195 51405456 : assert(Location && "cl::location(...) not specified for a command "
1196 51405577 : "line option with external storage, "
1197 48119877 : "or cl::init specified before cl::location()!!");
1198 57786096 : }
1199 3610376 :
1200 3610666 : public:
1201 2747243 : opt_storage() = default;
1202 5799317 :
1203 7339811 : bool setLocation(Option &O, DataType &L) {
1204 6806473 : if (Location)
1205 6581335 : return O.error("cl::location(x) specified more than once!");
1206 8640221 : Location = &L;
1207 9766155 : Default = L;
1208 9896068 : return false;
1209 10454347 : }
1210 7842871 :
1211 10509162 : template <class T> void setValue(const T &V, bool initial = false) {
1212 10507860 : check_location();
1213 8963003 : *Location = V;
1214 18488509 : if (initial)
1215 19036384 : Default = V;
1216 19822646 : }
1217 18484944 :
1218 8943625 : DataType &getValue() {
1219 9217340 : check_location();
1220 9216215 : return *Location;
1221 16159403 : }
1222 16745325 : const DataType &getValue() const {
1223 14930195 : check_location();
1224 15197290 : return *Location;
1225 8736624 : }
1226 9093101 :
1227 7159803 : operator DataType() const { return this->getValue(); }
1228 6934322 :
1229 7041402 : const OptionValue<DataType> &getDefault() const { return Default; }
1230 7687451 : };
1231 6878290 :
1232 6777391 : // Define how to hold a class type object, such as a string. Since we can
1233 5359245 : // inherit from a class, we do so. This makes us exactly compatible with the
1234 5448083 : // object in all cases that it is used.
1235 4368024 : //
1236 12380665 : template <class DataType>
1237 12497481 : class opt_storage<DataType, false, true> : public DataType {
1238 11833385 : public:
1239 13199468 : OptionValue<DataType> Default;
1240 5222160 :
1241 6021938 : template <class T> void setValue(const T &V, bool initial = false) {
1242 5751760 : DataType::operator=(V);
1243 4824109 : if (initial)
1244 5178506 : Default = V;
1245 4414597 : }
1246 4584452 :
1247 4177352 : DataType &getValue() { return *this; }
1248 4236835 : const DataType &getValue() const { return *this; }
1249 4456720 :
1250 4143224 : const OptionValue<DataType> &getDefault() const { return Default; }
1251 4452804 : };
1252 4570870 :
1253 4245208 : // Define a partial specialization to handle things we cannot inherit from. In
1254 4175899 : // this case, we store an instance through containment, and overload operators
1255 3271696 : // to get at the value.
1256 3355739 : //
1257 3335043 : template <class DataType> class opt_storage<DataType, false, false> {
1258 3452710 : public:
1259 4032882 : DataType Value;
1260 3221732 : OptionValue<DataType> Default;
1261 3220201 :
1262 2365606 : // Make sure we initialize the value with the default constructor for the
1263 2014858 : // type.
1264 3631023 : opt_storage() : Value(DataType()), Default(DataType()) {}
1265 2311267 :
1266 1671116 : template <class T> void setValue(const T &V, bool initial = false) {
1267 2820531 : Value = V;
1268 1627765 : if (initial)
1269 1433847 : Default = V;
1270 1788409 : }
1271 1548325 : DataType &getValue() { return Value; }
1272 12036606 : DataType getValue() const { return Value; }
1273 1550076 :
1274 1552976 : const OptionValue<DataType> &getDefault() const { return Default; }
1275 9924336 :
1276 61507479 : operator DataType() const { return getValue(); }
1277 1837452 :
1278 2642581 : // If the datatype is a pointer, support -> on it.
1279 11599723 : DataType operator->() const { return Value; }
1280 6974919 : };
1281 1658167 :
1282 1154089 : //===----------------------------------------------------------------------===//
1283 5936769 : // opt - A scalar command line option.
1284 242656396 : //
1285 349423 : template <class DataType, bool ExternalStorage = false,
1286 294636 : class ParserClass = parser<DataType>>
1287 5373707 : class opt : public Option,
1288 16772898 : public opt_storage<DataType, ExternalStorage,
1289 517975 : std::is_class<DataType>::value> {
1290 478134 : ParserClass Parser;
1291 3087505 :
1292 4671485 : bool handleOccurrence(unsigned pos, StringRef ArgName,
1293 463504 : StringRef Arg) override {
1294 942013 : typename ParserClass::parser_data_type Val =
1295 1164308 : typename ParserClass::parser_data_type();
1296 4740898 : if (Parser.parse(*this, ArgName, Arg, Val))
1297 590586 : return true; // Parse error!
1298 468259 : this->setValue(Val);
1299 232746 : this->setPosition(pos);
1300 3282628 : return false;
1301 801800 : }
1302 3768996 :
1303 3041701 : enum ValueExpected getValueExpectedFlagDefault() const override {
1304 1659253 : return Parser.getValueExpectedFlagDefault();
1305 2133500 : }
1306 3569621 :
1307 1122223 : void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1308 3715479 : return Parser.getExtraOptionNames(OptionNames);
1309 2741968 : }
1310 6088870 :
1311 814949 : // Forward printing stuff to the parser...
1312 6178916 : size_t getOptionWidth() const override {
1313 3716620 : return Parser.getOptionWidth(*this);
1314 7190407 : }
1315 2295865 :
1316 3923731 : void printOptionInfo(size_t GlobalWidth) const override {
1317 4972014 : Parser.printOptionInfo(*this, GlobalWidth);
1318 5084837 : }
1319 2799426 :
1320 1568772 : void printOptionValue(size_t GlobalWidth, bool Force) const override {
1321 2687784 : if (Force || this->getDefault().compare(this->getValue())) {
1322 6320763 : cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
1323 5166812 : this->getDefault(), GlobalWidth);
1324 980752 : }
1325 2571125 : }
1326 8675935 :
1327 807434 : template <class T, class = typename std::enable_if<
1328 3087249 : std::is_assignable<T&, T>::value>::type>
1329 3038427 : void setDefaultImpl() {
1330 3701303 : const OptionValue<DataType> &V = this->getDefault();
1331 456985 : if (V.hasValue())
1332 3589581 : this->setValue(V.getValue());
1333 3143908 : }
1334 17182298 :
1335 342472 : template <class T, class = typename std::enable_if<
1336 4331373 : !std::is_assignable<T&, T>::value>::type>
1337 2052320 : void setDefaultImpl(...) {}
1338 2154502 :
1339 230391 : void setDefault() override { setDefaultImpl<DataType>(); }
1340 2506709 :
1341 2701841 : void done() {
1342 3627658 : addArgument();
1343 1367871 : Parser.initialize();
1344 3455160 : }
1345 2296680 :
1346 4313222 : public:
1347 1254028 : // Command line options should not be copyable
1348 1367889 : opt(const opt &) = delete;
1349 9911102 : opt &operator=(const opt &) = delete;
1350 26048532 :
1351 799689 : // setInitialValue - Used by the cl::init modifier...
1352 2240360 : void setInitialValue(const DataType &V) { this->setValue(V, true); }
1353 2396013 :
1354 14284145 : ParserClass &getParser() { return Parser; }
1355 114872 :
1356 115525 : template <class T> DataType &operator=(const T &Val) {
1357 2503039 : this->setValue(Val);
1358 5983499 : return this->getValue();
1359 858270 : }
1360 2454244 :
1361 98520112 : template <class... Mods>
1362 9413142 : explicit opt(const Mods &... Ms)
1363 7032852 : : Option(Optional, NotHidden), Parser(*this) {
1364 2992690 : apply(this, Ms...);
1365 179578 : done();
1366 6517922 : }
1367 2168770 : };
1368 1456920 :
1369 2200620 : extern template class opt<unsigned>;
1370 18921286 : extern template class opt<int>;
1371 11913673 : extern template class opt<std::string>;
1372 15433084 : extern template class opt<char>;
1373 5989662 : extern template class opt<bool>;
1374 27274092 :
1375 20174313 : //===----------------------------------------------------------------------===//
1376 16920984 : // list_storage class
1377 6394660 :
1378 20262162 : // Default storage class definition: external storage. This implementation
1379 8282078 : // assumes the user will specify a variable to store the data into with the
1380 9934741 : // cl::location(x) modifier.
1381 3151869 : //
1382 13281498 : template <class DataType, class StorageClass> class list_storage {
1383 9244438 : StorageClass *Location = nullptr; // Where to store the object...
1384 12625327 :
1385 4397737 : public:
1386 13129403 : list_storage() = default;
1387 8437193 :
1388 16267435 : bool setLocation(Option &O, StorageClass &L) {
1389 24204502 : if (Location)
1390 9637611 : return O.error("cl::location(x) specified more than once!");
1391 3887954 : Location = &L;
1392 11488102 : return false;
1393 384080403 : }
1394 6565121 :
1395 2704830 : template <class T> void addValue(const T &V) {
1396 9131241 : assert(Location != 0 && "cl::location(...) not specified for a command "
1397 4870224 : "line option with external storage!");
1398 7763383 : Location->push_back(V);
1399 5091335 : }
1400 12842860 : };
1401 5568584 :
1402 8834486 : // Define how to hold a class type object, such as a string.
1403 3781348 : // Originally this code inherited from std::vector. In transitioning to a new
1404 8453264 : // API for command line options we should change this. The new implementation
1405 6041314 : // of this list_storage specialization implements the minimum subset of the
1406 10043750 : // std::vector API required for all the current clients.
1407 7483013 : //
1408 14851504 : // FIXME: Reduce this API to a more narrow subset of std::vector
1409 8729949 : //
1410 13344053 : template <class DataType> class list_storage<DataType, bool> {
1411 6890039 : std::vector<DataType> Storage;
1412 18935965 :
1413 9427869 : public:
1414 14044010 : using iterator = typename std::vector<DataType>::iterator;
1415 8961111 :
1416 15493864 : iterator begin() { return Storage.begin(); }
1417 9192349 : iterator end() { return Storage.end(); }
1418 14157429 :
1419 7245637 : using const_iterator = typename std::vector<DataType>::const_iterator;
1420 14719279 :
1421 9550975 : const_iterator begin() const { return Storage.begin(); }
1422 11474785 : const_iterator end() const { return Storage.end(); }
1423 6511514 :
1424 12966486 : using size_type = typename std::vector<DataType>::size_type;
1425 5529494 :
1426 8218435 : size_type size() const { return Storage.size(); }
1427 9700754 :
1428 12388353 : bool empty() const { return Storage.empty(); }
1429 11191205 :
1430 8113115 : void push_back(const DataType &value) { Storage.push_back(value); }
1431 7428904 : void push_back(DataType &&value) { Storage.push_back(value); }
1432 12971784 :
1433 6879936 : using reference = typename std::vector<DataType>::reference;
1434 6804356 : using const_reference = typename std::vector<DataType>::const_reference;
1435 5767404 :
1436 282732329 : reference operator[](size_type pos) { return Storage[pos]; }
1437 8643435 : const_reference operator[](size_type pos) const { return Storage[pos]; }
1438 9579754 :
1439 7361591 : iterator erase(const_iterator pos) { return Storage.erase(pos); }
1440 12441642 : iterator erase(const_iterator first, const_iterator last) {
1441 6734577 : return Storage.erase(first, last);
1442 6559515 : }
1443 4653789 :
1444 9859276 : iterator erase(iterator pos) { return Storage.erase(pos); }
1445 8305848 : iterator erase(iterator first, iterator last) {
1446 7332756 : return Storage.erase(first, last);
1447 7555546 : }
1448 14473123 :
1449 8335650 : iterator insert(const_iterator pos, const DataType &value) {
1450 9190929 : return Storage.insert(pos, value);
1451 5943805 : }
1452 8076606 : iterator insert(const_iterator pos, DataType &&value) {
1453 7783249 : return Storage.insert(pos, value);
1454 5487512 : }
1455 3781097 :
1456 5505438 : iterator insert(iterator pos, const DataType &value) {
1457 4947716 : return Storage.insert(pos, value);
1458 6340036 : }
1459 8495106 : iterator insert(iterator pos, DataType &&value) {
1460 6286693 : return Storage.insert(pos, value);
1461 4112600 : }
1462 5088339 :
1463 3998906 : reference front() { return Storage.front(); }
1464 2757142 : const_reference front() const { return Storage.front(); }
1465 3482717 :
1466 3487722 : operator std::vector<DataType>&() { return Storage; }
1467 6823313 : operator ArrayRef<DataType>() { return Storage; }
1468 7722732 : std::vector<DataType> *operator&() { return &Storage; }
1469 7510210 : const std::vector<DataType> *operator&() const { return &Storage; }
1470 9662164 :
1471 6329413 : template <class T> void addValue(const T &V) { Storage.push_back(V); }
1472 9444870 : };
1473 3614940 :
1474 7268876 : //===----------------------------------------------------------------------===//
1475 5023642 : // list - A list of command line options.
1476 3164913 : //
1477 4235604 : template <class DataType, class StorageClass = bool,
1478 3504526 : class ParserClass = parser<DataType>>
1479 4073594 : class list : public Option, public list_storage<DataType, StorageClass> {
1480 2986991 : std::vector<unsigned> Positions;
1481 1963616 : ParserClass Parser;
1482 1680624 :
1483 2769661 : enum ValueExpected getValueExpectedFlagDefault() const override {
1484 2823632 : return Parser.getValueExpectedFlagDefault();
1485 4003859 : }
1486 2776730 :
1487 8530339 : void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1488 8517897 : return Parser.getExtraOptionNames(OptionNames);
1489 6894273 : }
1490 4623238 :
1491 6069728 : bool handleOccurrence(unsigned pos, StringRef ArgName,
1492 2524259 : StringRef Arg) override {
1493 5008739 : typename ParserClass::parser_data_type Val =
1494 3744891 : typename ParserClass::parser_data_type();
1495 7602170 : if (Parser.parse(*this, ArgName, Arg, Val))
1496 3010528 : return true; // Parse Error!
1497 1791603 : list_storage<DataType, StorageClass>::addValue(Val);
1498 3229001 : setPosition(pos);
1499 1904557 : Positions.push_back(pos);
1500 1555208 : return false;
1501 1396403 : }
1502 1627374 :
1503 2100597 : // Forward printing stuff to the parser...
1504 3124263 : size_t getOptionWidth() const override {
1505 3739097 : return Parser.getOptionWidth(*this);
1506 2757977 : }
1507 3598677 :
1508 1288394 : void printOptionInfo(size_t GlobalWidth) const override {
1509 3103849 : Parser.printOptionInfo(*this, GlobalWidth);
1510 2587239 : }
1511 2530029 :
1512 3179884 : // Unimplemented: list options don't currently store their default value.
1513 3152824 : void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1514 4643109 : }
1515 3084202 :
1516 1280460 : void setDefault() override {}
1517 3333374 :
1518 5316337 : void done() {
1519 2089065 : addArgument();
1520 1562864 : Parser.initialize();
1521 1268510 : }
1522 1851367 :
1523 1165048 : public:
1524 2663529 : // Command line options should not be copyable
1525 2461218 : list(const list &) = delete;
1526 2180298 : list &operator=(const list &) = delete;
1527 1902743 :
1528 2352314 : ParserClass &getParser() { return Parser; }
1529 1708188 :
1530 1774507 : unsigned getPosition(unsigned optnum) const {
1531 2735552 : assert(optnum < this->size() && "Invalid option index");
1532 1337339 : return Positions[optnum];
1533 1330040 : }
1534 1681765 :
1535 3316040 : void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
1536 1217872 :
1537 1433422 : template <class... Mods>
1538 4768622 : explicit list(const Mods &... Ms)
1539 5338678 : : Option(ZeroOrMore, NotHidden), Parser(*this) {
1540 4996564 : apply(this, Ms...);
1541 1003407 : done();
1542 4996181 : }
1543 1390890 : };
1544 1676421 :
1545 1544176 : // multi_val - Modifier to set the number of additional values.
1546 1270523 : struct multi_val {
1547 1902250 : unsigned AdditionalVals;
1548 1560629 : explicit multi_val(unsigned N) : AdditionalVals(N) {}
1549 1316231 :
1550 1446242 : template <typename D, typename S, typename P>
1551 2150354 : void apply(list<D, S, P> &L) const {
1552 2507337 : L.setNumAdditionalVals(AdditionalVals);
1553 1937635 : }
1554 1448064 : };
1555 2180898 :
1556 1093580 : //===----------------------------------------------------------------------===//
1557 945568 : // bits_storage class
1558 1132158 :
1559 1382306 : // Default storage class definition: external storage. This implementation
1560 1041291 : // assumes the user will specify a variable to store the data into with the
1561 857993 : // cl::location(x) modifier.
1562 1044148 : //
1563 2802885 : template <class DataType, class StorageClass> class bits_storage {
1564 2970791 : unsigned *Location = nullptr; // Where to store the bits...
1565 2909600 :
1566 865336 : template <class T> static unsigned Bit(const T &V) {
1567 2936457 : unsigned BitPos = reinterpret_cast<unsigned>(V);
1568 724431 : assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1569 1098505 : "enum exceeds width of bit vector!");
1570 842372 : return 1 << BitPos;
1571 523820 : }
1572 935023 :
1573 5830075 : public:
1574 1348648 : bits_storage() = default;
1575 705097 :
1576 524327 : bool setLocation(Option &O, unsigned &L) {
1577 944104 : if (Location)
1578 690126 : return O.error("cl::location(x) specified more than once!");
1579 692126 : Location = &L;
1580 575525 : return false;
1581 119562 : }
1582 634619 :
1583 363986 : template <class T> void addValue(const T &V) {
1584 245772 : assert(Location != 0 && "cl::location(...) not specified for a command "
1585 356682 : "line option with external storage!");
1586 68128 : *Location |= Bit(V);
1587 296864 : }
1588 307393 :
1589 361725 : unsigned getBits() { return *Location; }
1590 421464 :
1591 59705 : template <class T> bool isSet(const T &V) {
1592 307187 : return (*Location & Bit(V)) != 0;
1593 5887146 : }
1594 5147108 : };
1595 5207596 :
1596 62109 : // Define how to hold bits. Since we can inherit from a class, we do so.
1597 5204239 : // This makes us exactly compatible with the bits in all cases that it is used.
1598 462036 : //
1599 349170 : template <class DataType> class bits_storage<DataType, bool> {
1600 409819 : unsigned Bits; // Where to store the bits...
1601 60425 :
1602 584588 : template <class T> static unsigned Bit(const T &V) {
1603 413522 : unsigned BitPos = (unsigned)V;
1604 690731 : assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1605 1211703 : "enum exceeds width of bit vector!");
1606 236599 : return 1 << BitPos;
1607 413003 : }
1608 527708 :
1609 600369 : public:
1610 974837 : template <class T> void addValue(const T &V) { Bits |= Bit(V); }
1611 219021 :
1612 527200 : unsigned getBits() { return Bits; }
1613 1493947 :
1614 1548443 : template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
1615 1380139 : };
1616 412724 :
1617 1379788 : //===----------------------------------------------------------------------===//
1618 690352 : // bits - A bit vector of command options.
1619 579724 : //
1620 615631 : template <class DataType, class Storage = bool,
1621 60277 : class ParserClass = parser<DataType>>
1622 631614 : class bits : public Option, public bits_storage<DataType, Storage> {
1623 864108 : std::vector<unsigned> Positions;
1624 1098341 : ParserClass Parser;
1625 1326830 :
1626 404917 : enum ValueExpected getValueExpectedFlagDefault() const override {
1627 916821 : return Parser.getValueExpectedFlagDefault();
1628 1160357 : }
1629 979702 :
1630 983751 : void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1631 118840 : return Parser.getExtraOptionNames(OptionNames);
1632 1016556 : }
1633 639053 :
1634 580482 : bool handleOccurrence(unsigned pos, StringRef ArgName,
1635 698202 : StringRef Arg) override {
1636 117912 : typename ParserClass::parser_data_type Val =
1637 580440 : typename ParserClass::parser_data_type();
1638 580259 : if (Parser.parse(*this, ArgName, Arg, Val))
1639 522227 : return true; // Parse Error!
1640 613388 : this->addValue(Val);
1641 113160 : setPosition(pos);
1642 5071389 : Positions.push_back(pos);
1643 348639 : return false;
1644 536016 : }
1645 578327 :
1646 294049 : // Forward printing stuff to the parser...
1647 293213 : size_t getOptionWidth() const override {
1648 236945 : return Parser.getOptionWidth(*this);
1649 238008 : }
1650 275453 :
1651 62026 : void printOptionInfo(size_t GlobalWidth) const override {
1652 182311 : Parser.printOptionInfo(*this, GlobalWidth);
1653 235033 : }
1654 1856944 :
1655 1429814 : // Unimplemented: bits options don't currently store their default values.
1656 3728 : void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1657 177380 : }
1658 239720 :
1659 235415 : void setDefault() override {}
1660 236251 :
1661 3752 : void done() {
1662 367830 : addArgument();
1663 175983 : Parser.initialize();
1664 236374 : }
1665 363430 :
1666 565541 : public:
1667 2266674 : // Command line options should not be copyable
1668 117819 : bits(const bits &) = delete;
1669 365925 : bits &operator=(const bits &) = delete;
1670 370568 :
1671 183717 : ParserClass &getParser() { return Parser; }
1672 180538 :
1673 230959 : unsigned getPosition(unsigned optnum) const {
1674 505728 : assert(optnum < this->size() && "Invalid option index");
1675 1560103 : return Positions[optnum];
1676 1965548 : }
1677 1585307 :
1678 129122 : template <class... Mods>
1679 1436369 : explicit bits(const Mods &... Ms)
1680 295427 : : Option(ZeroOrMore, NotHidden), Parser(*this) {
1681 363333 : apply(this, Ms...);
1682 295219 : done();
1683 116109 : }
1684 296629 : };
1685 233548 :
1686 360026 : //===----------------------------------------------------------------------===//
1687 230545 : // Aliased command line option (alias this name to a preexisting name)
1688 114581 : //
1689 241509 :
1690 231099 : class alias : public Option {
1691 231982 : Option *AliasFor;
1692 230072 :
1693 297826 : bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1694 414086 : StringRef Arg) override {
1695 346949 : return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1696 461193 : }
1697 344864 :
1698 114892 : bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
1699 343163 : bool MultiArg = false) override {
1700 229851 : return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
1701 115915 : }
1702 233844 :
1703 114520 : // Handle printing stuff...
1704 234210 : size_t getOptionWidth() const override;
1705 275997 : void printOptionInfo(size_t GlobalWidth) const override;
1706 509139 :
1707 230267 : // Aliases do not need to print their values.
1708 128122 : void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1709 255184 : }
1710 361732 :
1711 358558 : void setDefault() override { AliasFor->setDefault(); }
1712 482990 :
1713 456256 : ValueExpected getValueExpectedFlagDefault() const override {
1714 687639 : return AliasFor->getValueExpectedFlag();
1715 574117 : }
1716 116890 :
1717 576241 : void done() {
1718 118141 : if (!hasArgStr())
1719 231052 : error("cl::alias must have argument name specified!");
1720 231280 : if (!AliasFor)
1721 128366 : error("cl::alias must have an cl::aliasopt(option) specified!");
1722 242430 : Subs = AliasFor->Subs;
1723 115490 : addArgument();
1724 233022 : }
1725 461557 :
1726 917390 : public:
1727 1751977 : // Command line options should not be copyable
1728 115844 : alias(const alias &) = delete;
1729 458251 : alias &operator=(const alias &) = delete;
1730 229752 :
1731 229867 : void setAliasFor(Option &O) {
1732 233540 : if (AliasFor)
1733 121554 : error("cl::alias must only have one cl::aliasopt(...) specified!");
1734 242258 : AliasFor = &O;
1735 231831 : }
1736 350021 :
1737 233420 : template <class... Mods>
1738 116623 : explicit alias(const Mods &... Ms)
1739 234245 : : Option(Optional, Hidden), AliasFor(nullptr) {
1740 234684 : apply(this, Ms...);
1741 344015 : done();
1742 230308 : }
1743 2661 : };
1744 116647 :
1745 9087 : // aliasfor - Modifier to set the option an alias aliases.
1746 4471 : struct aliasopt {
1747 1227 : Option &Opt;
1748 664 :
1749 1352 : explicit aliasopt(Option &O) : Opt(O) {}
1750 3853 :
1751 3613 : void apply(alias &A) const { A.setAliasFor(Opt); }
1752 4044 : };
1753 5461 :
1754 6560 : // extrahelp - provide additional help at the end of the normal help
1755 1649 : // output. All occurrences of cl::extrahelp will be accumulated and
1756 1595 : // printed to stderr at the end of the regular help, just before
1757 934 : // exit is called.
1758 743 : struct extrahelp {
1759 1222 : StringRef morehelp;
1760 1150 :
1761 167872 : explicit extrahelp(StringRef help);
1762 6545 : };
1763 5624 :
1764 10224 : void PrintVersionMessage();
1765 18756 :
1766 9750 : /// This function just prints the help message, exactly the same way as if the
1767 167213 : /// -help or -help-hidden option had been given on the command line.
1768 9239 : ///
1769 3241 : /// \param Hidden if true will print hidden options
1770 6153 : /// \param Categorized if true print options in categories
1771 3316 : void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
1772 11466 :
1773 5705 : //===----------------------------------------------------------------------===//
1774 5800 : // Public interface for accessing registered options.
1775 6493 : //
1776 5534 :
1777 136 : /// Use this to get a StringMap to all registered named options
1778 15204 : /// (e.g. -help). Note \p Map Should be an empty StringMap.
1779 5568 : ///
1780 8442 : /// \return A reference to the StringMap used by the cl APIs to parse options.
1781 3163 : ///
1782 4101 : /// Access to unnamed arguments (i.e. positional) are not provided because
1783 3309 : /// it is expected that the client already has access to these.
1784 655572 : ///
1785 243 : /// Typical usage:
1786 325 : /// \code
1787 383 : /// main(int argc,char* argv[]) {
1788 333 : /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
1789 139 : /// assert(opts.count("help") == 1)
1790 1010719 : /// opts["help"]->setDescription("Show alphabetical help information")
1791 1422 : /// // More code
1792 2171 : /// llvm::cl::ParseCommandLineOptions(argc,argv);
1793 118055 : /// //More code
1794 3232 : /// }
1795 3358 : /// \endcode
1796 572 : ///
1797 562 : /// This interface is useful for modifying options in libraries that are out of
1798 496 : /// the control of the client. The options should be modified before calling
1799 517 : /// llvm::cl::ParseCommandLineOptions().
1800 471 : ///
1801 640 : /// Hopefully this API can be deprecated soon. Any situation where options need
1802 3744 : /// to be modified by tools or libraries should be handled by sane APIs rather
1803 654 : /// than just handing around a global list.
1804 393 : StringMap<Option *> &getRegisteredOptions(SubCommand &Sub = *TopLevelSubCommand);
1805 167229 :
1806 170307 : /// Use this to get all registered SubCommands from the provided parser.
1807 14783 : ///
1808 167159 : /// \return A range of all SubCommand pointers registered with the parser.
1809 6964 : ///
1810 6705 : /// Typical usage:
1811 6894 : /// \code
1812 121616 : /// main(int argc, char* argv[]) {
1813 247700 : /// llvm::cl::ParseCommandLineOptions(argc, argv);
1814 128769 : /// for (auto* S : llvm::cl::getRegisteredSubcommands()) {
1815 0 : /// if (*S) {
1816 139138 : /// std::cout << "Executing subcommand: " << S->getName() << std::endl;
1817 73066 : /// // Execute some function based on the name...
1818 122970 : /// }
1819 69649 : /// }
1820 1331188 : /// }
1821 62923 : /// \endcode
1822 62875 : ///
1823 118028 : /// This interface is useful for defining subcommands in libraries and
1824 63110 : /// the dispatch from a single point (like in the main function).
1825 4096 : iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
1826 1511411 : getRegisteredSubcommands();
1827 351 :
1828 1013452 : //===----------------------------------------------------------------------===//
1829 1013451 : // Standalone command line processing utilities.
1830 183340 : //
1831 1009882 :
1832 4810990 : /// Tokenizes a command line that can contain escapes and quotes.
1833 139849 : //
1834 3954 : /// The quoting rules match those used by GCC and other tools that use
1835 21974 : /// libiberty's buildargv() or expandargv() utilities, and do not match bash.
1836 25776 : /// They differ from buildargv() on treatment of backslashes that do not escape
1837 25851 : /// a special character to make it possible to accept most Windows file paths.
1838 3747 : ///
1839 198667 : /// \param [in] Source The string to be split on whitespace with quotes.
1840 389874 : /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1841 213378 : /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1842 16983 : /// lines and end of the response file to be marked with a nullptr string.
1843 213161 : /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1844 892858 : void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver,
1845 153803 : SmallVectorImpl<const char *> &NewArgv,
1846 62654 : bool MarkEOLs = false);
1847 32501 :
1848 88229 : /// Tokenizes a Windows command line which may contain quotes and escaped
1849 83937 : /// quotes.
1850 142248 : ///
1851 80309 : /// See MSDN docs for CommandLineToArgvW for information on the quoting rules.
1852 4810696 : /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
1853 5225628 : ///
1854 4854579 : /// \param [in] Source The string to be split on whitespace with quotes.
1855 145559 : /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1856 4865321 : /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1857 128784 : /// lines and end of the response file to be marked with a nullptr string.
1858 179581 : /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1859 188748 : void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver,
1860 81016 : SmallVectorImpl<const char *> &NewArgv,
1861 131235 : bool MarkEOLs = false);
1862 189560 :
1863 127812 : /// String tokenization function type. Should be compatible with either
1864 1916401 : /// Windows or Unix command line tokenizers.
1865 1529449 : using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver,
1866 543771 : SmallVectorImpl<const char *> &NewArgv,
1867 1574061 : bool MarkEOLs);
1868 152693 :
1869 153652 : /// Tokenizes content of configuration file.
1870 9589 : ///
1871 149368 : /// \param [in] Source The string representing content of config file.
1872 491847 : /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1873 555803 : /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1874 520776 : /// \param [in] MarkEOLs Added for compatibility with TokenizerCallback.
1875 32541 : ///
1876 1971899 : /// It works like TokenizeGNUCommandLine with ability to skip comment lines.
1877 264783 : ///
1878 118406 : void tokenizeConfigFile(StringRef Source, StringSaver &Saver,
1879 199741 : SmallVectorImpl<const char *> &NewArgv,
1880 129620 : bool MarkEOLs = false);
1881 244450 :
1882 180355 : /// Reads command line options from the given configuration file.
1883 239591 : ///
1884 498796 : /// \param [in] CfgFileName Path to configuration file.
1885 344163 : /// \param [in] Saver Objects that saves allocated strings.
1886 400345 : /// \param [out] Argv Array to which the read options are added.
1887 162263 : /// \return true if the file was successfully read.
1888 438865 : ///
1889 184968 : /// It reads content of the specified file, tokenizes it and expands "@file"
1890 33051 : /// commands resolving file names in them relative to the directory where
1891 166229 : /// CfgFilename resides.
1892 150047 : ///
1893 220291 : bool readConfigFile(StringRef CfgFileName, StringSaver &Saver,
1894 136669 : SmallVectorImpl<const char *> &Argv);
1895 48603 :
1896 159161 : /// Expand response files on a command line recursively using the given
1897 382968 : /// StringSaver and tokenization strategy. Argv should contain the command line
1898 379903 : /// before expansion and will be modified in place. If requested, Argv will
1899 383325 : /// also be populated with nullptrs indicating where each response file line
1900 47557 : /// ends, which is useful for the "/link" argument that needs to consume all
1901 383450 : /// remaining arguments only until the next end of line, when in a response
1902 570737 : /// file.
1903 611779 : ///
1904 610618 : /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1905 40718 : /// \param [in] Tokenizer Tokenization strategy. Typically Unix or Windows.
1906 579313 : /// \param [in,out] Argv Command line into which to expand response files.
1907 146552 : /// \param [in] MarkEOLs Mark end of lines and the end of the response file
1908 154454 : /// with nullptrs in the Argv vector.
1909 158122 : /// \param [in] RelativeNames true if names of nested response files must be
1910 12577 : /// resolved relative to including file.
1911 155130 : /// \return true if all @files were expanded successfully or there were none.
1912 285188 : bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
1913 293891 : SmallVectorImpl<const char *> &Argv,
1914 240494 : bool MarkEOLs = false, bool RelativeNames = false);
1915 61946 :
1916 1561865 : /// Mark all options not part of this category as cl::ReallyHidden.
1917 1442778 : ///
1918 1395033 : /// \param Category the category of options to keep displaying
1919 192580 : ///
1920 1329481 : /// Some tools (like clang-format) like to be able to hide all options that are
1921 356105 : /// not specific to the tool. This function allows a tool to specify a single
1922 527480 : /// option category to display in the -help output.
1923 580557 : void HideUnrelatedOptions(cl::OptionCategory &Category,
1924 418572 : SubCommand &Sub = *TopLevelSubCommand);
1925 258907 :
1926 537868 : /// Mark all options not part of the categories as cl::ReallyHidden.
1927 360862 : ///
1928 476943 : /// \param Categories the categories of options to keep displaying.
1929 182894 : ///
1930 216500 : /// Some tools (like clang-format) like to be able to hide all options that are
1931 360278 : /// not specific to the tool. This function allows a tool to specify a single
1932 331719 : /// option category to display in the -help output.
1933 339552 : void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
1934 134692 : SubCommand &Sub = *TopLevelSubCommand);
1935 230652 :
1936 373002 : /// Reset all command line options to a state that looks as if they have
1937 359414 : /// never appeared on the command line. This is useful for being able to parse
1938 318793 : /// a command line multiple times (especially useful for writing tests).
1939 180541 : void ResetAllOptionOccurrences();
1940 229290 :
1941 338332 : /// Reset the command line parser back to its initial state. This
1942 298992 : /// removes
1943 337610 : /// all options, categories, and subcommands and returns the parser to a state
1944 165909 : /// where no options are supported.
1945 1591062 : void ResetCommandLineParser();
1946 308235 :
1947 340901 : } // end namespace cl
1948 340922 :
1949 162500 : } // end namespace llvm
1950 197982 :
1951 337194 : #endif // LLVM_SUPPORT_COMMANDLINE_H
|