diff options
author | Eric Liu <ioeric@google.com> | 2017-03-15 08:41:00 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2017-03-15 08:41:00 +0000 |
commit | e51ee0668c8eec639964234a1d716e30f0449399 (patch) | |
tree | 6688a3660f133e71d912313b5ed2c222166a6ede /llvm/lib/Support/CommandLine.cpp | |
parent | 654cb8263a3c3c8ec24285e260da3ab0a0c7abf0 (diff) | |
download | bcm5719-llvm-e51ee0668c8eec639964234a1d716e30f0449399.tar.gz bcm5719-llvm-e51ee0668c8eec639964234a1d716e30f0449399.zip |
[Support][CommandLine] Make it possible to get error messages from ParseCommandLineOptions when ignoring errors.
Summary:
Previously, ParseCommandLineOptions returns false and ignores error messages
when IgnoreErrors. It would be useful to also return error messages if users
decide to check parsing result instead of having the program exit on error.
Reviewers: chandlerc, mehdi_amini, rnk
Reviewed By: rnk
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D30893
llvm-svn: 297810
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r-- | llvm/lib/Support/CommandLine.cpp | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index b1142d79cdb..65747d69291 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -123,7 +123,7 @@ public: void ResetAllOptionOccurrences(); bool ParseCommandLineOptions(int argc, const char *const *argv, - StringRef Overview, bool IgnoreErrors); + StringRef Overview, raw_ostream *Errs = nullptr); void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) { if (Opt.hasArgStr()) @@ -1013,9 +1013,9 @@ void cl::ParseEnvironmentOptions(const char *progName, const char *envVar, } bool cl::ParseCommandLineOptions(int argc, const char *const *argv, - StringRef Overview, bool IgnoreErrors) { + StringRef Overview, raw_ostream *Errs) { return GlobalParser->ParseCommandLineOptions(argc, argv, Overview, - IgnoreErrors); + Errs); } void CommandLineParser::ResetAllOptionOccurrences() { @@ -1030,7 +1030,7 @@ void CommandLineParser::ResetAllOptionOccurrences() { bool CommandLineParser::ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview, - bool IgnoreErrors) { + raw_ostream *Errs) { assert(hasOptions() && "No options specified!"); // Expand response files. @@ -1045,6 +1045,9 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, ProgramName = sys::path::filename(StringRef(argv[0])); ProgramOverview = Overview; + bool IgnoreErrors = Errs; + if (!Errs) + Errs = &errs(); bool ErrorParsing = false; // Check out the positional arguments to collect information about them. @@ -1097,15 +1100,14 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, // not specified after an option that eats all extra arguments, or this // one will never get any! // - if (!IgnoreErrors) { + if (!IgnoreErrors) Opt->error("error - option can never match, because " "another positional argument will match an " "unbounded number of values, and this option" " does not require a value!"); - errs() << ProgramName << ": CommandLine Error: Option '" - << Opt->ArgStr << "' is all messed up!\n"; - errs() << PositionalOpts.size(); - } + *Errs << ProgramName << ": CommandLine Error: Option '" << Opt->ArgStr + << "' is all messed up!\n"; + *Errs << PositionalOpts.size(); ErrorParsing = true; } UnboundedFound |= EatsUnboundedNumberOfValues(Opt); @@ -1200,15 +1202,13 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, if (!Handler) { if (SinkOpts.empty()) { - if (!IgnoreErrors) { - errs() << ProgramName << ": Unknown command line argument '" - << argv[i] << "'. Try: '" << argv[0] << " -help'\n"; - - if (NearestHandler) { - // If we know a near match, report it as well. - errs() << ProgramName << ": Did you mean '-" << NearestHandlerString - << "'?\n"; - } + *Errs << ProgramName << ": Unknown command line argument '" << argv[i] + << "'. Try: '" << argv[0] << " -help'\n"; + + if (NearestHandler) { + // If we know a near match, report it as well. + *Errs << ProgramName << ": Did you mean '-" << NearestHandlerString + << "'?\n"; } ErrorParsing = true; @@ -1231,22 +1231,18 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, // Check and handle positional arguments now... if (NumPositionalRequired > PositionalVals.size()) { - if (!IgnoreErrors) { - errs() << ProgramName + *Errs << ProgramName << ": Not enough positional command line arguments specified!\n" << "Must specify at least " << NumPositionalRequired << " positional argument" << (NumPositionalRequired > 1 ? "s" : "") << ": See: " << argv[0] << " - help\n"; - } ErrorParsing = true; } else if (!HasUnlimitedPositionals && PositionalVals.size() > PositionalOpts.size()) { - if (!IgnoreErrors) { - errs() << ProgramName << ": Too many positional arguments specified!\n" - << "Can specify at most " << PositionalOpts.size() - << " positional arguments: See: " << argv[0] << " -help\n"; - } + *Errs << ProgramName << ": Too many positional arguments specified!\n" + << "Can specify at most " << PositionalOpts.size() + << " positional arguments: See: " << argv[0] << " -help\n"; ErrorParsing = true; } else if (!ConsumeAfterOpt) { @@ -2182,5 +2178,6 @@ void cl::ResetAllOptionOccurrences() { void LLVMParseCommandLineOptions(int argc, const char *const *argv, const char *Overview) { - llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), true); + llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), + &llvm::nulls()); } |