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