[Lldb-commits] [lldb] r369352 - [lldb] D66174 `RegularExpression` cleanup
Jan Kratochvil via lldb-commits
lldb-commits at lists.llvm.org
Tue Aug 20 02:24:20 PDT 2019
Author: jankratochvil
Date: Tue Aug 20 02:24:20 2019
New Revision: 369352
URL: http://llvm.org/viewvc/llvm-project?rev=369352&view=rev
Log:
[lldb] D66174 `RegularExpression` cleanup
I find as a good cleanup to drop the Compile method. As I do not find TIMTOWTDI
as an advantage and there is already constructor parameter to compile the
regex.
Differential Revision: https://reviews.llvm.org/D66392
Modified:
lldb/trunk/include/lldb/Interpreter/OptionValueRegex.h
lldb/trunk/include/lldb/Utility/RegularExpression.h
lldb/trunk/source/Breakpoint/BreakpointResolverName.cpp
lldb/trunk/source/Commands/CommandCompletions.cpp
lldb/trunk/source/Commands/CommandObjectFrame.cpp
lldb/trunk/source/Commands/CommandObjectType.cpp
lldb/trunk/source/Core/AddressResolverName.cpp
lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp
lldb/trunk/source/Interpreter/OptionValueRegex.cpp
lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
lldb/trunk/source/Target/ThreadPlanStepInRange.cpp
lldb/trunk/source/Utility/RegularExpression.cpp
Modified: lldb/trunk/include/lldb/Interpreter/OptionValueRegex.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/OptionValueRegex.h?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/OptionValueRegex.h (original)
+++ lldb/trunk/include/lldb/Interpreter/OptionValueRegex.h Tue Aug 20 02:24:20 2019
@@ -50,7 +50,7 @@ public:
void SetCurrentValue(const char *value) {
if (value && value[0])
- m_regex.Compile(llvm::StringRef(value));
+ m_regex = RegularExpression(llvm::StringRef(value));
else
m_regex = RegularExpression();
}
Modified: lldb/trunk/include/lldb/Utility/RegularExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/RegularExpression.h?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/RegularExpression.h (original)
+++ lldb/trunk/include/lldb/Utility/RegularExpression.h Tue Aug 20 02:24:20 2019
@@ -23,7 +23,18 @@ public:
/// contains no compiled regular expression.
RegularExpression() = default;
+ /// Constructor for a regular expression.
+ ///
+ /// Compile a regular expression using the supplied regular expression text.
+ /// The compiled regular expression lives in this object so that it can be
+ /// readily used for regular expression matches. Execute() can be called
+ /// after the regular expression is compiled.
+ ///
+ /// \param[in] string
+ /// An llvm::StringRef that represents the regular expression to compile.
+ // String is not referenced anymore after the object is constructed.
explicit RegularExpression(llvm::StringRef string);
+
~RegularExpression() = default;
RegularExpression(const RegularExpression &rhs);
@@ -32,22 +43,6 @@ public:
RegularExpression &operator=(RegularExpression &&rhs) = default;
RegularExpression &operator=(const RegularExpression &rhs) = default;
- /// Compile a regular expression.
- ///
- /// Compile a regular expression using the supplied regular expression text.
- /// The compiled regular expression lives in this object so that it can be
- /// readily used for regular expression matches. Execute() can be called
- /// after the regular expression is compiled. Any previously compiled
- /// regular expression contained in this object will be freed.
- ///
- /// \param[in] re
- /// A NULL terminated C string that represents the regular
- /// expression to compile.
- ///
- /// \return \b true if the regular expression compiles successfully, \b false
- /// otherwise.
- bool Compile(llvm::StringRef string);
-
/// Executes a regular expression.
///
/// Execute a regular expression match using the compiled regular expression
@@ -65,7 +60,7 @@ public:
/// matches, or nullptr if no parenthesized matching is needed.
///
/// \return \b true if \a string matches the compiled regular expression, \b
- /// false otherwise.
+ /// false otherwise incl. the case regular exression failed to compile.
bool Execute(llvm::StringRef string,
llvm::SmallVectorImpl<llvm::StringRef> *matches = nullptr) const;
Modified: lldb/trunk/source/Breakpoint/BreakpointResolverName.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/BreakpointResolverName.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/BreakpointResolverName.cpp (original)
+++ lldb/trunk/source/Breakpoint/BreakpointResolverName.cpp Tue Aug 20 02:24:20 2019
@@ -31,7 +31,8 @@ BreakpointResolverName::BreakpointResolv
m_class_name(), m_regex(), m_match_type(type), m_language(language),
m_skip_prologue(skip_prologue) {
if (m_match_type == Breakpoint::Regexp) {
- if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
+ m_regex = RegularExpression(llvm::StringRef::withNullAsEmpty(name_cstr));
+ if (!m_regex.IsValid()) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
if (log)
Modified: lldb/trunk/source/Commands/CommandCompletions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandCompletions.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandCompletions.cpp (original)
+++ lldb/trunk/source/Commands/CommandCompletions.cpp Tue Aug 20 02:24:20 2019
@@ -454,7 +454,7 @@ CommandCompletions::SymbolCompleter::Sym
pos = regex_str.insert(pos, '\\');
pos = find_if(pos + 2, regex_str.end(), regex_chars);
}
- m_regex.Compile(regex_str);
+ m_regex = RegularExpression(regex_str);
}
lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
Modified: lldb/trunk/source/Commands/CommandObjectFrame.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectFrame.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectFrame.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectFrame.cpp Tue Aug 20 02:24:20 2019
@@ -534,7 +534,7 @@ protected:
const size_t regex_start_index = regex_var_list.GetSize();
llvm::StringRef name_str = entry.ref;
RegularExpression regex(name_str);
- if (regex.Compile(name_str)) {
+ if (regex.IsValid()) {
size_t num_matches = 0;
const size_t num_new_regex_vars =
variable_list->AppendVariablesIfUnique(regex, regex_var_list,
Modified: lldb/trunk/source/Commands/CommandObjectType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectType.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectType.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectType.cpp Tue Aug 20 02:24:20 2019
@@ -691,8 +691,8 @@ protected:
ConstString typeCS(arg_entry.ref);
if (m_command_options.m_regex) {
- RegularExpressionSP typeRX(new RegularExpression());
- if (!typeRX->Compile(arg_entry.ref)) {
+ RegularExpressionSP typeRX(new RegularExpression(arg_entry.ref));
+ if (!typeRX->IsValid()) {
result.AppendError(
"regex format error (maybe this is not really a regex?)");
result.SetStatus(eReturnStatusFailed);
@@ -1043,9 +1043,9 @@ protected:
std::unique_ptr<RegularExpression> formatter_regex;
if (m_options.m_category_regex.OptionWasSet()) {
- category_regex.reset(new RegularExpression());
- if (!category_regex->Compile(
- m_options.m_category_regex.GetCurrentValueAsRef())) {
+ category_regex.reset(new RegularExpression(
+ m_options.m_category_regex.GetCurrentValueAsRef()));
+ if (!category_regex->IsValid()) {
result.AppendErrorWithFormat(
"syntax error in category regular expression '%s'",
m_options.m_category_regex.GetCurrentValueAsRef().str().c_str());
@@ -1056,8 +1056,9 @@ protected:
if (argc == 1) {
const char *arg = command.GetArgumentAtIndex(0);
- formatter_regex.reset(new RegularExpression());
- if (!formatter_regex->Compile(llvm::StringRef::withNullAsEmpty(arg))) {
+ formatter_regex.reset(
+ new RegularExpression(llvm::StringRef::withNullAsEmpty(arg)));
+ if (!formatter_regex->IsValid()) {
result.AppendErrorWithFormat("syntax error in regular expression '%s'",
arg);
result.SetStatus(eReturnStatusFailed);
@@ -1629,8 +1630,8 @@ bool CommandObjectTypeSummaryAdd::AddSum
}
if (type == eRegexSummary) {
- RegularExpressionSP typeRX(new RegularExpression());
- if (!typeRX->Compile(type_name.GetStringRef())) {
+ RegularExpressionSP typeRX(new RegularExpression(type_name.GetStringRef()));
+ if (!typeRX->IsValid()) {
if (error)
error->SetErrorString(
"regex format error (maybe this is not really a regex?)");
@@ -2115,9 +2116,9 @@ protected:
std::unique_ptr<RegularExpression> regex;
if (argc == 1) {
- regex.reset(new RegularExpression());
const char *arg = command.GetArgumentAtIndex(0);
- if (!regex->Compile(llvm::StringRef::withNullAsEmpty(arg))) {
+ regex.reset(new RegularExpression(llvm::StringRef::withNullAsEmpty(arg)));
+ if (!regex->IsValid()) {
result.AppendErrorWithFormat(
"syntax error in category regular expression '%s'", arg);
result.SetStatus(eReturnStatusFailed);
@@ -2369,8 +2370,8 @@ bool CommandObjectTypeSynthAdd::AddSynth
}
if (type == eRegexSynth) {
- RegularExpressionSP typeRX(new RegularExpression());
- if (!typeRX->Compile(type_name.GetStringRef())) {
+ RegularExpressionSP typeRX(new RegularExpression(type_name.GetStringRef()));
+ if (!typeRX->IsValid()) {
if (error)
error->SetErrorString(
"regex format error (maybe this is not really a regex?)");
@@ -2497,8 +2498,9 @@ private:
}
if (type == eRegexFilter) {
- RegularExpressionSP typeRX(new RegularExpression());
- if (!typeRX->Compile(type_name.GetStringRef())) {
+ RegularExpressionSP typeRX(
+ new RegularExpression(type_name.GetStringRef()));
+ if (!typeRX->IsValid()) {
if (error)
error->SetErrorString(
"regex format error (maybe this is not really a regex?)");
Modified: lldb/trunk/source/Core/AddressResolverName.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/AddressResolverName.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Core/AddressResolverName.cpp (original)
+++ lldb/trunk/source/Core/AddressResolverName.cpp Tue Aug 20 02:24:20 2019
@@ -36,7 +36,8 @@ AddressResolverName::AddressResolverName
: AddressResolver(), m_func_name(func_name), m_class_name(nullptr),
m_regex(), m_match_type(type) {
if (m_match_type == AddressResolver::Regexp) {
- if (!m_regex.Compile(m_func_name.GetStringRef())) {
+ m_regex = RegularExpression(m_func_name.GetStringRef());
+ if (!m_regex.IsValid()) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
if (log)
Modified: lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp Tue Aug 20 02:24:20 2019
@@ -73,8 +73,9 @@ bool CommandObjectRegexCommand::AddRegex
const char *command_cstr) {
m_entries.resize(m_entries.size() + 1);
// Only add the regular expression if it compiles
- if (m_entries.back().regex.Compile(
- llvm::StringRef::withNullAsEmpty(re_cstr))) {
+ m_entries.back().regex =
+ RegularExpression(llvm::StringRef::withNullAsEmpty(re_cstr));
+ if (m_entries.back().regex.IsValid()) {
m_entries.back().command.assign(command_cstr);
return true;
}
Modified: lldb/trunk/source/Interpreter/OptionValueRegex.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueRegex.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueRegex.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueRegex.cpp Tue Aug 20 02:24:20 2019
@@ -46,7 +46,8 @@ Status OptionValueRegex::SetValueFromStr
case eVarSetOperationReplace:
case eVarSetOperationAssign:
- if (m_regex.Compile(value)) {
+ m_regex = RegularExpression(value);
+ if (m_regex.IsValid()) {
m_value_was_set = true;
NotifyValueChanged();
} else if (llvm::Error err = m_regex.GetError()) {
Modified: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp (original)
+++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp Tue Aug 20 02:24:20 2019
@@ -582,9 +582,9 @@ protected:
case 0:
break;
case 1: {
- regex_up.reset(new RegularExpression());
- if (!regex_up->Compile(llvm::StringRef::withNullAsEmpty(
- command.GetArgumentAtIndex(0)))) {
+ regex_up.reset(new RegularExpression(
+ llvm::StringRef::withNullAsEmpty(command.GetArgumentAtIndex(0))));
+ if (!regex_up->IsValid()) {
result.AppendError(
"invalid argument - please provide a valid regular expression");
result.SetStatus(lldb::eReturnStatusFailed);
Modified: lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp (original)
+++ lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp Tue Aug 20 02:24:20 2019
@@ -442,21 +442,12 @@ bool ParseCoordinate(llvm::StringRef coo
// elements fails the contents of &coord are undefined and `false` is
// returned, `true` otherwise
- RegularExpression regex;
llvm::SmallVector<llvm::StringRef, 4> matches;
- bool matched = false;
- if (regex.Compile(llvm::StringRef("^([0-9]+),([0-9]+),([0-9]+)$")) &&
- regex.Execute(coord_s, &matches))
- matched = true;
- else if (regex.Compile(llvm::StringRef("^([0-9]+),([0-9]+)$")) &&
- regex.Execute(coord_s, &matches))
- matched = true;
- else if (regex.Compile(llvm::StringRef("^([0-9]+)$")) &&
- regex.Execute(coord_s, &matches))
- matched = true;
-
- if (!matched)
+ if (!RegularExpression("^([0-9]+),([0-9]+),([0-9]+)$")
+ .Execute(coord_s, &matches) &&
+ !RegularExpression("^([0-9]+),([0-9]+)$").Execute(coord_s, &matches) &&
+ !RegularExpression("^([0-9]+)$").Execute(coord_s, &matches))
return false;
auto get_index = [&](size_t idx, uint32_t &i) -> bool {
Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp Tue Aug 20 02:24:20 2019
@@ -332,11 +332,12 @@ GDBRemoteCommunication::WaitForPacketNoL
std::string regex_str = "^";
regex_str += echo_packet;
regex_str += "$";
- response_regex.Compile(regex_str);
+ response_regex = RegularExpression(regex_str);
} else {
echo_packet_len =
::snprintf(echo_packet, sizeof(echo_packet), "qC");
- response_regex.Compile(llvm::StringRef("^QC[0-9A-Fa-f]+$"));
+ response_regex =
+ RegularExpression(llvm::StringRef("^QC[0-9A-Fa-f]+$"));
}
PacketResult echo_packet_result =
Modified: lldb/trunk/source/Target/ThreadPlanStepInRange.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ThreadPlanStepInRange.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Target/ThreadPlanStepInRange.cpp (original)
+++ lldb/trunk/source/Target/ThreadPlanStepInRange.cpp Tue Aug 20 02:24:20 2019
@@ -313,10 +313,10 @@ bool ThreadPlanStepInRange::ShouldStop(E
void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
auto name_ref = llvm::StringRef::withNullAsEmpty(name);
- if (!m_avoid_regexp_up)
+ if (m_avoid_regexp_up)
+ *m_avoid_regexp_up = RegularExpression(name_ref);
+ else
m_avoid_regexp_up.reset(new RegularExpression(name_ref));
-
- m_avoid_regexp_up->Compile(name_ref);
}
void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
Modified: lldb/trunk/source/Utility/RegularExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/RegularExpression.cpp?rev=369352&r1=369351&r2=369352&view=diff
==============================================================================
--- lldb/trunk/source/Utility/RegularExpression.cpp (original)
+++ lldb/trunk/source/Utility/RegularExpression.cpp Tue Aug 20 02:24:20 2019
@@ -12,22 +12,19 @@
using namespace lldb_private;
-RegularExpression::RegularExpression(llvm::StringRef str) { Compile(str); }
+RegularExpression::RegularExpression(llvm::StringRef str)
+ : m_regex_text(str),
+ // m_regex does not reference str anymore after it is constructed.
+ m_regex(llvm::Regex(str)) {}
RegularExpression::RegularExpression(const RegularExpression &rhs)
- : RegularExpression() {
- Compile(rhs.GetText());
-}
-
-bool RegularExpression::Compile(llvm::StringRef str) {
- m_regex_text = str;
- m_regex = llvm::Regex(str);
- return IsValid();
-}
+ : RegularExpression(rhs.GetText()) {}
bool RegularExpression::Execute(
llvm::StringRef str,
llvm::SmallVectorImpl<llvm::StringRef> *matches) const {
+ if (!IsValid())
+ return false;
return m_regex.match(str, matches);
}
More information about the lldb-commits
mailing list