LLVM  8.0.0svn
CommandLine.h
Go to the documentation of this file.
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"
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 //
60 // If EnvVar is not nullptr, command-line options are also parsed from the
61 // environment variable named by EnvVar. Precedence is given to occurrences
62 // from argv. This precedence is currently implemented by parsing argv after
63 // the environment variable, so it is only implemented correctly for options
64 // that give precedence to later occurrences. If your program supports options
65 // that give precedence to earlier occurrences, you will need to extend this
66 // function to support it correctly.
67 bool ParseCommandLineOptions(int argc, const char *const *argv,
68  StringRef Overview = "",
69  raw_ostream *Errs = nullptr,
70  const char *EnvVar = nullptr);
71 
72 //===----------------------------------------------------------------------===//
73 // ParseEnvironmentOptions - Environment variable option processing alternate
74 // entry point.
75 //
76 void ParseEnvironmentOptions(const char *progName, const char *envvar,
77  const char *Overview = "");
78 
79 // Function pointer type for printing version information.
80 using VersionPrinterTy = std::function<void(raw_ostream &)>;
81 
82 ///===---------------------------------------------------------------------===//
83 /// SetVersionPrinter - Override the default (LLVM specific) version printer
84 /// used to print out the version when --version is given
85 /// on the command line. This allows other systems using the
86 /// CommandLine utilities to print their own version string.
88 
89 ///===---------------------------------------------------------------------===//
90 /// AddExtraVersionPrinter - Add an extra printer to use in addition to the
91 /// default one. This can be called multiple times,
92 /// and each time it adds a new function to the list
93 /// which will be called after the basic LLVM version
94 /// printing is complete. Each can then add additional
95 /// information specific to the tool.
97 
98 // PrintOptionValues - Print option values.
99 // With -print-options print the difference between option values and defaults.
100 // With -print-all-options print all option values.
101 // (Currently not perfect, but best-effort.)
102 void PrintOptionValues();
103 
104 // Forward declaration - AddLiteralOption needs to be up here to make gcc happy.
105 class Option;
106 
107 /// Adds a new option for parsing and provides the option it refers to.
108 ///
109 /// \param O pointer to the option
110 /// \param Name the string name for the option to handle during parsing
111 ///
112 /// Literal options are used by some parsers to register special option values.
113 /// This is how the PassNameParser registers pass names for opt.
115 
116 //===----------------------------------------------------------------------===//
117 // Flags permitted to be passed to command line arguments
118 //
119 
120 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
121  Optional = 0x00, // Zero or One occurrence
122  ZeroOrMore = 0x01, // Zero or more occurrences allowed
123  Required = 0x02, // One occurrence required
124  OneOrMore = 0x03, // One or more occurrences required
125 
126  // ConsumeAfter - Indicates that this option is fed anything that follows the
127  // last positional argument required by the application (it is an error if
128  // there are zero positional arguments, and a ConsumeAfter option is used).
129  // Thus, for example, all arguments to LLI are processed until a filename is
130  // found. Once a filename is found, all of the succeeding arguments are
131  // passed, unprocessed, to the ConsumeAfter option.
132  //
134 };
135 
136 enum ValueExpected { // Is a value required for the option?
137  // zero reserved for the unspecified value
138  ValueOptional = 0x01, // The value can appear... or not
139  ValueRequired = 0x02, // The value is required to appear!
140  ValueDisallowed = 0x03 // A value may not be specified (for flags)
141 };
142 
143 enum OptionHidden { // Control whether -help shows this option
144  NotHidden = 0x00, // Option included in -help & -help-hidden
145  Hidden = 0x01, // -help doesn't, but -help-hidden does
146  ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
147 };
148 
149 // Formatting flags - This controls special features that the option might have
150 // that cause it to be parsed differently...
151 //
152 // Prefix - This option allows arguments that are otherwise unrecognized to be
153 // matched by options that are a prefix of the actual value. This is useful for
154 // cases like a linker, where options are typically of the form '-lfoo' or
155 // '-L../../include' where -l or -L are the actual flags. When prefix is
156 // enabled, and used, the value for the flag comes from the suffix of the
157 // argument.
158 //
159 // Grouping - With this option enabled, multiple letter options are allowed to
160 // bunch together with only a single hyphen for the whole group. This allows
161 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
162 //
163 
165  NormalFormatting = 0x00, // Nothing special
166  Positional = 0x01, // Is a positional argument, no '-' required
167  Prefix = 0x02, // Can this option directly prefix its value?
168  Grouping = 0x03 // Can this option group with other options?
169 };
170 
171 enum MiscFlags { // Miscellaneous flags to adjust argument
172  CommaSeparated = 0x01, // Should this cl::list split between commas?
173  PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
174  Sink = 0x04 // Should this cl::list eat all unknown options?
175 };
176 
177 //===----------------------------------------------------------------------===//
178 // Option Category class
179 //
181 private:
182  StringRef const Name;
183  StringRef const Description;
184 
185  void registerCategory();
186 
187 public:
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).
200 
201 //===----------------------------------------------------------------------===//
202 // SubCommand class
203 //
204 class SubCommand {
205 private:
206  StringRef Name;
207  StringRef Description;
208 
209 protected:
210  void registerSubCommand();
211  void unregisterSubCommand();
212 
213 public:
214  SubCommand(StringRef Name, StringRef Description = "")
215  : Name(Name), Description(Description) {
216  registerSubCommand();
217  }
218  SubCommand() = default;
219 
220  void reset();
221 
222  explicit operator bool() const;
223 
224  StringRef getName() const { return Name; }
225  StringRef getDescription() const { return Description; }
226 
230 
231  Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists.
232 };
233 
234 // A special subcommand representing no subcommand
236 
237 // A special subcommand that can be used to put an option into all subcommands.
239 
240 //===----------------------------------------------------------------------===//
241 // Option Base class
242 //
243 class Option {
244  friend class alias;
245 
246  // handleOccurrences - Overriden by subclasses to handle the value passed into
247  // an argument. Should return true if there was an error processing the
248  // argument and the program should exit.
249  //
250  virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
251  StringRef Arg) = 0;
252 
253  virtual enum ValueExpected getValueExpectedFlagDefault() const {
254  return ValueOptional;
255  }
256 
257  // Out of line virtual function to provide home for the class.
258  virtual void anchor();
259 
260  int NumOccurrences = 0; // The number of times specified
261  // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
262  // problems with signed enums in bitfields.
263  unsigned Occurrences : 3; // enum NumOccurrencesFlag
264  // not using the enum type for 'Value' because zero is an implementation
265  // detail representing the non-value
266  unsigned Value : 2;
267  unsigned HiddenFlag : 2; // enum OptionHidden
268  unsigned Formatting : 2; // enum FormattingFlags
269  unsigned Misc : 3;
270  unsigned Position = 0; // Position of last occurrence of the option
271  unsigned AdditionalVals = 0; // Greater than 0 for multi-valued option.
272 
273 public:
274  StringRef ArgStr; // The argument string itself (ex: "help", "o")
275  StringRef HelpStr; // The descriptive text message for -help
276  StringRef ValueStr; // String describing what the value of this option is
277  OptionCategory *Category; // The Category this option belongs to
278  SmallPtrSet<SubCommand *, 4> Subs; // The subcommands this option belongs to.
279  bool FullyInitialized = false; // Has addArgument been called?
280 
281  inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
282  return (enum NumOccurrencesFlag)Occurrences;
283  }
284 
285  inline enum ValueExpected getValueExpectedFlag() const {
286  return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
287  }
288 
289  inline enum OptionHidden getOptionHiddenFlag() const {
290  return (enum OptionHidden)HiddenFlag;
291  }
292 
293  inline enum FormattingFlags getFormattingFlag() const {
294  return (enum FormattingFlags)Formatting;
295  }
296 
297  inline unsigned getMiscFlags() const { return Misc; }
298  inline unsigned getPosition() const { return Position; }
299  inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
300 
301  // hasArgStr - Return true if the argstr != ""
302  bool hasArgStr() const { return !ArgStr.empty(); }
303  bool isPositional() const { return getFormattingFlag() == cl::Positional; }
304  bool isSink() const { return getMiscFlags() & cl::Sink; }
305 
306  bool isConsumeAfter() const {
307  return getNumOccurrencesFlag() == cl::ConsumeAfter;
308  }
309 
310  bool isInAllSubCommands() const {
311  return any_of(Subs, [](const SubCommand *SC) {
312  return SC == &*AllSubCommands;
313  });
314  }
315 
316  //-------------------------------------------------------------------------===
317  // Accessor functions set by OptionModifiers
318  //
319  void setArgStr(StringRef S);
320  void setDescription(StringRef S) { HelpStr = S; }
321  void setValueStr(StringRef S) { ValueStr = S; }
322  void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
323  void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
324  void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
325  void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
326  void setMiscFlag(enum MiscFlags M) { Misc |= M; }
327  void setPosition(unsigned pos) { Position = pos; }
328  void setCategory(OptionCategory &C) { Category = &C; }
329  void addSubCommand(SubCommand &S) { Subs.insert(&S); }
330 
331 protected:
332  explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
333  enum OptionHidden Hidden)
334  : Occurrences(OccurrencesFlag), Value(0), HiddenFlag(Hidden),
335  Formatting(NormalFormatting), Misc(0), Category(&GeneralCategory) {}
336 
337  inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
338 
339 public:
340  virtual ~Option() = default;
341 
342  // addArgument - Register this argument with the commandline system.
343  //
344  void addArgument();
345 
346  /// Unregisters this option from the CommandLine system.
347  ///
348  /// This option must have been the last option registered.
349  /// For testing purposes only.
350  void removeArgument();
351 
352  // Return the width of the option tag for printing...
353  virtual size_t getOptionWidth() const = 0;
354 
355  // printOptionInfo - Print out information about this option. The
356  // to-be-maintained width is specified.
357  //
358  virtual void printOptionInfo(size_t GlobalWidth) const = 0;
359 
360  virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
361 
362  virtual void setDefault() = 0;
363 
364  static void printHelpStr(StringRef HelpStr, size_t Indent,
365  size_t FirstLineIndentedBy);
366 
368 
369  // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
370  //
371  virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
372  bool MultiArg = false);
373 
374  // Prints option name followed by message. Always returns true.
375  bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs());
376  bool error(const Twine &Message, raw_ostream &Errs) {
377  return error(Message, StringRef(), Errs);
378  }
379 
380  inline int getNumOccurrences() const { return NumOccurrences; }
381  inline void reset() { NumOccurrences = 0; }
382 };
383 
384 //===----------------------------------------------------------------------===//
385 // Command line option modifiers that can be used to modify the behavior of
386 // command line option parsers...
387 //
388 
389 // desc - Modifier to set the description shown in the -help output...
390 struct desc {
392 
393  desc(StringRef Str) : Desc(Str) {}
394 
395  void apply(Option &O) const { O.setDescription(Desc); }
396 };
397 
398 // value_desc - Modifier to set the value description shown in the -help
399 // output...
400 struct value_desc {
402 
403  value_desc(StringRef Str) : Desc(Str) {}
404 
405  void apply(Option &O) const { O.setValueStr(Desc); }
406 };
407 
408 // init - Specify a default (initial) value for the command line argument, if
409 // the default constructor for the argument type does not give you what you
410 // want. This is only valid on "opt" arguments, not on "list" arguments.
411 //
412 template <class Ty> struct initializer {
413  const Ty &Init;
414  initializer(const Ty &Val) : Init(Val) {}
415 
416  template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
417 };
418 
419 template <class Ty> initializer<Ty> init(const Ty &Val) {
420  return initializer<Ty>(Val);
421 }
422 
423 // location - Allow the user to specify which external variable they want to
424 // store the results of the command line argument processing into, if they don't
425 // want to store it in the option itself.
426 //
427 template <class Ty> struct LocationClass {
428  Ty &Loc;
429 
430  LocationClass(Ty &L) : Loc(L) {}
431 
432  template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
433 };
434 
435 template <class Ty> LocationClass<Ty> location(Ty &L) {
436  return LocationClass<Ty>(L);
437 }
438 
439 // cat - Specifiy the Option category for the command line argument to belong
440 // to.
441 struct cat {
443 
444  cat(OptionCategory &c) : Category(c) {}
445 
446  template <class Opt> void apply(Opt &O) const { O.setCategory(Category); }
447 };
448 
449 // sub - Specify the subcommand that this option belongs to.
450 struct sub {
452 
453  sub(SubCommand &S) : Sub(S) {}
454 
455  template <class Opt> void apply(Opt &O) const { O.addSubCommand(Sub); }
456 };
457 
458 //===----------------------------------------------------------------------===//
459 // OptionValue class
460 
461 // Support value comparison outside the template.
463  virtual bool compare(const GenericOptionValue &V) const = 0;
464 
465 protected:
466  GenericOptionValue() = default;
467  GenericOptionValue(const GenericOptionValue&) = default;
468  GenericOptionValue &operator=(const GenericOptionValue &) = default;
469  ~GenericOptionValue() = default;
470 
471 private:
472  virtual void anchor();
473 };
474 
475 template <class DataType> struct OptionValue;
476 
477 // The default value safely does nothing. Option value printing is only
478 // best-effort.
479 template <class DataType, bool isClass>
481  // Temporary storage for argument passing.
483 
484  bool hasValue() const { return false; }
485 
486  const DataType &getValue() const { llvm_unreachable("no default value"); }
487 
488  // Some options may take their value from a different data type.
489  template <class DT> void setValue(const DT & /*V*/) {}
490 
491  bool compare(const DataType & /*V*/) const { return false; }
492 
493  bool compare(const GenericOptionValue & /*V*/) const override {
494  return false;
495  }
496 
497 protected:
498  ~OptionValueBase() = default;
499 };
500 
501 // Simple copy of the option value.
502 template <class DataType> class OptionValueCopy : public GenericOptionValue {
503  DataType Value;
504  bool Valid = false;
505 
506 protected:
507  OptionValueCopy(const OptionValueCopy&) = default;
508  OptionValueCopy &operator=(const OptionValueCopy &) = default;
509  ~OptionValueCopy() = default;
510 
511 public:
512  OptionValueCopy() = default;
513 
514  bool hasValue() const { return Valid; }
515 
516  const DataType &getValue() const {
517  assert(Valid && "invalid option value");
518  return Value;
519  }
520 
521  void setValue(const DataType &V) {
522  Valid = true;
523  Value = V;
524  }
525 
526  bool compare(const DataType &V) const { return Valid && (Value != V); }
527 
528  bool compare(const GenericOptionValue &V) const override {
530  static_cast<const OptionValueCopy<DataType> &>(V);
531  if (!VC.hasValue())
532  return false;
533  return compare(VC.getValue());
534  }
535 };
536 
537 // Non-class option values.
538 template <class DataType>
541 
542 protected:
543  OptionValueBase() = default;
544  OptionValueBase(const OptionValueBase&) = default;
545  OptionValueBase &operator=(const OptionValueBase &) = default;
546  ~OptionValueBase() = default;
547 };
548 
549 // Top-level option class.
550 template <class DataType>
551 struct OptionValue final
552  : OptionValueBase<DataType, std::is_class<DataType>::value> {
553  OptionValue() = default;
554 
555  OptionValue(const DataType &V) { this->setValue(V); }
556 
557  // Some options may take their value from a different data type.
558  template <class DT> OptionValue<DataType> &operator=(const DT &V) {
559  this->setValue(V);
560  return *this;
561  }
562 };
563 
564 // Other safe-to-copy-by-value common option types.
566 template <>
567 struct OptionValue<cl::boolOrDefault> final
568  : OptionValueCopy<cl::boolOrDefault> {
570 
571  OptionValue() = default;
572 
573  OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
574 
576  setValue(V);
577  return *this;
578  }
579 
580 private:
581  void anchor() override;
582 };
583 
584 template <>
585 struct OptionValue<std::string> final : OptionValueCopy<std::string> {
587 
588  OptionValue() = default;
589 
590  OptionValue(const std::string &V) { this->setValue(V); }
591 
592  OptionValue<std::string> &operator=(const std::string &V) {
593  setValue(V);
594  return *this;
595  }
596 
597 private:
598  void anchor() override;
599 };
600 
601 //===----------------------------------------------------------------------===//
602 // Enum valued command line option
603 //
604 
605 // This represents a single enum value, using "int" as the underlying type.
608  int Value;
610 };
611 
612 #define clEnumVal(ENUMVAL, DESC) \
613  llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC }
614 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) \
615  llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC }
616 
617 // values - For custom data types, allow specifying a group of values together
618 // as the values that go into the mapping that the option handler uses.
619 //
620 class ValuesClass {
621  // Use a vector instead of a map, because the lists should be short,
622  // the overhead is less, and most importantly, it keeps them in the order
623  // inserted so we can print our option out nicely.
625 
626 public:
627  ValuesClass(std::initializer_list<OptionEnumValue> Options)
628  : Values(Options) {}
629 
630  template <class Opt> void apply(Opt &O) const {
631  for (auto Value : Values)
632  O.getParser().addLiteralOption(Value.Name, Value.Value,
633  Value.Description);
634  }
635 };
636 
637 /// Helper to build a ValuesClass by forwarding a variable number of arguments
638 /// as an initializer list to the ValuesClass constructor.
639 template <typename... OptsTy> ValuesClass values(OptsTy... Options) {
640  return ValuesClass({Options...});
641 }
642 
643 //===----------------------------------------------------------------------===//
644 // parser class - Parameterizable parser for different data types. By default,
645 // known data types (string, int, bool) have specialized parsers, that do what
646 // you would expect. The default parser, used for data types that are not
647 // built-in, uses a mapping table to map specific options to values, which is
648 // used, among other things, to handle enum types.
649 
650 //--------------------------------------------------
651 // generic_parser_base - This class holds all the non-generic code that we do
652 // not need replicated for every instance of the generic parser. This also
653 // allows us to put stuff into CommandLine.cpp
654 //
656 protected:
658  public:
660  : Name(name), HelpStr(helpStr) {}
663  };
664 
665 public:
666  generic_parser_base(Option &O) : Owner(O) {}
667 
668  virtual ~generic_parser_base() = default;
669  // Base class should have virtual-destructor
670 
671  // getNumOptions - Virtual function implemented by generic subclass to
672  // indicate how many entries are in Values.
673  //
674  virtual unsigned getNumOptions() const = 0;
675 
676  // getOption - Return option name N.
677  virtual StringRef getOption(unsigned N) const = 0;
678 
679  // getDescription - Return description N
680  virtual StringRef getDescription(unsigned N) const = 0;
681 
682  // Return the width of the option tag for printing...
683  virtual size_t getOptionWidth(const Option &O) const;
684 
685  virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
686 
687  // printOptionInfo - Print out information about this option. The
688  // to-be-maintained width is specified.
689  //
690  virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
691 
692  void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
693  const GenericOptionValue &Default,
694  size_t GlobalWidth) const;
695 
696  // printOptionDiff - print the value of an option and it's default.
697  //
698  // Template definition ensures that the option and default have the same
699  // DataType (via the same AnyOptionValue).
700  template <class AnyOptionValue>
701  void printOptionDiff(const Option &O, const AnyOptionValue &V,
702  const AnyOptionValue &Default,
703  size_t GlobalWidth) const {
704  printGenericOptionDiff(O, V, Default, GlobalWidth);
705  }
706 
707  void initialize() {}
708 
710  // If there has been no argstr specified, that means that we need to add an
711  // argument for every possible option. This ensures that our options are
712  // vectored to us.
713  if (!Owner.hasArgStr())
714  for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
715  OptionNames.push_back(getOption(i));
716  }
717 
718  enum ValueExpected getValueExpectedFlagDefault() const {
719  // If there is an ArgStr specified, then we are of the form:
720  //
721  // -opt=O2 or -opt O2 or -optO2
722  //
723  // In which case, the value is required. Otherwise if an arg str has not
724  // been specified, we are of the form:
725  //
726  // -O2 or O2 or -la (where -l and -a are separate options)
727  //
728  // If this is the case, we cannot allow a value.
729  //
730  if (Owner.hasArgStr())
731  return ValueRequired;
732  else
733  return ValueDisallowed;
734  }
735 
736  // findOption - Return the option number corresponding to the specified
737  // argument string. If the option is not found, getNumOptions() is returned.
738  //
739  unsigned findOption(StringRef Name);
740 
741 protected:
743 };
744 
745 // Default parser implementation - This implementation depends on having a
746 // mapping of recognized options to values of some sort. In addition to this,
747 // each entry in the mapping also tracks a help message that is printed with the
748 // command line option for -help. Because this is a simple mapping parser, the
749 // data type can be any unsupported type.
750 //
751 template <class DataType> class parser : public generic_parser_base {
752 protected:
753  class OptionInfo : public GenericOptionInfo {
754  public:
756  : GenericOptionInfo(name, helpStr), V(v) {}
757 
759  };
761 
762 public:
764 
766 
767  // Implement virtual functions needed by generic_parser_base
768  unsigned getNumOptions() const override { return unsigned(Values.size()); }
769  StringRef getOption(unsigned N) const override { return Values[N].Name; }
770  StringRef getDescription(unsigned N) const override {
771  return Values[N].HelpStr;
772  }
773 
774  // getOptionValue - Return the value of option name N.
775  const GenericOptionValue &getOptionValue(unsigned N) const override {
776  return Values[N].V;
777  }
778 
779  // parse - Return true on error.
780  bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
781  StringRef ArgVal;
782  if (Owner.hasArgStr())
783  ArgVal = Arg;
784  else
785  ArgVal = ArgName;
786 
787  for (size_t i = 0, e = Values.size(); i != e; ++i)
788  if (Values[i].Name == ArgVal) {
789  V = Values[i].V.getValue();
790  return false;
791  }
792 
793  return O.error("Cannot find option named '" + ArgVal + "'!");
794  }
795 
796  /// addLiteralOption - Add an entry to the mapping table.
797  ///
798  template <class DT>
799  void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
800  assert(findOption(Name) == Values.size() && "Option already exists!");
801  OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
802  Values.push_back(X);
803  AddLiteralOption(Owner, Name);
804  }
805 
806  /// removeLiteralOption - Remove the specified option.
807  ///
809  unsigned N = findOption(Name);
810  assert(N != Values.size() && "Option not found!");
811  Values.erase(Values.begin() + N);
812  }
813 };
814 
815 //--------------------------------------------------
816 // basic_parser - Super class of parsers to provide boilerplate code
817 //
818 class basic_parser_impl { // non-template implementation of basic_parser<t>
819 public:
821 
822  enum ValueExpected getValueExpectedFlagDefault() const {
823  return ValueRequired;
824  }
825 
827 
828  void initialize() {}
829 
830  // Return the width of the option tag for printing...
831  size_t getOptionWidth(const Option &O) const;
832 
833  // printOptionInfo - Print out information about this option. The
834  // to-be-maintained width is specified.
835  //
836  void printOptionInfo(const Option &O, size_t GlobalWidth) const;
837 
838  // printOptionNoValue - Print a placeholder for options that don't yet support
839  // printOptionDiff().
840  void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
841 
842  // getValueName - Overload in subclass to provide a better default value.
843  virtual StringRef getValueName() const { return "value"; }
844 
845  // An out-of-line virtual method to provide a 'home' for this class.
846  virtual void anchor();
847 
848 protected:
849  ~basic_parser_impl() = default;
850 
851  // A helper for basic_parser::printOptionDiff.
852  void printOptionName(const Option &O, size_t GlobalWidth) const;
853 };
854 
855 // basic_parser - The real basic parser is just a template wrapper that provides
856 // a typedef for the provided data type.
857 //
858 template <class DataType> class basic_parser : public basic_parser_impl {
859 public:
862 
864 
865 protected:
866  ~basic_parser() = default;
867 };
868 
869 //--------------------------------------------------
870 // parser<bool>
871 //
872 template <> class parser<bool> final : public basic_parser<bool> {
873 public:
875 
876  // parse - Return true on error.
877  bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
878 
879  void initialize() {}
880 
881  enum ValueExpected getValueExpectedFlagDefault() const {
882  return ValueOptional;
883  }
884 
885  // getValueName - Do not print =<value> at all.
886  StringRef getValueName() const override { return StringRef(); }
887 
888  void printOptionDiff(const Option &O, bool V, OptVal Default,
889  size_t GlobalWidth) const;
890 
891  // An out-of-line virtual method to provide a 'home' for this class.
892  void anchor() override;
893 };
894 
895 extern template class basic_parser<bool>;
896 
897 //--------------------------------------------------
898 // parser<boolOrDefault>
899 template <>
901 public:
903 
904  // parse - Return true on error.
905  bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
906 
907  enum ValueExpected getValueExpectedFlagDefault() const {
908  return ValueOptional;
909  }
910 
911  // getValueName - Do not print =<value> at all.
912  StringRef getValueName() const override { return StringRef(); }
913 
914  void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
915  size_t GlobalWidth) const;
916 
917  // An out-of-line virtual method to provide a 'home' for this class.
918  void anchor() override;
919 };
920 
921 extern template class basic_parser<boolOrDefault>;
922 
923 //--------------------------------------------------
924 // parser<int>
925 //
926 template <> class parser<int> final : public basic_parser<int> {
927 public:
929 
930  // parse - Return true on error.
931  bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
932 
933  // getValueName - Overload in subclass to provide a better default value.
934  StringRef getValueName() const override { return "int"; }
935 
936  void printOptionDiff(const Option &O, int V, OptVal Default,
937  size_t GlobalWidth) const;
938 
939  // An out-of-line virtual method to provide a 'home' for this class.
940  void anchor() override;
941 };
942 
943 extern template class basic_parser<int>;
944 
945 //--------------------------------------------------
946 // parser<unsigned>
947 //
948 template <> class parser<unsigned> final : public basic_parser<unsigned> {
949 public:
951 
952  // parse - Return true on error.
953  bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
954 
955  // getValueName - Overload in subclass to provide a better default value.
956  StringRef getValueName() const override { return "uint"; }
957 
958  void printOptionDiff(const Option &O, unsigned V, OptVal Default,
959  size_t GlobalWidth) const;
960 
961  // An out-of-line virtual method to provide a 'home' for this class.
962  void anchor() override;
963 };
964 
965 extern template class basic_parser<unsigned>;
966 
967 //--------------------------------------------------
968 // parser<unsigned long long>
969 //
970 template <>
973 public:
975 
976  // parse - Return true on error.
977  bool parse(Option &O, StringRef ArgName, StringRef Arg,
978  unsigned long long &Val);
979 
980  // getValueName - Overload in subclass to provide a better default value.
981  StringRef getValueName() const override { return "uint"; }
982 
983  void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
984  size_t GlobalWidth) const;
985 
986  // An out-of-line virtual method to provide a 'home' for this class.
987  void anchor() override;
988 };
989 
990 extern template class basic_parser<unsigned long long>;
991 
992 //--------------------------------------------------
993 // parser<double>
994 //
995 template <> class parser<double> final : public basic_parser<double> {
996 public:
998 
999  // parse - Return true on error.
1000  bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
1001 
1002  // getValueName - Overload in subclass to provide a better default value.
1003  StringRef getValueName() const override { return "number"; }
1004 
1005  void printOptionDiff(const Option &O, double V, OptVal Default,
1006  size_t GlobalWidth) const;
1007 
1008  // An out-of-line virtual method to provide a 'home' for this class.
1009  void anchor() override;
1010 };
1011 
1012 extern template class basic_parser<double>;
1013 
1014 //--------------------------------------------------
1015 // parser<float>
1016 //
1017 template <> class parser<float> final : public basic_parser<float> {
1018 public:
1020 
1021  // parse - Return true on error.
1022  bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
1023 
1024  // getValueName - Overload in subclass to provide a better default value.
1025  StringRef getValueName() const override { return "number"; }
1026 
1027  void printOptionDiff(const Option &O, float V, OptVal Default,
1028  size_t GlobalWidth) const;
1029 
1030  // An out-of-line virtual method to provide a 'home' for this class.
1031  void anchor() override;
1032 };
1033 
1034 extern template class basic_parser<float>;
1035 
1036 //--------------------------------------------------
1037 // parser<std::string>
1038 //
1039 template <> class parser<std::string> final : public basic_parser<std::string> {
1040 public:
1042 
1043  // parse - Return true on error.
1044  bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
1045  Value = Arg.str();
1046  return false;
1047  }
1048 
1049  // getValueName - Overload in subclass to provide a better default value.
1050  StringRef getValueName() const override { return "string"; }
1051 
1052  void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
1053  size_t GlobalWidth) const;
1054 
1055  // An out-of-line virtual method to provide a 'home' for this class.
1056  void anchor() override;
1057 };
1058 
1059 extern template class basic_parser<std::string>;
1060 
1061 //--------------------------------------------------
1062 // parser<char>
1063 //
1064 template <> class parser<char> final : public basic_parser<char> {
1065 public:
1067 
1068  // parse - Return true on error.
1070  Value = Arg[0];
1071  return false;
1072  }
1073 
1074  // getValueName - Overload in subclass to provide a better default value.
1075  StringRef getValueName() const override { return "char"; }
1076 
1077  void printOptionDiff(const Option &O, char V, OptVal Default,
1078  size_t GlobalWidth) const;
1079 
1080  // An out-of-line virtual method to provide a 'home' for this class.
1081  void anchor() override;
1082 };
1083 
1084 extern template class basic_parser<char>;
1085 
1086 //--------------------------------------------------
1087 // PrintOptionDiff
1088 //
1089 // This collection of wrappers is the intermediary between class opt and class
1090 // parser to handle all the template nastiness.
1091 
1092 // This overloaded function is selected by the generic parser.
1093 template <class ParserClass, class DT>
1094 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
1095  const OptionValue<DT> &Default, size_t GlobalWidth) {
1096  OptionValue<DT> OV = V;
1097  P.printOptionDiff(O, OV, Default, GlobalWidth);
1098 }
1099 
1100 // This is instantiated for basic parsers when the parsed value has a different
1101 // type than the option value. e.g. HelpPrinter.
1102 template <class ParserDT, class ValDT> struct OptionDiffPrinter {
1103  void print(const Option &O, const parser<ParserDT> &P, const ValDT & /*V*/,
1104  const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) {
1105  P.printOptionNoValue(O, GlobalWidth);
1106  }
1107 };
1108 
1109 // This is instantiated for basic parsers when the parsed value has the same
1110 // type as the option value.
1111 template <class DT> struct OptionDiffPrinter<DT, DT> {
1112  void print(const Option &O, const parser<DT> &P, const DT &V,
1113  const OptionValue<DT> &Default, size_t GlobalWidth) {
1114  P.printOptionDiff(O, V, Default, GlobalWidth);
1115  }
1116 };
1117 
1118 // This overloaded function is selected by the basic parser, which may parse a
1119 // different type than the option type.
1120 template <class ParserClass, class ValDT>
1122  const Option &O,
1124  const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
1125 
1127  printer.print(O, static_cast<const ParserClass &>(P), V, Default,
1128  GlobalWidth);
1129 }
1130 
1131 //===----------------------------------------------------------------------===//
1132 // applicator class - This class is used because we must use partial
1133 // specialization to handle literal string arguments specially (const char* does
1134 // not correctly respond to the apply method). Because the syntax to use this
1135 // is a pain, we have the 'apply' method below to handle the nastiness...
1136 //
1137 template <class Mod> struct applicator {
1138  template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
1139 };
1140 
1141 // Handle const char* as a special case...
1142 template <unsigned n> struct applicator<char[n]> {
1143  template <class Opt> static void opt(StringRef Str, Opt &O) {
1144  O.setArgStr(Str);
1145  }
1146 };
1147 template <unsigned n> struct applicator<const char[n]> {
1148  template <class Opt> static void opt(StringRef Str, Opt &O) {
1149  O.setArgStr(Str);
1150  }
1151 };
1152 template <> struct applicator<StringRef > {
1153  template <class Opt> static void opt(StringRef Str, Opt &O) {
1154  O.setArgStr(Str);
1155  }
1156 };
1157 
1159  static void opt(NumOccurrencesFlag N, Option &O) {
1160  O.setNumOccurrencesFlag(N);
1161  }
1162 };
1163 
1164 template <> struct applicator<ValueExpected> {
1165  static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
1166 };
1167 
1168 template <> struct applicator<OptionHidden> {
1169  static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
1170 };
1171 
1172 template <> struct applicator<FormattingFlags> {
1173  static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
1174 };
1175 
1176 template <> struct applicator<MiscFlags> {
1177  static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
1178 };
1179 
1180 // apply method - Apply modifiers to an option in a type safe way.
1181 template <class Opt, class Mod, class... Mods>
1182 void apply(Opt *O, const Mod &M, const Mods &... Ms) {
1183  applicator<Mod>::opt(M, *O);
1184  apply(O, Ms...);
1185 }
1186 
1187 template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
1188  applicator<Mod>::opt(M, *O);
1189 }
1190 
1191 //===----------------------------------------------------------------------===//
1192 // opt_storage class
1193 
1194 // Default storage class definition: external storage. This implementation
1195 // assumes the user will specify a variable to store the data into with the
1196 // cl::location(x) modifier.
1197 //
1198 template <class DataType, bool ExternalStorage, bool isClass>
1200  DataType *Location = nullptr; // Where to store the object...
1201  OptionValue<DataType> Default;
1202 
1203  void check_location() const {
1204  assert(Location && "cl::location(...) not specified for a command "
1205  "line option with external storage, "
1206  "or cl::init specified before cl::location()!!");
1207  }
1208 
1209 public:
1210  opt_storage() = default;
1211 
1213  if (Location)
1214  return O.error("cl::location(x) specified more than once!");
1215  Location = &L;
1216  Default = L;
1217  return false;
1218  }
1219 
1220  template <class T> void setValue(const T &V, bool initial = false) {
1221  check_location();
1222  *Location = V;
1223  if (initial)
1224  Default = V;
1225  }
1226 
1228  check_location();
1229  return *Location;
1230  }
1231  const DataType &getValue() const {
1232  check_location();
1233  return *Location;
1234  }
1235 
1236  operator DataType() const { return this->getValue(); }
1237 
1238  const OptionValue<DataType> &getDefault() const { return Default; }
1239 };
1240 
1241 // Define how to hold a class type object, such as a string. Since we can
1242 // inherit from a class, we do so. This makes us exactly compatible with the
1243 // object in all cases that it is used.
1244 //
1245 template <class DataType>
1247 public:
1249 
1250  template <class T> void setValue(const T &V, bool initial = false) {
1251  DataType::operator=(V);
1252  if (initial)
1253  Default = V;
1254  }
1255 
1256  DataType &getValue() { return *this; }
1257  const DataType &getValue() const { return *this; }
1258 
1259  const OptionValue<DataType> &getDefault() const { return Default; }
1260 };
1261 
1262 // Define a partial specialization to handle things we cannot inherit from. In
1263 // this case, we store an instance through containment, and overload operators
1264 // to get at the value.
1265 //
1266 template <class DataType> class opt_storage<DataType, false, false> {
1267 public:
1270 
1271  // Make sure we initialize the value with the default constructor for the
1272  // type.
1273  opt_storage() : Value(DataType()), Default(DataType()) {}
1274 
1275  template <class T> void setValue(const T &V, bool initial = false) {
1276  Value = V;
1277  if (initial)
1278  Default = V;
1279  }
1280  DataType &getValue() { return Value; }
1281  DataType getValue() const { return Value; }
1282 
1283  const OptionValue<DataType> &getDefault() const { return Default; }
1284 
1285  operator DataType() const { return getValue(); }
1286 
1287  // If the datatype is a pointer, support -> on it.
1288  DataType operator->() const { return Value; }
1289 };
1290 
1291 //===----------------------------------------------------------------------===//
1292 // opt - A scalar command line option.
1293 //
1294 template <class DataType, bool ExternalStorage = false,
1295  class ParserClass = parser<DataType>>
1296 class opt : public Option,
1297  public opt_storage<DataType, ExternalStorage,
1298  std::is_class<DataType>::value> {
1299  ParserClass Parser;
1300 
1301  bool handleOccurrence(unsigned pos, StringRef ArgName,
1302  StringRef Arg) override {
1303  typename ParserClass::parser_data_type Val =
1304  typename ParserClass::parser_data_type();
1305  if (Parser.parse(*this, ArgName, Arg, Val))
1306  return true; // Parse error!
1307  this->setValue(Val);
1308  this->setPosition(pos);
1309  return false;
1310  }
1311 
1312  enum ValueExpected getValueExpectedFlagDefault() const override {
1313  return Parser.getValueExpectedFlagDefault();
1314  }
1315 
1316  void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1317  return Parser.getExtraOptionNames(OptionNames);
1318  }
1319 
1320  // Forward printing stuff to the parser...
1321  size_t getOptionWidth() const override {
1322  return Parser.getOptionWidth(*this);
1323  }
1324 
1325  void printOptionInfo(size_t GlobalWidth) const override {
1326  Parser.printOptionInfo(*this, GlobalWidth);
1327  }
1328 
1329  void printOptionValue(size_t GlobalWidth, bool Force) const override {
1330  if (Force || this->getDefault().compare(this->getValue())) {
1331  cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
1332  this->getDefault(), GlobalWidth);
1333  }
1334  }
1335 
1336  template <class T, class = typename std::enable_if<
1337  std::is_assignable<T&, T>::value>::type>
1338  void setDefaultImpl() {
1339  const OptionValue<DataType> &V = this->getDefault();
1340  if (V.hasValue())
1341  this->setValue(V.getValue());
1342  }
1343 
1344  template <class T, class = typename std::enable_if<
1345  !std::is_assignable<T&, T>::value>::type>
1346  void setDefaultImpl(...) {}
1347 
1348  void setDefault() override { setDefaultImpl<DataType>(); }
1349 
1350  void done() {
1351  addArgument();
1352  Parser.initialize();
1353  }
1354 
1355 public:
1356  // Command line options should not be copyable
1357  opt(const opt &) = delete;
1358  opt &operator=(const opt &) = delete;
1359 
1360  // setInitialValue - Used by the cl::init modifier...
1361  void setInitialValue(const DataType &V) { this->setValue(V, true); }
1362 
1363  ParserClass &getParser() { return Parser; }
1364 
1365  template <class T> DataType &operator=(const T &Val) {
1366  this->setValue(Val);
1367  return this->getValue();
1368  }
1369 
1370  template <class... Mods>
1371  explicit opt(const Mods &... Ms)
1372  : Option(Optional, NotHidden), Parser(*this) {
1373  apply(this, Ms...);
1374  done();
1375  }
1376 };
1377 
1378 extern template class opt<unsigned>;
1379 extern template class opt<int>;
1380 extern template class opt<std::string>;
1381 extern template class opt<char>;
1382 extern template class opt<bool>;
1383 
1384 //===----------------------------------------------------------------------===//
1385 // list_storage class
1386 
1387 // Default storage class definition: external storage. This implementation
1388 // assumes the user will specify a variable to store the data into with the
1389 // cl::location(x) modifier.
1390 //
1391 template <class DataType, class StorageClass> class list_storage {
1392  StorageClass *Location = nullptr; // Where to store the object...
1393 
1394 public:
1395  list_storage() = default;
1396 
1398  if (Location)
1399  return O.error("cl::location(x) specified more than once!");
1400  Location = &L;
1401  return false;
1402  }
1403 
1404  template <class T> void addValue(const T &V) {
1405  assert(Location != 0 && "cl::location(...) not specified for a command "
1406  "line option with external storage!");
1407  Location->push_back(V);
1408  }
1409 };
1410 
1411 // Define how to hold a class type object, such as a string.
1412 // Originally this code inherited from std::vector. In transitioning to a new
1413 // API for command line options we should change this. The new implementation
1414 // of this list_storage specialization implements the minimum subset of the
1415 // std::vector API required for all the current clients.
1416 //
1417 // FIXME: Reduce this API to a more narrow subset of std::vector
1418 //
1419 template <class DataType> class list_storage<DataType, bool> {
1420  std::vector<DataType> Storage;
1421 
1422 public:
1423  using iterator = typename std::vector<DataType>::iterator;
1424 
1425  iterator begin() { return Storage.begin(); }
1426  iterator end() { return Storage.end(); }
1427 
1428  using const_iterator = typename std::vector<DataType>::const_iterator;
1429 
1430  const_iterator begin() const { return Storage.begin(); }
1431  const_iterator end() const { return Storage.end(); }
1432 
1433  using size_type = typename std::vector<DataType>::size_type;
1434 
1435  size_type size() const { return Storage.size(); }
1436 
1437  bool empty() const { return Storage.empty(); }
1438 
1439  void push_back(const DataType &value) { Storage.push_back(value); }
1440  void push_back(DataType &&value) { Storage.push_back(value); }
1441 
1442  using reference = typename std::vector<DataType>::reference;
1443  using const_reference = typename std::vector<DataType>::const_reference;
1444 
1445  reference operator[](size_type pos) { return Storage[pos]; }
1446  const_reference operator[](size_type pos) const { return Storage[pos]; }
1447 
1448  iterator erase(const_iterator pos) { return Storage.erase(pos); }
1450  return Storage.erase(first, last);
1451  }
1452 
1453  iterator erase(iterator pos) { return Storage.erase(pos); }
1455  return Storage.erase(first, last);
1456  }
1457 
1459  return Storage.insert(pos, value);
1460  }
1462  return Storage.insert(pos, value);
1463  }
1464 
1465  iterator insert(iterator pos, const DataType &value) {
1466  return Storage.insert(pos, value);
1467  }
1469  return Storage.insert(pos, value);
1470  }
1471 
1472  reference front() { return Storage.front(); }
1473  const_reference front() const { return Storage.front(); }
1474 
1475  operator std::vector<DataType>&() { return Storage; }
1476  operator ArrayRef<DataType>() { return Storage; }
1477  std::vector<DataType> *operator&() { return &Storage; }
1478  const std::vector<DataType> *operator&() const { return &Storage; }
1479 
1480  template <class T> void addValue(const T &V) { Storage.push_back(V); }
1481 };
1482 
1483 //===----------------------------------------------------------------------===//
1484 // list - A list of command line options.
1485 //
1486 template <class DataType, class StorageClass = bool,
1487  class ParserClass = parser<DataType>>
1488 class list : public Option, public list_storage<DataType, StorageClass> {
1489  std::vector<unsigned> Positions;
1490  ParserClass Parser;
1491 
1492  enum ValueExpected getValueExpectedFlagDefault() const override {
1493  return Parser.getValueExpectedFlagDefault();
1494  }
1495 
1496  void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1497  return Parser.getExtraOptionNames(OptionNames);
1498  }
1499 
1500  bool handleOccurrence(unsigned pos, StringRef ArgName,
1501  StringRef Arg) override {
1502  typename ParserClass::parser_data_type Val =
1503  typename ParserClass::parser_data_type();
1504  if (Parser.parse(*this, ArgName, Arg, Val))
1505  return true; // Parse Error!
1507  setPosition(pos);
1508  Positions.push_back(pos);
1509  return false;
1510  }
1511 
1512  // Forward printing stuff to the parser...
1513  size_t getOptionWidth() const override {
1514  return Parser.getOptionWidth(*this);
1515  }
1516 
1517  void printOptionInfo(size_t GlobalWidth) const override {
1518  Parser.printOptionInfo(*this, GlobalWidth);
1519  }
1520 
1521  // Unimplemented: list options don't currently store their default value.
1522  void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1523  }
1524 
1525  void setDefault() override {}
1526 
1527  void done() {
1528  addArgument();
1529  Parser.initialize();
1530  }
1531 
1532 public:
1533  // Command line options should not be copyable
1534  list(const list &) = delete;
1535  list &operator=(const list &) = delete;
1536 
1537  ParserClass &getParser() { return Parser; }
1538 
1539  unsigned getPosition(unsigned optnum) const {
1540  assert(optnum < this->size() && "Invalid option index");
1541  return Positions[optnum];
1542  }
1543 
1545 
1546  template <class... Mods>
1547  explicit list(const Mods &... Ms)
1548  : Option(ZeroOrMore, NotHidden), Parser(*this) {
1549  apply(this, Ms...);
1550  done();
1551  }
1552 };
1553 
1554 // multi_val - Modifier to set the number of additional values.
1555 struct multi_val {
1556  unsigned AdditionalVals;
1557  explicit multi_val(unsigned N) : AdditionalVals(N) {}
1558 
1559  template <typename D, typename S, typename P>
1560  void apply(list<D, S, P> &L) const {
1561  L.setNumAdditionalVals(AdditionalVals);
1562  }
1563 };
1564 
1565 //===----------------------------------------------------------------------===//
1566 // bits_storage class
1567 
1568 // Default storage class definition: external storage. This implementation
1569 // assumes the user will specify a variable to store the data into with the
1570 // cl::location(x) modifier.
1571 //
1572 template <class DataType, class StorageClass> class bits_storage {
1573  unsigned *Location = nullptr; // Where to store the bits...
1574 
1575  template <class T> static unsigned Bit(const T &V) {
1576  unsigned BitPos = reinterpret_cast<unsigned>(V);
1577  assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1578  "enum exceeds width of bit vector!");
1579  return 1 << BitPos;
1580  }
1581 
1582 public:
1583  bits_storage() = default;
1584 
1585  bool setLocation(Option &O, unsigned &L) {
1586  if (Location)
1587  return O.error("cl::location(x) specified more than once!");
1588  Location = &L;
1589  return false;
1590  }
1591 
1592  template <class T> void addValue(const T &V) {
1593  assert(Location != 0 && "cl::location(...) not specified for a command "
1594  "line option with external storage!");
1595  *Location |= Bit(V);
1596  }
1597 
1598  unsigned getBits() { return *Location; }
1599 
1600  template <class T> bool isSet(const T &V) {
1601  return (*Location & Bit(V)) != 0;
1602  }
1603 };
1604 
1605 // Define how to hold bits. Since we can inherit from a class, we do so.
1606 // This makes us exactly compatible with the bits in all cases that it is used.
1607 //
1608 template <class DataType> class bits_storage<DataType, bool> {
1609  unsigned Bits; // Where to store the bits...
1610 
1611  template <class T> static unsigned Bit(const T &V) {
1612  unsigned BitPos = (unsigned)V;
1613  assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1614  "enum exceeds width of bit vector!");
1615  return 1 << BitPos;
1616  }
1617 
1618 public:
1619  template <class T> void addValue(const T &V) { Bits |= Bit(V); }
1620 
1621  unsigned getBits() { return Bits; }
1622 
1623  template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
1624 };
1625 
1626 //===----------------------------------------------------------------------===//
1627 // bits - A bit vector of command options.
1628 //
1629 template <class DataType, class Storage = bool,
1630  class ParserClass = parser<DataType>>
1631 class bits : public Option, public bits_storage<DataType, Storage> {
1632  std::vector<unsigned> Positions;
1633  ParserClass Parser;
1634 
1635  enum ValueExpected getValueExpectedFlagDefault() const override {
1636  return Parser.getValueExpectedFlagDefault();
1637  }
1638 
1639  void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1640  return Parser.getExtraOptionNames(OptionNames);
1641  }
1642 
1643  bool handleOccurrence(unsigned pos, StringRef ArgName,
1644  StringRef Arg) override {
1645  typename ParserClass::parser_data_type Val =
1646  typename ParserClass::parser_data_type();
1647  if (Parser.parse(*this, ArgName, Arg, Val))
1648  return true; // Parse Error!
1649  this->addValue(Val);
1650  setPosition(pos);
1651  Positions.push_back(pos);
1652  return false;
1653  }
1654 
1655  // Forward printing stuff to the parser...
1656  size_t getOptionWidth() const override {
1657  return Parser.getOptionWidth(*this);
1658  }
1659 
1660  void printOptionInfo(size_t GlobalWidth) const override {
1661  Parser.printOptionInfo(*this, GlobalWidth);
1662  }
1663 
1664  // Unimplemented: bits options don't currently store their default values.
1665  void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1666  }
1667 
1668  void setDefault() override {}
1669 
1670  void done() {
1671  addArgument();
1672  Parser.initialize();
1673  }
1674 
1675 public:
1676  // Command line options should not be copyable
1677  bits(const bits &) = delete;
1678  bits &operator=(const bits &) = delete;
1679 
1680  ParserClass &getParser() { return Parser; }
1681 
1682  unsigned getPosition(unsigned optnum) const {
1683  assert(optnum < this->size() && "Invalid option index");
1684  return Positions[optnum];
1685  }
1686 
1687  template <class... Mods>
1688  explicit bits(const Mods &... Ms)
1689  : Option(ZeroOrMore, NotHidden), Parser(*this) {
1690  apply(this, Ms...);
1691  done();
1692  }
1693 };
1694 
1695 //===----------------------------------------------------------------------===//
1696 // Aliased command line option (alias this name to a preexisting name)
1697 //
1698 
1699 class alias : public Option {
1700  Option *AliasFor;
1701 
1702  bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1703  StringRef Arg) override {
1704  return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1705  }
1706 
1707  bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
1708  bool MultiArg = false) override {
1709  return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
1710  }
1711 
1712  // Handle printing stuff...
1713  size_t getOptionWidth() const override;
1714  void printOptionInfo(size_t GlobalWidth) const override;
1715 
1716  // Aliases do not need to print their values.
1717  void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1718  }
1719 
1720  void setDefault() override { AliasFor->setDefault(); }
1721 
1722  ValueExpected getValueExpectedFlagDefault() const override {
1723  return AliasFor->getValueExpectedFlag();
1724  }
1725 
1726  void done() {
1727  if (!hasArgStr())
1728  error("cl::alias must have argument name specified!");
1729  if (!AliasFor)
1730  error("cl::alias must have an cl::aliasopt(option) specified!");
1731  Subs = AliasFor->Subs;
1732  addArgument();
1733  }
1734 
1735 public:
1736  // Command line options should not be copyable
1737  alias(const alias &) = delete;
1738  alias &operator=(const alias &) = delete;
1739 
1741  if (AliasFor)
1742  error("cl::alias must only have one cl::aliasopt(...) specified!");
1743  AliasFor = &O;
1744  }
1745 
1746  template <class... Mods>
1747  explicit alias(const Mods &... Ms)
1748  : Option(Optional, Hidden), AliasFor(nullptr) {
1749  apply(this, Ms...);
1750  done();
1751  }
1752 };
1753 
1754 // aliasfor - Modifier to set the option an alias aliases.
1755 struct aliasopt {
1757 
1758  explicit aliasopt(Option &O) : Opt(O) {}
1759 
1760  void apply(alias &A) const { A.setAliasFor(Opt); }
1761 };
1762 
1763 // extrahelp - provide additional help at the end of the normal help
1764 // output. All occurrences of cl::extrahelp will be accumulated and
1765 // printed to stderr at the end of the regular help, just before
1766 // exit is called.
1767 struct extrahelp {
1769 
1770  explicit extrahelp(StringRef help);
1771 };
1772 
1773 void PrintVersionMessage();
1774 
1775 /// This function just prints the help message, exactly the same way as if the
1776 /// -help or -help-hidden option had been given on the command line.
1777 ///
1778 /// \param Hidden if true will print hidden options
1779 /// \param Categorized if true print options in categories
1780 void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
1781 
1782 //===----------------------------------------------------------------------===//
1783 // Public interface for accessing registered options.
1784 //
1785 
1786 /// Use this to get a StringMap to all registered named options
1787 /// (e.g. -help). Note \p Map Should be an empty StringMap.
1788 ///
1789 /// \return A reference to the StringMap used by the cl APIs to parse options.
1790 ///
1791 /// Access to unnamed arguments (i.e. positional) are not provided because
1792 /// it is expected that the client already has access to these.
1793 ///
1794 /// Typical usage:
1795 /// \code
1796 /// main(int argc,char* argv[]) {
1797 /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
1798 /// assert(opts.count("help") == 1)
1799 /// opts["help"]->setDescription("Show alphabetical help information")
1800 /// // More code
1801 /// llvm::cl::ParseCommandLineOptions(argc,argv);
1802 /// //More code
1803 /// }
1804 /// \endcode
1805 ///
1806 /// This interface is useful for modifying options in libraries that are out of
1807 /// the control of the client. The options should be modified before calling
1808 /// llvm::cl::ParseCommandLineOptions().
1809 ///
1810 /// Hopefully this API can be deprecated soon. Any situation where options need
1811 /// to be modified by tools or libraries should be handled by sane APIs rather
1812 /// than just handing around a global list.
1813 StringMap<Option *> &getRegisteredOptions(SubCommand &Sub = *TopLevelSubCommand);
1814 
1815 /// Use this to get all registered SubCommands from the provided parser.
1816 ///
1817 /// \return A range of all SubCommand pointers registered with the parser.
1818 ///
1819 /// Typical usage:
1820 /// \code
1821 /// main(int argc, char* argv[]) {
1822 /// llvm::cl::ParseCommandLineOptions(argc, argv);
1823 /// for (auto* S : llvm::cl::getRegisteredSubcommands()) {
1824 /// if (*S) {
1825 /// std::cout << "Executing subcommand: " << S->getName() << std::endl;
1826 /// // Execute some function based on the name...
1827 /// }
1828 /// }
1829 /// }
1830 /// \endcode
1831 ///
1832 /// This interface is useful for defining subcommands in libraries and
1833 /// the dispatch from a single point (like in the main function).
1836 
1837 //===----------------------------------------------------------------------===//
1838 // Standalone command line processing utilities.
1839 //
1840 
1841 /// Tokenizes a command line that can contain escapes and quotes.
1842 //
1843 /// The quoting rules match those used by GCC and other tools that use
1844 /// libiberty's buildargv() or expandargv() utilities, and do not match bash.
1845 /// They differ from buildargv() on treatment of backslashes that do not escape
1846 /// a special character to make it possible to accept most Windows file paths.
1847 ///
1848 /// \param [in] Source The string to be split on whitespace with quotes.
1849 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1850 /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1851 /// lines and end of the response file to be marked with a nullptr string.
1852 /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1855  bool MarkEOLs = false);
1856 
1857 /// Tokenizes a Windows command line which may contain quotes and escaped
1858 /// quotes.
1859 ///
1860 /// See MSDN docs for CommandLineToArgvW for information on the quoting rules.
1861 /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
1862 ///
1863 /// \param [in] Source The string to be split on whitespace with quotes.
1864 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1865 /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1866 /// lines and end of the response file to be marked with a nullptr string.
1867 /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1870  bool MarkEOLs = false);
1871 
1872 /// String tokenization function type. Should be compatible with either
1873 /// Windows or Unix command line tokenizers.
1874 using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver,
1876  bool MarkEOLs);
1877 
1878 /// Tokenizes content of configuration file.
1879 ///
1880 /// \param [in] Source The string representing content of config file.
1881 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1882 /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1883 /// \param [in] MarkEOLs Added for compatibility with TokenizerCallback.
1884 ///
1885 /// It works like TokenizeGNUCommandLine with ability to skip comment lines.
1886 ///
1887 void tokenizeConfigFile(StringRef Source, StringSaver &Saver,
1889  bool MarkEOLs = false);
1890 
1891 /// Reads command line options from the given configuration file.
1892 ///
1893 /// \param [in] CfgFileName Path to configuration file.
1894 /// \param [in] Saver Objects that saves allocated strings.
1895 /// \param [out] Argv Array to which the read options are added.
1896 /// \return true if the file was successfully read.
1897 ///
1898 /// It reads content of the specified file, tokenizes it and expands "@file"
1899 /// commands resolving file names in them relative to the directory where
1900 /// CfgFilename resides.
1901 ///
1902 bool readConfigFile(StringRef CfgFileName, StringSaver &Saver,
1904 
1905 /// Expand response files on a command line recursively using the given
1906 /// StringSaver and tokenization strategy. Argv should contain the command line
1907 /// before expansion and will be modified in place. If requested, Argv will
1908 /// also be populated with nullptrs indicating where each response file line
1909 /// ends, which is useful for the "/link" argument that needs to consume all
1910 /// remaining arguments only until the next end of line, when in a response
1911 /// file.
1912 ///
1913 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1914 /// \param [in] Tokenizer Tokenization strategy. Typically Unix or Windows.
1915 /// \param [in,out] Argv Command line into which to expand response files.
1916 /// \param [in] MarkEOLs Mark end of lines and the end of the response file
1917 /// with nullptrs in the Argv vector.
1918 /// \param [in] RelativeNames true if names of nested response files must be
1919 /// resolved relative to including file.
1920 /// \return true if all @files were expanded successfully or there were none.
1921 bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
1923  bool MarkEOLs = false, bool RelativeNames = false);
1924 
1925 /// Mark all options not part of this category as cl::ReallyHidden.
1926 ///
1927 /// \param Category the category of options to keep displaying
1928 ///
1929 /// Some tools (like clang-format) like to be able to hide all options that are
1930 /// not specific to the tool. This function allows a tool to specify a single
1931 /// option category to display in the -help output.
1933  SubCommand &Sub = *TopLevelSubCommand);
1934 
1935 /// Mark all options not part of the categories as cl::ReallyHidden.
1936 ///
1937 /// \param Categories the categories of options to keep displaying.
1938 ///
1939 /// Some tools (like clang-format) like to be able to hide all options that are
1940 /// not specific to the tool. This function allows a tool to specify a single
1941 /// option category to display in the -help output.
1943  SubCommand &Sub = *TopLevelSubCommand);
1944 
1945 /// Reset all command line options to a state that looks as if they have
1946 /// never appeared on the command line. This is useful for being able to parse
1947 /// a command line multiple times (especially useful for writing tests).
1949 
1950 /// Reset the command line parser back to its initial state. This
1951 /// removes
1952 /// all options, categories, and subcommands and returns the parser to a state
1953 /// where no options are supported.
1954 void ResetCommandLineParser();
1955 
1956 } // end namespace cl
1957 
1958 } // end namespace llvm
1959 
1960 #endif // LLVM_SUPPORT_COMMANDLINE_H
iterator erase(const_iterator pos)
Definition: CommandLine.h:1448
void addValue(const T &V)
Definition: CommandLine.h:1404
uint64_t CallInst * C
bool isPositional() const
Definition: CommandLine.h:303
void setValue(const T &V, bool initial=false)
Definition: CommandLine.h:1220
void push_back(const T &Elt)
Definition: SmallVector.h:218
SmallVector< OptionInfo, 8 > Values
Definition: CommandLine.h:760
void print(const Option &O, const parser< DT > &P, const DT &V, const OptionValue< DT > &Default, size_t GlobalWidth)
Definition: CommandLine.h:1112
aliasopt(Option &O)
Definition: CommandLine.h:1758
ParserClass & getParser()
Definition: CommandLine.h:1537
void ResetCommandLineParser()
Reset the command line parser back to its initial state.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
enum ValueExpected getValueExpectedFlag() const
Definition: CommandLine.h:285
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
OptionValue< std::string > & operator=(const std::string &V)
Definition: CommandLine.h:592
OptionCategory GeneralCategory
OptionValue< cl::boolOrDefault > & operator=(const cl::boolOrDefault &V)
Definition: CommandLine.h:575
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
void push_back(const DataType &value)
Definition: CommandLine.h:1439
bool parse(Option &, StringRef, StringRef Arg, char &Value)
Definition: CommandLine.h:1069
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const GenericOptionValue & getOptionValue(unsigned N) const override
Definition: CommandLine.h:775
OptionValue(const DataType &V)
Definition: CommandLine.h:555
void apply(Option &O) const
Definition: CommandLine.h:395
bool compare(const DataType &V) const
Definition: CommandLine.h:526
bool isConsumeAfter() const
Definition: CommandLine.h:306
unsigned getNumOptions() const override
Definition: CommandLine.h:768
void PrintVersionMessage()
Utility function for printing version number.
void print(const Option &O, const parser< ParserDT > &P, const ValDT &, const OptionValue< ValDT > &, size_t GlobalWidth)
Definition: CommandLine.h:1103
void apply(Option &O) const
Definition: CommandLine.h:405
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void setValueStr(StringRef S)
Definition: CommandLine.h:321
void setValue(const T &V, bool initial=false)
Definition: CommandLine.h:1250
void ResetAllOptionOccurrences()
Reset all command line options to a state that looks as if they have never appeared on the command li...
void tokenizeConfigFile(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char *> &NewArgv, bool MarkEOLs=false)
Tokenizes content of configuration file.
iterator insert(iterator pos, const DataType &value)
Definition: CommandLine.h:1465
demanded bits
#define error(X)
const OptionValue< DataType > & getDefault() const
Definition: CommandLine.h:1259
APInt operator &(APInt a, const APInt &b)
Definition: APInt.h:1973
void ParseEnvironmentOptions(const char *progName, const char *envvar, const char *Overview="")
ParseEnvironmentOptions - An alternative entry point to the CommandLine library, which allows you to ...
block Block Frequency true
SmallVector< Option *, 4 > SinkOpts
Definition: CommandLine.h:228
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:354
GenericOptionInfo(StringRef name, StringRef helpStr)
Definition: CommandLine.h:659
bool compare(const GenericOptionValue &V) const override
Definition: CommandLine.h:528
StringRef getValueName() const override
Definition: CommandLine.h:956
static void opt(OptionHidden OH, Option &O)
Definition: CommandLine.h:1169
StringMap< Option * > & getRegisteredOptions(SubCommand &Sub= *TopLevelSubCommand)
Use this to get a StringMap to all registered named options (e.g.
bool readConfigFile(StringRef CfgFileName, StringSaver &Saver, SmallVectorImpl< const char *> &Argv)
Reads command line options from the given configuration file.
bool compare(const DataType &) const
Definition: CommandLine.h:491
bool parse(Option &, StringRef, StringRef Arg, std::string &Value)
Definition: CommandLine.h:1044
StringRef getValueName() const override
Definition: CommandLine.h:1003
Option(enum NumOccurrencesFlag OccurrencesFlag, enum OptionHidden Hidden)
Definition: CommandLine.h:332
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Definition: BitVector.h:938
void getExtraOptionNames(SmallVectorImpl< StringRef > &OptionNames)
Definition: CommandLine.h:709
StringRef getValueName() const override
Definition: CommandLine.h:1025
unsigned getPosition(unsigned optnum) const
Definition: CommandLine.h:1682
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void setNumAdditionalVals(unsigned n)
Definition: CommandLine.h:337
void apply(Opt &O) const
Definition: CommandLine.h:630
ParserClass & getParser()
Definition: CommandLine.h:1680
void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V, const OptionValue< DT > &Default, size_t GlobalWidth)
Definition: CommandLine.h:1094
void apply(Opt *O, const Mod &M, const Mods &... Ms)
Definition: CommandLine.h:1182
OptionValue< DataType > V
Definition: CommandLine.h:758
void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char *> &NewArgv, bool MarkEOLs=false)
Tokenizes a Windows command line which may contain quotes and escaped quotes.
DataType & getValue()
Definition: CommandLine.h:1227
Position
Position to insert a new instruction relative to an existing instruction.
void getExtraOptionNames(SmallVectorImpl< StringRef > &)
Definition: CommandLine.h:826
cat(OptionCategory &c)
Definition: CommandLine.h:444
StringRef getDescription() const
Definition: CommandLine.h:195
virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value, bool MultiArg=false)
static void opt(StringRef Str, Opt &O)
Definition: CommandLine.h:1153
OptionValue(const cl::boolOrDefault &V)
Definition: CommandLine.h:573
StringRef getValueName() const override
Definition: CommandLine.h:886
NumOccurrencesFlag
Definition: CommandLine.h:120
std::function< void(raw_ostream &)> VersionPrinterTy
Definition: CommandLine.h:80
SmallVector< Option *, 4 > PositionalOpts
Definition: CommandLine.h:227
StringRef getValueName() const override
Definition: CommandLine.h:1050
iterator erase(iterator first, iterator last)
Definition: CommandLine.h:1454
StringRef Desc
Definition: CommandLine.h:391
#define T
void apply(Opt &O) const
Definition: CommandLine.h:455
StringRef getValueName() const override
Definition: CommandLine.h:981
typename std::vector< DataType >::size_type size_type
Definition: CommandLine.h:1433
void PrintOptionValues()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
StringRef getValueName() const override
Definition: CommandLine.h:934
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
multi_val(unsigned N)
Definition: CommandLine.h:1557
iterator insert(iterator pos, DataType &&value)
Definition: CommandLine.h:1468
void apply(Opt &O) const
Definition: CommandLine.h:432
StringRef getName() const
Definition: CommandLine.h:224
ParserClass & getParser()
Definition: CommandLine.h:1363
reference operator[](size_type pos)
Definition: CommandLine.h:1445
ManagedStatic< SubCommand > AllSubCommands
bool setLocation(Option &O, DataType &L)
Definition: CommandLine.h:1212
StringMap< Option * > OptionsMap
Definition: CommandLine.h:229
ManagedStatic< SubCommand > TopLevelSubCommand
void HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub= *TopLevelSubCommand)
Mark all options not part of this category as cl::ReallyHidden.
static void opt(const Mod &M, Opt &O)
Definition: CommandLine.h:1138
const DataType & getValue() const
Definition: CommandLine.h:516
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:419
bool isSet(const T &V)
Definition: CommandLine.h:1600
void apply(Opt &O) const
Definition: CommandLine.h:446
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:639
bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V)
Definition: CommandLine.h:780
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
void setNumOccurrencesFlag(enum NumOccurrencesFlag Val)
Definition: CommandLine.h:322
SubCommand(StringRef Name, StringRef Description="")
Definition: CommandLine.h:214
unsigned getNumAdditionalVals() const
Definition: CommandLine.h:299
void PrintHelpMessage(bool Hidden=false, bool Categorized=false)
This function just prints the help message, exactly the same way as if the -help or -help-hidden opti...
value_desc(StringRef Str)
Definition: CommandLine.h:403
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
typename std::vector< DataType >::iterator iterator
Definition: CommandLine.h:1423
bool setLocation(Option &O, StorageClass &L)
Definition: CommandLine.h:1397
StringRef ValueStr
Definition: CommandLine.h:276
int getNumOccurrences() const
Definition: CommandLine.h:380
void setHiddenFlag(enum OptionHidden Val)
Definition: CommandLine.h:324
typename std::vector< DataType >::reference reference
Definition: CommandLine.h:1442
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1055
OptionCategory * Category
Definition: CommandLine.h:277
const OptionValue< DataType > & getDefault() const
Definition: CommandLine.h:1283
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr)
void SetVersionPrinter(VersionPrinterTy func)
===------------------------------------------------------------------—===// SetVersionPrinter - Over...
StringRef getDescription() const
Definition: CommandLine.h:225
void setPosition(unsigned pos)
Definition: CommandLine.h:327
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:505
bool isInAllSubCommands() const
Definition: CommandLine.h:310
void setValue(const DataType &V)
Definition: CommandLine.h:521
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:31
iterator erase(const_iterator CI)
Definition: SmallVector.h:445
Value(Type *Ty, unsigned scid)
Definition: Value.cpp:54
size_t size() const
Definition: SmallVector.h:53
void setCategory(OptionCategory &C)
Definition: CommandLine.h:328
iterator insert(const_iterator pos, DataType &&value)
Definition: CommandLine.h:1461
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool setLocation(Option &O, unsigned &L)
Definition: CommandLine.h:1585
unsigned first
void removeLiteralOption(StringRef Name)
removeLiteralOption - Remove the specified option.
Definition: CommandLine.h:808
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1029
void setFormattingFlag(enum FormattingFlags V)
Definition: CommandLine.h:325
static void opt(FormattingFlags FF, Option &O)
Definition: CommandLine.h:1173
OptionValue(const std::string &V)
Definition: CommandLine.h:590
void setInitialValue(const DataType &V)
Definition: CommandLine.h:1361
void setValueExpectedFlag(enum ValueExpected Val)
Definition: CommandLine.h:323
void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char *> &NewArgv, bool MarkEOLs=false)
Tokenizes a command line that can contain escapes and quotes.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
const DataType & getValue() const
Definition: CommandLine.h:1231
void apply(list< D, S, P > &L) const
Definition: CommandLine.h:1560
void setDescription(StringRef S)
Definition: CommandLine.h:320
StringRef getValueName() const override
Definition: CommandLine.h:912
void setValue(const T &V, bool initial=false)
Definition: CommandLine.h:1275
unsigned getPosition() const
Definition: CommandLine.h:298
bool error(const Twine &Message, raw_ostream &Errs)
Definition: CommandLine.h:376
CHAIN = SC CHAIN, Imm128 - System call.
void setAliasFor(Option &O)
Definition: CommandLine.h:1740
OptionInfo(StringRef name, DataType v, StringRef helpStr)
Definition: CommandLine.h:755
void apply(Opt &O) const
Definition: CommandLine.h:416
void setNumAdditionalVals(unsigned n)
Definition: CommandLine.h:1544
virtual void setDefault()=0
void printOptionDiff(const Option &O, const AnyOptionValue &V, const AnyOptionValue &Default, size_t GlobalWidth) const
Definition: CommandLine.h:701
The access may modify the value stored in memory.
StringRef ArgStr
Definition: CommandLine.h:274
A range adaptor for a pair of iterators.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
const DataType & getValue() const
Definition: CommandLine.h:486
sub(SubCommand &S)
Definition: CommandLine.h:453
void AddLiteralOption(Option &O, StringRef Name)
Adds a new option for parsing and provides the option it refers to.
void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr)
addLiteralOption - Add an entry to the mapping table.
Definition: CommandLine.h:799
amdgpu Simplify well known AMD library false Value Value * Arg
ValuesClass(std::initializer_list< OptionEnumValue > Options)
Definition: CommandLine.h:627
static void opt(StringRef Str, Opt &O)
Definition: CommandLine.h:1148
iterator erase(const_iterator first, const_iterator last)
Definition: CommandLine.h:1449
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Definition: StringSaver.h:22
StringRef HelpStr
Definition: CommandLine.h:275
StringRef getValueName() const override
Definition: CommandLine.h:1075
void(*)(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs) TokenizerCallback
String tokenization function type.
Definition: CommandLine.h:1876
static void opt(MiscFlags MF, Option &O)
Definition: CommandLine.h:1177
virtual void getExtraOptionNames(SmallVectorImpl< StringRef > &)
Definition: CommandLine.h:367
void setMiscFlag(enum MiscFlags M)
Definition: CommandLine.h:326
OptionCategory(StringRef const Name, StringRef const Description="")
Definition: CommandLine.h:188
#define N
static void opt(ValueExpected VE, Option &O)
Definition: CommandLine.h:1165
SmallPtrSet< SubCommand *, 4 > Subs
Definition: CommandLine.h:278
bool error(const Twine &Message, StringRef ArgName=StringRef(), raw_ostream &Errs=llvm::errs())
void setValue(const DT &)
Definition: CommandLine.h:489
bool compare(const GenericOptionValue &) const override
Definition: CommandLine.h:493
DataType & operator=(const T &Val)
Definition: CommandLine.h:1365
opt(const Mods &... Ms)
Definition: CommandLine.h:1371
desc(StringRef Str)
Definition: CommandLine.h:393
bool hasArgStr() const
Definition: CommandLine.h:302
iterator insert(const_iterator pos, const DataType &value)
Definition: CommandLine.h:1458
typename std::vector< DataType >::const_iterator const_iterator
Definition: CommandLine.h:1428
typename std::vector< DataType >::const_reference const_reference
Definition: CommandLine.h:1443
SubCommand & Sub
Definition: CommandLine.h:451
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
Definition: ScaledNumber.h:252
const_reference operator[](size_type pos) const
Definition: CommandLine.h:1446
static void opt(NumOccurrencesFlag N, Option &O)
Definition: CommandLine.h:1159
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Definition: JSON.cpp:592
const OptionValue< DataType > & getDefault() const
Definition: CommandLine.h:1238
list(const Mods &... Ms)
Definition: CommandLine.h:1547
LLVM Value Representation.
Definition: Value.h:73
StringRef getOption(unsigned N) const override
Definition: CommandLine.h:769
static const char * name
void AddExtraVersionPrinter(VersionPrinterTy func)
===------------------------------------------------------------------—===// AddExtraVersionPrinter -...
StringRef getDescription(unsigned N) const override
Definition: CommandLine.h:770
OptionValue< DataType > & operator=(const DT &V)
Definition: CommandLine.h:558
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void addSubCommand(SubCommand &S)
Definition: CommandLine.h:329
bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, SmallVectorImpl< const char *> &Argv, bool MarkEOLs=false, bool RelativeNames=false)
Expand response files on a command line recursively using the given StringSaver and tokenization stra...
unsigned getMiscFlags() const
Definition: CommandLine.h:297
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
hexagon cext opt
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
bits(const Mods &... Ms)
Definition: CommandLine.h:1688
parser(Option &O)
Definition: CommandLine.h:763
static void opt(StringRef Str, Opt &O)
Definition: CommandLine.h:1143
iterator_range< typename SmallPtrSet< SubCommand *, 4 >::iterator > getRegisteredSubcommands()
Use this to get all registered SubCommands from the provided parser.
void addValue(const T &V)
Definition: CommandLine.h:1592
void apply(alias &A) const
Definition: CommandLine.h:1760
bool isSink() const
Definition: CommandLine.h:304
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:435
StringRef getName() const
Definition: CommandLine.h:194
alias(const Mods &... Ms)
Definition: CommandLine.h:1747
initializer(const Ty &Val)
Definition: CommandLine.h:414
virtual StringRef getValueName() const
Definition: CommandLine.h:843
unsigned getPosition(unsigned optnum) const
Definition: CommandLine.h:1539
OptionCategory & Category
Definition: CommandLine.h:442