[lldb][NFC] Remove all uses of StringRef::withNullAsEmpty in LLDB

A long time ago LLDB wanted to start using StringRef instead of
C-Strings/ConstString but was blocked by the fact that the StringRef constructor
that takes a C-string was asserting that the C-string isn't a nullptr. To
workaround this, D24697 introduced a special function called `withNullAsEmpty`
and that's what LLDB (and only LLDB) started to use to build StringRefs from
C-strings.

A bit later it seems that `withNullAsEmpty` was declared too awkward to use and
instead the assert in the StringRef constructor got removed (see D24904). The
rest of LLDB was then converted to StringRef by just calling the now perfectly
usable implicit constructor.

However, all the calls to `withNullAsEmpty` just remained and are now just
strange artefacts in the code base that just look out of place. It's also
curiously a LLDB-exclusive function and no other project ever called it since
it's introduction half a decade ago.

This patch removes all uses of `withNullAsEmpty`. The follow up will be to
remove the function from StringRef.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D102597
This commit is contained in:
Raphael Isemann 2021-05-18 09:03:28 +02:00
parent 2e92f1a9bc
commit bbea361039
14 changed files with 28 additions and 47 deletions

View file

@ -17,8 +17,7 @@ namespace lldb_private {
class OptionValueRegex : public Cloneable<OptionValueRegex, OptionValue> {
public:
OptionValueRegex(const char *value = nullptr)
: m_regex(llvm::StringRef::withNullAsEmpty(value)),
m_default_regex_str(llvm::StringRef::withNullAsEmpty(value).str()) {}
: m_regex(value), m_default_regex_str(value) {}
~OptionValueRegex() override = default;

View file

@ -85,7 +85,7 @@ public:
const Flags &GetOptions() const { return m_options; }
const char *operator=(const char *value) {
SetCurrentValue(llvm::StringRef::withNullAsEmpty(value));
SetCurrentValue(value);
return m_current_value.c_str();
}

View file

@ -1387,7 +1387,7 @@ void SBDebugger::SetPrompt(const char *prompt) {
LLDB_RECORD_METHOD(void, SBDebugger, SetPrompt, (const char *), prompt);
if (m_opaque_sp)
m_opaque_sp->SetPrompt(llvm::StringRef::withNullAsEmpty(prompt));
m_opaque_sp->SetPrompt(llvm::StringRef(prompt));
}
const char *SBDebugger::GetReproducerPath() const {

View file

@ -18,8 +18,7 @@ SBLanguageRuntime::GetLanguageTypeFromString(const char *string) {
LLDB_RECORD_STATIC_METHOD(lldb::LanguageType, SBLanguageRuntime,
GetLanguageTypeFromString, (const char *), string);
return Language::GetLanguageTypeFromString(
llvm::StringRef::withNullAsEmpty(string));
return Language::GetLanguageTypeFromString(llvm::StringRef(string));
}
const char *

View file

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

View file

@ -363,9 +363,7 @@ bool SBTypeCategory::AddTypeFormat(SBTypeNameSpecifier type_name,
if (type_name.IsRegex())
m_opaque_sp->GetRegexTypeFormatsContainer()->Add(
RegularExpression(
llvm::StringRef::withNullAsEmpty(type_name.GetName())),
format.GetSP());
RegularExpression(type_name.GetName()), format.GetSP());
else
m_opaque_sp->GetTypeFormatsContainer()->Add(
ConstString(type_name.GetName()), format.GetSP());
@ -442,9 +440,7 @@ bool SBTypeCategory::AddTypeSummary(SBTypeNameSpecifier type_name,
if (type_name.IsRegex())
m_opaque_sp->GetRegexTypeSummariesContainer()->Add(
RegularExpression(
llvm::StringRef::withNullAsEmpty(type_name.GetName())),
summary.GetSP());
RegularExpression(type_name.GetName()), summary.GetSP());
else
m_opaque_sp->GetTypeSummariesContainer()->Add(
ConstString(type_name.GetName()), summary.GetSP());
@ -487,9 +483,7 @@ bool SBTypeCategory::AddTypeFilter(SBTypeNameSpecifier type_name,
if (type_name.IsRegex())
m_opaque_sp->GetRegexTypeFiltersContainer()->Add(
RegularExpression(
llvm::StringRef::withNullAsEmpty(type_name.GetName())),
filter.GetSP());
RegularExpression(type_name.GetName()), filter.GetSP());
else
m_opaque_sp->GetTypeFiltersContainer()->Add(
ConstString(type_name.GetName()), filter.GetSP());
@ -566,9 +560,7 @@ bool SBTypeCategory::AddTypeSynthetic(SBTypeNameSpecifier type_name,
if (type_name.IsRegex())
m_opaque_sp->GetRegexTypeSyntheticsContainer()->Add(
RegularExpression(
llvm::StringRef::withNullAsEmpty(type_name.GetName())),
synth.GetSP());
RegularExpression(type_name.GetName()), synth.GetSP());
else
m_opaque_sp->GetTypeSyntheticsContainer()->Add(
ConstString(type_name.GetName()), synth.GetSP());

View file

@ -31,7 +31,7 @@ BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
m_class_name(), m_regex(), m_match_type(type), m_language(language),
m_skip_prologue(skip_prologue) {
if (m_match_type == Breakpoint::Regexp) {
m_regex = RegularExpression(llvm::StringRef::withNullAsEmpty(name_cstr));
m_regex = RegularExpression(name_cstr);
if (!m_regex.IsValid()) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));

View file

@ -460,7 +460,7 @@ public:
protected:
llvm::StringRef GetScopeString(VariableSP var_sp) {
if (!var_sp)
return llvm::StringRef::withNullAsEmpty(nullptr);
return llvm::StringRef();
switch (var_sp->GetScope()) {
case eValueTypeVariableGlobal:
@ -477,7 +477,7 @@ protected:
break;
}
return llvm::StringRef::withNullAsEmpty(nullptr);
return llvm::StringRef();
}
bool DoExecute(Args &command, CommandReturnObject &result) override {

View file

@ -848,8 +848,7 @@ protected:
size_t matches = 0;
bool use_var_name = false;
if (m_option_variable.use_regex) {
RegularExpression regex(
llvm::StringRef::withNullAsEmpty(arg.c_str()));
RegularExpression regex(arg.ref());
if (!regex.IsValid()) {
result.GetErrorStream().Printf(
"error: invalid regular expression: '%s'\n", arg.c_str());

View file

@ -1080,8 +1080,7 @@ protected:
if (argc == 1) {
const char *arg = command.GetArgumentAtIndex(0);
formatter_regex = std::make_unique<RegularExpression>(
llvm::StringRef::withNullAsEmpty(arg));
formatter_regex = std::make_unique<RegularExpression>(arg);
if (!formatter_regex->IsValid()) {
result.AppendErrorWithFormat("syntax error in regular expression '%s'",
arg);
@ -1167,9 +1166,7 @@ protected:
bool escape = true;
if (category->GetName() == category_regex->GetText()) {
escape = false;
} else if (category_regex->Execute(
llvm::StringRef::withNullAsEmpty(
category->GetName()))) {
} else if (category_regex->Execute(category->GetName())) {
escape = false;
}
@ -2175,8 +2172,7 @@ protected:
if (argc == 1) {
const char *arg = command.GetArgumentAtIndex(0);
regex = std::make_unique<RegularExpression>(
llvm::StringRef::withNullAsEmpty(arg));
regex = std::make_unique<RegularExpression>(arg);
if (!regex->IsValid()) {
result.AppendErrorWithFormat(
"syntax error in category regular expression '%s'", arg);
@ -2196,8 +2192,7 @@ protected:
bool escape = true;
if (regex->GetText() == category_sp->GetName()) {
escape = false;
} else if (regex->Execute(llvm::StringRef::withNullAsEmpty(
category_sp->GetName()))) {
} else if (regex->Execute(category_sp->GetName())) {
escape = false;
}

View file

@ -2986,9 +2986,9 @@ void CommandInterpreter::GetLLDBCommandsFromIOHandler(
IOHandlerSP io_handler_sp(
new IOHandlerEditline(debugger, IOHandler::Type::CommandList,
"lldb", // Name of input reader for history
llvm::StringRef::withNullAsEmpty(prompt), // Prompt
llvm::StringRef(), // Continuation prompt
true, // Get multiple lines
llvm::StringRef(prompt), // Prompt
llvm::StringRef(), // Continuation prompt
true, // Get multiple lines
debugger.GetUseColor(),
0, // Don't show line numbers
delegate, // IOHandlerDelegate
@ -3006,9 +3006,9 @@ void CommandInterpreter::GetPythonCommandsFromIOHandler(
IOHandlerSP io_handler_sp(
new IOHandlerEditline(debugger, IOHandler::Type::PythonCode,
"lldb-python", // Name of input reader for history
llvm::StringRef::withNullAsEmpty(prompt), // Prompt
llvm::StringRef(), // Continuation prompt
true, // Get multiple lines
llvm::StringRef(prompt), // Prompt
llvm::StringRef(), // Continuation prompt
true, // Get multiple lines
debugger.GetUseColor(),
0, // Don't show line numbers
delegate, // IOHandlerDelegate

View file

@ -543,8 +543,7 @@ lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(
}
if (value_sp)
error = value_sp->SetValueFromString(
llvm::StringRef::withNullAsEmpty(value_cstr), eVarSetOperationAssign);
error = value_sp->SetValueFromString(value_cstr, eVarSetOperationAssign);
else
error.SetErrorString("unsupported type mask");
return value_sp;

View file

@ -663,8 +663,8 @@ protected:
case 0:
break;
case 1: {
regex_up = std::make_unique<RegularExpression>(
llvm::StringRef::withNullAsEmpty(command.GetArgumentAtIndex(0)));
regex_up =
std::make_unique<RegularExpression>(command.GetArgumentAtIndex(0));
if (!regex_up->IsValid()) {
result.AppendError(
"invalid argument - please provide a valid regular expression");

View file

@ -291,11 +291,10 @@ bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
}
void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
auto name_ref = llvm::StringRef::withNullAsEmpty(name);
if (m_avoid_regexp_up)
*m_avoid_regexp_up = RegularExpression(name_ref);
*m_avoid_regexp_up = RegularExpression(name);
else
m_avoid_regexp_up = std::make_unique<RegularExpression>(name_ref);
m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
}
void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {