Skip to content

Commit

Permalink
Move Args::StringTo*** functions to a new OptionArgParser class
Browse files Browse the repository at this point in the history
Summary:
The idea behind this is to move the functionality which depend on other lldb
classes into a separate class. This way, the Args class can be turned
into a lightweight arc+argv wrapper and moved into the lower lldb
layers.

Reviewers: jingham, zturner

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D44306

llvm-svn: 329677
  • Loading branch information
labath committed Apr 10, 2018
1 parent bfa20dd commit 47cbf4a
Show file tree
Hide file tree
Showing 35 changed files with 555 additions and 454 deletions.
26 changes: 0 additions & 26 deletions lldb/include/lldb/Interpreter/Args.h
Expand Up @@ -12,7 +12,6 @@

// C Includes
// C++ Includes
#include <list>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -22,7 +21,6 @@
#include "llvm/ADT/StringRef.h"
// Project includes
#include "lldb/Utility/Environment.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-private-types.h"
#include "lldb/lldb-types.h"

Expand Down Expand Up @@ -343,30 +341,6 @@ class Args {
return min <= sval64 && sval64 <= max;
}

static lldb::addr_t StringToAddress(const ExecutionContext *exe_ctx,
llvm::StringRef s,
lldb::addr_t fail_value, Status *error);

static bool StringToBoolean(llvm::StringRef s, bool fail_value,
bool *success_ptr);

static char StringToChar(llvm::StringRef s, char fail_value,
bool *success_ptr);

static int64_t StringToOptionEnum(llvm::StringRef s,
OptionEnumValueElement *enum_values,
int32_t fail_value, Status &error);

static lldb::ScriptLanguage
StringToScriptLanguage(llvm::StringRef s, lldb::ScriptLanguage fail_value,
bool *success_ptr);

// TODO: Use StringRef
static Status StringToFormat(const char *s, lldb::Format &format,
size_t *byte_size_ptr); // If non-NULL, then a
// byte size can precede
// the format character

static lldb::Encoding
StringToEncoding(llvm::StringRef s,
lldb::Encoding fail_value = lldb::eEncodingInvalid);
Expand Down
43 changes: 43 additions & 0 deletions lldb/include/lldb/Interpreter/OptionArgParser.h
@@ -0,0 +1,43 @@
//===-- OptionArgParser.h ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_INTERPRETER_OPTIONARGPARSER_H
#define LLDB_INTERPRETER_OPTIONARGPARSER_H

#include "lldb/lldb-private-types.h"

namespace lldb_private {

struct OptionArgParser {
static lldb::addr_t ToAddress(const ExecutionContext *exe_ctx,
llvm::StringRef s, lldb::addr_t fail_value,
Status *error);

static bool ToBoolean(llvm::StringRef s, bool fail_value, bool *success_ptr);

static char ToChar(llvm::StringRef s, char fail_value, bool *success_ptr);

static int64_t ToOptionEnum(llvm::StringRef s,
OptionEnumValueElement *enum_values,
int32_t fail_value, Status &error);

static lldb::ScriptLanguage ToScriptLanguage(llvm::StringRef s,
lldb::ScriptLanguage fail_value,
bool *success_ptr);

// TODO: Use StringRef
static Status ToFormat(const char *s, lldb::Format &format,
size_t *byte_size_ptr); // If non-NULL, then a
// byte size can precede
// the format character
};

} // namespace lldb_private

#endif // LLDB_INTERPRETER_OPTIONARGPARSER_H
1 change: 1 addition & 0 deletions lldb/include/lldb/Interpreter/Options.h
Expand Up @@ -18,6 +18,7 @@
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-defines.h"
#include "lldb/lldb-private.h"

Expand Down
5 changes: 3 additions & 2 deletions lldb/source/API/SBDebugger.cpp
Expand Up @@ -47,6 +47,7 @@
#include "lldb/Initialization/SystemLifetimeManager.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/OptionGroupPlatform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/TargetList.h"
Expand Down Expand Up @@ -480,8 +481,8 @@ bool SBDebugger::SetDefaultArchitecture(const char *arch_name) {
ScriptLanguage
SBDebugger::GetScriptingLanguage(const char *script_language_name) {
if (!script_language_name) return eScriptLanguageDefault;
return Args::StringToScriptLanguage(llvm::StringRef(script_language_name),
eScriptLanguageDefault, nullptr);
return OptionArgParser::ToScriptLanguage(
llvm::StringRef(script_language_name), eScriptLanguageDefault, nullptr);
}

const char *SBDebugger::GetVersionString() {
Expand Down
27 changes: 14 additions & 13 deletions lldb/source/Commands/CommandObjectBreakpoint.cpp
Expand Up @@ -22,6 +22,7 @@
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/OptionValueBoolean.h"
#include "lldb/Interpreter/OptionValueString.h"
#include "lldb/Interpreter/OptionValueUInt64.h"
Expand Down Expand Up @@ -100,7 +101,7 @@ class lldb_private::BreakpointOptionGroup : public OptionGroup
break;
case 'G': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = OptionArgParser::ToBoolean(option_arg, false, &success);
if (success) {
m_bp_opts.SetAutoContinue(value);
} else
Expand All @@ -121,7 +122,7 @@ class lldb_private::BreakpointOptionGroup : public OptionGroup
break;
case 'o': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = OptionArgParser::ToBoolean(option_arg, false, &success);
if (success) {
m_bp_opts.SetOneShot(value);
} else
Expand Down Expand Up @@ -377,8 +378,8 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {

switch (short_option) {
case 'a': {
m_load_addr = Args::StringToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
} break;

case 'A':
Expand Down Expand Up @@ -442,7 +443,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {

case 'h': {
bool success;
m_catch_bp = Args::StringToBoolean(option_arg, true, &success);
m_catch_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"Invalid boolean value for on-catch option: '%s'",
Expand All @@ -456,7 +457,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
case 'K': {
bool success;
bool value;
value = Args::StringToBoolean(option_arg, true, &success);
value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (value)
m_skip_prologue = eLazyBoolYes;
else
Expand Down Expand Up @@ -485,7 +486,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
case 'm': {
bool success;
bool value;
value = Args::StringToBoolean(option_arg, true, &success);
value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (value)
m_move_to_nearest_code = eLazyBoolYes;
else
Expand Down Expand Up @@ -519,8 +520,8 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {

case 'R': {
lldb::addr_t tmp_offset_addr;
tmp_offset_addr =
Args::StringToAddress(execution_context, option_arg, 0, &error);
tmp_offset_addr = OptionArgParser::ToAddress(execution_context,
option_arg, 0, &error);
if (error.Success())
m_offset_addr = tmp_offset_addr;
} break;
Expand Down Expand Up @@ -549,7 +550,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {

case 'w': {
bool success;
m_throw_bp = Args::StringToBoolean(option_arg, true, &success);
m_throw_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"Invalid boolean value for on-throw option: '%s'",
Expand Down Expand Up @@ -1783,7 +1784,7 @@ class BreakpointAccessOptionGroup : public OptionGroup
switch (short_option) {
case 'L': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = OptionArgParser::ToBoolean(option_arg, false, &success);
if (success) {
m_permissions.SetAllowList(value);
} else
Expand All @@ -1793,7 +1794,7 @@ class BreakpointAccessOptionGroup : public OptionGroup
} break;
case 'A': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = OptionArgParser::ToBoolean(option_arg, false, &success);
if (success) {
m_permissions.SetAllowDisable(value);
} else
Expand All @@ -1803,7 +1804,7 @@ class BreakpointAccessOptionGroup : public OptionGroup
} break;
case 'D': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = OptionArgParser::ToBoolean(option_arg, false, &success);
if (success) {
m_permissions.SetAllowDelete(value);
} else
Expand Down
6 changes: 4 additions & 2 deletions lldb/source/Commands/CommandObjectBreakpointCommand.cpp
Expand Up @@ -22,6 +22,7 @@
#include "lldb/Host/OptionParser.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"

Expand Down Expand Up @@ -293,7 +294,7 @@ are no syntax errors may indicate that a function was declared but never called.
break;

case 's':
m_script_language = (lldb::ScriptLanguage)Args::StringToOptionEnum(
m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
option_arg, g_breakpoint_add_options[option_idx].enum_values,
eScriptLanguageNone, error);

Expand All @@ -307,7 +308,8 @@ are no syntax errors may indicate that a function was declared but never called.

case 'e': {
bool success = false;
m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
m_stop_on_error =
OptionArgParser::ToBoolean(option_arg, false, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid value for stop-on-error: \"%s\"",
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Commands/CommandObjectCommands.cpp
Expand Up @@ -23,6 +23,7 @@
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandObjectRegexCommand.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/OptionValueBoolean.h"
#include "lldb/Interpreter/OptionValueString.h"
#include "lldb/Interpreter/OptionValueUInt64.h"
Expand Down Expand Up @@ -1639,7 +1640,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
break;
case 's':
m_synchronicity =
(ScriptedCommandSynchronicity)Args::StringToOptionEnum(
(ScriptedCommandSynchronicity)OptionArgParser::ToOptionEnum(
option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
if (!error.Success())
error.SetErrorStringWithFormat(
Expand Down
11 changes: 6 additions & 5 deletions lldb/source/Commands/CommandObjectDisassemble.cpp
Expand Up @@ -20,6 +20,7 @@
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/Options.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/Symbol.h"
Expand Down Expand Up @@ -101,14 +102,14 @@ Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
break;

case 's': {
start_addr = Args::StringToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
start_addr = OptionArgParser::ToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
if (start_addr != LLDB_INVALID_ADDRESS)
some_location_specified = true;
} break;
case 'e': {
end_addr = Args::StringToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
end_addr = OptionArgParser::ToAddress(execution_context, option_arg,
LLDB_INVALID_ADDRESS, &error);
if (end_addr != LLDB_INVALID_ADDRESS)
some_location_specified = true;
} break;
Expand Down Expand Up @@ -168,7 +169,7 @@ Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
break;

case 'a': {
symbol_containing_addr = Args::StringToAddress(
symbol_containing_addr = OptionArgParser::ToAddress(
execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
if (symbol_containing_addr != LLDB_INVALID_ADDRESS) {
some_location_specified = true;
Expand Down
15 changes: 8 additions & 7 deletions lldb/source/Commands/CommandObjectExpression.cpp
Expand Up @@ -27,6 +27,7 @@
#include "lldb/Host/OptionParser.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/Language.h"
Expand Down Expand Up @@ -88,7 +89,7 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(
case 'a': {
bool success;
bool result;
result = Args::StringToBoolean(option_arg, true, &success);
result = OptionArgParser::ToBoolean(option_arg, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid all-threads value setting: \"%s\"",
Expand All @@ -99,7 +100,7 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(

case 'i': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (success)
ignore_breakpoints = tmp_value;
else
Expand All @@ -111,7 +112,7 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(

case 'j': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (success)
allow_jit = tmp_value;
else
Expand All @@ -131,7 +132,7 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(

case 'u': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (success)
unwind_on_error = tmp_value;
else
Expand All @@ -146,8 +147,8 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(
m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
break;
}
m_verbosity =
(LanguageRuntimeDescriptionDisplayVerbosity)Args::StringToOptionEnum(
m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity)
OptionArgParser::ToOptionEnum(
option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
if (!error.Success())
error.SetErrorStringWithFormat(
Expand All @@ -167,7 +168,7 @@ Status CommandObjectExpression::CommandOptions::SetOptionValue(

case 'X': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
if (success)
auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo;
else
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Commands/CommandObjectLog.cpp
Expand Up @@ -19,6 +19,7 @@
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/Options.h"
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/ObjectFile.h"
Expand Down Expand Up @@ -345,7 +346,7 @@ class CommandObjectLogTimer : public CommandObjectParsed {
}
} else if (sub_command.equals_lower("increment")) {
bool success;
bool increment = Args::StringToBoolean(param, false, &success);
bool increment = OptionArgParser::ToBoolean(param, false, &success);
if (success) {
Timer::SetQuiet(!increment);
result.SetStatus(eReturnStatusSuccessFinishNoResult);
Expand Down

0 comments on commit 47cbf4a

Please sign in to comment.