Index: lldb/include/lldb/Breakpoint/BreakpointResolverName.h =================================================================== --- lldb/include/lldb/Breakpoint/BreakpointResolverName.h +++ lldb/include/lldb/Breakpoint/BreakpointResolverName.h @@ -44,7 +44,7 @@ // Creates a function breakpoint by regular expression. Takes over control // of the lifespan of func_regex. - BreakpointResolverName(Breakpoint *bkpt, RegularExpression func_regex, + BreakpointResolverName(Breakpoint *bkpt, RegularExpression &&func_regex, lldb::LanguageType language, lldb::addr_t offset, bool skip_prologue); Index: lldb/include/lldb/DataFormatters/FormattersContainer.h =================================================================== --- lldb/include/lldb/DataFormatters/FormattersContainer.h +++ lldb/include/lldb/DataFormatters/FormattersContainer.h @@ -67,19 +67,19 @@ typedef typename ValueType::SharedPointer ValueSP; typedef std::map MapType; typedef typename MapType::iterator MapIterator; - typedef std::function ForEachCallback; + typedef std::function ForEachCallback; FormatMap(IFormatChangeListener *lst) : m_map(), m_map_mutex(), listener(lst) {} - void Add(KeyType name, const ValueSP &entry) { + void Add(KeyType &&name, const ValueSP &entry) { if (listener) entry->GetRevision() = listener->GetCurrentRevision(); else entry->GetRevision() = 0; std::lock_guard guard(m_map_mutex); - m_map[name] = entry; + m_map[std::move(name)] = entry; if (listener) listener->Changed(); } @@ -116,7 +116,7 @@ std::lock_guard guard(m_map_mutex); MapIterator pos, end = m_map.end(); for (pos = m_map.begin(); pos != end; pos++) { - KeyType type = pos->first; + const KeyType &type = pos->first; if (!callback(type, pos->second)) break; } @@ -138,7 +138,7 @@ return iter->second; } - KeyType GetKeyAtIndex(size_t index) { + const KeyType *GetKeyAtIndex(size_t index) { std::lock_guard guard(m_map_mutex); MapIterator iter = m_map.begin(); MapIterator end = m_map.end(); @@ -146,9 +146,9 @@ iter++; index--; if (end == iter) - return KeyType(); + return nullptr; } - return iter->first; + return &iter->first; } protected: @@ -182,8 +182,8 @@ FormattersContainer(std::string name, IFormatChangeListener *lst) : m_format_map(lst), m_name(name) {} - void Add(const MapKeyType &type, const MapValueType &entry) { - Add_Impl(type, entry, static_cast(nullptr)); + void Add(MapKeyType &&type, const MapValueType &entry) { + Add_Impl(std::move(type), entry, static_cast(nullptr)); } bool Delete(ConstString type) { @@ -233,9 +233,9 @@ DISALLOW_COPY_AND_ASSIGN(FormattersContainer); - void Add_Impl(const MapKeyType &type, const MapValueType &entry, - lldb::RegularExpressionSP *dummy) { - m_format_map.Add(type, entry); + void Add_Impl(MapKeyType &&type, const MapValueType &entry, + RegularExpression *dummy) { + m_format_map.Add(std::move(type), entry); } void Add_Impl(ConstString type, const MapValueType &entry, @@ -247,12 +247,12 @@ return m_format_map.Delete(type); } - bool Delete_Impl(ConstString type, lldb::RegularExpressionSP *dummy) { + bool Delete_Impl(ConstString type, RegularExpression *dummy) { std::lock_guard guard(m_format_map.mutex()); MapIterator pos, end = m_format_map.map().end(); for (pos = m_format_map.map().begin(); pos != end; pos++) { - lldb::RegularExpressionSP regex = pos->first; - if (type.GetStringRef() == regex->GetText()) { + const RegularExpression ®ex = pos->first; + if (type.GetStringRef() == regex.GetText()) { m_format_map.map().erase(pos); if (m_format_map.listener) m_format_map.listener->Changed(); @@ -273,32 +273,31 @@ lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex_Impl(size_t index, ConstString *dummy) { - ConstString key = m_format_map.GetKeyAtIndex(index); + const ConstString *key = m_format_map.GetKeyAtIndex(index); if (key) return lldb::TypeNameSpecifierImplSP( - new TypeNameSpecifierImpl(key.AsCString(), false)); + new TypeNameSpecifierImpl(key->AsCString(), false)); else return lldb::TypeNameSpecifierImplSP(); } lldb::TypeNameSpecifierImplSP - GetTypeNameSpecifierAtIndex_Impl(size_t index, - lldb::RegularExpressionSP *dummy) { - lldb::RegularExpressionSP regex = m_format_map.GetKeyAtIndex(index); - if (regex.get() == nullptr) + GetTypeNameSpecifierAtIndex_Impl(size_t index, RegularExpression *dummy) { + const RegularExpression *regex = m_format_map.GetKeyAtIndex(index); + if (!regex) return lldb::TypeNameSpecifierImplSP(); return lldb::TypeNameSpecifierImplSP( new TypeNameSpecifierImpl(regex->GetText().str().c_str(), true)); } bool Get_Impl(ConstString key, MapValueType &value, - lldb::RegularExpressionSP *dummy) { + RegularExpression *dummy) { llvm::StringRef key_str = key.GetStringRef(); std::lock_guard guard(m_format_map.mutex()); MapIterator pos, end = m_format_map.map().end(); for (pos = m_format_map.map().begin(); pos != end; pos++) { - lldb::RegularExpressionSP regex = pos->first; - if (regex->Execute(key_str)) { + const RegularExpression ®ex = pos->first; + if (regex.Execute(key_str)) { value = pos->second; return true; } @@ -307,12 +306,12 @@ } bool GetExact_Impl(ConstString key, MapValueType &value, - lldb::RegularExpressionSP *dummy) { + RegularExpression *dummy) { std::lock_guard guard(m_format_map.mutex()); MapIterator pos, end = m_format_map.map().end(); for (pos = m_format_map.map().begin(); pos != end; pos++) { - lldb::RegularExpressionSP regex = pos->first; - if (regex->GetText() == key.GetStringRef()) { + const RegularExpression ®ex = pos->first; + if (regex.GetText() == key.GetStringRef()) { value = pos->second; return true; } Index: lldb/include/lldb/DataFormatters/TypeCategory.h =================================================================== --- lldb/include/lldb/DataFormatters/TypeCategory.h +++ lldb/include/lldb/DataFormatters/TypeCategory.h @@ -26,7 +26,7 @@ template class FormatterContainerPair { public: typedef FormattersContainer ExactMatchContainer; - typedef FormattersContainer + typedef FormattersContainer RegexMatchContainer; typedef typename ExactMatchContainer::MapType ExactMatchMap; Index: lldb/include/lldb/Interpreter/OptionValueRegex.h =================================================================== --- lldb/include/lldb/Interpreter/OptionValueRegex.h +++ lldb/include/lldb/Interpreter/OptionValueRegex.h @@ -50,7 +50,7 @@ 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(); } Index: lldb/include/lldb/Target/Target.h =================================================================== --- lldb/include/lldb/Target/Target.h +++ lldb/include/lldb/Target/Target.h @@ -598,7 +598,7 @@ const FileSpecList *containingModules, const FileSpecList *source_file_list, const std::unordered_set &function_names, - RegularExpression &source_regex, bool internal, bool request_hardware, + RegularExpression &&source_regex, bool internal, bool request_hardware, LazyBool move_to_nearest_code); // Use this to create a breakpoint from a load address @@ -621,9 +621,9 @@ // target setting, else we use the values passed in lldb::BreakpointSP CreateFuncRegexBreakpoint( const FileSpecList *containingModules, - const FileSpecList *containingSourceFiles, RegularExpression &func_regexp, - lldb::LanguageType requested_language, LazyBool skip_prologue, - bool internal, bool request_hardware); + const FileSpecList *containingSourceFiles, + RegularExpression &&func_regexp, lldb::LanguageType requested_language, + LazyBool skip_prologue, bool internal, bool request_hardware); // Use this to create a function breakpoint by name in containingModule, or // all modules if it is nullptr When "skip_prologue is set to Index: lldb/include/lldb/Utility/RegularExpression.h =================================================================== --- lldb/include/lldb/Utility/RegularExpression.h +++ lldb/include/lldb/Utility/RegularExpression.h @@ -15,7 +15,7 @@ namespace lldb_private { -class RegularExpression : public llvm::Regex { +class RegularExpression { public: /// Default constructor. /// @@ -27,31 +27,14 @@ ~RegularExpression() = default; RegularExpression(const RegularExpression &rhs); - RegularExpression(RegularExpression &&rhs) = default; - - 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); + RegularExpression(RegularExpression &&rhs); + RegularExpression &operator=(const RegularExpression &rhs); + RegularExpression &operator=(RegularExpression &&rhs); /// Executes a regular expression. /// /// Execute a regular expression match using the compiled regular expression - /// that is already in this object against the match string \a s. If any + /// that is in this object against the match string \a s. If any /// parens are used for regular expression matches \a match_count should /// indicate the number of regmatch_t values that are present in \a /// match_ptr. @@ -88,8 +71,16 @@ bool IsValid() const; /// Return an error if the regular expression failed to compile. + /// The value is not valid if the object has been copy/move-ctored. llvm::Error GetError() const; + bool operator<(const RegularExpression &rhs) const { + return GetText() < rhs.GetText(); + } + bool operator==(const RegularExpression &rhs) const { + return GetText() == rhs.GetText(); + } + private: /// A copy of the original regular expression text. std::string m_regex_text; Index: lldb/source/API/SBTarget.cpp =================================================================== --- lldb/source/API/SBTarget.cpp +++ lldb/source/API/SBTarget.cpp @@ -960,8 +960,8 @@ const LazyBool skip_prologue = eLazyBoolCalculate; sb_bp = target_sp->CreateFuncRegexBreakpoint( - module_list.get(), comp_unit_list.get(), regexp, symbol_language, - skip_prologue, internal, hardware); + module_list.get(), comp_unit_list.get(), std::move(regexp), + symbol_language, skip_prologue, internal, hardware); } return LLDB_RECORD_RESULT(sb_bp); @@ -1061,8 +1061,8 @@ } sb_bp = target_sp->CreateSourceRegexBreakpoint( - module_list.get(), source_file_list.get(), func_names_set, regexp, - false, hardware, move_to_nearest_code); + module_list.get(), source_file_list.get(), func_names_set, + std::move(regexp), false, hardware, move_to_nearest_code); } return LLDB_RECORD_RESULT(sb_bp); Index: lldb/source/API/SBTypeCategory.cpp =================================================================== --- lldb/source/API/SBTypeCategory.cpp +++ lldb/source/API/SBTypeCategory.cpp @@ -364,8 +364,8 @@ if (type_name.IsRegex()) m_opaque_sp->GetRegexTypeFormatsContainer()->Add( - lldb::RegularExpressionSP(new RegularExpression( - llvm::StringRef::withNullAsEmpty(type_name.GetName()))), + RegularExpression( + llvm::StringRef::withNullAsEmpty(type_name.GetName())), format.GetSP()); else m_opaque_sp->GetTypeFormatsContainer()->Add( @@ -443,8 +443,8 @@ if (type_name.IsRegex()) m_opaque_sp->GetRegexTypeSummariesContainer()->Add( - lldb::RegularExpressionSP(new RegularExpression( - llvm::StringRef::withNullAsEmpty(type_name.GetName()))), + RegularExpression( + llvm::StringRef::withNullAsEmpty(type_name.GetName())), summary.GetSP()); else m_opaque_sp->GetTypeSummariesContainer()->Add( @@ -488,8 +488,8 @@ if (type_name.IsRegex()) m_opaque_sp->GetRegexTypeFiltersContainer()->Add( - lldb::RegularExpressionSP(new RegularExpression( - llvm::StringRef::withNullAsEmpty(type_name.GetName()))), + RegularExpression( + llvm::StringRef::withNullAsEmpty(type_name.GetName())), filter.GetSP()); else m_opaque_sp->GetTypeFiltersContainer()->Add( @@ -567,8 +567,8 @@ if (type_name.IsRegex()) m_opaque_sp->GetRegexTypeSyntheticsContainer()->Add( - lldb::RegularExpressionSP(new RegularExpression( - llvm::StringRef::withNullAsEmpty(type_name.GetName()))), + RegularExpression( + llvm::StringRef::withNullAsEmpty(type_name.GetName())), synth.GetSP()); else m_opaque_sp->GetTypeSyntheticsContainer()->Add( Index: lldb/source/Breakpoint/BreakpointResolverName.cpp =================================================================== --- lldb/source/Breakpoint/BreakpointResolverName.cpp +++ lldb/source/Breakpoint/BreakpointResolverName.cpp @@ -31,7 +31,8 @@ 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) @@ -70,7 +71,7 @@ } BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt, - RegularExpression func_regex, + RegularExpression &&func_regex, lldb::LanguageType language, lldb::addr_t offset, bool skip_prologue) @@ -125,9 +126,8 @@ success = options_dict.GetValueForKeyAsString( GetKey(OptionNames::RegexString), regex_text); if (success) { - RegularExpression regex(regex_text); - return new BreakpointResolverName(bkpt, regex, language, offset, - skip_prologue); + return new BreakpointResolverName(bkpt, RegularExpression(regex_text), + language, offset, skip_prologue); } else { StructuredData::Array *names_array; success = options_dict.GetValueForKeyAsArray( Index: lldb/source/Commands/CommandCompletions.cpp =================================================================== --- lldb/source/Commands/CommandCompletions.cpp +++ lldb/source/Commands/CommandCompletions.cpp @@ -448,7 +448,7 @@ 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() { Index: lldb/source/Commands/CommandObjectBreakpoint.cpp =================================================================== --- lldb/source/Commands/CommandObjectBreakpoint.cpp +++ lldb/source/Commands/CommandObjectBreakpoint.cpp @@ -690,13 +690,10 @@ return false; } - bp_sp = target->CreateFuncRegexBreakpoint(&(m_options.m_modules), - &(m_options.m_filenames), - regexp, - m_options.m_language, - m_options.m_skip_prologue, - internal, - m_options.m_hardware); + bp_sp = target->CreateFuncRegexBreakpoint( + &(m_options.m_modules), &(m_options.m_filenames), std::move(regexp), + m_options.m_language, m_options.m_skip_prologue, internal, + m_options.m_hardware); } break; case eSetTypeSourceRegexp: // Breakpoint by regexp on source text. @@ -723,15 +720,10 @@ result.SetStatus(eReturnStatusFailed); return false; } - bp_sp = - target->CreateSourceRegexBreakpoint(&(m_options.m_modules), - &(m_options.m_filenames), - m_options - .m_source_regex_func_names, - regexp, - internal, - m_options.m_hardware, - m_options.m_move_to_nearest_code); + bp_sp = target->CreateSourceRegexBreakpoint( + &(m_options.m_modules), &(m_options.m_filenames), + m_options.m_source_regex_func_names, std::move(regexp), internal, + m_options.m_hardware, m_options.m_move_to_nearest_code); } break; case eSetTypeException: { Status precond_error; Index: lldb/source/Commands/CommandObjectFrame.cpp =================================================================== --- lldb/source/Commands/CommandObjectFrame.cpp +++ lldb/source/Commands/CommandObjectFrame.cpp @@ -534,7 +534,7 @@ 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, Index: lldb/source/Commands/CommandObjectType.cpp =================================================================== --- lldb/source/Commands/CommandObjectType.cpp +++ lldb/source/Commands/CommandObjectType.cpp @@ -692,17 +692,18 @@ ConstString typeCS(arg_entry.ref); if (m_command_options.m_regex) { - RegularExpressionSP typeRX(new RegularExpression()); - if (!typeRX->Compile(arg_entry.ref)) { + RegularExpression typeRX(arg_entry.ref); + if (!typeRX.IsValid()) { result.AppendError( "regex format error (maybe this is not really a regex?)"); result.SetStatus(eReturnStatusFailed); return false; } category_sp->GetRegexTypeSummariesContainer()->Delete(typeCS); - category_sp->GetRegexTypeFormatsContainer()->Add(typeRX, entry); + category_sp->GetRegexTypeFormatsContainer()->Add(std::move(typeRX), + entry); } else - category_sp->GetTypeFormatsContainer()->Add(typeCS, entry); + category_sp->GetTypeFormatsContainer()->Add(std::move(typeCS), entry); } result.SetStatus(eReturnStatusSuccessFinishNoResult); @@ -1044,9 +1045,9 @@ std::unique_ptr 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()); @@ -1057,8 +1058,9 @@ 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); @@ -1099,13 +1101,13 @@ foreach .SetWithRegex([&result, &formatter_regex, &any_printed]( - RegularExpressionSP regex_sp, + const RegularExpression ®ex, const FormatterSharedPointer &format_sp) -> bool { if (formatter_regex) { bool escape = true; - if (regex_sp->GetText() == formatter_regex->GetText()) { + if (regex.GetText() == formatter_regex->GetText()) { escape = false; - } else if (formatter_regex->Execute(regex_sp->GetText())) { + } else if (formatter_regex->Execute(regex.GetText())) { escape = false; } @@ -1115,7 +1117,7 @@ any_printed = true; result.GetOutputStream().Printf("%s: %s\n", - regex_sp->GetText().str().c_str(), + regex.GetText().str().c_str(), format_sp->GetDescription().c_str()); return true; }); @@ -1630,8 +1632,8 @@ } if (type == eRegexSummary) { - RegularExpressionSP typeRX(new RegularExpression()); - if (!typeRX->Compile(type_name.GetStringRef())) { + RegularExpression typeRX(type_name.GetStringRef()); + if (!typeRX.IsValid()) { if (error) error->SetErrorString( "regex format error (maybe this is not really a regex?)"); @@ -1639,7 +1641,7 @@ } category->GetRegexTypeSummariesContainer()->Delete(type_name); - category->GetRegexTypeSummariesContainer()->Add(typeRX, entry); + category->GetRegexTypeSummariesContainer()->Add(std::move(typeRX), entry); return true; } else if (type == eNamedSummary) { @@ -1647,7 +1649,7 @@ DataVisualization::NamedSummaryFormats::Add(type_name, entry); return true; } else { - category->GetTypeSummariesContainer()->Add(type_name, entry); + category->GetTypeSummariesContainer()->Add(std::move(type_name), entry); return true; } } @@ -2116,9 +2118,9 @@ std::unique_ptr 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); @@ -2370,8 +2372,8 @@ } if (type == eRegexSynth) { - RegularExpressionSP typeRX(new RegularExpression()); - if (!typeRX->Compile(type_name.GetStringRef())) { + RegularExpression typeRX(type_name.GetStringRef()); + if (!typeRX.IsValid()) { if (error) error->SetErrorString( "regex format error (maybe this is not really a regex?)"); @@ -2379,11 +2381,11 @@ } category->GetRegexTypeSyntheticsContainer()->Delete(type_name); - category->GetRegexTypeSyntheticsContainer()->Add(typeRX, entry); + category->GetRegexTypeSyntheticsContainer()->Add(std::move(typeRX), entry); return true; } else { - category->GetTypeSyntheticsContainer()->Add(type_name, entry); + category->GetTypeSyntheticsContainer()->Add(std::move(type_name), entry); return true; } } @@ -2498,8 +2500,8 @@ } if (type == eRegexFilter) { - RegularExpressionSP typeRX(new RegularExpression()); - if (!typeRX->Compile(type_name.GetStringRef())) { + RegularExpression typeRX(type_name.GetStringRef()); + if (!typeRX.IsValid()) { if (error) error->SetErrorString( "regex format error (maybe this is not really a regex?)"); @@ -2507,11 +2509,11 @@ } category->GetRegexTypeFiltersContainer()->Delete(type_name); - category->GetRegexTypeFiltersContainer()->Add(typeRX, entry); + category->GetRegexTypeFiltersContainer()->Add(std::move(typeRX), entry); return true; } else { - category->GetTypeFiltersContainer()->Add(type_name, entry); + category->GetTypeFiltersContainer()->Add(std::move(type_name), entry); return true; } } Index: lldb/source/Core/AddressResolverName.cpp =================================================================== --- lldb/source/Core/AddressResolverName.cpp +++ lldb/source/Core/AddressResolverName.cpp @@ -36,7 +36,8 @@ : 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) Index: lldb/source/DataFormatters/FormatManager.cpp =================================================================== --- lldb/source/DataFormatters/FormatManager.cpp +++ lldb/source/DataFormatters/FormatManager.cpp @@ -946,10 +946,10 @@ lldb::TypeSummaryImplSP string_array_format( new StringSummaryFormat(string_array_flags, "${var%s}")); - lldb::RegularExpressionSP any_size_char_arr( - new RegularExpression(llvm::StringRef("char \\[[0-9]+\\]"))); - lldb::RegularExpressionSP any_size_wchar_arr( - new RegularExpression(llvm::StringRef("wchar_t \\[[0-9]+\\]"))); + RegularExpression any_size_char_arr(llvm::StringRef("char \\[[0-9]+\\]")); +#if 0 // FIXME: unused: + RegularExpression any_size_wchar_arr(llvm::StringRef("wchar_t \\[[0-9]+\\]")); +#endif TypeCategoryImpl::SharedPointer sys_category_sp = GetCategory(m_system_category_name); @@ -958,8 +958,8 @@ string_format); sys_category_sp->GetTypeSummariesContainer()->Add( ConstString("unsigned char *"), string_format); - sys_category_sp->GetRegexTypeSummariesContainer()->Add(any_size_char_arr, - string_array_format); + sys_category_sp->GetRegexTypeSummariesContainer()->Add( + std::move(any_size_char_arr), string_array_format); lldb::TypeSummaryImplSP ostype_summary( new StringSummaryFormat(TypeSummaryImpl::Flags() Index: lldb/source/DataFormatters/FormattersHelpers.cpp =================================================================== --- lldb/source/DataFormatters/FormattersHelpers.cpp +++ lldb/source/DataFormatters/FormattersHelpers.cpp @@ -29,10 +29,10 @@ if (regex) category_sp->GetRegexTypeFormatsContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - format_sp); + RegularExpression(type_name.GetStringRef()), format_sp); else - category_sp->GetTypeFormatsContainer()->Add(type_name, format_sp); + category_sp->GetTypeFormatsContainer()->Add(std::move(type_name), + format_sp); } void lldb_private::formatters::AddSummary( @@ -40,10 +40,10 @@ ConstString type_name, bool regex) { if (regex) category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - summary_sp); + RegularExpression(type_name.GetStringRef()), summary_sp); else - category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp); + category_sp->GetTypeSummariesContainer()->Add(std::move(type_name), + summary_sp); } void lldb_private::formatters::AddStringSummary( @@ -53,10 +53,10 @@ if (regex) category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - summary_sp); + RegularExpression(type_name.GetStringRef()), summary_sp); else - category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp); + category_sp->GetTypeSummariesContainer()->Add(std::move(type_name), + summary_sp); } void lldb_private::formatters::AddOneLineSummary( @@ -67,10 +67,10 @@ if (regex) category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - summary_sp); + RegularExpression(type_name.GetStringRef()), summary_sp); else - category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp); + category_sp->GetTypeSummariesContainer()->Add(std::move(type_name), + summary_sp); } void lldb_private::formatters::AddCXXSummary( @@ -81,10 +81,10 @@ new CXXFunctionSummaryFormat(flags, funct, description)); if (regex) category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - summary_sp); + RegularExpression(type_name.GetStringRef()), summary_sp); else - category_sp->GetTypeSummariesContainer()->Add(type_name, summary_sp); + category_sp->GetTypeSummariesContainer()->Add(std::move(type_name), + summary_sp); } void lldb_private::formatters::AddCXXSynthetic( @@ -96,10 +96,10 @@ new CXXSyntheticChildren(flags, description, generator)); if (regex) category_sp->GetRegexTypeSyntheticsContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - synth_sp); + RegularExpression(type_name.GetStringRef()), synth_sp); else - category_sp->GetTypeSyntheticsContainer()->Add(type_name, synth_sp); + category_sp->GetTypeSyntheticsContainer()->Add(std::move(type_name), + synth_sp); } void lldb_private::formatters::AddFilter( @@ -111,10 +111,10 @@ filter_sp->AddExpressionPath(child); if (regex) category_sp->GetRegexTypeFiltersContainer()->Add( - RegularExpressionSP(new RegularExpression(type_name.GetStringRef())), - filter_sp); + RegularExpression(type_name.GetStringRef()), filter_sp); else - category_sp->GetTypeFiltersContainer()->Add(type_name, filter_sp); + category_sp->GetTypeFiltersContainer()->Add(std::move(type_name), + filter_sp); } size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) { Index: lldb/source/Interpreter/CommandObjectRegexCommand.cpp =================================================================== --- lldb/source/Interpreter/CommandObjectRegexCommand.cpp +++ lldb/source/Interpreter/CommandObjectRegexCommand.cpp @@ -73,8 +73,9 @@ 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; } Index: lldb/source/Interpreter/OptionValueRegex.cpp =================================================================== --- lldb/source/Interpreter/OptionValueRegex.cpp +++ lldb/source/Interpreter/OptionValueRegex.cpp @@ -46,7 +46,8 @@ 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()) { Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp =================================================================== --- lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp +++ lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp @@ -547,8 +547,8 @@ ConstString("^std::__[[:alnum:]]+::atomic<.+>$"), stl_synth_flags, true); cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add( - RegularExpressionSP(new RegularExpression( - llvm::StringRef("^(std::__[[:alnum:]]+::)deque<.+>(( )?&)?$"))), + RegularExpression( + llvm::StringRef("^(std::__[[:alnum:]]+::)deque<.+>(( )?&)?$")), SyntheticChildrenSP(new ScriptedSyntheticChildren( stl_synth_flags, "lldb.formatters.cpp.libcxx.stddeque_SynthProvider"))); @@ -744,38 +744,32 @@ false); cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add( - RegularExpressionSP( - new RegularExpression(llvm::StringRef("^std::vector<.+>(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::vector<.+>(( )?&)?$")), SyntheticChildrenSP(new ScriptedSyntheticChildren( stl_synth_flags, "lldb.formatters.cpp.gnu_libstdcpp.StdVectorSynthProvider"))); cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add( - RegularExpressionSP( - new RegularExpression(llvm::StringRef("^std::map<.+> >(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::map<.+> >(( )?&)?$")), SyntheticChildrenSP(new ScriptedSyntheticChildren( stl_synth_flags, "lldb.formatters.cpp.gnu_libstdcpp.StdMapSynthProvider"))); cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add( - RegularExpressionSP(new RegularExpression( - llvm::StringRef("^std::(__cxx11::)?list<.+>(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::(__cxx11::)?list<.+>(( )?&)?$")), SyntheticChildrenSP(new ScriptedSyntheticChildren( stl_synth_flags, "lldb.formatters.cpp.gnu_libstdcpp.StdListSynthProvider"))); stl_summary_flags.SetDontShowChildren(false); stl_summary_flags.SetSkipPointers(true); cpp_category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP( - new RegularExpression(llvm::StringRef("^std::vector<.+>(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::vector<.+>(( )?&)?$")), TypeSummaryImplSP( new StringSummaryFormat(stl_summary_flags, "size=${svar%#}"))); cpp_category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP( - new RegularExpression(llvm::StringRef("^std::map<.+> >(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::map<.+> >(( )?&)?$")), TypeSummaryImplSP( new StringSummaryFormat(stl_summary_flags, "size=${svar%#}"))); cpp_category_sp->GetRegexTypeSummariesContainer()->Add( - RegularExpressionSP(new RegularExpression( - llvm::StringRef("^std::(__cxx11::)?list<.+>(( )?&)?$"))), + RegularExpression(llvm::StringRef("^std::(__cxx11::)?list<.+>(( )?&)?$")), TypeSummaryImplSP( new StringSummaryFormat(stl_summary_flags, "size=${svar%#}"))); Index: lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp +++ lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp @@ -582,9 +582,9 @@ 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); Index: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp +++ lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp @@ -446,15 +446,21 @@ llvm::SmallVector 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) { + regex = RegularExpression(llvm::StringRef("^([0-9]+),([0-9]+),([0-9]+)$")); + if (regex.Execute(coord_s, &matches)) + matched = true; + } + if (!matched) { + regex = RegularExpression(llvm::StringRef("^([0-9]+),([0-9]+)$")); + if (regex.Execute(coord_s, &matches)) + matched = true; + } + if (!matched) { + regex = RegularExpression(llvm::StringRef("^([0-9]+)$")); + if (regex.Execute(coord_s, &matches)) + matched = true; + } if (!matched) return false; Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp @@ -332,11 +332,12 @@ 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 = Index: lldb/source/Target/Target.cpp =================================================================== --- lldb/source/Target/Target.cpp +++ lldb/source/Target/Target.cpp @@ -307,14 +307,14 @@ const FileSpecList *containingModules, const FileSpecList *source_file_spec_list, const std::unordered_set &function_names, - RegularExpression &source_regex, bool internal, bool hardware, + RegularExpression &&source_regex, bool internal, bool hardware, LazyBool move_to_nearest_code) { SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( containingModules, source_file_spec_list)); if (move_to_nearest_code == eLazyBoolCalculate) move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo; BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex( - nullptr, source_regex, function_names, + nullptr, std::move(source_regex), function_names, !static_cast(move_to_nearest_code))); return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true); @@ -549,7 +549,7 @@ BreakpointSP Target::CreateFuncRegexBreakpoint( const FileSpecList *containingModules, - const FileSpecList *containingSourceFiles, RegularExpression &func_regex, + const FileSpecList *containingSourceFiles, RegularExpression &&func_regex, lldb::LanguageType requested_language, LazyBool skip_prologue, bool internal, bool hardware) { SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( @@ -558,7 +558,7 @@ ? GetSkipPrologue() : static_cast(skip_prologue); BreakpointResolverSP resolver_sp(new BreakpointResolverName( - nullptr, func_regex, requested_language, 0, skip)); + nullptr, std::move(func_regex), requested_language, 0, skip)); return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true); } Index: lldb/source/Target/ThreadPlanStepInRange.cpp =================================================================== --- lldb/source/Target/ThreadPlanStepInRange.cpp +++ lldb/source/Target/ThreadPlanStepInRange.cpp @@ -315,8 +315,8 @@ auto name_ref = llvm::StringRef::withNullAsEmpty(name); if (!m_avoid_regexp_up) m_avoid_regexp_up.reset(new RegularExpression(name_ref)); - - m_avoid_regexp_up->Compile(name_ref); + else + *m_avoid_regexp_up = RegularExpression(name_ref); } void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) { Index: lldb/source/Utility/RegularExpression.cpp =================================================================== --- lldb/source/Utility/RegularExpression.cpp +++ lldb/source/Utility/RegularExpression.cpp @@ -12,26 +12,43 @@ using namespace lldb_private; -RegularExpression::RegularExpression(llvm::StringRef str) { Compile(str); } +RegularExpression::RegularExpression(llvm::StringRef str) { + m_regex_text = str; +} +// m_regex must not be copied as it contains pointers to RHS m_regex_text. RegularExpression::RegularExpression(const RegularExpression &rhs) - : RegularExpression() { - Compile(rhs.GetText()); + : RegularExpression(rhs.m_regex_text) {} + +// m_regex must not be moved as it contains pointers to RHS m_regex_text. +RegularExpression::RegularExpression(RegularExpression &&rhs) + : RegularExpression(std::move(rhs.m_regex_text)) {} + +// m_regex must not be copied as it contains pointers to RHS m_regex_text. +RegularExpression &RegularExpression::operator=(const RegularExpression &rhs) { + m_regex_text = rhs.m_regex_text; + m_regex = llvm::Regex(); + return *this; } -bool RegularExpression::Compile(llvm::StringRef str) { - m_regex_text = str; - m_regex = llvm::Regex(str); - return IsValid(); +// m_regex must not be moved as it contains pointers to RHS m_regex_text. +RegularExpression &RegularExpression::operator=(RegularExpression &&rhs) { + m_regex_text = std::move(rhs.m_regex_text); + m_regex = llvm::Regex(); + return *this; } bool RegularExpression::Execute( llvm::StringRef str, llvm::SmallVectorImpl *matches) const { + if (!IsValid()) + return false; return m_regex.match(str, matches); } bool RegularExpression::IsValid() const { + if (m_regex.empty()) + m_regex = llvm::Regex(m_regex_text); std::string discarded; return m_regex.isValid(discarded); } @@ -39,6 +56,8 @@ llvm::StringRef RegularExpression::GetText() const { return m_regex_text; } llvm::Error RegularExpression::GetError() const { + if (m_regex.empty()) + m_regex = llvm::Regex(m_regex_text); std::string error; if (!m_regex.isValid(error)) return llvm::make_error(llvm::inconvertibleErrorCode(), Index: llvm/include/llvm/Support/Regex.h =================================================================== --- llvm/include/llvm/Support/Regex.h +++ llvm/include/llvm/Support/Regex.h @@ -58,6 +58,9 @@ /// matching, if any. bool isValid(std::string &Error) const; + // Return whether constructor has been called with no parameters. + bool empty() const { return preg == nullptr; } + /// getNumMatches - In a valid regex, return the number of parenthesized /// matches it contains. The number filled in by match will include this /// many entries plus one for the whole regex (as element 0).