File indexing completed on 2026-05-10 08:44:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #ifndef LLVM_SUPPORT_COMMANDLINE_H
0020 #define LLVM_SUPPORT_COMMANDLINE_H
0021
0022 #include "llvm/ADT/ArrayRef.h"
0023 #include "llvm/ADT/STLExtras.h"
0024 #include "llvm/ADT/SmallPtrSet.h"
0025 #include "llvm/ADT/SmallVector.h"
0026 #include "llvm/ADT/StringMap.h"
0027 #include "llvm/ADT/StringRef.h"
0028 #include "llvm/ADT/Twine.h"
0029 #include "llvm/ADT/iterator_range.h"
0030 #include "llvm/Support/ErrorHandling.h"
0031 #include "llvm/Support/StringSaver.h"
0032 #include "llvm/Support/raw_ostream.h"
0033 #include <cassert>
0034 #include <climits>
0035 #include <cstddef>
0036 #include <functional>
0037 #include <initializer_list>
0038 #include <string>
0039 #include <type_traits>
0040 #include <vector>
0041
0042 namespace llvm {
0043
0044 namespace vfs {
0045 class FileSystem;
0046 }
0047
0048 class StringSaver;
0049
0050
0051
0052 namespace cl {
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 bool ParseCommandLineOptions(int argc, const char *const *argv,
0069 StringRef Overview = "",
0070 raw_ostream *Errs = nullptr,
0071 const char *EnvVar = nullptr,
0072 bool LongOptionsUseDoubleDash = false);
0073
0074
0075 using VersionPrinterTy = std::function<void(raw_ostream &)>;
0076
0077
0078
0079
0080
0081 void SetVersionPrinter(VersionPrinterTy func);
0082
0083
0084
0085
0086
0087
0088 void AddExtraVersionPrinter(VersionPrinterTy func);
0089
0090
0091
0092
0093
0094 void PrintOptionValues();
0095
0096
0097 class Option;
0098
0099
0100
0101
0102
0103
0104
0105
0106 void AddLiteralOption(Option &O, StringRef Name);
0107
0108
0109
0110
0111
0112 enum NumOccurrencesFlag {
0113 Optional = 0x00,
0114 ZeroOrMore = 0x01,
0115 Required = 0x02,
0116 OneOrMore = 0x03,
0117
0118
0119
0120
0121
0122
0123
0124
0125 ConsumeAfter = 0x04
0126 };
0127
0128 enum ValueExpected {
0129
0130 ValueOptional = 0x01,
0131 ValueRequired = 0x02,
0132 ValueDisallowed = 0x03
0133 };
0134
0135 enum OptionHidden {
0136 NotHidden = 0x00,
0137 Hidden = 0x01,
0138 ReallyHidden = 0x02
0139 };
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 enum FormattingFlags {
0156 NormalFormatting = 0x00,
0157 Positional = 0x01,
0158 Prefix = 0x02,
0159 AlwaysPrefix = 0x03
0160 };
0161
0162 enum MiscFlags {
0163 CommaSeparated = 0x01,
0164 PositionalEatsArgs = 0x02,
0165 Sink = 0x04,
0166
0167
0168
0169
0170
0171 Grouping = 0x08,
0172
0173
0174 DefaultOption = 0x10
0175 };
0176
0177
0178
0179 class OptionCategory {
0180 private:
0181 StringRef const Name;
0182 StringRef const Description;
0183
0184 void registerCategory();
0185
0186 public:
0187 OptionCategory(StringRef const Name,
0188 StringRef const Description = "")
0189 : Name(Name), Description(Description) {
0190 registerCategory();
0191 }
0192
0193 StringRef getName() const { return Name; }
0194 StringRef getDescription() const { return Description; }
0195 };
0196
0197
0198 OptionCategory &getGeneralCategory();
0199
0200
0201
0202 class SubCommand {
0203 private:
0204 StringRef Name;
0205 StringRef Description;
0206
0207 protected:
0208 void registerSubCommand();
0209 void unregisterSubCommand();
0210
0211 public:
0212 SubCommand(StringRef Name, StringRef Description = "")
0213 : Name(Name), Description(Description) {
0214 registerSubCommand();
0215 }
0216 SubCommand() = default;
0217
0218
0219 static SubCommand &getTopLevel();
0220
0221
0222
0223 static SubCommand &getAll();
0224
0225 void reset();
0226
0227 explicit operator bool() const;
0228
0229 StringRef getName() const { return Name; }
0230 StringRef getDescription() const { return Description; }
0231
0232 SmallVector<Option *, 4> PositionalOpts;
0233 SmallVector<Option *, 4> SinkOpts;
0234 StringMap<Option *> OptionsMap;
0235
0236 Option *ConsumeAfterOpt = nullptr;
0237 };
0238
0239 class SubCommandGroup {
0240 SmallVector<SubCommand *, 4> Subs;
0241
0242 public:
0243 SubCommandGroup(std::initializer_list<SubCommand *> IL) : Subs(IL) {}
0244
0245 ArrayRef<SubCommand *> getSubCommands() const { return Subs; }
0246 };
0247
0248
0249
0250 class Option {
0251 friend class alias;
0252
0253
0254
0255
0256
0257 virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
0258 StringRef Arg) = 0;
0259
0260 virtual enum ValueExpected getValueExpectedFlagDefault() const {
0261 return ValueOptional;
0262 }
0263
0264
0265 virtual void anchor();
0266
0267 uint16_t NumOccurrences;
0268
0269
0270 uint16_t Occurrences : 3;
0271
0272
0273 uint16_t Value : 2;
0274 uint16_t HiddenFlag : 2;
0275 uint16_t Formatting : 2;
0276 uint16_t Misc : 5;
0277 uint16_t FullyInitialized : 1;
0278 uint16_t Position;
0279 uint16_t AdditionalVals;
0280
0281 public:
0282 StringRef ArgStr;
0283 StringRef HelpStr;
0284 StringRef ValueStr;
0285 SmallVector<OptionCategory *, 1>
0286 Categories;
0287 SmallPtrSet<SubCommand *, 1> Subs;
0288
0289 inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
0290 return (enum NumOccurrencesFlag)Occurrences;
0291 }
0292
0293 inline enum ValueExpected getValueExpectedFlag() const {
0294 return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
0295 }
0296
0297 inline enum OptionHidden getOptionHiddenFlag() const {
0298 return (enum OptionHidden)HiddenFlag;
0299 }
0300
0301 inline enum FormattingFlags getFormattingFlag() const {
0302 return (enum FormattingFlags)Formatting;
0303 }
0304
0305 inline unsigned getMiscFlags() const { return Misc; }
0306 inline unsigned getPosition() const { return Position; }
0307 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
0308
0309
0310 bool hasArgStr() const { return !ArgStr.empty(); }
0311 bool isPositional() const { return getFormattingFlag() == cl::Positional; }
0312 bool isSink() const { return getMiscFlags() & cl::Sink; }
0313 bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; }
0314
0315 bool isConsumeAfter() const {
0316 return getNumOccurrencesFlag() == cl::ConsumeAfter;
0317 }
0318
0319
0320
0321
0322 void setArgStr(StringRef S);
0323 void setDescription(StringRef S) { HelpStr = S; }
0324 void setValueStr(StringRef S) { ValueStr = S; }
0325 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
0326 void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
0327 void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
0328 void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
0329 void setMiscFlag(enum MiscFlags M) { Misc |= M; }
0330 void setPosition(unsigned pos) { Position = pos; }
0331 void addCategory(OptionCategory &C);
0332 void addSubCommand(SubCommand &S) { Subs.insert(&S); }
0333
0334 protected:
0335 explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
0336 enum OptionHidden Hidden)
0337 : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
0338 HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0),
0339 FullyInitialized(false), Position(0), AdditionalVals(0) {
0340 Categories.push_back(&getGeneralCategory());
0341 }
0342
0343 inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
0344
0345 public:
0346 virtual ~Option() = default;
0347
0348
0349
0350 void addArgument();
0351
0352
0353
0354
0355
0356 void removeArgument();
0357
0358
0359 virtual size_t getOptionWidth() const = 0;
0360
0361
0362
0363
0364 virtual void printOptionInfo(size_t GlobalWidth) const = 0;
0365
0366 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
0367
0368 virtual void setDefault() = 0;
0369
0370
0371
0372
0373
0374
0375 static void printHelpStr(StringRef HelpStr, size_t Indent,
0376 size_t FirstLineIndentedBy);
0377
0378
0379
0380
0381
0382
0383 static void printEnumValHelpStr(StringRef HelpStr, size_t Indent,
0384 size_t FirstLineIndentedBy);
0385
0386 virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
0387
0388
0389
0390 virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
0391 bool MultiArg = false);
0392
0393
0394 bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs());
0395 bool error(const Twine &Message, raw_ostream &Errs) {
0396 return error(Message, StringRef(), Errs);
0397 }
0398
0399 inline int getNumOccurrences() const { return NumOccurrences; }
0400 void reset();
0401 };
0402
0403
0404
0405
0406
0407
0408
0409 struct desc {
0410 StringRef Desc;
0411
0412 desc(StringRef Str) : Desc(Str) {}
0413
0414 void apply(Option &O) const { O.setDescription(Desc); }
0415 };
0416
0417
0418 struct value_desc {
0419 StringRef Desc;
0420
0421 value_desc(StringRef Str) : Desc(Str) {}
0422
0423 void apply(Option &O) const { O.setValueStr(Desc); }
0424 };
0425
0426
0427
0428
0429 template <class Ty> struct initializer {
0430 const Ty &Init;
0431 initializer(const Ty &Val) : Init(Val) {}
0432
0433 template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
0434 };
0435
0436 template <class Ty> struct list_initializer {
0437 ArrayRef<Ty> Inits;
0438 list_initializer(ArrayRef<Ty> Vals) : Inits(Vals) {}
0439
0440 template <class Opt> void apply(Opt &O) const { O.setInitialValues(Inits); }
0441 };
0442
0443 template <class Ty> initializer<Ty> init(const Ty &Val) {
0444 return initializer<Ty>(Val);
0445 }
0446
0447 template <class Ty>
0448 list_initializer<Ty> list_init(ArrayRef<Ty> Vals) {
0449 return list_initializer<Ty>(Vals);
0450 }
0451
0452
0453
0454
0455 template <class Ty> struct LocationClass {
0456 Ty &Loc;
0457
0458 LocationClass(Ty &L) : Loc(L) {}
0459
0460 template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
0461 };
0462
0463 template <class Ty> LocationClass<Ty> location(Ty &L) {
0464 return LocationClass<Ty>(L);
0465 }
0466
0467
0468 struct cat {
0469 OptionCategory &Category;
0470
0471 cat(OptionCategory &c) : Category(c) {}
0472
0473 template <class Opt> void apply(Opt &O) const { O.addCategory(Category); }
0474 };
0475
0476
0477 struct sub {
0478 SubCommand *Sub = nullptr;
0479 SubCommandGroup *Group = nullptr;
0480
0481 sub(SubCommand &S) : Sub(&S) {}
0482 sub(SubCommandGroup &G) : Group(&G) {}
0483
0484 template <class Opt> void apply(Opt &O) const {
0485 if (Sub)
0486 O.addSubCommand(*Sub);
0487 else if (Group)
0488 for (SubCommand *SC : Group->getSubCommands())
0489 O.addSubCommand(*SC);
0490 }
0491 };
0492
0493
0494
0495 template <typename R, typename Ty> struct cb {
0496 std::function<R(Ty)> CB;
0497
0498 cb(std::function<R(Ty)> CB) : CB(CB) {}
0499
0500 template <typename Opt> void apply(Opt &O) const { O.setCallback(CB); }
0501 };
0502
0503 namespace detail {
0504 template <typename F>
0505 struct callback_traits : public callback_traits<decltype(&F::operator())> {};
0506
0507 template <typename R, typename C, typename... Args>
0508 struct callback_traits<R (C::*)(Args...) const> {
0509 using result_type = R;
0510 using arg_type = std::tuple_element_t<0, std::tuple<Args...>>;
0511 static_assert(sizeof...(Args) == 1, "callback function must have one and only one parameter");
0512 static_assert(std::is_same_v<result_type, void>,
0513 "callback return type must be void");
0514 static_assert(std::is_lvalue_reference_v<arg_type> &&
0515 std::is_const_v<std::remove_reference_t<arg_type>>,
0516 "callback arg_type must be a const lvalue reference");
0517 };
0518 }
0519
0520 template <typename F>
0521 cb<typename detail::callback_traits<F>::result_type,
0522 typename detail::callback_traits<F>::arg_type>
0523 callback(F CB) {
0524 using result_type = typename detail::callback_traits<F>::result_type;
0525 using arg_type = typename detail::callback_traits<F>::arg_type;
0526 return cb<result_type, arg_type>(CB);
0527 }
0528
0529
0530
0531
0532 struct GenericOptionValue {
0533 virtual bool compare(const GenericOptionValue &V) const = 0;
0534
0535 protected:
0536 GenericOptionValue() = default;
0537 GenericOptionValue(const GenericOptionValue&) = default;
0538 GenericOptionValue &operator=(const GenericOptionValue &) = default;
0539 ~GenericOptionValue() = default;
0540
0541 private:
0542 virtual void anchor();
0543 };
0544
0545 template <class DataType> struct OptionValue;
0546
0547
0548
0549 template <class DataType, bool isClass>
0550 struct OptionValueBase : public GenericOptionValue {
0551
0552 using WrapperType = OptionValue<DataType>;
0553
0554 bool hasValue() const { return false; }
0555
0556 const DataType &getValue() const { llvm_unreachable("no default value"); }
0557
0558
0559 template <class DT> void setValue(const DT & ) {}
0560
0561
0562 bool compare(const DataType & ) const { return false; }
0563
0564 bool compare(const GenericOptionValue & ) const override {
0565 return false;
0566 }
0567
0568 protected:
0569 ~OptionValueBase() = default;
0570 };
0571
0572
0573 template <class DataType> class OptionValueCopy : public GenericOptionValue {
0574 DataType Value;
0575 bool Valid = false;
0576
0577 protected:
0578 OptionValueCopy(const OptionValueCopy&) = default;
0579 OptionValueCopy &operator=(const OptionValueCopy &) = default;
0580 ~OptionValueCopy() = default;
0581
0582 public:
0583 OptionValueCopy() = default;
0584
0585 bool hasValue() const { return Valid; }
0586
0587 const DataType &getValue() const {
0588 assert(Valid && "invalid option value");
0589 return Value;
0590 }
0591
0592 void setValue(const DataType &V) {
0593 Valid = true;
0594 Value = V;
0595 }
0596
0597
0598 bool compare(const DataType &V) const { return Valid && (Value == V); }
0599
0600 bool compare(const GenericOptionValue &V) const override {
0601 const OptionValueCopy<DataType> &VC =
0602 static_cast<const OptionValueCopy<DataType> &>(V);
0603 if (!VC.hasValue())
0604 return false;
0605 return compare(VC.getValue());
0606 }
0607 };
0608
0609
0610 template <class DataType>
0611 struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
0612 using WrapperType = DataType;
0613
0614 protected:
0615 OptionValueBase() = default;
0616 OptionValueBase(const OptionValueBase&) = default;
0617 OptionValueBase &operator=(const OptionValueBase &) = default;
0618 ~OptionValueBase() = default;
0619 };
0620
0621
0622 template <class DataType>
0623 struct OptionValue final
0624 : OptionValueBase<DataType, std::is_class_v<DataType>> {
0625 OptionValue() = default;
0626
0627 OptionValue(const DataType &V) { this->setValue(V); }
0628
0629
0630 template <class DT> OptionValue<DataType> &operator=(const DT &V) {
0631 this->setValue(V);
0632 return *this;
0633 }
0634 };
0635
0636
0637 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
0638 template <>
0639 struct OptionValue<cl::boolOrDefault> final
0640 : OptionValueCopy<cl::boolOrDefault> {
0641 using WrapperType = cl::boolOrDefault;
0642
0643 OptionValue() = default;
0644
0645 OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
0646
0647 OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) {
0648 setValue(V);
0649 return *this;
0650 }
0651
0652 private:
0653 void anchor() override;
0654 };
0655
0656 template <>
0657 struct OptionValue<std::string> final : OptionValueCopy<std::string> {
0658 using WrapperType = StringRef;
0659
0660 OptionValue() = default;
0661
0662 OptionValue(const std::string &V) { this->setValue(V); }
0663
0664 OptionValue<std::string> &operator=(const std::string &V) {
0665 setValue(V);
0666 return *this;
0667 }
0668
0669 private:
0670 void anchor() override;
0671 };
0672
0673
0674
0675
0676
0677
0678 struct OptionEnumValue {
0679 StringRef Name;
0680 int Value;
0681 StringRef Description;
0682 };
0683
0684 #define clEnumVal(ENUMVAL, DESC) \
0685 llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC }
0686 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) \
0687 llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC }
0688
0689
0690
0691
0692 class ValuesClass {
0693
0694
0695
0696 SmallVector<OptionEnumValue, 4> Values;
0697
0698 public:
0699 ValuesClass(std::initializer_list<OptionEnumValue> Options)
0700 : Values(Options) {}
0701
0702 template <class Opt> void apply(Opt &O) const {
0703 for (const auto &Value : Values)
0704 O.getParser().addLiteralOption(Value.Name, Value.Value,
0705 Value.Description);
0706 }
0707 };
0708
0709
0710
0711 template <typename... OptsTy> ValuesClass values(OptsTy... Options) {
0712 return ValuesClass({Options...});
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727 class generic_parser_base {
0728 protected:
0729 class GenericOptionInfo {
0730 public:
0731 GenericOptionInfo(StringRef name, StringRef helpStr)
0732 : Name(name), HelpStr(helpStr) {}
0733 StringRef Name;
0734 StringRef HelpStr;
0735 };
0736
0737 public:
0738 generic_parser_base(Option &O) : Owner(O) {}
0739
0740 virtual ~generic_parser_base() = default;
0741
0742
0743
0744
0745
0746 virtual unsigned getNumOptions() const = 0;
0747
0748
0749 virtual StringRef getOption(unsigned N) const = 0;
0750
0751
0752 virtual StringRef getDescription(unsigned N) const = 0;
0753
0754
0755 virtual size_t getOptionWidth(const Option &O) const;
0756
0757 virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
0758
0759
0760
0761
0762 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
0763
0764 void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
0765 const GenericOptionValue &Default,
0766 size_t GlobalWidth) const;
0767
0768
0769
0770
0771
0772 template <class AnyOptionValue>
0773 void printOptionDiff(const Option &O, const AnyOptionValue &V,
0774 const AnyOptionValue &Default,
0775 size_t GlobalWidth) const {
0776 printGenericOptionDiff(O, V, Default, GlobalWidth);
0777 }
0778
0779 void initialize() {}
0780
0781 void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
0782
0783
0784
0785 if (!Owner.hasArgStr())
0786 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
0787 OptionNames.push_back(getOption(i));
0788 }
0789
0790 enum ValueExpected getValueExpectedFlagDefault() const {
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802 if (Owner.hasArgStr())
0803 return ValueRequired;
0804 else
0805 return ValueDisallowed;
0806 }
0807
0808
0809
0810
0811 unsigned findOption(StringRef Name);
0812
0813 protected:
0814 Option &Owner;
0815 };
0816
0817
0818
0819
0820
0821
0822
0823 template <class DataType> class parser : public generic_parser_base {
0824 protected:
0825 class OptionInfo : public GenericOptionInfo {
0826 public:
0827 OptionInfo(StringRef name, DataType v, StringRef helpStr)
0828 : GenericOptionInfo(name, helpStr), V(v) {}
0829
0830 OptionValue<DataType> V;
0831 };
0832 SmallVector<OptionInfo, 8> Values;
0833
0834 public:
0835 parser(Option &O) : generic_parser_base(O) {}
0836
0837 using parser_data_type = DataType;
0838
0839
0840 unsigned getNumOptions() const override { return unsigned(Values.size()); }
0841 StringRef getOption(unsigned N) const override { return Values[N].Name; }
0842 StringRef getDescription(unsigned N) const override {
0843 return Values[N].HelpStr;
0844 }
0845
0846
0847 const GenericOptionValue &getOptionValue(unsigned N) const override {
0848 return Values[N].V;
0849 }
0850
0851
0852 bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
0853 StringRef ArgVal;
0854 if (Owner.hasArgStr())
0855 ArgVal = Arg;
0856 else
0857 ArgVal = ArgName;
0858
0859 for (size_t i = 0, e = Values.size(); i != e; ++i)
0860 if (Values[i].Name == ArgVal) {
0861 V = Values[i].V.getValue();
0862 return false;
0863 }
0864
0865 return O.error("Cannot find option named '" + ArgVal + "'!");
0866 }
0867
0868
0869
0870 template <class DT>
0871 void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
0872 #ifndef NDEBUG
0873 if (findOption(Name) != Values.size())
0874 report_fatal_error("Option '" + Name + "' already exists!");
0875 #endif
0876 OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
0877 Values.push_back(X);
0878 AddLiteralOption(Owner, Name);
0879 }
0880
0881
0882
0883 void removeLiteralOption(StringRef Name) {
0884 unsigned N = findOption(Name);
0885 assert(N != Values.size() && "Option not found!");
0886 Values.erase(Values.begin() + N);
0887 }
0888 };
0889
0890
0891
0892
0893 class basic_parser_impl {
0894 public:
0895 basic_parser_impl(Option &) {}
0896
0897 virtual ~basic_parser_impl() = default;
0898
0899 enum ValueExpected getValueExpectedFlagDefault() const {
0900 return ValueRequired;
0901 }
0902
0903 void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
0904
0905 void initialize() {}
0906
0907
0908 size_t getOptionWidth(const Option &O) const;
0909
0910
0911
0912
0913 void printOptionInfo(const Option &O, size_t GlobalWidth) const;
0914
0915
0916 void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
0917
0918
0919 virtual StringRef getValueName() const { return "value"; }
0920
0921
0922 virtual void anchor();
0923
0924 protected:
0925
0926 void printOptionName(const Option &O, size_t GlobalWidth) const;
0927 };
0928
0929
0930
0931
0932 template <class DataType> class basic_parser : public basic_parser_impl {
0933 public:
0934 using parser_data_type = DataType;
0935 using OptVal = OptionValue<DataType>;
0936
0937 basic_parser(Option &O) : basic_parser_impl(O) {}
0938 };
0939
0940
0941
0942 extern template class basic_parser<bool>;
0943
0944 template <> class parser<bool> : public basic_parser<bool> {
0945 public:
0946 parser(Option &O) : basic_parser(O) {}
0947
0948
0949 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
0950
0951 void initialize() {}
0952
0953 enum ValueExpected getValueExpectedFlagDefault() const {
0954 return ValueOptional;
0955 }
0956
0957
0958 StringRef getValueName() const override { return StringRef(); }
0959
0960 void printOptionDiff(const Option &O, bool V, OptVal Default,
0961 size_t GlobalWidth) const;
0962
0963
0964 void anchor() override;
0965 };
0966
0967
0968
0969 extern template class basic_parser<boolOrDefault>;
0970
0971 template <> class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
0972 public:
0973 parser(Option &O) : basic_parser(O) {}
0974
0975
0976 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
0977
0978 enum ValueExpected getValueExpectedFlagDefault() const {
0979 return ValueOptional;
0980 }
0981
0982
0983 StringRef getValueName() const override { return StringRef(); }
0984
0985 void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
0986 size_t GlobalWidth) const;
0987
0988
0989 void anchor() override;
0990 };
0991
0992
0993
0994 extern template class basic_parser<int>;
0995
0996 template <> class parser<int> : public basic_parser<int> {
0997 public:
0998 parser(Option &O) : basic_parser(O) {}
0999
1000
1001 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
1002
1003
1004 StringRef getValueName() const override { return "int"; }
1005
1006 void printOptionDiff(const Option &O, int V, OptVal Default,
1007 size_t GlobalWidth) const;
1008
1009
1010 void anchor() override;
1011 };
1012
1013
1014
1015 extern template class basic_parser<long>;
1016
1017 template <> class parser<long> final : public basic_parser<long> {
1018 public:
1019 parser(Option &O) : basic_parser(O) {}
1020
1021
1022 bool parse(Option &O, StringRef ArgName, StringRef Arg, long &Val);
1023
1024
1025 StringRef getValueName() const override { return "long"; }
1026
1027 void printOptionDiff(const Option &O, long V, OptVal Default,
1028 size_t GlobalWidth) const;
1029
1030
1031 void anchor() override;
1032 };
1033
1034
1035
1036 extern template class basic_parser<long long>;
1037
1038 template <> class parser<long long> : public basic_parser<long long> {
1039 public:
1040 parser(Option &O) : basic_parser(O) {}
1041
1042
1043 bool parse(Option &O, StringRef ArgName, StringRef Arg, long long &Val);
1044
1045
1046 StringRef getValueName() const override { return "long"; }
1047
1048 void printOptionDiff(const Option &O, long long V, OptVal Default,
1049 size_t GlobalWidth) const;
1050
1051
1052 void anchor() override;
1053 };
1054
1055
1056
1057 extern template class basic_parser<unsigned>;
1058
1059 template <> class parser<unsigned> : public basic_parser<unsigned> {
1060 public:
1061 parser(Option &O) : basic_parser(O) {}
1062
1063
1064 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
1065
1066
1067 StringRef getValueName() const override { return "uint"; }
1068
1069 void printOptionDiff(const Option &O, unsigned V, OptVal Default,
1070 size_t GlobalWidth) const;
1071
1072
1073 void anchor() override;
1074 };
1075
1076
1077
1078 extern template class basic_parser<unsigned long>;
1079
1080 template <>
1081 class parser<unsigned long> final : public basic_parser<unsigned long> {
1082 public:
1083 parser(Option &O) : basic_parser(O) {}
1084
1085
1086 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned long &Val);
1087
1088
1089 StringRef getValueName() const override { return "ulong"; }
1090
1091 void printOptionDiff(const Option &O, unsigned long V, OptVal Default,
1092 size_t GlobalWidth) const;
1093
1094
1095 void anchor() override;
1096 };
1097
1098
1099
1100 extern template class basic_parser<unsigned long long>;
1101
1102 template <>
1103 class parser<unsigned long long> : public basic_parser<unsigned long long> {
1104 public:
1105 parser(Option &O) : basic_parser(O) {}
1106
1107
1108 bool parse(Option &O, StringRef ArgName, StringRef Arg,
1109 unsigned long long &Val);
1110
1111
1112 StringRef getValueName() const override { return "ulong"; }
1113
1114 void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
1115 size_t GlobalWidth) const;
1116
1117
1118 void anchor() override;
1119 };
1120
1121
1122
1123 extern template class basic_parser<double>;
1124
1125 template <> class parser<double> : public basic_parser<double> {
1126 public:
1127 parser(Option &O) : basic_parser(O) {}
1128
1129
1130 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
1131
1132
1133 StringRef getValueName() const override { return "number"; }
1134
1135 void printOptionDiff(const Option &O, double V, OptVal Default,
1136 size_t GlobalWidth) const;
1137
1138
1139 void anchor() override;
1140 };
1141
1142
1143
1144 extern template class basic_parser<float>;
1145
1146 template <> class parser<float> : public basic_parser<float> {
1147 public:
1148 parser(Option &O) : basic_parser(O) {}
1149
1150
1151 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
1152
1153
1154 StringRef getValueName() const override { return "number"; }
1155
1156 void printOptionDiff(const Option &O, float V, OptVal Default,
1157 size_t GlobalWidth) const;
1158
1159
1160 void anchor() override;
1161 };
1162
1163
1164
1165 extern template class basic_parser<std::string>;
1166
1167 template <> class parser<std::string> : public basic_parser<std::string> {
1168 public:
1169 parser(Option &O) : basic_parser(O) {}
1170
1171
1172 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
1173 Value = Arg.str();
1174 return false;
1175 }
1176
1177
1178 StringRef getValueName() const override { return "string"; }
1179
1180 void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
1181 size_t GlobalWidth) const;
1182
1183
1184 void anchor() override;
1185 };
1186
1187
1188
1189 extern template class basic_parser<char>;
1190
1191 template <> class parser<char> : public basic_parser<char> {
1192 public:
1193 parser(Option &O) : basic_parser(O) {}
1194
1195
1196 bool parse(Option &, StringRef, StringRef Arg, char &Value) {
1197 Value = Arg[0];
1198 return false;
1199 }
1200
1201
1202 StringRef getValueName() const override { return "char"; }
1203
1204 void printOptionDiff(const Option &O, char V, OptVal Default,
1205 size_t GlobalWidth) const;
1206
1207
1208 void anchor() override;
1209 };
1210
1211
1212
1213
1214
1215
1216 template <class ParserClass, class DT>
1217 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
1218 const OptionValue<DT> &Default, size_t GlobalWidth) {
1219 OptionValue<DT> OV = V;
1220 P.printOptionDiff(O, OV, Default, GlobalWidth);
1221 }
1222
1223
1224
1225 template <class ParserDT, class ValDT> struct OptionDiffPrinter {
1226 void print(const Option &O, const parser<ParserDT> &P, const ValDT & ,
1227 const OptionValue<ValDT> & , size_t GlobalWidth) {
1228 P.printOptionNoValue(O, GlobalWidth);
1229 }
1230 };
1231
1232
1233
1234 template <class DT> struct OptionDiffPrinter<DT, DT> {
1235 void print(const Option &O, const parser<DT> &P, const DT &V,
1236 const OptionValue<DT> &Default, size_t GlobalWidth) {
1237 P.printOptionDiff(O, V, Default, GlobalWidth);
1238 }
1239 };
1240
1241
1242
1243 template <class ParserClass, class ValDT>
1244 void printOptionDiff(
1245 const Option &O,
1246 const basic_parser<typename ParserClass::parser_data_type> &P,
1247 const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
1248
1249 OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
1250 printer.print(O, static_cast<const ParserClass &>(P), V, Default,
1251 GlobalWidth);
1252 }
1253
1254
1255
1256
1257
1258
1259
1260 template <class Mod> struct applicator {
1261 template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
1262 };
1263
1264
1265 template <unsigned n> struct applicator<char[n]> {
1266 template <class Opt> static void opt(StringRef Str, Opt &O) {
1267 O.setArgStr(Str);
1268 }
1269 };
1270 template <unsigned n> struct applicator<const char[n]> {
1271 template <class Opt> static void opt(StringRef Str, Opt &O) {
1272 O.setArgStr(Str);
1273 }
1274 };
1275 template <> struct applicator<StringRef > {
1276 template <class Opt> static void opt(StringRef Str, Opt &O) {
1277 O.setArgStr(Str);
1278 }
1279 };
1280
1281 template <> struct applicator<NumOccurrencesFlag> {
1282 static void opt(NumOccurrencesFlag N, Option &O) {
1283 O.setNumOccurrencesFlag(N);
1284 }
1285 };
1286
1287 template <> struct applicator<ValueExpected> {
1288 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
1289 };
1290
1291 template <> struct applicator<OptionHidden> {
1292 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
1293 };
1294
1295 template <> struct applicator<FormattingFlags> {
1296 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
1297 };
1298
1299 template <> struct applicator<MiscFlags> {
1300 static void opt(MiscFlags MF, Option &O) {
1301 assert((MF != Grouping || O.ArgStr.size() == 1) &&
1302 "cl::Grouping can only apply to single character Options.");
1303 O.setMiscFlag(MF);
1304 }
1305 };
1306
1307
1308 template <class Opt, class Mod, class... Mods>
1309 void apply(Opt *O, const Mod &M, const Mods &... Ms) {
1310 applicator<Mod>::opt(M, *O);
1311 apply(O, Ms...);
1312 }
1313
1314 template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
1315 applicator<Mod>::opt(M, *O);
1316 }
1317
1318
1319
1320
1321
1322
1323 template <class DataType, bool ExternalStorage, bool isClass>
1324 class opt_storage {
1325 DataType *Location = nullptr;
1326 OptionValue<DataType> Default;
1327
1328 void check_location() const {
1329 assert(Location && "cl::location(...) not specified for a command "
1330 "line option with external storage, "
1331 "or cl::init specified before cl::location()!!");
1332 }
1333
1334 public:
1335 opt_storage() = default;
1336
1337 bool setLocation(Option &O, DataType &L) {
1338 if (Location)
1339 return O.error("cl::location(x) specified more than once!");
1340 Location = &L;
1341 Default = L;
1342 return false;
1343 }
1344
1345 template <class T> void setValue(const T &V, bool initial = false) {
1346 check_location();
1347 *Location = V;
1348 if (initial)
1349 Default = V;
1350 }
1351
1352 DataType &getValue() {
1353 check_location();
1354 return *Location;
1355 }
1356 const DataType &getValue() const {
1357 check_location();
1358 return *Location;
1359 }
1360
1361 operator DataType() const { return this->getValue(); }
1362
1363 const OptionValue<DataType> &getDefault() const { return Default; }
1364 };
1365
1366
1367
1368
1369
1370 template <class DataType>
1371 class opt_storage<DataType, false, true> : public DataType {
1372 public:
1373 OptionValue<DataType> Default;
1374
1375 template <class T> void setValue(const T &V, bool initial = false) {
1376 DataType::operator=(V);
1377 if (initial)
1378 Default = V;
1379 }
1380
1381 DataType &getValue() { return *this; }
1382 const DataType &getValue() const { return *this; }
1383
1384 const OptionValue<DataType> &getDefault() const { return Default; }
1385 };
1386
1387
1388
1389
1390
1391 template <class DataType> class opt_storage<DataType, false, false> {
1392 public:
1393 DataType Value;
1394 OptionValue<DataType> Default;
1395
1396
1397
1398 opt_storage() : Value(DataType()), Default() {}
1399
1400 template <class T> void setValue(const T &V, bool initial = false) {
1401 Value = V;
1402 if (initial)
1403 Default = V;
1404 }
1405 DataType &getValue() { return Value; }
1406 DataType getValue() const { return Value; }
1407
1408 const OptionValue<DataType> &getDefault() const { return Default; }
1409
1410 operator DataType() const { return getValue(); }
1411
1412
1413 DataType operator->() const { return Value; }
1414 };
1415
1416
1417
1418
1419 template <class DataType, bool ExternalStorage = false,
1420 class ParserClass = parser<DataType>>
1421 class opt
1422 : public Option,
1423 public opt_storage<DataType, ExternalStorage, std::is_class_v<DataType>> {
1424 ParserClass Parser;
1425
1426 bool handleOccurrence(unsigned pos, StringRef ArgName,
1427 StringRef Arg) override {
1428 typename ParserClass::parser_data_type Val =
1429 typename ParserClass::parser_data_type();
1430 if (Parser.parse(*this, ArgName, Arg, Val))
1431 return true;
1432 this->setValue(Val);
1433 this->setPosition(pos);
1434 Callback(Val);
1435 return false;
1436 }
1437
1438 enum ValueExpected getValueExpectedFlagDefault() const override {
1439 return Parser.getValueExpectedFlagDefault();
1440 }
1441
1442 void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1443 return Parser.getExtraOptionNames(OptionNames);
1444 }
1445
1446
1447 size_t getOptionWidth() const override {
1448 return Parser.getOptionWidth(*this);
1449 }
1450
1451 void printOptionInfo(size_t GlobalWidth) const override {
1452 Parser.printOptionInfo(*this, GlobalWidth);
1453 }
1454
1455 void printOptionValue(size_t GlobalWidth, bool Force) const override {
1456 if (Force || !this->getDefault().compare(this->getValue())) {
1457 cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
1458 this->getDefault(), GlobalWidth);
1459 }
1460 }
1461
1462 template <class T, class = std::enable_if_t<std::is_assignable_v<T &, T>>>
1463 void setDefaultImpl() {
1464 const OptionValue<DataType> &V = this->getDefault();
1465 if (V.hasValue())
1466 this->setValue(V.getValue());
1467 else
1468 this->setValue(T());
1469 }
1470
1471 template <class T, class = std::enable_if_t<!std::is_assignable_v<T &, T>>>
1472 void setDefaultImpl(...) {}
1473
1474 void setDefault() override { setDefaultImpl<DataType>(); }
1475
1476 void done() {
1477 addArgument();
1478 Parser.initialize();
1479 }
1480
1481 public:
1482
1483 opt(const opt &) = delete;
1484 opt &operator=(const opt &) = delete;
1485
1486
1487 void setInitialValue(const DataType &V) { this->setValue(V, true); }
1488
1489 ParserClass &getParser() { return Parser; }
1490
1491 template <class T> DataType &operator=(const T &Val) {
1492 this->setValue(Val);
1493 Callback(Val);
1494 return this->getValue();
1495 }
1496
1497 template <class... Mods>
1498 explicit opt(const Mods &... Ms)
1499 : Option(llvm::cl::Optional, NotHidden), Parser(*this) {
1500 apply(this, Ms...);
1501 done();
1502 }
1503
1504 void setCallback(
1505 std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1506 Callback = CB;
1507 }
1508
1509 std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1510 [](const typename ParserClass::parser_data_type &) {};
1511 };
1512
1513 extern template class opt<unsigned>;
1514 extern template class opt<int>;
1515 extern template class opt<std::string>;
1516 extern template class opt<char>;
1517 extern template class opt<bool>;
1518
1519
1520
1521
1522
1523
1524 template <class DataType, class StorageClass> class list_storage {
1525 StorageClass *Location = nullptr;
1526 std::vector<OptionValue<DataType>> Default =
1527 std::vector<OptionValue<DataType>>();
1528 bool DefaultAssigned = false;
1529
1530 public:
1531 list_storage() = default;
1532
1533 void clear() {}
1534
1535 bool setLocation(Option &O, StorageClass &L) {
1536 if (Location)
1537 return O.error("cl::location(x) specified more than once!");
1538 Location = &L;
1539 return false;
1540 }
1541
1542 template <class T> void addValue(const T &V, bool initial = false) {
1543 assert(Location != nullptr &&
1544 "cl::location(...) not specified for a command "
1545 "line option with external storage!");
1546 Location->push_back(V);
1547 if (initial)
1548 Default.push_back(V);
1549 }
1550
1551 const std::vector<OptionValue<DataType>> &getDefault() const {
1552 return Default;
1553 }
1554
1555 void assignDefault() { DefaultAssigned = true; }
1556 void overwriteDefault() { DefaultAssigned = false; }
1557 bool isDefaultAssigned() { return DefaultAssigned; }
1558 };
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 template <class DataType> class list_storage<DataType, bool> {
1569 std::vector<DataType> Storage;
1570 std::vector<OptionValue<DataType>> Default;
1571 bool DefaultAssigned = false;
1572
1573 public:
1574 using iterator = typename std::vector<DataType>::iterator;
1575
1576 iterator begin() { return Storage.begin(); }
1577 iterator end() { return Storage.end(); }
1578
1579 using const_iterator = typename std::vector<DataType>::const_iterator;
1580
1581 const_iterator begin() const { return Storage.begin(); }
1582 const_iterator end() const { return Storage.end(); }
1583
1584 using size_type = typename std::vector<DataType>::size_type;
1585
1586 size_type size() const { return Storage.size(); }
1587
1588 bool empty() const { return Storage.empty(); }
1589
1590 void push_back(const DataType &value) { Storage.push_back(value); }
1591 void push_back(DataType &&value) { Storage.push_back(value); }
1592
1593 using reference = typename std::vector<DataType>::reference;
1594 using const_reference = typename std::vector<DataType>::const_reference;
1595
1596 reference operator[](size_type pos) { return Storage[pos]; }
1597 const_reference operator[](size_type pos) const { return Storage[pos]; }
1598
1599 void clear() {
1600 Storage.clear();
1601 }
1602
1603 iterator erase(const_iterator pos) { return Storage.erase(pos); }
1604 iterator erase(const_iterator first, const_iterator last) {
1605 return Storage.erase(first, last);
1606 }
1607
1608 iterator erase(iterator pos) { return Storage.erase(pos); }
1609 iterator erase(iterator first, iterator last) {
1610 return Storage.erase(first, last);
1611 }
1612
1613 iterator insert(const_iterator pos, const DataType &value) {
1614 return Storage.insert(pos, value);
1615 }
1616 iterator insert(const_iterator pos, DataType &&value) {
1617 return Storage.insert(pos, value);
1618 }
1619
1620 iterator insert(iterator pos, const DataType &value) {
1621 return Storage.insert(pos, value);
1622 }
1623 iterator insert(iterator pos, DataType &&value) {
1624 return Storage.insert(pos, value);
1625 }
1626
1627 reference front() { return Storage.front(); }
1628 const_reference front() const { return Storage.front(); }
1629
1630 operator std::vector<DataType> &() { return Storage; }
1631 operator ArrayRef<DataType>() const { return Storage; }
1632 std::vector<DataType> *operator&() { return &Storage; }
1633 const std::vector<DataType> *operator&() const { return &Storage; }
1634
1635 template <class T> void addValue(const T &V, bool initial = false) {
1636 Storage.push_back(V);
1637 if (initial)
1638 Default.push_back(OptionValue<DataType>(V));
1639 }
1640
1641 const std::vector<OptionValue<DataType>> &getDefault() const {
1642 return Default;
1643 }
1644
1645 void assignDefault() { DefaultAssigned = true; }
1646 void overwriteDefault() { DefaultAssigned = false; }
1647 bool isDefaultAssigned() { return DefaultAssigned; }
1648 };
1649
1650
1651
1652
1653 template <class DataType, class StorageClass = bool,
1654 class ParserClass = parser<DataType>>
1655 class list : public Option, public list_storage<DataType, StorageClass> {
1656 std::vector<unsigned> Positions;
1657 ParserClass Parser;
1658
1659 enum ValueExpected getValueExpectedFlagDefault() const override {
1660 return Parser.getValueExpectedFlagDefault();
1661 }
1662
1663 void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1664 return Parser.getExtraOptionNames(OptionNames);
1665 }
1666
1667 bool handleOccurrence(unsigned pos, StringRef ArgName,
1668 StringRef Arg) override {
1669 typename ParserClass::parser_data_type Val =
1670 typename ParserClass::parser_data_type();
1671 if (list_storage<DataType, StorageClass>::isDefaultAssigned()) {
1672 clear();
1673 list_storage<DataType, StorageClass>::overwriteDefault();
1674 }
1675 if (Parser.parse(*this, ArgName, Arg, Val))
1676 return true;
1677 list_storage<DataType, StorageClass>::addValue(Val);
1678 setPosition(pos);
1679 Positions.push_back(pos);
1680 Callback(Val);
1681 return false;
1682 }
1683
1684
1685 size_t getOptionWidth() const override {
1686 return Parser.getOptionWidth(*this);
1687 }
1688
1689 void printOptionInfo(size_t GlobalWidth) const override {
1690 Parser.printOptionInfo(*this, GlobalWidth);
1691 }
1692
1693
1694 void printOptionValue(size_t , bool ) const override {
1695 }
1696
1697 void setDefault() override {
1698 Positions.clear();
1699 list_storage<DataType, StorageClass>::clear();
1700 for (auto &Val : list_storage<DataType, StorageClass>::getDefault())
1701 list_storage<DataType, StorageClass>::addValue(Val.getValue());
1702 }
1703
1704 void done() {
1705 addArgument();
1706 Parser.initialize();
1707 }
1708
1709 public:
1710
1711 list(const list &) = delete;
1712 list &operator=(const list &) = delete;
1713
1714 ParserClass &getParser() { return Parser; }
1715
1716 unsigned getPosition(unsigned optnum) const {
1717 assert(optnum < this->size() && "Invalid option index");
1718 return Positions[optnum];
1719 }
1720
1721 void clear() {
1722 Positions.clear();
1723 list_storage<DataType, StorageClass>::clear();
1724 }
1725
1726
1727 void setInitialValues(ArrayRef<DataType> Vs) {
1728 assert(!(list_storage<DataType, StorageClass>::isDefaultAssigned()) &&
1729 "Cannot have two default values");
1730 list_storage<DataType, StorageClass>::assignDefault();
1731 for (auto &Val : Vs)
1732 list_storage<DataType, StorageClass>::addValue(Val, true);
1733 }
1734
1735 void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
1736
1737 template <class... Mods>
1738 explicit list(const Mods &... Ms)
1739 : Option(ZeroOrMore, NotHidden), Parser(*this) {
1740 apply(this, Ms...);
1741 done();
1742 }
1743
1744 void setCallback(
1745 std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1746 Callback = CB;
1747 }
1748
1749 std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1750 [](const typename ParserClass::parser_data_type &) {};
1751 };
1752
1753
1754 struct multi_val {
1755 unsigned AdditionalVals;
1756 explicit multi_val(unsigned N) : AdditionalVals(N) {}
1757
1758 template <typename D, typename S, typename P>
1759 void apply(list<D, S, P> &L) const {
1760 L.setNumAdditionalVals(AdditionalVals);
1761 }
1762 };
1763
1764
1765
1766
1767
1768
1769 template <class DataType, class StorageClass> class bits_storage {
1770 unsigned *Location = nullptr;
1771
1772 template <class T> static unsigned Bit(const T &V) {
1773 unsigned BitPos = static_cast<unsigned>(V);
1774 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1775 "enum exceeds width of bit vector!");
1776 return 1 << BitPos;
1777 }
1778
1779 public:
1780 bits_storage() = default;
1781
1782 bool setLocation(Option &O, unsigned &L) {
1783 if (Location)
1784 return O.error("cl::location(x) specified more than once!");
1785 Location = &L;
1786 return false;
1787 }
1788
1789 template <class T> void addValue(const T &V) {
1790 assert(Location != nullptr &&
1791 "cl::location(...) not specified for a command "
1792 "line option with external storage!");
1793 *Location |= Bit(V);
1794 }
1795
1796 unsigned getBits() { return *Location; }
1797
1798 void clear() {
1799 if (Location)
1800 *Location = 0;
1801 }
1802
1803 template <class T> bool isSet(const T &V) {
1804 return (*Location & Bit(V)) != 0;
1805 }
1806 };
1807
1808
1809
1810
1811 template <class DataType> class bits_storage<DataType, bool> {
1812 unsigned Bits{0};
1813
1814 template <class T> static unsigned Bit(const T &V) {
1815 unsigned BitPos = static_cast<unsigned>(V);
1816 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1817 "enum exceeds width of bit vector!");
1818 return 1 << BitPos;
1819 }
1820
1821 public:
1822 template <class T> void addValue(const T &V) { Bits |= Bit(V); }
1823
1824 unsigned getBits() { return Bits; }
1825
1826 void clear() { Bits = 0; }
1827
1828 template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
1829 };
1830
1831
1832
1833
1834 template <class DataType, class Storage = bool,
1835 class ParserClass = parser<DataType>>
1836 class bits : public Option, public bits_storage<DataType, Storage> {
1837 std::vector<unsigned> Positions;
1838 ParserClass Parser;
1839
1840 enum ValueExpected getValueExpectedFlagDefault() const override {
1841 return Parser.getValueExpectedFlagDefault();
1842 }
1843
1844 void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
1845 return Parser.getExtraOptionNames(OptionNames);
1846 }
1847
1848 bool handleOccurrence(unsigned pos, StringRef ArgName,
1849 StringRef Arg) override {
1850 typename ParserClass::parser_data_type Val =
1851 typename ParserClass::parser_data_type();
1852 if (Parser.parse(*this, ArgName, Arg, Val))
1853 return true;
1854 this->addValue(Val);
1855 setPosition(pos);
1856 Positions.push_back(pos);
1857 Callback(Val);
1858 return false;
1859 }
1860
1861
1862 size_t getOptionWidth() const override {
1863 return Parser.getOptionWidth(*this);
1864 }
1865
1866 void printOptionInfo(size_t GlobalWidth) const override {
1867 Parser.printOptionInfo(*this, GlobalWidth);
1868 }
1869
1870
1871 void printOptionValue(size_t , bool ) const override {
1872 }
1873
1874 void setDefault() override { bits_storage<DataType, Storage>::clear(); }
1875
1876 void done() {
1877 addArgument();
1878 Parser.initialize();
1879 }
1880
1881 public:
1882
1883 bits(const bits &) = delete;
1884 bits &operator=(const bits &) = delete;
1885
1886 ParserClass &getParser() { return Parser; }
1887
1888 unsigned getPosition(unsigned optnum) const {
1889 assert(optnum < this->size() && "Invalid option index");
1890 return Positions[optnum];
1891 }
1892
1893 template <class... Mods>
1894 explicit bits(const Mods &... Ms)
1895 : Option(ZeroOrMore, NotHidden), Parser(*this) {
1896 apply(this, Ms...);
1897 done();
1898 }
1899
1900 void setCallback(
1901 std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1902 Callback = CB;
1903 }
1904
1905 std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1906 [](const typename ParserClass::parser_data_type &) {};
1907 };
1908
1909
1910
1911
1912
1913 class alias : public Option {
1914 Option *AliasFor;
1915
1916 bool handleOccurrence(unsigned pos, StringRef ,
1917 StringRef Arg) override {
1918 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1919 }
1920
1921 bool addOccurrence(unsigned pos, StringRef , StringRef Value,
1922 bool MultiArg = false) override {
1923 return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
1924 }
1925
1926
1927 size_t getOptionWidth() const override;
1928 void printOptionInfo(size_t GlobalWidth) const override;
1929
1930
1931 void printOptionValue(size_t , bool ) const override {
1932 }
1933
1934 void setDefault() override { AliasFor->setDefault(); }
1935
1936 ValueExpected getValueExpectedFlagDefault() const override {
1937 return AliasFor->getValueExpectedFlag();
1938 }
1939
1940 void done() {
1941 if (!hasArgStr())
1942 error("cl::alias must have argument name specified!");
1943 if (!AliasFor)
1944 error("cl::alias must have an cl::aliasopt(option) specified!");
1945 if (!Subs.empty())
1946 error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!");
1947 Subs = AliasFor->Subs;
1948 Categories = AliasFor->Categories;
1949 addArgument();
1950 }
1951
1952 public:
1953
1954 alias(const alias &) = delete;
1955 alias &operator=(const alias &) = delete;
1956
1957 void setAliasFor(Option &O) {
1958 if (AliasFor)
1959 error("cl::alias must only have one cl::aliasopt(...) specified!");
1960 AliasFor = &O;
1961 }
1962
1963 template <class... Mods>
1964 explicit alias(const Mods &... Ms)
1965 : Option(Optional, Hidden), AliasFor(nullptr) {
1966 apply(this, Ms...);
1967 done();
1968 }
1969 };
1970
1971
1972 struct aliasopt {
1973 Option &Opt;
1974
1975 explicit aliasopt(Option &O) : Opt(O) {}
1976
1977 void apply(alias &A) const { A.setAliasFor(Opt); }
1978 };
1979
1980
1981
1982
1983 struct extrahelp {
1984 StringRef morehelp;
1985
1986 explicit extrahelp(StringRef help);
1987 };
1988
1989 void PrintVersionMessage();
1990
1991
1992
1993
1994
1995
1996 void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
1997
1998
1999
2000
2001 ArrayRef<StringRef> getCompilerBuildConfig();
2002
2003
2004
2005
2006 void printBuildConfig(raw_ostream &OS);
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039 StringMap<Option *> &
2040 getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel());
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061 iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
2062 getRegisteredSubcommands();
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080 void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver,
2081 SmallVectorImpl<const char *> &NewArgv,
2082 bool MarkEOLs = false);
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098 void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver,
2099 SmallVectorImpl<const char *> &NewArgv,
2100 bool MarkEOLs = false);
2101
2102
2103
2104
2105
2106 void TokenizeWindowsCommandLineNoCopy(StringRef Source, StringSaver &Saver,
2107 SmallVectorImpl<StringRef> &NewArgv);
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122 void TokenizeWindowsCommandLineFull(StringRef Source, StringSaver &Saver,
2123 SmallVectorImpl<const char *> &NewArgv,
2124 bool MarkEOLs = false);
2125
2126
2127
2128 using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver,
2129 SmallVectorImpl<const char *> &NewArgv,
2130 bool MarkEOLs);
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141 void tokenizeConfigFile(StringRef Source, StringSaver &Saver,
2142 SmallVectorImpl<const char *> &NewArgv,
2143 bool MarkEOLs = false);
2144
2145
2146 class ExpansionContext {
2147
2148 StringSaver Saver;
2149
2150
2151 TokenizerCallback Tokenizer;
2152
2153
2154 vfs::FileSystem *FS;
2155
2156
2157
2158 StringRef CurrentDir;
2159
2160
2161 ArrayRef<StringRef> SearchDirs;
2162
2163
2164
2165 bool RelativeNames = false;
2166
2167
2168
2169 bool MarkEOLs = false;
2170
2171
2172 bool InConfigFile = false;
2173
2174 llvm::Error expandResponseFile(StringRef FName,
2175 SmallVectorImpl<const char *> &NewArgv);
2176
2177 public:
2178 ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T);
2179
2180 ExpansionContext &setMarkEOLs(bool X) {
2181 MarkEOLs = X;
2182 return *this;
2183 }
2184
2185 ExpansionContext &setRelativeNames(bool X) {
2186 RelativeNames = X;
2187 return *this;
2188 }
2189
2190 ExpansionContext &setCurrentDir(StringRef X) {
2191 CurrentDir = X;
2192 return *this;
2193 }
2194
2195 ExpansionContext &setSearchDirs(ArrayRef<StringRef> X) {
2196 SearchDirs = X;
2197 return *this;
2198 }
2199
2200 ExpansionContext &setVFS(vfs::FileSystem *X) {
2201 FS = X;
2202 return *this;
2203 }
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214 bool findConfigFile(StringRef FileName, SmallVectorImpl<char> &FilePath);
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226 Error readConfigFile(StringRef CfgFile, SmallVectorImpl<const char *> &Argv);
2227
2228
2229 Error expandResponseFiles(SmallVectorImpl<const char *> &Argv);
2230 };
2231
2232
2233
2234
2235
2236 bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2237 SmallVectorImpl<const char *> &NewArgv);
2238
2239
2240
2241 bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
2242 SmallVectorImpl<const char *> &Argv);
2243
2244
2245
2246
2247
2248 bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2249 StringSaver &Saver,
2250 SmallVectorImpl<const char *> &NewArgv);
2251
2252
2253
2254
2255
2256
2257
2258
2259 void HideUnrelatedOptions(cl::OptionCategory &Category,
2260 SubCommand &Sub = SubCommand::getTopLevel());
2261
2262
2263
2264
2265
2266
2267
2268
2269 void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
2270 SubCommand &Sub = SubCommand::getTopLevel());
2271
2272
2273
2274
2275 void ResetAllOptionOccurrences();
2276
2277
2278
2279
2280
2281 void ResetCommandLineParser();
2282
2283
2284 bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i);
2285
2286 }
2287
2288 }
2289
2290 #endif