Convert 3 more functions to use a StringRef.
This converts Args::Unshift, Args::AddOrReplaceEnvironmentVariable, and Args::ContainsEnvironmentVariable to use StringRefs. The code is also simplified somewhat as a result. llvm-svn: 281942
This commit is contained in:
parent
bf9a7c43b0
commit
5c725f3a06
|
@ -187,32 +187,30 @@ public:
|
||||||
void AppendArguments(const char **argv);
|
void AppendArguments(const char **argv);
|
||||||
|
|
||||||
// Delete const char* versions of StringRef functions. Normally this would
|
// Delete const char* versions of StringRef functions. Normally this would
|
||||||
// not
|
// not be necessary, as const char * is implicitly convertible to StringRef.
|
||||||
// be necessary, as const char * is implicitly convertible to StringRef.
|
// However, since the use of const char* is so pervasive, and since StringRef
|
||||||
// However,
|
// will assert if you try to construct one from nullptr, this allows the
|
||||||
// since the use of const char* is so pervasive, and since StringRef will
|
// compiler to catch instances of the function being invoked with a
|
||||||
// assert
|
// const char *, allowing us to replace them with explicit conversions at each
|
||||||
// if you try to construct one from nullptr, this allows the compiler to catch
|
// call-site. This ensures that no callsites slip through the cracks where
|
||||||
// instances of the function being invoked with a const char *, allowing us to
|
// we would be trying to implicitly convert from nullptr, since it will force
|
||||||
// replace them with explicit conversions at each call-site. Once StringRef
|
// us to evaluate and explicitly convert each one.
|
||||||
// use becomes more pervasive, there will be fewer implicit conversions
|
//
|
||||||
// because
|
// Once StringRef use becomes more pervasive, there will be fewer
|
||||||
// we will be using StringRefs across the whole pipeline, so we won't have to
|
// implicit conversions because we will be using StringRefs across the whole
|
||||||
// have
|
// pipeline, so we won't have to have this "glue" that converts between the
|
||||||
// this "glue" that converts between the two, and at that point it becomes
|
// two, and at that point it becomes easy to just make sure you don't pass
|
||||||
// easy
|
// nullptr into the function on the odd occasion that you do pass a
|
||||||
// to just make sure you don't pass nullptr into the function.
|
// const char *.
|
||||||
// Call-site fixing methodology:
|
// Call-site fixing methodology:
|
||||||
// 1. If you know the string cannot be null (e.g. it's a const char[], or
|
// 1. If you know the string cannot be null (e.g. it's a const char[], or
|
||||||
// it's
|
// it's been checked for null), use llvm::StringRef(ptr).
|
||||||
// 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
|
// 2. If you don't know if it can be null (e.g. it's returned from a
|
||||||
// function
|
// function whose semantics are unclear), use
|
||||||
// whose semantics are unclear), use
|
|
||||||
// llvm::StringRef::withNullAsEmpty(ptr).
|
// llvm::StringRef::withNullAsEmpty(ptr).
|
||||||
// 3. If it's .c_str() of a std::string, just pass the std::string directly.
|
// 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
|
// 4. If it's .str().c_str() of a StringRef, just pass the StringRef
|
||||||
// directly.
|
// directly.
|
||||||
void ReplaceArgumentAtIndex(size_t, const char *, char = '\0') = delete;
|
void ReplaceArgumentAtIndex(size_t, const char *, char = '\0') = delete;
|
||||||
void AppendArgument(const char *arg_str, char quote_char = '\0') = delete;
|
void AppendArgument(const char *arg_str, char quote_char = '\0') = delete;
|
||||||
void InsertArgumentAtIndex(size_t, const char *, char = '\0') = delete;
|
void InsertArgumentAtIndex(size_t, const char *, char = '\0') = delete;
|
||||||
|
@ -225,6 +223,10 @@ public:
|
||||||
static uint32_t StringToGenericRegister(const char *) = delete;
|
static uint32_t StringToGenericRegister(const char *) = delete;
|
||||||
static bool StringToVersion(const char *, uint32_t &, uint32_t &,
|
static bool StringToVersion(const char *, uint32_t &, uint32_t &,
|
||||||
uint32_t &) = delete;
|
uint32_t &) = delete;
|
||||||
|
const char *Unshift(const char *, char = '\0') = delete;
|
||||||
|
void AddOrReplaceEnvironmentVariable(const char *, const char *) = delete;
|
||||||
|
bool ContainsEnvironmentVariable(const char *,
|
||||||
|
size_t * = nullptr) const = delete;
|
||||||
|
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
/// Insert the argument value at index \a idx to \a arg_cstr.
|
/// Insert the argument value at index \a idx to \a arg_cstr.
|
||||||
|
@ -315,7 +317,7 @@ public:
|
||||||
/// @return
|
/// @return
|
||||||
/// A pointer to the copy of \a arg_cstr that was made.
|
/// A pointer to the copy of \a arg_cstr that was made.
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
const char *Unshift(const char *arg_cstr, char quote_char = '\0');
|
llvm::StringRef Unshift(llvm::StringRef arg_str, char quote_char = '\0');
|
||||||
|
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
/// Parse the arguments in the contained arguments.
|
/// Parse the arguments in the contained arguments.
|
||||||
|
@ -465,8 +467,8 @@ public:
|
||||||
/// already in the list, it replaces the first such occurrence
|
/// already in the list, it replaces the first such occurrence
|
||||||
/// with the new value.
|
/// with the new value.
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
void AddOrReplaceEnvironmentVariable(const char *env_var_name,
|
void AddOrReplaceEnvironmentVariable(llvm::StringRef env_var_name,
|
||||||
const char *new_value);
|
llvm::StringRef new_value);
|
||||||
|
|
||||||
/// Return whether a given environment variable exists.
|
/// Return whether a given environment variable exists.
|
||||||
///
|
///
|
||||||
|
@ -486,7 +488,7 @@ public:
|
||||||
/// true if the specified env var name exists in the list in
|
/// true if the specified env var name exists in the list in
|
||||||
/// either of the above-mentioned formats; otherwise, false.
|
/// either of the above-mentioned formats; otherwise, false.
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
bool ContainsEnvironmentVariable(const char *env_var_name,
|
bool ContainsEnvironmentVariable(llvm::StringRef env_var_name,
|
||||||
size_t *argument_index = nullptr) const;
|
size_t *argument_index = nullptr) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -352,11 +352,11 @@ void Args::Shift() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *Args::Unshift(const char *arg_cstr, char quote_char) {
|
llvm::StringRef Args::Unshift(llvm::StringRef arg_str, char quote_char) {
|
||||||
m_args.push_front(arg_cstr);
|
m_args.push_front(arg_str);
|
||||||
m_argv.insert(m_argv.begin(), m_args.front().c_str());
|
m_argv.insert(m_argv.begin(), m_args.front().c_str());
|
||||||
m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
|
m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
|
||||||
return GetArgumentAtIndex(0);
|
return llvm::StringRef::withNullAsEmpty(GetArgumentAtIndex(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Args::AppendArguments(const Args &rhs) {
|
void Args::AppendArguments(const Args &rhs) {
|
||||||
|
@ -972,72 +972,42 @@ void Args::LongestCommonPrefix(std::string &common_prefix) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Args::AddOrReplaceEnvironmentVariable(const char *env_var_name,
|
void Args::AddOrReplaceEnvironmentVariable(llvm::StringRef env_var_name,
|
||||||
const char *new_value) {
|
llvm::StringRef new_value) {
|
||||||
if (!env_var_name || !new_value)
|
if (env_var_name.empty() || new_value.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Build the new entry.
|
// Build the new entry.
|
||||||
StreamString stream;
|
std::string var_string(env_var_name);
|
||||||
stream << env_var_name;
|
var_string += "=";
|
||||||
stream << '=';
|
var_string += new_value;
|
||||||
stream << new_value;
|
|
||||||
stream.Flush();
|
|
||||||
|
|
||||||
// Find the environment variable if present and replace it.
|
size_t index = 0;
|
||||||
for (size_t i = 0; i < GetArgumentCount(); ++i) {
|
if (ContainsEnvironmentVariable(env_var_name, &index)) {
|
||||||
// Get the env var value.
|
ReplaceArgumentAtIndex(index, var_string);
|
||||||
const char *arg_value = GetArgumentAtIndex(i);
|
return;
|
||||||
if (!arg_value)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Find the name of the env var: before the first =.
|
|
||||||
auto equal_p = strchr(arg_value, '=');
|
|
||||||
if (!equal_p)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We didn't find it. Append it instead.
|
// We didn't find it. Append it instead.
|
||||||
AppendArgument(stream.GetString());
|
AppendArgument(var_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Args::ContainsEnvironmentVariable(const char *env_var_name,
|
bool Args::ContainsEnvironmentVariable(llvm::StringRef env_var_name,
|
||||||
size_t *argument_index) const {
|
size_t *argument_index) const {
|
||||||
// Validate args.
|
// Validate args.
|
||||||
if (!env_var_name)
|
if (env_var_name.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Check each arg to see if it matches the env var name.
|
// Check each arg to see if it matches the env var name.
|
||||||
for (size_t i = 0; i < GetArgumentCount(); ++i) {
|
for (size_t i = 0; i < GetArgumentCount(); ++i) {
|
||||||
// Get the arg value.
|
auto arg_value = llvm::StringRef::withNullAsEmpty(GetArgumentAtIndex(0));
|
||||||
const char *argument_value = GetArgumentAtIndex(i);
|
|
||||||
if (!argument_value)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Check if we are the "{env_var_name}={env_var_value}" style.
|
llvm::StringRef name, value;
|
||||||
const char *equal_p = strchr(argument_value, '=');
|
std::tie(name, value) = arg_value.split('=');
|
||||||
if (equal_p) {
|
if (name == env_var_name && !value.empty()) {
|
||||||
if (strncmp(env_var_name, argument_value, equal_p - argument_value) ==
|
if (argument_index)
|
||||||
0) {
|
*argument_index = i;
|
||||||
// We matched.
|
return true;
|
||||||
if (argument_index)
|
|
||||||
*argument_index = i;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// We're a simple {env_var_name}-style entry.
|
|
||||||
if (strcmp(argument_value, env_var_name) == 0) {
|
|
||||||
// We matched.
|
|
||||||
if (argument_index)
|
|
||||||
*argument_index = i;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ static bool ProcessAliasOptionsArgs(lldb::CommandObjectSP &cmd_obj_sp,
|
||||||
ExecutionContext exe_ctx =
|
ExecutionContext exe_ctx =
|
||||||
cmd_obj_sp->GetCommandInterpreter().GetExecutionContext();
|
cmd_obj_sp->GetCommandInterpreter().GetExecutionContext();
|
||||||
options->NotifyOptionParsingStarting(&exe_ctx);
|
options->NotifyOptionParsingStarting(&exe_ctx);
|
||||||
args.Unshift("dummy_arg");
|
args.Unshift(llvm::StringRef("dummy_arg"));
|
||||||
args.ParseAliasOptions(*options, result, option_arg_vector, options_string);
|
args.ParseAliasOptions(*options, result, option_arg_vector, options_string);
|
||||||
args.Shift();
|
args.Shift();
|
||||||
if (result.Succeeded())
|
if (result.Succeeded())
|
||||||
|
|
|
@ -1333,7 +1333,7 @@ CommandObject *CommandInterpreter::BuildAliasResult(
|
||||||
std::string alias_name_str = alias_name;
|
std::string alias_name_str = alias_name;
|
||||||
if ((cmd_args.GetArgumentCount() == 0) ||
|
if ((cmd_args.GetArgumentCount() == 0) ||
|
||||||
(alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0))
|
(alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0))
|
||||||
cmd_args.Unshift(alias_name);
|
cmd_args.Unshift(alias_name_str);
|
||||||
|
|
||||||
result_str.Printf("%s", alias_cmd_obj->GetCommandName());
|
result_str.Printf("%s", alias_cmd_obj->GetCommandName());
|
||||||
|
|
||||||
|
@ -1953,7 +1953,7 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
|
||||||
// Make sure that the alias name is the 0th element in cmd_args
|
// Make sure that the alias name is the 0th element in cmd_args
|
||||||
std::string alias_name_str = alias_name;
|
std::string alias_name_str = alias_name;
|
||||||
if (alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0)
|
if (alias_name_str.compare(cmd_args.GetArgumentAtIndex(0)) != 0)
|
||||||
cmd_args.Unshift(alias_name);
|
cmd_args.Unshift(alias_name_str);
|
||||||
|
|
||||||
Args new_args(alias_cmd_obj->GetCommandName());
|
Args new_args(alias_cmd_obj->GetCommandName());
|
||||||
if (new_args.GetArgumentCount() == 2)
|
if (new_args.GetArgumentCount() == 2)
|
||||||
|
|
|
@ -116,7 +116,7 @@ bool CommandObject::ParseOptions(Args &args, CommandReturnObject &result) {
|
||||||
// ParseOptions calls getopt_long_only, which always skips the zero'th item
|
// ParseOptions calls getopt_long_only, which always skips the zero'th item
|
||||||
// in the array and starts at position 1,
|
// in the array and starts at position 1,
|
||||||
// so we need to push a dummy value into position zero.
|
// so we need to push a dummy value into position zero.
|
||||||
args.Unshift("dummy_string");
|
args.Unshift(llvm::StringRef("dummy_string"));
|
||||||
const bool require_validation = true;
|
const bool require_validation = true;
|
||||||
error = args.ParseOptions(*options, &exe_ctx,
|
error = args.ParseOptions(*options, &exe_ctx,
|
||||||
GetCommandInterpreter().GetPlatform(true),
|
GetCommandInterpreter().GetPlatform(true),
|
||||||
|
@ -296,7 +296,7 @@ int CommandObject::HandleCompletion(Args &input, int &cursor_index,
|
||||||
if (cur_options != nullptr) {
|
if (cur_options != nullptr) {
|
||||||
// Re-insert the dummy command name string which will have been
|
// Re-insert the dummy command name string which will have been
|
||||||
// stripped off:
|
// stripped off:
|
||||||
input.Unshift("dummy-string");
|
input.Unshift(llvm::StringRef("dummy-string"));
|
||||||
cursor_index++;
|
cursor_index++;
|
||||||
|
|
||||||
// I stick an element on the end of the input, because if the last element
|
// I stick an element on the end of the input, because if the last element
|
||||||
|
|
|
@ -1881,11 +1881,12 @@ PlatformDarwin::LaunchProcess(lldb_private::ProcessLaunchInfo &launch_info) {
|
||||||
// specifically want it unset.
|
// specifically want it unset.
|
||||||
const char *disable_env_var = "IDE_DISABLED_OS_ACTIVITY_DT_MODE";
|
const char *disable_env_var = "IDE_DISABLED_OS_ACTIVITY_DT_MODE";
|
||||||
auto &env_vars = launch_info.GetEnvironmentEntries();
|
auto &env_vars = launch_info.GetEnvironmentEntries();
|
||||||
if (!env_vars.ContainsEnvironmentVariable(disable_env_var)) {
|
if (!env_vars.ContainsEnvironmentVariable(llvm::StringRef(disable_env_var))) {
|
||||||
// We want to make sure that OS_ACTIVITY_DT_MODE is set so that
|
// We want to make sure that OS_ACTIVITY_DT_MODE is set so that
|
||||||
// we get os_log and NSLog messages mirrored to the target process
|
// we get os_log and NSLog messages mirrored to the target process
|
||||||
// stderr.
|
// stderr.
|
||||||
if (!env_vars.ContainsEnvironmentVariable("OS_ACTIVITY_DT_MODE"))
|
if (!env_vars.ContainsEnvironmentVariable(
|
||||||
|
llvm::StringRef("OS_ACTIVITY_DT_MODE")))
|
||||||
env_vars.AppendArgument(llvm::StringRef("OS_ACTIVITY_DT_MODE=enable"));
|
env_vars.AppendArgument(llvm::StringRef("OS_ACTIVITY_DT_MODE=enable"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1091,7 +1091,7 @@ EnableOptionsSP ParseAutoEnableOptions(Error &error, Debugger &debugger) {
|
||||||
// ParseOptions calls getopt_long_only, which always skips the zero'th item in
|
// ParseOptions calls getopt_long_only, which always skips the zero'th item in
|
||||||
// the array and starts at position 1,
|
// the array and starts at position 1,
|
||||||
// so we need to push a dummy value into position zero.
|
// so we need to push a dummy value into position zero.
|
||||||
args.Unshift("dummy_string");
|
args.Unshift(llvm::StringRef("dummy_string"));
|
||||||
bool require_validation = false;
|
bool require_validation = false;
|
||||||
error = args.ParseOptions(*options_sp.get(), &exe_ctx, PlatformSP(),
|
error = args.ParseOptions(*options_sp.get(), &exe_ctx, PlatformSP(),
|
||||||
require_validation);
|
require_validation);
|
||||||
|
@ -1547,14 +1547,15 @@ Error StructuredDataDarwinLog::FilterLaunchInfo(ProcessLaunchInfo &launch_info,
|
||||||
// Here we need to strip out any OS_ACTIVITY_DT_MODE setting to prevent
|
// Here we need to strip out any OS_ACTIVITY_DT_MODE setting to prevent
|
||||||
// echoing of os_log()/NSLog() to stderr in the target program.
|
// echoing of os_log()/NSLog() to stderr in the target program.
|
||||||
size_t argument_index;
|
size_t argument_index;
|
||||||
if (env_vars.ContainsEnvironmentVariable("OS_ACTIVITY_DT_MODE",
|
if (env_vars.ContainsEnvironmentVariable(
|
||||||
&argument_index))
|
llvm::StringRef("OS_ACTIVITY_DT_MODE"), &argument_index))
|
||||||
env_vars.DeleteArgumentAtIndex(argument_index);
|
env_vars.DeleteArgumentAtIndex(argument_index);
|
||||||
|
|
||||||
// We will also set the env var that tells any downstream launcher
|
// We will also set the env var that tells any downstream launcher
|
||||||
// from adding OS_ACTIVITY_DT_MODE.
|
// from adding OS_ACTIVITY_DT_MODE.
|
||||||
env_vars.AddOrReplaceEnvironmentVariable("IDE_DISABLED_OS_ACTIVITY_DT_MODE",
|
env_vars.AddOrReplaceEnvironmentVariable(
|
||||||
"1");
|
llvm::StringRef("IDE_DISABLED_OS_ACTIVITY_DT_MODE"),
|
||||||
|
llvm::StringRef("1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the OS_ACTIVITY_MODE env var appropriately to enable/disable
|
// Set the OS_ACTIVITY_MODE env var appropriately to enable/disable
|
||||||
|
@ -1568,9 +1569,8 @@ Error StructuredDataDarwinLog::FilterLaunchInfo(ProcessLaunchInfo &launch_info,
|
||||||
env_var_value = "";
|
env_var_value = "";
|
||||||
|
|
||||||
if (env_var_value) {
|
if (env_var_value) {
|
||||||
const char *env_var_name = "OS_ACTIVITY_MODE";
|
|
||||||
launch_info.GetEnvironmentEntries().AddOrReplaceEnvironmentVariable(
|
launch_info.GetEnvironmentEntries().AddOrReplaceEnvironmentVariable(
|
||||||
env_var_name, env_var_value);
|
llvm::StringRef("OS_ACTIVITY_MODE"), llvm::StringRef(env_var_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return error;
|
return error;
|
||||||
|
|
Loading…
Reference in New Issue