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);
|
||||
|
||||
// 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.
|
||||
// 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. This ensures that no callsites slip through the cracks where
|
||||
// we would be trying to implicitly convert from nullptr, since it will force
|
||||
// us to evaluate and explicitly convert each one.
|
||||
//
|
||||
// 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 on the odd occasion that you do pass a
|
||||
// const char *.
|
||||
// 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).
|
||||
// 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
|
||||
// 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.
|
||||
// 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;
|
||||
|
@ -225,6 +223,10 @@ public:
|
|||
static uint32_t StringToGenericRegister(const char *) = delete;
|
||||
static bool StringToVersion(const char *, uint32_t &, uint32_t &,
|
||||
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.
|
||||
|
@ -315,7 +317,7 @@ public:
|
|||
/// @return
|
||||
/// 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.
|
||||
|
@ -465,8 +467,8 @@ public:
|
|||
/// already in the list, it replaces the first such occurrence
|
||||
/// with the new value.
|
||||
//------------------------------------------------------------------
|
||||
void AddOrReplaceEnvironmentVariable(const char *env_var_name,
|
||||
const char *new_value);
|
||||
void AddOrReplaceEnvironmentVariable(llvm::StringRef env_var_name,
|
||||
llvm::StringRef new_value);
|
||||
|
||||
/// Return whether a given environment variable exists.
|
||||
///
|
||||
|
@ -486,7 +488,7 @@ public:
|
|||
/// true if the specified env var name exists in the list in
|
||||
/// 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;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -352,11 +352,11 @@ void Args::Shift() {
|
|||
}
|
||||
}
|
||||
|
||||
const char *Args::Unshift(const char *arg_cstr, char quote_char) {
|
||||
m_args.push_front(arg_cstr);
|
||||
llvm::StringRef Args::Unshift(llvm::StringRef arg_str, char quote_char) {
|
||||
m_args.push_front(arg_str);
|
||||
m_argv.insert(m_argv.begin(), m_args.front().c_str());
|
||||
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) {
|
||||
|
@ -972,72 +972,42 @@ void Args::LongestCommonPrefix(std::string &common_prefix) {
|
|||
}
|
||||
}
|
||||
|
||||
void Args::AddOrReplaceEnvironmentVariable(const char *env_var_name,
|
||||
const char *new_value) {
|
||||
if (!env_var_name || !new_value)
|
||||
void Args::AddOrReplaceEnvironmentVariable(llvm::StringRef env_var_name,
|
||||
llvm::StringRef new_value) {
|
||||
if (env_var_name.empty() || new_value.empty())
|
||||
return;
|
||||
|
||||
// Build the new entry.
|
||||
StreamString stream;
|
||||
stream << env_var_name;
|
||||
stream << '=';
|
||||
stream << new_value;
|
||||
stream.Flush();
|
||||
std::string var_string(env_var_name);
|
||||
var_string += "=";
|
||||
var_string += new_value;
|
||||
|
||||
// Find the environment variable if present and replace it.
|
||||
for (size_t i = 0; i < GetArgumentCount(); ++i) {
|
||||
// Get the env var value.
|
||||
const char *arg_value = GetArgumentAtIndex(i);
|
||||
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;
|
||||
}
|
||||
size_t index = 0;
|
||||
if (ContainsEnvironmentVariable(env_var_name, &index)) {
|
||||
ReplaceArgumentAtIndex(index, var_string);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// Validate args.
|
||||
if (!env_var_name)
|
||||
if (env_var_name.empty())
|
||||
return false;
|
||||
|
||||
// Check each arg to see if it matches the env var name.
|
||||
for (size_t i = 0; i < GetArgumentCount(); ++i) {
|
||||
// Get the arg value.
|
||||
const char *argument_value = GetArgumentAtIndex(i);
|
||||
if (!argument_value)
|
||||
continue;
|
||||
auto arg_value = llvm::StringRef::withNullAsEmpty(GetArgumentAtIndex(0));
|
||||
|
||||
// Check if we are the "{env_var_name}={env_var_value}" style.
|
||||
const char *equal_p = strchr(argument_value, '=');
|
||||
if (equal_p) {
|
||||
if (strncmp(env_var_name, argument_value, equal_p - argument_value) ==
|
||||
0) {
|
||||
// We matched.
|
||||
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;
|
||||
}
|
||||
llvm::StringRef name, value;
|
||||
std::tie(name, value) = arg_value.split('=');
|
||||
if (name == env_var_name && !value.empty()) {
|
||||
if (argument_index)
|
||||
*argument_index = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ static bool ProcessAliasOptionsArgs(lldb::CommandObjectSP &cmd_obj_sp,
|
|||
ExecutionContext exe_ctx =
|
||||
cmd_obj_sp->GetCommandInterpreter().GetExecutionContext();
|
||||
options->NotifyOptionParsingStarting(&exe_ctx);
|
||||
args.Unshift("dummy_arg");
|
||||
args.Unshift(llvm::StringRef("dummy_arg"));
|
||||
args.ParseAliasOptions(*options, result, option_arg_vector, options_string);
|
||||
args.Shift();
|
||||
if (result.Succeeded())
|
||||
|
|
|
@ -1333,7 +1333,7 @@ CommandObject *CommandInterpreter::BuildAliasResult(
|
|||
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);
|
||||
cmd_args.Unshift(alias_name_str);
|
||||
|
||||
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
|
||||
std::string alias_name_str = alias_name;
|
||||
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());
|
||||
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
|
||||
// in the array and starts at position 1,
|
||||
// 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;
|
||||
error = args.ParseOptions(*options, &exe_ctx,
|
||||
GetCommandInterpreter().GetPlatform(true),
|
||||
|
@ -296,7 +296,7 @@ int CommandObject::HandleCompletion(Args &input, int &cursor_index,
|
|||
if (cur_options != nullptr) {
|
||||
// Re-insert the dummy command name string which will have been
|
||||
// stripped off:
|
||||
input.Unshift("dummy-string");
|
||||
input.Unshift(llvm::StringRef("dummy-string"));
|
||||
cursor_index++;
|
||||
|
||||
// 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.
|
||||
const char *disable_env_var = "IDE_DISABLED_OS_ACTIVITY_DT_MODE";
|
||||
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 get os_log and NSLog messages mirrored to the target process
|
||||
// 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"));
|
||||
}
|
||||
|
||||
|
|
|
@ -1091,7 +1091,7 @@ EnableOptionsSP ParseAutoEnableOptions(Error &error, Debugger &debugger) {
|
|||
// ParseOptions calls getopt_long_only, which always skips the zero'th item in
|
||||
// the array and starts at position 1,
|
||||
// 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;
|
||||
error = args.ParseOptions(*options_sp.get(), &exe_ctx, PlatformSP(),
|
||||
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
|
||||
// echoing of os_log()/NSLog() to stderr in the target program.
|
||||
size_t argument_index;
|
||||
if (env_vars.ContainsEnvironmentVariable("OS_ACTIVITY_DT_MODE",
|
||||
&argument_index))
|
||||
if (env_vars.ContainsEnvironmentVariable(
|
||||
llvm::StringRef("OS_ACTIVITY_DT_MODE"), &argument_index))
|
||||
env_vars.DeleteArgumentAtIndex(argument_index);
|
||||
|
||||
// We will also set the env var that tells any downstream launcher
|
||||
// from adding OS_ACTIVITY_DT_MODE.
|
||||
env_vars.AddOrReplaceEnvironmentVariable("IDE_DISABLED_OS_ACTIVITY_DT_MODE",
|
||||
"1");
|
||||
env_vars.AddOrReplaceEnvironmentVariable(
|
||||
llvm::StringRef("IDE_DISABLED_OS_ACTIVITY_DT_MODE"),
|
||||
llvm::StringRef("1"));
|
||||
}
|
||||
|
||||
// Set the OS_ACTIVITY_MODE env var appropriately to enable/disable
|
||||
|
@ -1568,9 +1569,8 @@ Error StructuredDataDarwinLog::FilterLaunchInfo(ProcessLaunchInfo &launch_info,
|
|||
env_var_value = "";
|
||||
|
||||
if (env_var_value) {
|
||||
const char *env_var_name = "OS_ACTIVITY_MODE";
|
||||
launch_info.GetEnvironmentEntries().AddOrReplaceEnvironmentVariable(
|
||||
env_var_name, env_var_value);
|
||||
llvm::StringRef("OS_ACTIVITY_MODE"), llvm::StringRef(env_var_value));
|
||||
}
|
||||
|
||||
return error;
|
||||
|
|
Loading…
Reference in New Issue