From 1abaeece719c99ad9b408257b71c19c62db438cc Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 10 Jul 2019 17:09:47 +0000 Subject: [PATCH] Options: Reduce code duplication Summary: While investigating breakages caused by D63110, I noticed we were building the short options strings in three places. Some of them used a leading ':' to detect missing arguments, and some didn't. This was the indirect cause of D63110. Here, I move the common code into a utility function. Also, unify the code which appends the sentinel value at the end of the option vector, and make it harder for users to pass invalid argc-argv combos to getopt (another component of D63110) by having the OptionParser::Parse function take a (Mutable)ArrayRef. This unification has uncovered that we don't handle missing arguments while building aliases, However, it's not possible to write an effective test for this, as right now it is not possible to return an error out of the alias parsing code (which means we are printing the generic "failure" message even after this patch). Reviewers: mgorny, aprantl Reviewed By: mgorny Subscribers: lldb-commits Differential Revision: https://reviews.llvm.org/D63770 llvm-svn: 365665 --- lldb/include/lldb/Host/OptionParser.h | 8 +- lldb/source/Host/common/OptionParser.cpp | 8 +- lldb/source/Interpreter/CommandAlias.cpp | 2 + lldb/source/Interpreter/Options.cpp | 102 ++++++++--------------- 4 files changed, 48 insertions(+), 72 deletions(-) diff --git a/lldb/include/lldb/Host/OptionParser.h b/lldb/include/lldb/Host/OptionParser.h index ca0594645645..b03eeb74ec4e 100644 --- a/lldb/include/lldb/Host/OptionParser.h +++ b/lldb/include/lldb/Host/OptionParser.h @@ -13,6 +13,7 @@ #include #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/ArrayRef.h" struct option; @@ -37,8 +38,11 @@ public: static void EnableError(bool error); - static int Parse(int argc, char *const argv[], llvm::StringRef optstring, - const Option *longopts, int *longindex); + /// Argv must be an argument vector "as passed to main", i.e. terminated with + /// a nullptr. + static int Parse(llvm::MutableArrayRef argv, + llvm::StringRef optstring, const Option *longopts, + int *longindex); static char *GetOptionArgument(); static int GetOptionIndex(); diff --git a/lldb/source/Host/common/OptionParser.cpp b/lldb/source/Host/common/OptionParser.cpp index 92ff6f63d951..1e76f9b8f9f1 100644 --- a/lldb/source/Host/common/OptionParser.cpp +++ b/lldb/source/Host/common/OptionParser.cpp @@ -27,8 +27,9 @@ void OptionParser::Prepare(std::unique_lock &lock) { void OptionParser::EnableError(bool error) { opterr = error ? 1 : 0; } -int OptionParser::Parse(int argc, char *const argv[], llvm::StringRef optstring, - const Option *longopts, int *longindex) { +int OptionParser::Parse(llvm::MutableArrayRef argv, + llvm::StringRef optstring, const Option *longopts, + int *longindex) { std::vector