[Lldb-commits] [lldb] r283159 - Modernize some code related to Args usage / implementation.
Zachary Turner via lldb-commits
lldb-commits at lists.llvm.org
Mon Oct 3 16:20:36 PDT 2016
Author: zturner
Date: Mon Oct 3 18:20:36 2016
New Revision: 283159
URL: http://llvm.org/viewvc/llvm-project?rev=283159&view=rev
Log:
Modernize some code related to Args usage / implementation.
Mostly this involves simplifying some logical constructs and using
some ranges instead of index-based iteration. NFC
Modified:
lldb/trunk/include/lldb/Interpreter/Args.h
lldb/trunk/source/Interpreter/Args.cpp
lldb/trunk/source/Interpreter/CommandAlias.cpp
lldb/trunk/source/Interpreter/CommandInterpreter.cpp
Modified: lldb/trunk/include/lldb/Interpreter/Args.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/Args.h?rev=283159&r1=283158&r2=283159&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/Args.h (original)
+++ lldb/trunk/include/lldb/Interpreter/Args.h Mon Oct 3 18:20:36 2016
@@ -27,9 +27,7 @@
namespace lldb_private {
-typedef std::pair<int, std::string> OptionArgValue;
-typedef std::pair<std::string, OptionArgValue> OptionArgPair;
-typedef std::vector<OptionArgPair> OptionArgVector;
+typedef std::vector<std::tuple<std::string, int, std::string>> OptionArgVector;
typedef std::shared_ptr<OptionArgVector> OptionArgVectorSP;
struct OptionArgElement {
Modified: lldb/trunk/source/Interpreter/Args.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/Args.cpp?rev=283159&r1=283158&r2=283159&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/Args.cpp (original)
+++ lldb/trunk/source/Interpreter/Args.cpp Mon Oct 3 18:20:36 2016
@@ -1015,6 +1015,7 @@ void Args::ParseAliasOptions(Options &op
std::unique_lock<std::mutex> lock;
OptionParser::Prepare(lock);
+ result.SetStatus(eReturnStatusSuccessFinishNoResult);
int val;
while (1) {
int long_options_index = -1;
@@ -1049,95 +1050,73 @@ void Args::ParseAliasOptions(Options &op
}
// See if the option takes an argument, and see if one was supplied.
- if (long_options_index >= 0) {
- StreamString option_str;
- option_str.Printf("-%c", val);
- const OptionDefinition *def = long_options[long_options_index].definition;
- int has_arg =
- (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
-
- switch (has_arg) {
- case OptionParser::eNoArgument:
- option_arg_vector->push_back(OptionArgPair(
- std::string(option_str.GetData()),
- OptionArgValue(OptionParser::eNoArgument, "<no-argument>")));
- result.SetStatus(eReturnStatusSuccessFinishNoResult);
- break;
- case OptionParser::eRequiredArgument:
- if (OptionParser::GetOptionArgument() != nullptr) {
- option_arg_vector->push_back(OptionArgPair(
- std::string(option_str.GetData()),
- OptionArgValue(OptionParser::eRequiredArgument,
- std::string(OptionParser::GetOptionArgument()))));
- result.SetStatus(eReturnStatusSuccessFinishNoResult);
- } else {
- result.AppendErrorWithFormat(
- "Option '%s' is missing argument specifier.\n",
- option_str.GetData());
- result.SetStatus(eReturnStatusFailed);
- }
- break;
- case OptionParser::eOptionalArgument:
- if (OptionParser::GetOptionArgument() != nullptr) {
- option_arg_vector->push_back(OptionArgPair(
- std::string(option_str.GetData()),
- OptionArgValue(OptionParser::eOptionalArgument,
- std::string(OptionParser::GetOptionArgument()))));
- result.SetStatus(eReturnStatusSuccessFinishNoResult);
- } else {
- option_arg_vector->push_back(
- OptionArgPair(std::string(option_str.GetData()),
- OptionArgValue(OptionParser::eOptionalArgument,
- "<no-argument>")));
- result.SetStatus(eReturnStatusSuccessFinishNoResult);
- }
- break;
- default:
- result.AppendErrorWithFormat("error with options table; invalid value "
- "in has_arg field for option '%c'.\n",
- val);
- result.SetStatus(eReturnStatusFailed);
- break;
- }
- } else {
+ if (long_options_index == -1) {
result.AppendErrorWithFormat("Invalid option with value '%c'.\n", val);
result.SetStatus(eReturnStatusFailed);
+ return;
}
- if (long_options_index >= 0) {
- // Find option in the argument list; also see if it was supposed to take
- // an argument and if one was
- // supplied. Remove option (and argument, if given) from the argument
- // list. Also remove them from
- // the raw_input_string, if one was passed in.
- size_t idx = FindArgumentIndexForOption(long_options, long_options_index);
- if (idx < GetArgumentCount()) {
+ StreamString option_str;
+ option_str.Printf("-%c", val);
+ const OptionDefinition *def = long_options[long_options_index].definition;
+ int has_arg =
+ (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
+
+ const char *option_arg = nullptr;
+ switch (has_arg) {
+ case OptionParser::eRequiredArgument:
+ if (OptionParser::GetOptionArgument() == nullptr) {
+ result.AppendErrorWithFormat(
+ "Option '%s' is missing argument specifier.\n",
+ option_str.GetData());
+ result.SetStatus(eReturnStatusFailed);
+ return;
+ }
+ LLVM_FALLTHROUGH;
+ case OptionParser::eOptionalArgument:
+ option_arg = OptionParser::GetOptionArgument();
+ LLVM_FALLTHROUGH;
+ case OptionParser::eNoArgument:
+ break;
+ default:
+ result.AppendErrorWithFormat("error with options table; invalid value "
+ "in has_arg field for option '%c'.\n",
+ val);
+ result.SetStatus(eReturnStatusFailed);
+ return;
+ }
+ if (!option_arg)
+ option_arg = "<no-argument>";
+ option_arg_vector->emplace_back(option_str.GetData(), has_arg, option_arg);
+
+ // Find option in the argument list; also see if it was supposed to take
+ // an argument and if one was supplied. Remove option (and argument, if
+ // given) from the argument list. Also remove them from the
+ // raw_input_string, if one was passed in.
+ size_t idx = FindArgumentIndexForOption(long_options, long_options_index);
+ if (idx < GetArgumentCount()) {
+ if (raw_input_string.size() > 0) {
+ const char *tmp_arg = GetArgumentAtIndex(idx);
+ size_t pos = raw_input_string.find(tmp_arg);
+ if (pos != std::string::npos)
+ raw_input_string.erase(pos, strlen(tmp_arg));
+ }
+ ReplaceArgumentAtIndex(idx, llvm::StringRef());
+ if ((long_options[long_options_index].definition->option_has_arg !=
+ OptionParser::eNoArgument) &&
+ (OptionParser::GetOptionArgument() != nullptr) &&
+ (idx + 1 < GetArgumentCount()) &&
+ (strcmp(OptionParser::GetOptionArgument(),
+ GetArgumentAtIndex(idx + 1)) == 0)) {
if (raw_input_string.size() > 0) {
- const char *tmp_arg = GetArgumentAtIndex(idx);
+ const char *tmp_arg = GetArgumentAtIndex(idx + 1);
size_t pos = raw_input_string.find(tmp_arg);
if (pos != std::string::npos)
raw_input_string.erase(pos, strlen(tmp_arg));
}
- ReplaceArgumentAtIndex(idx, llvm::StringRef());
- if ((long_options[long_options_index].definition->option_has_arg !=
- OptionParser::eNoArgument) &&
- (OptionParser::GetOptionArgument() != nullptr) &&
- (idx + 1 < GetArgumentCount()) &&
- (strcmp(OptionParser::GetOptionArgument(),
- GetArgumentAtIndex(idx + 1)) == 0)) {
- if (raw_input_string.size() > 0) {
- const char *tmp_arg = GetArgumentAtIndex(idx + 1);
- size_t pos = raw_input_string.find(tmp_arg);
- if (pos != std::string::npos)
- raw_input_string.erase(pos, strlen(tmp_arg));
- }
- ReplaceArgumentAtIndex(idx + 1, llvm::StringRef());
- }
+ ReplaceArgumentAtIndex(idx + 1, llvm::StringRef());
}
}
-
- if (!result.Succeeded())
- break;
}
}
Modified: lldb/trunk/source/Interpreter/CommandAlias.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandAlias.cpp?rev=283159&r1=283158&r2=283159&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandAlias.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandAlias.cpp Mon Oct 3 18:20:36 2016
@@ -55,15 +55,13 @@ static bool ProcessAliasOptionsArgs(lldb
if (!options_string.empty()) {
if (cmd_obj_sp->WantsRawCommandString())
- option_arg_vector->push_back(
- OptionArgPair("<argument>", OptionArgValue(-1, options_string)));
+ option_arg_vector->emplace_back("<argument>", -1, options_string);
else {
const size_t argc = args.GetArgumentCount();
for (size_t i = 0; i < argc; ++i)
if (strcmp(args.GetArgumentAtIndex(i), "") != 0)
- option_arg_vector->push_back(OptionArgPair(
- "<argument>",
- OptionArgValue(-1, std::string(args.GetArgumentAtIndex(i)))));
+ option_arg_vector->emplace_back("<argument>", -1,
+ args.GetArgumentAtIndex(i));
}
}
@@ -149,21 +147,24 @@ void CommandAlias::GetAliasExpansion(Str
const char *command_name = m_underlying_command_sp->GetCommandName();
help_string.Printf("'%s", command_name);
- if (m_option_args_sp) {
- OptionArgVector *options = m_option_args_sp.get();
- for (size_t i = 0; i < options->size(); ++i) {
- OptionArgPair cur_option = (*options)[i];
- std::string opt = cur_option.first;
- OptionArgValue value_pair = cur_option.second;
- std::string value = value_pair.second;
- if (opt.compare("<argument>") == 0) {
+ if (!m_option_args_sp) {
+ help_string.Printf("'");
+ return;
+ }
+
+ OptionArgVector *options = m_option_args_sp.get();
+ std::string opt;
+ std::string value;
+
+ for (const auto &opt_entry : *options) {
+ std::tie(opt, std::ignore, value) = opt_entry;
+ if (opt == "<argument>") {
+ help_string.Printf(" %s", value.c_str());
+ } else {
+ help_string.Printf(" %s", opt.c_str());
+ if ((value.compare("<no-argument>") != 0) &&
+ (value.compare("<need-argument") != 0)) {
help_string.Printf(" %s", value.c_str());
- } else {
- help_string.Printf(" %s", opt.c_str());
- if ((value.compare("<no-argument>") != 0) &&
- (value.compare("<need-argument") != 0)) {
- help_string.Printf(" %s", value.c_str());
- }
}
}
}
@@ -172,24 +173,30 @@ void CommandAlias::GetAliasExpansion(Str
}
bool CommandAlias::IsDashDashCommand() {
- if (m_is_dashdash_alias == eLazyBoolCalculate) {
- m_is_dashdash_alias = eLazyBoolNo;
- if (IsValid()) {
- for (const OptionArgPair &opt_arg : *GetOptionArguments()) {
- if (opt_arg.first == "<argument>" && !opt_arg.second.second.empty() &&
- llvm::StringRef(opt_arg.second.second).endswith("--")) {
- m_is_dashdash_alias = eLazyBoolYes;
- break;
- }
- }
- // if this is a nested alias, it may be adding arguments on top of an
- // already dash-dash alias
- if ((m_is_dashdash_alias == eLazyBoolNo) && IsNestedAlias())
- m_is_dashdash_alias =
- (GetUnderlyingCommand()->IsDashDashCommand() ? eLazyBoolYes
- : eLazyBoolNo);
+ if (m_is_dashdash_alias != eLazyBoolCalculate)
+ return (m_is_dashdash_alias == eLazyBoolYes);
+ m_is_dashdash_alias = eLazyBoolNo;
+ if (!IsValid())
+ return false;
+
+ std::string opt;
+ std::string value;
+
+ for (const auto &opt_entry : *GetOptionArguments()) {
+ std::tie(opt, std::ignore, value) = opt_entry;
+ if (opt == "<argument>" && !value.empty() &&
+ llvm::StringRef(value).endswith("--")) {
+ m_is_dashdash_alias = eLazyBoolYes;
+ break;
}
}
+
+ // if this is a nested alias, it may be adding arguments on top of an
+ // already dash-dash alias
+ if ((m_is_dashdash_alias == eLazyBoolNo) && IsNestedAlias())
+ m_is_dashdash_alias =
+ (GetUnderlyingCommand()->IsDashDashCommand() ? eLazyBoolYes
+ : eLazyBoolNo);
return (m_is_dashdash_alias == eLazyBoolYes);
}
Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=283159&r1=283158&r2=283159&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Mon Oct 3 18:20:36 2016
@@ -1325,61 +1325,63 @@ CommandObject *CommandInterpreter::Build
alias_cmd_obj = GetCommandObject(alias_name);
StreamString result_str;
- if (alias_cmd_obj && alias_cmd_obj->IsAlias()) {
- std::pair<CommandObjectSP, OptionArgVectorSP> desugared =
- ((CommandAlias *)alias_cmd_obj)->Desugar();
- OptionArgVectorSP option_arg_vector_sp = desugared.second;
- alias_cmd_obj = desugared.first.get();
- std::string alias_name_str = alias_name;
- if ((cmd_args.GetArgumentCount() == 0) ||
- (alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0))
- cmd_args.Unshift(alias_name_str);
-
- result_str.Printf("%s", alias_cmd_obj->GetCommandName());
-
- if (option_arg_vector_sp.get()) {
- OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
-
- for (size_t i = 0; i < option_arg_vector->size(); ++i) {
- OptionArgPair option_pair = (*option_arg_vector)[i];
- OptionArgValue value_pair = option_pair.second;
- int value_type = value_pair.first;
- std::string option = option_pair.first;
- std::string value = value_pair.second;
- if (option.compare("<argument>") == 0)
- result_str.Printf(" %s", value.c_str());
- else {
- result_str.Printf(" %s", option.c_str());
- if (value_type != OptionParser::eNoArgument) {
- if (value_type != OptionParser::eOptionalArgument)
- result_str.Printf(" ");
- int index = GetOptionArgumentPosition(value.c_str());
- if (index == 0)
- result_str.Printf("%s", value.c_str());
- else if (static_cast<size_t>(index) >=
- cmd_args.GetArgumentCount()) {
-
- result.AppendErrorWithFormat("Not enough arguments provided; you "
- "need at least %d arguments to use "
- "this alias.\n",
- index);
- result.SetStatus(eReturnStatusFailed);
- return nullptr;
- } else {
- size_t strpos =
- raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
- if (strpos != std::string::npos)
- raw_input_string = raw_input_string.erase(
- strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
- result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
- }
- }
- }
- }
- }
+ if (!alias_cmd_obj || !alias_cmd_obj->IsAlias()) {
+ alias_result.clear();
+ return alias_cmd_obj;
+ }
+ std::pair<CommandObjectSP, OptionArgVectorSP> desugared =
+ ((CommandAlias *)alias_cmd_obj)->Desugar();
+ OptionArgVectorSP option_arg_vector_sp = desugared.second;
+ alias_cmd_obj = desugared.first.get();
+ std::string alias_name_str = alias_name;
+ if ((cmd_args.GetArgumentCount() == 0) ||
+ (alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0))
+ cmd_args.Unshift(alias_name_str);
+
+ result_str.Printf("%s", alias_cmd_obj->GetCommandName());
+ if (!option_arg_vector_sp.get()) {
alias_result = result_str.GetData();
+ return alias_cmd_obj;
+ }
+ OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
+
+ int value_type;
+ std::string option;
+ std::string value;
+ for (const auto &entry : *option_arg_vector) {
+ std::tie(option, value_type, value) = entry;
+ if (option == "<argument>") {
+ result_str.Printf(" %s", value.c_str());
+ continue;
+ }
+
+ result_str.Printf(" %s", option.c_str());
+ if (value_type == OptionParser::eNoArgument)
+ continue;
+
+ if (value_type != OptionParser::eOptionalArgument)
+ result_str.Printf(" ");
+ int index = GetOptionArgumentPosition(value.c_str());
+ if (index == 0)
+ result_str.Printf("%s", value.c_str());
+ else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
+
+ result.AppendErrorWithFormat("Not enough arguments provided; you "
+ "need at least %d arguments to use "
+ "this alias.\n",
+ index);
+ result.SetStatus(eReturnStatusFailed);
+ return nullptr;
+ }
+ size_t strpos = raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
+ if (strpos != std::string::npos)
+ raw_input_string = raw_input_string.erase(
+ strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
+ result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
}
+
+ alias_result = result_str.GetData();
return alias_cmd_obj;
}
@@ -1977,73 +1979,68 @@ void CommandInterpreter::BuildAliasComma
used[0] = true;
- for (size_t i = 0; i < option_arg_vector->size(); ++i) {
- OptionArgPair option_pair = (*option_arg_vector)[i];
- OptionArgValue value_pair = option_pair.second;
- int value_type = value_pair.first;
- std::string option = option_pair.first;
- std::string value = value_pair.second;
- if (option.compare("<argument>") == 0) {
- if (!wants_raw_input || (value.compare("--") != 0)) // Since we inserted
- // this above, make
- // sure we don't
- // insert it twice
- new_args
- .AppendArgument(
- value);
- } else {
+ int value_type;
+ std::string option;
+ std::string value;
+ for (const auto &option_entry : *option_arg_vector) {
+ std::tie(option, value_type, value) = option_entry;
+ if (option == "<argument>") {
+ if (!wants_raw_input || (value != "--")) {
+ // Since we inserted this above, make sure we don't insert it twice
+ new_args.AppendArgument(value);
+ }
+ continue;
+ }
+
+ if (value_type != OptionParser::eOptionalArgument)
+ new_args.AppendArgument(option);
+
+ if (value == "<no-argument>")
+ continue;
+
+ int index = GetOptionArgumentPosition(value.c_str());
+ if (index == 0) {
+ // value was NOT a positional argument; must be a real value
if (value_type != OptionParser::eOptionalArgument)
- new_args.AppendArgument(option);
- if (value.compare("<no-argument>") != 0) {
- int index = GetOptionArgumentPosition(value.c_str());
- if (index == 0) {
- // value was NOT a positional argument; must be a real value
- if (value_type != OptionParser::eOptionalArgument)
- new_args.AppendArgument(value);
- else {
- char buffer[255];
- ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(),
- value.c_str());
- new_args.AppendArgument(llvm::StringRef(buffer));
- }
+ new_args.AppendArgument(value);
+ else {
+ char buffer[255];
+ ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(),
+ value.c_str());
+ new_args.AppendArgument(llvm::StringRef(buffer));
+ }
- } else if (static_cast<size_t>(index) >=
- cmd_args.GetArgumentCount()) {
- result.AppendErrorWithFormat("Not enough arguments provided; you "
- "need at least %d arguments to use "
- "this alias.\n",
- index);
- result.SetStatus(eReturnStatusFailed);
- return;
- } else {
- // Find and remove cmd_args.GetArgumentAtIndex(i) from
- // raw_input_string
- size_t strpos =
- raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
- if (strpos != std::string::npos) {
- raw_input_string = raw_input_string.erase(
- strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
- }
+ } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
+ result.AppendErrorWithFormat("Not enough arguments provided; you "
+ "need at least %d arguments to use "
+ "this alias.\n",
+ index);
+ result.SetStatus(eReturnStatusFailed);
+ return;
+ } else {
+ // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
+ size_t strpos =
+ raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
+ if (strpos != std::string::npos) {
+ raw_input_string = raw_input_string.erase(
+ strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
+ }
- if (value_type != OptionParser::eOptionalArgument)
- new_args.AppendArgument(llvm::StringRef::withNullAsEmpty(
- cmd_args.GetArgumentAtIndex(index)));
- else {
- char buffer[255];
- ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(),
- cmd_args.GetArgumentAtIndex(index));
- new_args.AppendArgument(llvm::StringRef(buffer));
- }
- used[index] = true;
- }
+ if (value_type != OptionParser::eOptionalArgument)
+ new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index));
+ else {
+ char buffer[255];
+ ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(),
+ cmd_args.GetArgumentAtIndex(index));
+ new_args.AppendArgument(buffer);
}
+ used[index] = true;
}
}
for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j) {
if (!used[j] && !wants_raw_input)
- new_args.AppendArgument(
- llvm::StringRef(cmd_args.GetArgumentAtIndex(j)));
+ new_args.AppendArgument(cmd_args.GetArgumentAtIndex(j));
}
cmd_args.Clear();
More information about the lldb-commits
mailing list