Fix more functions in Args to use StringRef.

This patch also marks the const char* versions as =delete to prevent
their use.  This has the potential to cause build breakages on some
platforms which I can't compile.  I have tested on Windows, Linux,
and OSX.  Best practices for fixing broken callsites are outlined in
Args.h in a comment above the deleted function declarations.

Eventually we can remove these =delete declarations, but for now they
are important to make sure that all implicit conversions from
const char * are manually audited to make sure that they do not invoke a
conversion from nullptr.

llvm-svn: 281919
This commit is contained in:
Zachary Turner 2016-09-19 17:54:06 +00:00
parent 495b211a6c
commit ecbb0bb169
34 changed files with 194 additions and 151 deletions

View File

@ -179,13 +179,53 @@ public:
/// @return
/// The NULL terminated C string of the copy of \a arg_cstr.
//------------------------------------------------------------------
// TODO: Convert this function to use a StringRef.
const char *AppendArgument(const char *arg_cstr, char quote_char = '\0');
llvm::StringRef AppendArgument(llvm::StringRef arg_str,
char quote_char = '\0');
void AppendArguments(const Args &rhs);
void AppendArguments(const char **argv);
// Delete const char* versions of StringRef functions. Normally this would
// not
// be necessary, as const char * is implicitly convertible to StringRef.
// However,
// since the use of const char* is so pervasive, and since StringRef will
// assert
// if you try to construct one from nullptr, this allows the compiler to catch
// instances of the function being invoked with a const char *, allowing us to
// replace them with explicit conversions at each call-site. Once StringRef
// use becomes more pervasive, there will be fewer implicit conversions
// because
// we will be using StringRefs across the whole pipeline, so we won't have to
// have
// this "glue" that converts between the two, and at that point it becomes
// easy
// to just make sure you don't pass nullptr into the function.
// Call-site fixing methodology:
// 1. If you know the string cannot be null (e.g. it's a const char[], or
// it's
// been checked for null), use llvm::StringRef(ptr).
// 2. If you don't know if it can be null (e.g. it's returned from a
// function
// whose semantics are unclear), use
// llvm::StringRef::withNullAsEmpty(ptr).
// 3. If it's .c_str() of a std::string, just pass the std::string directly.
// 4. If it's .str().c_str() of a StringRef, just pass the StringRef
// directly.
void ReplaceArgumentAtIndex(size_t, const char *, char = '\0') = delete;
void AppendArgument(const char *arg_str, char quote_char = '\0') = delete;
void InsertArgumentAtIndex(size_t, const char *, char = '\0') = delete;
static bool StringToBoolean(const char *, bool, bool *) = delete;
static lldb::ScriptLanguage
StringToScriptLanguage(const char *, lldb::ScriptLanguage, bool *) = delete;
static lldb::Encoding
StringToEncoding(const char *,
lldb::Encoding = lldb::eEncodingInvalid) = delete;
static uint32_t StringToGenericRegister(const char *) = delete;
static bool StringToVersion(const char *, uint32_t &, uint32_t &,
uint32_t &) = delete;
//------------------------------------------------------------------
/// Insert the argument value at index \a idx to \a arg_cstr.
///
@ -201,8 +241,8 @@ public:
/// @return
/// The NULL terminated C string of the copy of \a arg_cstr.
//------------------------------------------------------------------
const char *InsertArgumentAtIndex(size_t idx, const char *arg_cstr,
char quote_char = '\0');
llvm::StringRef InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
char quote_char = '\0');
//------------------------------------------------------------------
/// Replaces the argument value at index \a idx to \a arg_cstr
@ -221,8 +261,8 @@ public:
/// The NULL terminated C string of the copy of \a arg_cstr if
/// \a idx was a valid index, NULL otherwise.
//------------------------------------------------------------------
const char *ReplaceArgumentAtIndex(size_t idx, const char *arg_cstr,
char quote_char = '\0');
llvm::StringRef ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
char quote_char = '\0');
//------------------------------------------------------------------
/// Deletes the argument value at index
@ -359,9 +399,6 @@ public:
const char *s, lldb::addr_t fail_value,
Error *error);
static bool StringToBoolean(const char *s, bool fail_value,
bool *success_ptr);
static bool StringToBoolean(llvm::StringRef s, bool fail_value,
bool *success_ptr);
@ -382,16 +419,10 @@ public:
// byte size can precede
// the format character
static lldb::Encoding
StringToEncoding(const char *s,
lldb::Encoding fail_value = lldb::eEncodingInvalid);
static lldb::Encoding
StringToEncoding(llvm::StringRef s,
lldb::Encoding fail_value = lldb::eEncodingInvalid);
static uint32_t StringToGenericRegister(const char *s);
static uint32_t StringToGenericRegister(llvm::StringRef s);
static bool StringToVersion(llvm::StringRef string, uint32_t &major,

View File

@ -251,7 +251,8 @@ SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) {
PlatformSP platform_sp(GetSP());
if (platform_sp && connect_options.GetURL()) {
Args args;
args.AppendArgument(connect_options.GetURL());
args.AppendArgument(
llvm::StringRef::withNullAsEmpty(connect_options.GetURL()));
sb_error.ref() = platform_sp->ConnectRemote(args);
} else {
sb_error.SetErrorString("invalid platform");

View File

@ -215,7 +215,7 @@ void BreakpointIDList::FindAndReplaceIDRanges(Args &old_args, Target *target,
StreamString canonical_id_str;
BreakpointID::GetCanonicalReference(&canonical_id_str, bp_id,
bp_loc->GetID());
new_args.AppendArgument(canonical_id_str.GetData());
new_args.AppendArgument(canonical_id_str.GetString());
}
}
}
@ -309,7 +309,7 @@ void BreakpointIDList::FindAndReplaceIDRanges(Args &old_args, Target *target,
StreamString canonical_id_str;
BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
bp_loc->GetID());
new_args.AppendArgument(canonical_id_str.GetData());
new_args.AppendArgument(canonical_id_str.GetString());
}
}
} else if ((cur_bp_id == end_bp_id) &&
@ -321,19 +321,19 @@ void BreakpointIDList::FindAndReplaceIDRanges(Args &old_args, Target *target,
StreamString canonical_id_str;
BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
bp_loc->GetID());
new_args.AppendArgument(canonical_id_str.GetData());
new_args.AppendArgument(canonical_id_str.GetString());
}
}
} else {
StreamString canonical_id_str;
BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
LLDB_INVALID_BREAK_ID);
new_args.AppendArgument(canonical_id_str.GetData());
new_args.AppendArgument(canonical_id_str.GetString());
}
}
} else // else is_range was false
{
new_args.AppendArgument(current_arg);
new_args.AppendArgument(llvm::StringRef::withNullAsEmpty(current_arg));
}
}
@ -345,7 +345,7 @@ void BreakpointIDList::FindAndReplaceIDRanges(Args &old_args, Target *target,
StreamString canonical_id_str;
BreakpointID::GetCanonicalReference(
&canonical_id_str, bkpt_sp->GetID(), LLDB_INVALID_BREAK_ID);
new_args.AppendArgument(canonical_id_str.GetData());
new_args.AppendArgument(canonical_id_str.GetString());
}
}
}

View File

@ -172,7 +172,7 @@ public:
case 'h': {
bool success;
m_catch_bp = Args::StringToBoolean(option_arg, true, &success);
m_catch_bp = Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"Invalid boolean value for on-catch option: '%s'", option_arg);
@ -192,7 +192,7 @@ public:
case 'K': {
bool success;
bool value;
value = Args::StringToBoolean(option_arg, true, &success);
value = Args::StringToBoolean(option_strref, true, &success);
if (value)
m_skip_prologue = eLazyBoolYes;
else
@ -223,7 +223,7 @@ public:
case 'm': {
bool success;
bool value;
value = Args::StringToBoolean(option_arg, true, &success);
value = Args::StringToBoolean(option_strref, true, &success);
if (value)
m_move_to_nearest_code = eLazyBoolYes;
else
@ -265,8 +265,8 @@ public:
break;
case 'O':
m_exception_extra_args.AppendArgument("-O");
m_exception_extra_args.AppendArgument(option_arg);
m_exception_extra_args.AppendArgument(llvm::StringRef("-O"));
m_exception_extra_args.AppendArgument(option_strref);
break;
case 'p':
@ -304,7 +304,7 @@ public:
case 'w': {
bool success;
m_throw_bp = Args::StringToBoolean(option_arg, true, &success);
m_throw_bp = Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"Invalid boolean value for on-throw option: '%s'", option_arg);
@ -861,7 +861,8 @@ public:
break;
case 'o': {
bool value, success;
value = Args::StringToBoolean(option_arg, false, &success);
value = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_arg), false, &success);
if (success) {
m_one_shot_passed = true;
m_one_shot = value;

View File

@ -268,6 +268,7 @@ are no syntax errors may indicate that a function was declared but never called.
ExecutionContext *execution_context) override {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'o':
@ -290,7 +291,7 @@ 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 = Args::StringToBoolean(option_strref, false, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid value for stop-on-error: \"%s\"", option_arg);

View File

@ -78,6 +78,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
ExecutionContext *execution_context) {
Error error;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
const int short_option = g_option_table[option_idx].short_option;
switch (short_option) {
@ -91,7 +92,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
case 'a': {
bool success;
bool result;
result = Args::StringToBoolean(option_arg, true, &success);
result = Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid all-threads value setting: \"%s\"", option_arg);
@ -101,7 +102,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
case 'i': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = Args::StringToBoolean(option_strref, true, &success);
if (success)
ignore_breakpoints = tmp_value;
else
@ -112,7 +113,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
case 'j': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = Args::StringToBoolean(option_strref, true, &success);
if (success)
allow_jit = tmp_value;
else
@ -134,7 +135,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
case 'u': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = Args::StringToBoolean(option_strref, true, &success);
if (success)
unwind_on_error = tmp_value;
else
@ -168,7 +169,7 @@ Error CommandObjectExpression::CommandOptions::SetOptionValue(
case 'X': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
bool tmp_value = Args::StringToBoolean(option_strref, true, &success);
if (success)
auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo;
else

View File

@ -387,8 +387,9 @@ protected:
}
if (strcasecmp(sub_command, "increment") == 0) {
bool success;
bool increment =
Args::StringToBoolean(args.GetArgumentAtIndex(1), false, &success);
bool increment = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(args.GetArgumentAtIndex(1)), false,
&success);
if (success) {
Timer::SetQuiet(!increment);
result.SetStatus(eReturnStatusSuccessFinishNoResult);

View File

@ -1478,7 +1478,8 @@ protected:
break;
case eFormatBoolean:
uval64 = Args::StringToBoolean(value_str, false, &success);
uval64 = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(value_str), false, &success);
if (!success) {
result.AppendErrorWithFormat(
"'%s' is not a valid boolean string value.\n", value_str);

View File

@ -210,7 +210,7 @@ int CommandObjectMultiword::HandleCompletion(Args &input, int &cursor_index,
matches.DeleteStringAtIndex(0);
input.Shift();
cursor_char_position = 0;
input.AppendArgument("");
input.AppendArgument(llvm::StringRef());
return cmd_obj->HandleCompletion(
input, cursor_index, cursor_char_position, match_start_point,
max_return_elements, word_complete, matches);

View File

@ -29,6 +29,8 @@
#include "lldb/Target/Process.h"
#include "lldb/Utility/Utils.h"
#include "llvm/ADT/SmallString.h"
using namespace lldb;
using namespace lldb_private;
@ -1057,9 +1059,9 @@ protected:
Module *exe_module = target->GetExecutableModulePointer();
if (exe_module) {
m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
char exe_path[PATH_MAX];
if (m_options.launch_info.GetExecutableFile().GetPath(exe_path,
sizeof(exe_path)))
llvm::SmallString<PATH_MAX> exe_path;
m_options.launch_info.GetExecutableFile().GetPath(exe_path);
if (!exe_path.empty())
m_options.launch_info.GetArguments().AppendArgument(exe_path);
m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
}

View File

@ -212,7 +212,7 @@ protected:
if (target_settings_argv0) {
m_options.launch_info.GetArguments().AppendArgument(
target_settings_argv0);
llvm::StringRef(target_settings_argv0));
m_options.launch_info.SetExecutableFile(
exe_module_sp->GetPlatformFileSpec(), false);
} else {
@ -760,12 +760,13 @@ public:
ExecutionContext *execution_context) override {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 's':
bool tmp_result;
bool success;
tmp_result = Args::StringToBoolean(option_arg, false, &success);
tmp_result = Args::StringToBoolean(option_strref, false, &success);
if (!success)
error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
option_arg);
@ -1458,7 +1459,7 @@ public:
bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
bool okay = true;
bool success = false;
bool tmp_value = Args::StringToBoolean(option.c_str(), false, &success);
bool tmp_value = Args::StringToBoolean(option, false, &success);
if (success && tmp_value)
real_value = 1;

View File

@ -157,6 +157,7 @@ public:
ExecutionContext *execution_context) override {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'c': {
@ -181,7 +182,7 @@ public:
case 'e': {
bool success;
m_extended_backtrace =
Args::StringToBoolean(option_arg, false, &success);
Args::StringToBoolean(option_strref, false, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid boolean value for option '%c'", short_option);
@ -315,11 +316,13 @@ public:
ExecutionContext *execution_context) override {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'a': {
bool success;
bool avoid_no_debug = Args::StringToBoolean(option_arg, true, &success);
bool avoid_no_debug =
Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid boolean value for option '%c'", short_option);
@ -331,7 +334,8 @@ public:
case 'A': {
bool success;
bool avoid_no_debug = Args::StringToBoolean(option_arg, true, &success);
bool avoid_no_debug =
Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid boolean value for option '%c'", short_option);
@ -1441,11 +1445,12 @@ public:
ExecutionContext *execution_context) override {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'x': {
bool success;
bool tmp_value = Args::StringToBoolean(option_arg, false, &success);
bool tmp_value = Args::StringToBoolean(option_strref, false, &success);
if (success)
m_from_expression = tmp_value;
else {

View File

@ -298,7 +298,8 @@ private:
switch (short_option) {
case 'C':
m_cascade = Args::StringToBoolean(option_arg, true, &success);
m_cascade = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_arg), true, &success);
if (!success)
error.SetErrorStringWithFormat("invalid value for cascade: %s",
option_arg);
@ -530,7 +531,8 @@ private:
switch (short_option) {
case 'C':
m_cascade = Args::StringToBoolean(option_value, true, &success);
m_cascade = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_value), true, &success);
if (!success)
error.SetErrorStringWithFormat("invalid value for cascade: %s",
option_value);
@ -1246,7 +1248,8 @@ Error CommandObjectTypeSummaryAdd::CommandOptions::SetOptionValue(
switch (short_option) {
case 'C':
m_flags.SetCascades(Args::StringToBoolean(option_arg, true, &success));
m_flags.SetCascades(Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_arg), true, &success));
if (!success)
error.SetErrorStringWithFormat("invalid value for cascade: %s",
option_arg);
@ -2557,7 +2560,8 @@ private:
switch (short_option) {
case 'C':
m_cascade = Args::StringToBoolean(option_arg, true, &success);
m_cascade = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_arg), true, &success);
if (!success)
error.SetErrorStringWithFormat("invalid value for cascade: %s",
option_arg);

View File

@ -318,7 +318,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 = Args::StringToBoolean(
llvm::StringRef::withNullAsEmpty(option_arg), false, &success);
if (!success)
error.SetErrorStringWithFormat(
"invalid value for stop-on-error: \"%s\"", option_arg);

View File

@ -323,7 +323,7 @@ static bool GetProcessAndStatInfo(lldb::pid_t pid,
char *next_var = (char *)buf_sp->GetBytes();
char *end_buf = next_var + buf_sp->GetByteSize();
while (next_var < end_buf && 0 != *next_var) {
info_env.AppendArgument(next_var);
info_env.AppendArgument(llvm::StringRef(next_var));
next_var += strlen(next_var) + 1;
}
@ -340,7 +340,7 @@ static bool GetProcessAndStatInfo(lldb::pid_t pid,
char *next_arg = cmd + strlen(cmd) + 1;
end_buf = cmd + buf_sp->GetByteSize();
while (next_arg < end_buf && 0 != *next_arg) {
info_args.AppendArgument(next_arg);
info_args.AppendArgument(llvm::StringRef(next_arg));
next_arg += strlen(next_arg) + 1;
}
}

View File

@ -273,7 +273,7 @@ Error Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
if (!str_sp)
continue;
launch_info.GetArguments().AppendArgument(str_sp->GetValue().c_str());
launch_info.GetArguments().AppendArgument(str_sp->GetValue());
}
}

View File

@ -362,23 +362,23 @@ const char *Args::Unshift(const char *arg_cstr, char quote_char) {
void Args::AppendArguments(const Args &rhs) {
const size_t rhs_argc = rhs.GetArgumentCount();
for (size_t i = 0; i < rhs_argc; ++i)
AppendArgument(rhs.GetArgumentAtIndex(i),
AppendArgument(llvm::StringRef(rhs.GetArgumentAtIndex(i)),
rhs.GetArgumentQuoteCharAtIndex(i));
}
void Args::AppendArguments(const char **argv) {
if (argv) {
for (uint32_t i = 0; argv[i]; ++i)
AppendArgument(argv[i]);
AppendArgument(llvm::StringRef::withNullAsEmpty(argv[i]));
}
}
const char *Args::AppendArgument(const char *arg_cstr, char quote_char) {
return InsertArgumentAtIndex(GetArgumentCount(), arg_cstr, quote_char);
llvm::StringRef Args::AppendArgument(llvm::StringRef arg_str, char quote_char) {
return InsertArgumentAtIndex(GetArgumentCount(), arg_str, quote_char);
}
const char *Args::InsertArgumentAtIndex(size_t idx, const char *arg_cstr,
char quote_char) {
llvm::StringRef Args::InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
char quote_char) {
// Since we are using a std::list to hold onto the copied C string and
// we don't have direct access to the elements, we have to iterate to
// find the value.
@ -387,7 +387,7 @@ const char *Args::InsertArgumentAtIndex(size_t idx, const char *arg_cstr,
for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
--i;
pos = m_args.insert(pos, arg_cstr);
pos = m_args.insert(pos, arg_str);
if (idx >= m_args_quote_char.size()) {
m_args_quote_char.resize(idx + 1);
@ -399,8 +399,9 @@ const char *Args::InsertArgumentAtIndex(size_t idx, const char *arg_cstr,
return GetArgumentAtIndex(idx);
}
const char *Args::ReplaceArgumentAtIndex(size_t idx, const char *arg_cstr,
char quote_char) {
llvm::StringRef Args::ReplaceArgumentAtIndex(size_t idx,
llvm::StringRef arg_str,
char quote_char) {
// Since we are using a std::list to hold onto the copied C string and
// we don't have direct access to the elements, we have to iterate to
// find the value.
@ -410,7 +411,7 @@ const char *Args::ReplaceArgumentAtIndex(size_t idx, const char *arg_cstr,
--i;
if (pos != end) {
pos->assign(arg_cstr);
pos->assign(arg_str);
assert(idx < m_argv.size() - 1);
m_argv[idx] = pos->c_str();
if (idx >= m_args_quote_char.size())
@ -732,11 +733,6 @@ const char *Args::StripSpaces(std::string &s, bool leading, bool trailing,
return s.c_str();
}
bool Args::StringToBoolean(const char *s, bool fail_value,
bool *success_ptr) {
return StringToBoolean(llvm::StringRef(s ? s : ""), fail_value, success_ptr);
}
bool Args::StringToBoolean(llvm::StringRef ref, bool fail_value,
bool *success_ptr) {
if (success_ptr)
@ -915,13 +911,6 @@ Error Args::StringToFormat(const char *s, lldb::Format &format,
return error;
}
lldb::Encoding Args::StringToEncoding(const char *s,
lldb::Encoding fail_value) {
if (!s)
return fail_value;
return StringToEncoding(llvm::StringRef(s), fail_value);
}
lldb::Encoding Args::StringToEncoding(llvm::StringRef s,
lldb::Encoding fail_value) {
return llvm::StringSwitch<lldb::Encoding>(s)
@ -932,12 +921,6 @@ lldb::Encoding Args::StringToEncoding(llvm::StringRef s,
.Default(fail_value);
}
uint32_t Args::StringToGenericRegister(const char *s) {
if (!s)
return LLDB_INVALID_REGNUM;
return StringToGenericRegister(llvm::StringRef(s));
}
uint32_t Args::StringToGenericRegister(llvm::StringRef s) {
if (s.empty())
return LLDB_INVALID_REGNUM;
@ -1015,13 +998,13 @@ void Args::AddOrReplaceEnvironmentVariable(const char *env_var_name,
// Check if the name matches the given env_var_name.
if (strncmp(env_var_name, arg_value, equal_p - arg_value) == 0) {
ReplaceArgumentAtIndex(i, stream.GetString().c_str());
ReplaceArgumentAtIndex(i, stream.GetString());
return;
}
}
// We didn't find it. Append it instead.
AppendArgument(stream.GetString().c_str());
AppendArgument(stream.GetString());
}
bool Args::ContainsEnvironmentVariable(const char *env_var_name,
@ -1239,7 +1222,7 @@ void Args::ParseAliasOptions(Options &options, CommandReturnObject &result,
if (pos != std::string::npos)
raw_input_string.erase(pos, strlen(tmp_arg));
}
ReplaceArgumentAtIndex(idx, "");
ReplaceArgumentAtIndex(idx, llvm::StringRef());
if ((long_options[long_options_index].definition->option_has_arg !=
OptionParser::eNoArgument) &&
(OptionParser::GetOptionArgument() != nullptr) &&
@ -1252,7 +1235,7 @@ void Args::ParseAliasOptions(Options &options, CommandReturnObject &result,
if (pos != std::string::npos)
raw_input_string.erase(pos, strlen(tmp_arg));
}
ReplaceArgumentAtIndex(idx + 1, "");
ReplaceArgumentAtIndex(idx + 1, llvm::StringRef());
}
}
}

View File

@ -1760,7 +1760,7 @@ int CommandInterpreter::HandleCompletionMatches(
look_for_subcommand = true;
num_command_matches = 0;
matches.DeleteStringAtIndex(0);
parsed_line.AppendArgument("");
parsed_line.AppendArgument(llvm::StringRef());
cursor_index++;
cursor_char_position = 0;
}
@ -1842,7 +1842,8 @@ int CommandInterpreter::HandleCompletion(
partial_parsed_line.GetArgumentAtIndex(cursor_index);
if (cursor_char_position == 0 ||
current_elem[cursor_char_position - 1] != ' ') {
parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
parsed_line.InsertArgumentAtIndex(cursor_index + 1, llvm::StringRef(),
'\0');
cursor_index++;
cursor_char_position = 0;
}
@ -1987,21 +1988,23 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
// this above, make
// sure we don't
// insert it twice
new_args.AppendArgument(value.c_str());
new_args
.AppendArgument(
value);
} else {
if (value_type != OptionParser::eOptionalArgument)
new_args.AppendArgument(option.c_str());
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.c_str());
new_args.AppendArgument(value);
else {
char buffer[255];
::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(),
value.c_str());
new_args.AppendArgument(buffer);
new_args.AppendArgument(llvm::StringRef(buffer));
}
} else if (static_cast<size_t>(index) >=
@ -2023,12 +2026,13 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
}
if (value_type != OptionParser::eOptionalArgument)
new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index));
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(buffer);
new_args.AppendArgument(llvm::StringRef(buffer));
}
used[index] = true;
}
@ -2038,7 +2042,8 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j) {
if (!used[j] && !wants_raw_input)
new_args.AppendArgument(cmd_args.GetArgumentAtIndex(j));
new_args.AppendArgument(
llvm::StringRef(cmd_args.GetArgumentAtIndex(j)));
}
cmd_args.Clear();

View File

@ -300,10 +300,9 @@ int CommandObject::HandleCompletion(Args &input, int &cursor_index,
cursor_index++;
// I stick an element on the end of the input, because if the last element
// is
// option that requires an argument, getopt_long_only will freak out.
// is option that requires an argument, getopt_long_only will freak out.
input.AppendArgument("<FAKE-VALUE>");
input.AppendArgument(llvm::StringRef("<FAKE-VALUE>"));
input.ParseArgsForCompletion(*cur_options, opt_element_vector,
cursor_index);
@ -1001,7 +1000,8 @@ bool CommandObjectParsed::Execute(const char *args_string,
const char *tmp_str = cmd_args.GetArgumentAtIndex(i);
if (tmp_str[0] == '`') // back-quote
cmd_args.ReplaceArgumentAtIndex(
i, m_interpreter.ProcessEmbeddedScriptCommands(tmp_str));
i, llvm::StringRef::withNullAsEmpty(
m_interpreter.ProcessEmbeddedScriptCommands(tmp_str)));
}
if (CheckRequirements(result)) {

View File

@ -85,6 +85,8 @@ Error OptionGroupValueObjectDisplay::SetOptionValue(
const int short_option = g_option_table[option_idx].short_option;
bool success = false;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'd': {
int32_t result;
@ -141,13 +143,13 @@ Error OptionGroupValueObjectDisplay::SetOptionValue(
break;
case 'S':
use_synth = Args::StringToBoolean(option_arg, true, &success);
use_synth = Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat("invalid synthetic-type '%s'", option_arg);
break;
case 'V':
run_validator = Args::StringToBoolean(option_arg, true, &success);
run_validator = Args::StringToBoolean(option_strref, true, &success);
if (!success)
error.SetErrorStringWithFormat("invalid validate '%s'", option_arg);
break;

View File

@ -1,5 +1,4 @@
//===-- OptionValueBoolean.cpp ------------------------------------*- C++
//-*-===//
//===-- OptionValueBoolean.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@ -48,8 +47,7 @@ Error OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
case eVarSetOperationReplace:
case eVarSetOperationAssign: {
bool success = false;
bool value =
Args::StringToBoolean(value_str.str().c_str(), false, &success);
bool value = Args::StringToBoolean(value_str, false, &success);
if (success) {
m_value_was_set = true;
m_current_value = value;

View File

@ -85,7 +85,7 @@ size_t OptionValueDictionary::GetArgs(Args &args) const {
StreamString strm;
strm.Printf("%s=", pos->first.GetCString());
pos->second->DumpValue(nullptr, strm, eDumpOptionValue | eDumpOptionRaw);
args.AppendArgument(strm.GetString().c_str());
args.AppendArgument(strm.GetString());
}
return args.GetArgumentCount();
}

View File

@ -54,7 +54,7 @@ Property::Property(const PropertyDefinition &definition)
// default value.
if (definition.default_cstr_value)
m_value_sp.reset(new OptionValueBoolean(Args::StringToBoolean(
definition.default_cstr_value, false, nullptr)));
llvm::StringRef(definition.default_cstr_value), false, nullptr)));
else
m_value_sp.reset(
new OptionValueBoolean(definition.default_uint_value != 0));

View File

@ -137,7 +137,7 @@ Error PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
if (error.Fail())
return error;
args.ReplaceArgumentAtIndex(0, connect_url.c_str());
args.ReplaceArgumentAtIndex(0, connect_url);
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log)

View File

@ -1886,7 +1886,7 @@ PlatformDarwin::LaunchProcess(lldb_private::ProcessLaunchInfo &launch_info) {
// we get os_log and NSLog messages mirrored to the target process
// stderr.
if (!env_vars.ContainsEnvironmentVariable("OS_ACTIVITY_DT_MODE"))
env_vars.AppendArgument("OS_ACTIVITY_DT_MODE=enable");
env_vars.AppendArgument(llvm::StringRef("OS_ACTIVITY_DT_MODE=enable"));
}
// Let our parent class do the real launching.

View File

@ -305,8 +305,7 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
std::string encoding_str;
if (reg_info_dict->GetValueForKeyAsString("encoding", encoding_str))
reg_info.encoding =
Args::StringToEncoding(encoding_str.c_str(), eEncodingUint);
reg_info.encoding = Args::StringToEncoding(encoding_str, eEncodingUint);
else
reg_info_dict->GetValueForKeyAsInteger("encoding", reg_info.encoding,
eEncodingUint);
@ -337,7 +336,7 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
std::string generic_str;
if (reg_info_dict->GetValueForKeyAsString("generic", generic_str))
reg_info.kinds[lldb::eRegisterKindGeneric] =
Args::StringToGenericRegister(generic_str.c_str());
Args::StringToGenericRegister(generic_str);
else
reg_info_dict->GetValueForKeyAsInteger(
"generic", reg_info.kinds[lldb::eRegisterKindGeneric],

View File

@ -1067,31 +1067,31 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
char arg_cstr[PATH_MAX];
// Start args with "debugserver /file/path -r --"
debugserver_args.AppendArgument(debugserver_path);
debugserver_args.AppendArgument(llvm::StringRef(debugserver_path));
#if !defined(__APPLE__)
// First argument to lldb-server must be mode in which to run.
debugserver_args.AppendArgument("gdbserver");
debugserver_args.AppendArgument(llvm::StringRef("gdbserver"));
#endif
// If a url is supplied then use it
if (url)
debugserver_args.AppendArgument(url);
debugserver_args.AppendArgument(llvm::StringRef(url));
if (pass_comm_fd >= 0) {
StreamString fd_arg;
fd_arg.Printf("--fd=%i", pass_comm_fd);
debugserver_args.AppendArgument(fd_arg.GetData());
debugserver_args.AppendArgument(fd_arg.GetString());
// Send "pass_comm_fd" down to the inferior so it can use it to
// communicate back with this process
launch_info.AppendDuplicateFileAction(pass_comm_fd, pass_comm_fd);
}
// use native registers, not the GDB registers
debugserver_args.AppendArgument("--native-regs");
debugserver_args.AppendArgument(llvm::StringRef("--native-regs"));
if (launch_info.GetLaunchInSeparateProcessGroup()) {
debugserver_args.AppendArgument("--setsid");
debugserver_args.AppendArgument(llvm::StringRef("--setsid"));
}
llvm::SmallString<PATH_MAX> named_pipe_path;
@ -1137,8 +1137,8 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
return error;
}
int write_fd = socket_pipe.GetWriteFileDescriptor();
debugserver_args.AppendArgument("--pipe");
debugserver_args.AppendArgument(llvm::to_string(write_fd).c_str());
debugserver_args.AppendArgument(llvm::StringRef("--pipe"));
debugserver_args.AppendArgument(llvm::to_string(write_fd));
launch_info.AppendCloseFileAction(socket_pipe.GetReadFileDescriptor());
#endif
} else {
@ -1164,8 +1164,8 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
// Send the host and port down that debugserver and specify an option
// so that it connects back to the port we are listening to in this
// process
debugserver_args.AppendArgument("--reverse-connect");
debugserver_args.AppendArgument(port_cstr);
debugserver_args.AppendArgument(llvm::StringRef("--reverse-connect"));
debugserver_args.AppendArgument(llvm::StringRef(port_cstr));
if (port)
*port = port_;
} else {
@ -1182,7 +1182,7 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
if (env_debugserver_log_file) {
::snprintf(arg_cstr, sizeof(arg_cstr), "--log-file=%s",
env_debugserver_log_file);
debugserver_args.AppendArgument(arg_cstr);
debugserver_args.AppendArgument(llvm::StringRef(arg_cstr));
}
#if defined(__APPLE__)
@ -1199,7 +1199,7 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
if (env_debugserver_log_channels) {
::snprintf(arg_cstr, sizeof(arg_cstr), "--log-channels=%s",
env_debugserver_log_channels);
debugserver_args.AppendArgument(arg_cstr);
debugserver_args.AppendArgument(llvm::StringRef(arg_cstr));
}
#endif
@ -1215,7 +1215,7 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
has_env_var = extra_arg != nullptr;
if (has_env_var) {
debugserver_args.AppendArgument(extra_arg);
debugserver_args.AppendArgument(llvm::StringRef(extra_arg));
if (log)
log->Printf("GDBRemoteCommunication::%s adding env var %s contents "
"to stub command line (%s)",
@ -1224,7 +1224,7 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
} while (has_env_var);
if (inferior_args && inferior_args->GetArgumentCount() > 0) {
debugserver_args.AppendArgument("--");
debugserver_args.AppendArgument(llvm::StringRef("--"));
debugserver_args.AppendArguments(*inferior_args);
}
@ -1232,7 +1232,7 @@ Error GDBRemoteCommunication::StartDebugserverProcess(
StringList env;
if (Host::GetEnvironment(env)) {
for (size_t i = 0; i < env.GetSize(); ++i)
launch_info.GetEnvironmentEntries().AppendArgument(env[i].c_str());
launch_info.GetEnvironmentEntries().AppendArgument(env[i]);
}
// Close STDIN, STDOUT and STDERR.

View File

@ -946,7 +946,8 @@ GDBRemoteCommunicationServerCommon::Handle_QEnvironment(
packet.SetFilePos(::strlen("QEnvironment:"));
const uint32_t bytes_left = packet.GetBytesLeft();
if (bytes_left > 0) {
m_process_launch_info.GetEnvironmentEntries().AppendArgument(packet.Peek());
m_process_launch_info.GetEnvironmentEntries().AppendArgument(
llvm::StringRef::withNullAsEmpty(packet.Peek()));
return SendOKResponse();
}
return SendErrorResponse(12);
@ -960,7 +961,7 @@ GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded(
if (bytes_left > 0) {
std::string str;
packet.GetHexByteString(str);
m_process_launch_info.GetEnvironmentEntries().AppendArgument(str.c_str());
m_process_launch_info.GetEnvironmentEntries().AppendArgument(str);
return SendOKResponse();
}
return SendErrorResponse(12);
@ -1032,8 +1033,7 @@ GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) {
if (arg_idx == 0)
m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(),
false);
m_process_launch_info.GetArguments().AppendArgument(
arg.c_str());
m_process_launch_info.GetArguments().AppendArgument(arg);
if (log)
log->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"",
__FUNCTION__, actual_arg_index, arg.c_str());

View File

@ -4158,8 +4158,7 @@ bool ParseRegisters(XMLNode feature_node, GdbServerTargetInfo &target_info,
alt_name.SetString(value);
} else if (name == "encoding") {
encoding_set = true;
reg_info.encoding =
Args::StringToEncoding(value.data(), eEncodingUint);
reg_info.encoding = Args::StringToEncoding(value, eEncodingUint);
} else if (name == "format") {
format_set = true;
Format format = eFormatInvalid;
@ -4198,7 +4197,7 @@ bool ParseRegisters(XMLNode feature_node, GdbServerTargetInfo &target_info,
StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0);
} else if (name == "generic") {
reg_info.kinds[eRegisterKindGeneric] =
Args::StringToGenericRegister(value.data());
Args::StringToGenericRegister(value);
} else if (name == "value_regnums") {
SplitCommaSeparatedRegisterNumberString(value, value_regs, 0);
} else if (name == "invalidate_regnums") {

View File

@ -429,6 +429,7 @@ public:
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 'a':
m_include_any_process = true;
@ -442,7 +443,7 @@ public:
break;
case 'b':
m_broadcast_events = Args::StringToBoolean(option_arg, true, nullptr);
m_broadcast_events = Args::StringToBoolean(option_strref, true, nullptr);
break;
case 'c':
@ -458,7 +459,7 @@ public:
break;
case 'e':
m_echo_to_stderr = Args::StringToBoolean(option_arg, false, nullptr);
m_echo_to_stderr = Args::StringToBoolean(option_strref, false, nullptr);
break;
case 'f':
@ -469,12 +470,12 @@ public:
break;
case 'l':
m_live_stream = Args::StringToBoolean(option_arg, false, nullptr);
m_live_stream = Args::StringToBoolean(option_strref, false, nullptr);
break;
case 'n':
m_filter_fall_through_accepts =
Args::StringToBoolean(option_arg, true, nullptr);
Args::StringToBoolean(option_strref, true, nullptr);
break;
case 'r':

View File

@ -416,6 +416,7 @@ Error ProcessLaunchCommandOptions::SetOptionValue(
ExecutionContext *execution_context) {
Error error;
const int short_option = m_getopt_table[option_idx].val;
auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg);
switch (short_option) {
case 's': // Stop at program entry point
@ -484,7 +485,7 @@ Error ProcessLaunchCommandOptions::SetOptionValue(
{
bool success;
const bool disable_aslr_arg =
Args::StringToBoolean(option_arg, true, &success);
Args::StringToBoolean(option_strref, true, &success);
if (success)
disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
else
@ -497,7 +498,8 @@ Error ProcessLaunchCommandOptions::SetOptionValue(
case 'X': // shell expand args.
{
bool success;
const bool expand_args = Args::StringToBoolean(option_arg, true, &success);
const bool expand_args =
Args::StringToBoolean(option_strref, true, &success);
if (success)
launch_info.SetShellExpandArguments(expand_args);
else
@ -515,7 +517,8 @@ Error ProcessLaunchCommandOptions::SetOptionValue(
break;
case 'v':
launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
launch_info.GetEnvironmentEntries().AppendArgument(
llvm::StringRef::withNullAsEmpty(option_arg));
break;
default:

View File

@ -18,6 +18,8 @@
#include "lldb/Core/Stream.h"
#include "lldb/Host/PosixApi.h"
#include "llvm/ADT/SmallString.h"
using namespace lldb;
using namespace lldb_private;
@ -66,8 +68,9 @@ void ProcessInfo::SetExecutableFile(const FileSpec &exe_file,
if (exe_file) {
m_executable = exe_file;
if (add_exe_file_as_first_arg) {
char filename[PATH_MAX];
if (exe_file.GetPath(filename, sizeof(filename)))
llvm::SmallString<PATH_MAX> filename;
exe_file.GetPath(filename);
if (!filename.empty())
m_arguments.InsertArgumentAtIndex(0, filename);
}
} else {

View File

@ -344,13 +344,13 @@ bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell(
return false;
Args shell_arguments;
std::string safe_arg;
shell_arguments.AppendArgument(shell_executable.c_str());
shell_arguments.AppendArgument(shell_executable);
const llvm::Triple &triple = GetArchitecture().GetTriple();
if (triple.getOS() == llvm::Triple::Win32 &&
!triple.isWindowsCygwinEnvironment())
shell_arguments.AppendArgument("/C");
shell_arguments.AppendArgument(llvm::StringRef("/C"));
else
shell_arguments.AppendArgument("-c");
shell_arguments.AppendArgument(llvm::StringRef("-c"));
StreamString shell_command;
if (will_debug) {
@ -428,7 +428,7 @@ bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell(
shell_command.Printf(" %s", arg);
}
}
shell_arguments.AppendArgument(shell_command.GetString().c_str());
shell_arguments.AppendArgument(shell_command.GetString());
m_executable = m_shell;
m_arguments = shell_arguments;
return true;

View File

@ -57,7 +57,7 @@ TEST(ArgsTest, TestAppendArg) {
Args args;
args.SetCommandString("first_arg");
EXPECT_EQ(1u, args.GetArgumentCount());
args.AppendArgument("second_arg");
args.AppendArgument(llvm::StringRef("second_arg"));
EXPECT_EQ(2u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "first_arg");
EXPECT_STREQ(args.GetArgumentAtIndex(1), "second_arg");