[Lldb-commits] [lldb] r281942 - Convert 3 more functions to use a StringRef.
Zachary Turner via lldb-commits
lldb-commits at lists.llvm.org
Mon Sep 19 14:57:00 PDT 2016
Author: zturner
Date: Mon Sep 19 16:56:59 2016
New Revision: 281942
URL: http://llvm.org/viewvc/llvm-project?rev=281942&view=rev
Log:
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.
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
lldb/trunk/source/Interpreter/CommandObject.cpp
lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
lldb/trunk/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
Modified: lldb/trunk/include/lldb/Interpreter/Args.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/Args.h?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/Args.h (original)
+++ lldb/trunk/include/lldb/Interpreter/Args.h Mon Sep 19 16:56:59 2016
@@ -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:
Modified: lldb/trunk/source/Interpreter/Args.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/Args.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/Args.cpp (original)
+++ lldb/trunk/source/Interpreter/Args.cpp Mon Sep 19 16:56:59 2016
@@ -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::stri
}
}
-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();
-
- // 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;
- }
+ std::string var_string(env_var_name);
+ var_string += "=";
+ var_string += new_value;
+
+ 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;
}
}
Modified: lldb/trunk/source/Interpreter/CommandAlias.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandAlias.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandAlias.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandAlias.cpp Mon Sep 19 16:56:59 2016
@@ -41,7 +41,7 @@ static bool ProcessAliasOptionsArgs(lldb
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())
Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Mon Sep 19 16:56:59 2016
@@ -1333,7 +1333,7 @@ CommandObject *CommandInterpreter::Build
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::BuildAliasComma
// 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)
Modified: lldb/trunk/source/Interpreter/CommandObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObject.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObject.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandObject.cpp Mon Sep 19 16:56:59 2016
@@ -116,7 +116,7 @@ bool CommandObject::ParseOptions(Args &a
// 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
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
Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp Mon Sep 19 16:56:59 2016
@@ -1881,11 +1881,12 @@ PlatformDarwin::LaunchProcess(lldb_priva
// 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"));
}
Modified: lldb/trunk/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp?rev=281942&r1=281941&r2=281942&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp (original)
+++ lldb/trunk/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp Mon Sep 19 16:56:59 2016
@@ -1091,7 +1091,7 @@ EnableOptionsSP ParseAutoEnableOptions(E
// 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::FilterLau
// 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::FilterLau
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;
More information about the lldb-commits
mailing list