diff options
author | Thomas Preud'homme <thomasp@graphcore.ai> | 2019-01-14 09:28:53 +0000 |
---|---|---|
committer | Thomas Preud'homme <thomasp@graphcore.ai> | 2019-01-14 09:28:53 +0000 |
commit | bc5e6ee87a08618c3202eb1cecddd24267e64701 (patch) | |
tree | d6f4a11189b1f68206ce2c0f397faa7715a7284b /llvm/lib/Support/CommandLine.cpp | |
parent | 4b7e12f2cfc8ec1eb0ea537371b8d2f499426655 (diff) | |
download | bcm5719-llvm-bc5e6ee87a08618c3202eb1cecddd24267e64701.tar.gz bcm5719-llvm-bc5e6ee87a08618c3202eb1cecddd24267e64701.zip |
Add support for prefix-only CLI options
Summary:
Add support for options that always prefix their value, giving an error
if the value is in the next argument or if the option is given a value
assignment (ie. opt=val). This is the desired behavior for the -D option
of FileCheck for instance.
Copyright:
- Linaro (changes in version 2 of revision D55940)
- GraphCore (changes in later versions and introduced when creating
D56549)
Reviewers: jdenny
Subscribers: llvm-commits, probinson, kristina, hiraditya,
JonChesterfield
Differential Revision: https://reviews.llvm.org/D56549
llvm-svn: 351038
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r-- | llvm/lib/Support/CommandLine.cpp | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index cb2a2e557fa..f7290b54dcf 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -426,12 +426,17 @@ Option *CommandLineParser::LookupOption(SubCommand &Sub, StringRef &Arg, return I != Sub.OptionsMap.end() ? I->second : nullptr; } - // If the argument before the = is a valid option name, we match. If not, - // return Arg unmolested. + // If the argument before the = is a valid option name and the option allows + // non-prefix form (ie is not AlwaysPrefix), we match. If not, signal match + // failure by returning nullptr. auto I = Sub.OptionsMap.find(Arg.substr(0, EqualPos)); if (I == Sub.OptionsMap.end()) return nullptr; + auto O = I->second; + if (O->getFormattingFlag() == cl::AlwaysPrefix) + return nullptr; + Value = Arg.substr(EqualPos + 1); Arg = Arg.substr(0, EqualPos); return I->second; @@ -539,7 +544,9 @@ static inline bool ProvideOption(Option *Handler, StringRef ArgName, switch (Handler->getValueExpectedFlag()) { case ValueRequired: if (!Value.data()) { // No value specified? - if (i + 1 >= argc) + // If no other argument or the option only supports prefix form, we + // cannot look at the next argument. + if (i + 1 >= argc || Handler->getFormattingFlag() == cl::AlwaysPrefix) return Handler->error("requires a value!"); // Steal the next argument, like for '-o filename' assert(argv && "null check"); @@ -597,7 +604,8 @@ static inline bool isGrouping(const Option *O) { return O->getFormattingFlag() == cl::Grouping; } static inline bool isPrefixedOrGrouping(const Option *O) { - return isGrouping(O) || O->getFormattingFlag() == cl::Prefix; + return isGrouping(O) || O->getFormattingFlag() == cl::Prefix || + O->getFormattingFlag() == cl::AlwaysPrefix; } // getOptionPred - Check to see if there are any options that satisfy the @@ -647,7 +655,8 @@ HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value, // If the option is a prefixed option, then the value is simply the // rest of the name... so fall through to later processing, by // setting up the argument name flags and value fields. - if (PGOpt->getFormattingFlag() == cl::Prefix) { + if (PGOpt->getFormattingFlag() == cl::Prefix || + PGOpt->getFormattingFlag() == cl::AlwaysPrefix) { Value = Arg.substr(Length); Arg = Arg.substr(0, Length); assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt); |