diff options
Diffstat (limited to 'include/lldb/DataFormatters')
-rw-r--r-- | include/lldb/DataFormatters/FormattersContainer.h | 123 | ||||
-rw-r--r-- | include/lldb/DataFormatters/StringPrinter.h | 237 | ||||
-rw-r--r-- | include/lldb/DataFormatters/TypeCategory.h | 2 |
3 files changed, 117 insertions, 245 deletions
diff --git a/include/lldb/DataFormatters/FormattersContainer.h b/include/lldb/DataFormatters/FormattersContainer.h index 9d7d37343ac2..de2edb103151 100644 --- a/include/lldb/DataFormatters/FormattersContainer.h +++ b/include/lldb/DataFormatters/FormattersContainer.h @@ -47,7 +47,7 @@ static inline ConstString GetValidTypeName_Impl(ConstString type) { return type; std::string type_cstr(type.AsCString()); - lldb_utility::StringLexer type_lexer(type_cstr); + StringLexer type_lexer(type_cstr); type_lexer.AdvanceIf("class "); type_lexer.AdvanceIf("enum "); @@ -65,9 +65,9 @@ template <typename KeyType, typename ValueType> class FormattersContainer; template <typename KeyType, typename ValueType> class FormatMap { public: typedef typename ValueType::SharedPointer ValueSP; - typedef std::map<KeyType, ValueSP> MapType; + typedef std::vector<std::pair<KeyType, ValueSP>> MapType; typedef typename MapType::iterator MapIterator; - typedef std::function<bool(KeyType, const ValueSP &)> ForEachCallback; + typedef std::function<bool(const KeyType &, const ValueSP &)> ForEachCallback; FormatMap(IFormatChangeListener *lst) : m_map(), m_map_mutex(), listener(lst) {} @@ -79,20 +79,22 @@ public: entry->GetRevision() = 0; std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - m_map[name] = entry; + Delete(name); + m_map.emplace_back(std::move(name), std::move(entry)); if (listener) listener->Changed(); } - bool Delete(KeyType name) { + bool Delete(const KeyType &name) { std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - MapIterator iter = m_map.find(name); - if (iter == m_map.end()) - return false; - m_map.erase(name); - if (listener) - listener->Changed(); - return true; + for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter) + if (iter->first == name) { + m_map.erase(iter); + if (listener) + listener->Changed(); + return true; + } + return false; } void Clear() { @@ -102,22 +104,22 @@ public: listener->Changed(); } - bool Get(KeyType name, ValueSP &entry) { + bool Get(const KeyType &name, ValueSP &entry) { std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - MapIterator iter = m_map.find(name); - if (iter == m_map.end()) - return false; - entry = iter->second; - return true; + for (const auto &pos : m_map) + if (pos.first == name) { + entry = pos.second; + return true; + } + return false; } void ForEach(ForEachCallback callback) { if (callback) { std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - MapIterator pos, end = m_map.end(); - for (pos = m_map.begin(); pos != end; pos++) { - KeyType type = pos->first; - if (!callback(type, pos->second)) + for (const auto &pos : m_map) { + const KeyType &type = pos.first; + if (!callback(type, pos.second)) break; } } @@ -127,28 +129,17 @@ public: ValueSP GetValueAtIndex(size_t index) { std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - MapIterator iter = m_map.begin(); - MapIterator end = m_map.end(); - while (index > 0) { - iter++; - index--; - if (end == iter) - return ValueSP(); - } - return iter->second; + if (index >= m_map.size()) + return ValueSP(); + return m_map[index].second; } + // If caller holds the mutex we could return a reference without copy ctor. KeyType GetKeyAtIndex(size_t index) { std::lock_guard<std::recursive_mutex> guard(m_map_mutex); - MapIterator iter = m_map.begin(); - MapIterator end = m_map.end(); - while (index > 0) { - iter++; - index--; - if (end == iter) - return KeyType(); - } - return iter->first; + if (index >= m_map.size()) + return {}; + return m_map[index].first; } protected: @@ -171,8 +162,8 @@ protected: public: typedef typename BackEndType::MapType MapType; typedef typename MapType::iterator MapIterator; - typedef typename MapType::key_type MapKeyType; - typedef typename MapType::mapped_type MapValueType; + typedef KeyType MapKeyType; + typedef std::shared_ptr<ValueType> MapValueType; typedef typename BackEndType::ForEachCallback ForEachCallback; typedef typename std::shared_ptr<FormattersContainer<KeyType, ValueType>> SharedPointer; @@ -182,8 +173,8 @@ public: 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<KeyType *>(nullptr)); + void Add(MapKeyType type, const MapValueType &entry) { + Add_Impl(std::move(type), entry, static_cast<KeyType *>(nullptr)); } bool Delete(ConstString type) { @@ -233,9 +224,9 @@ protected: 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 +238,12 @@ protected: return m_format_map.Delete(type); } - bool Delete_Impl(ConstString type, lldb::RegularExpressionSP *dummy) { + bool Delete_Impl(ConstString type, RegularExpression *dummy) { std::lock_guard<std::recursive_mutex> 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(); @@ -282,24 +273,23 @@ protected: } 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) { + RegularExpression regex = m_format_map.GetKeyAtIndex(index); + if (regex == RegularExpression()) return lldb::TypeNameSpecifierImplSP(); return lldb::TypeNameSpecifierImplSP( - new TypeNameSpecifierImpl(regex->GetText().str().c_str(), true)); + 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<std::recursive_mutex> 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)) { - value = pos->second; + // Patterns are matched in reverse-chronological order. + for (const auto &pos : llvm::reverse(m_format_map.map())) { + const RegularExpression ®ex = pos.first; + if (regex.Execute(key_str)) { + value = pos.second; return true; } } @@ -307,13 +297,12 @@ protected: } bool GetExact_Impl(ConstString key, MapValueType &value, - lldb::RegularExpressionSP *dummy) { + RegularExpression *dummy) { std::lock_guard<std::recursive_mutex> 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()) { - value = pos->second; + for (const auto &pos : m_format_map.map()) { + const RegularExpression ®ex = pos.first; + if (regex.GetText() == key.GetStringRef()) { + value = pos.second; return true; } } diff --git a/include/lldb/DataFormatters/StringPrinter.h b/include/lldb/DataFormatters/StringPrinter.h index 41d5edd88021..43b92019e6fd 100644 --- a/include/lldb/DataFormatters/StringPrinter.h +++ b/include/lldb/DataFormatters/StringPrinter.h @@ -24,238 +24,121 @@ public: enum class GetPrintableElementType { ASCII, UTF8 }; - class ReadStringAndDumpToStreamOptions { + class DumpToStreamOptions { public: - ReadStringAndDumpToStreamOptions() - : m_location(0), m_process_sp(), m_stream(nullptr), m_prefix_token(), - m_suffix_token(), m_quote('"'), m_source_size(0), - m_needs_zero_termination(true), m_escape_non_printables(true), - m_ignore_max_length(false), m_zero_is_terminator(true), - m_language_type(lldb::eLanguageTypeUnknown) {} + DumpToStreamOptions() = default; - ReadStringAndDumpToStreamOptions(ValueObject &valobj); - - ReadStringAndDumpToStreamOptions &SetLocation(uint64_t l) { - m_location = l; - return *this; - } - - uint64_t GetLocation() const { return m_location; } - - ReadStringAndDumpToStreamOptions &SetProcessSP(lldb::ProcessSP p) { - m_process_sp = p; - return *this; - } - - lldb::ProcessSP GetProcessSP() const { return m_process_sp; } - - ReadStringAndDumpToStreamOptions &SetStream(Stream *s) { - m_stream = s; - return *this; - } + void SetStream(Stream *s) { m_stream = s; } Stream *GetStream() const { return m_stream; } - ReadStringAndDumpToStreamOptions &SetPrefixToken(const std::string &p) { - m_prefix_token = p; - return *this; - } + void SetPrefixToken(const std::string &p) { m_prefix_token = p; } - ReadStringAndDumpToStreamOptions &SetPrefixToken(std::nullptr_t) { - m_prefix_token.clear(); - return *this; - } + void SetPrefixToken(std::nullptr_t) { m_prefix_token.clear(); } const char *GetPrefixToken() const { return m_prefix_token.c_str(); } - ReadStringAndDumpToStreamOptions &SetSuffixToken(const std::string &p) { - m_suffix_token = p; - return *this; - } + void SetSuffixToken(const std::string &p) { m_suffix_token = p; } - ReadStringAndDumpToStreamOptions &SetSuffixToken(std::nullptr_t) { - m_suffix_token.clear(); - return *this; - } + void SetSuffixToken(std::nullptr_t) { m_suffix_token.clear(); } const char *GetSuffixToken() const { return m_suffix_token.c_str(); } - ReadStringAndDumpToStreamOptions &SetQuote(char q) { - m_quote = q; - return *this; - } + void SetQuote(char q) { m_quote = q; } char GetQuote() const { return m_quote; } - ReadStringAndDumpToStreamOptions &SetSourceSize(uint32_t s) { - m_source_size = s; - return *this; - } + void SetSourceSize(uint32_t s) { m_source_size = s; } uint32_t GetSourceSize() const { return m_source_size; } - ReadStringAndDumpToStreamOptions &SetNeedsZeroTermination(bool z) { - m_needs_zero_termination = z; - return *this; - } + void SetNeedsZeroTermination(bool z) { m_needs_zero_termination = z; } bool GetNeedsZeroTermination() const { return m_needs_zero_termination; } - ReadStringAndDumpToStreamOptions &SetBinaryZeroIsTerminator(bool e) { - m_zero_is_terminator = e; - return *this; - } + void SetBinaryZeroIsTerminator(bool e) { m_zero_is_terminator = e; } bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; } - ReadStringAndDumpToStreamOptions &SetEscapeNonPrintables(bool e) { - m_escape_non_printables = e; - return *this; - } + void SetEscapeNonPrintables(bool e) { m_escape_non_printables = e; } bool GetEscapeNonPrintables() const { return m_escape_non_printables; } - ReadStringAndDumpToStreamOptions &SetIgnoreMaxLength(bool e) { - m_ignore_max_length = e; - return *this; - } + void SetIgnoreMaxLength(bool e) { m_ignore_max_length = e; } bool GetIgnoreMaxLength() const { return m_ignore_max_length; } - ReadStringAndDumpToStreamOptions &SetLanguage(lldb::LanguageType l) { - m_language_type = l; - return *this; - } - - lldb::LanguageType GetLanguage() const + void SetLanguage(lldb::LanguageType l) { m_language_type = l; } - { - return m_language_type; - } + lldb::LanguageType GetLanguage() const { return m_language_type; } private: - uint64_t m_location; - lldb::ProcessSP m_process_sp; - Stream *m_stream; + /// The used output stream. + Stream *m_stream = nullptr; + /// String that should be printed before the heading quote character. std::string m_prefix_token; + /// String that should be printed after the trailing quote character. std::string m_suffix_token; - char m_quote; - uint32_t m_source_size; - bool m_needs_zero_termination; - bool m_escape_non_printables; - bool m_ignore_max_length; - bool m_zero_is_terminator; - lldb::LanguageType m_language_type; + /// The quote character that should surround the string. + char m_quote = '"'; + /// The length of the memory region that should be dumped in bytes. + uint32_t m_source_size = 0; + bool m_needs_zero_termination = true; + /// True iff non-printable characters should be escaped when dumping + /// them to the stream. + bool m_escape_non_printables = true; + /// True iff the max-string-summary-length setting of the target should + /// be ignored. + bool m_ignore_max_length = false; + /// True iff a zero bytes ('\0') should terminate the memory region that + /// is being dumped. + bool m_zero_is_terminator = true; + /// The language that the generated string literal is supposed to be valid + /// for. This changes for example what and how certain characters are + /// escaped. + /// For example, printing the a string containing only a quote (") char + /// with eLanguageTypeC would escape the quote character. + lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown; }; - class ReadBufferAndDumpToStreamOptions { + class ReadStringAndDumpToStreamOptions : public DumpToStreamOptions { public: - ReadBufferAndDumpToStreamOptions() - : m_data(), m_stream(nullptr), m_prefix_token(), m_suffix_token(), - m_quote('"'), m_source_size(0), m_escape_non_printables(true), - m_zero_is_terminator(true), m_is_truncated(false), - m_language_type(lldb::eLanguageTypeUnknown) {} + ReadStringAndDumpToStreamOptions() = default; - ReadBufferAndDumpToStreamOptions(ValueObject &valobj); - - ReadBufferAndDumpToStreamOptions( - const ReadStringAndDumpToStreamOptions &options); - - ReadBufferAndDumpToStreamOptions &SetData(DataExtractor d) { - m_data = d; - return *this; - } - - lldb_private::DataExtractor GetData() const { return m_data; } - - ReadBufferAndDumpToStreamOptions &SetStream(Stream *s) { - m_stream = s; - return *this; - } - - Stream *GetStream() const { return m_stream; } - - ReadBufferAndDumpToStreamOptions &SetPrefixToken(const std::string &p) { - m_prefix_token = p; - return *this; - } - - ReadBufferAndDumpToStreamOptions &SetPrefixToken(std::nullptr_t) { - m_prefix_token.clear(); - return *this; - } - - const char *GetPrefixToken() const { return m_prefix_token.c_str(); } - - ReadBufferAndDumpToStreamOptions &SetSuffixToken(const std::string &p) { - m_suffix_token = p; - return *this; - } + ReadStringAndDumpToStreamOptions(ValueObject &valobj); - ReadBufferAndDumpToStreamOptions &SetSuffixToken(std::nullptr_t) { - m_suffix_token.clear(); - return *this; - } + void SetLocation(uint64_t l) { m_location = l; } - const char *GetSuffixToken() const { return m_suffix_token.c_str(); } + uint64_t GetLocation() const { return m_location; } - ReadBufferAndDumpToStreamOptions &SetQuote(char q) { - m_quote = q; - return *this; - } + void SetProcessSP(lldb::ProcessSP p) { m_process_sp = p; } - char GetQuote() const { return m_quote; } + lldb::ProcessSP GetProcessSP() const { return m_process_sp; } - ReadBufferAndDumpToStreamOptions &SetSourceSize(uint32_t s) { - m_source_size = s; - return *this; - } + private: + uint64_t m_location = 0; + lldb::ProcessSP m_process_sp; + }; - uint32_t GetSourceSize() const { return m_source_size; } + class ReadBufferAndDumpToStreamOptions : public DumpToStreamOptions { + public: + ReadBufferAndDumpToStreamOptions() = default; - ReadBufferAndDumpToStreamOptions &SetEscapeNonPrintables(bool e) { - m_escape_non_printables = e; - return *this; - } + ReadBufferAndDumpToStreamOptions(ValueObject &valobj); - bool GetEscapeNonPrintables() const { return m_escape_non_printables; } + ReadBufferAndDumpToStreamOptions( + const ReadStringAndDumpToStreamOptions &options); - ReadBufferAndDumpToStreamOptions &SetBinaryZeroIsTerminator(bool e) { - m_zero_is_terminator = e; - return *this; - } + void SetData(DataExtractor d) { m_data = d; } - bool GetBinaryZeroIsTerminator() const { return m_zero_is_terminator; } + lldb_private::DataExtractor GetData() const { return m_data; } - ReadBufferAndDumpToStreamOptions &SetIsTruncated(bool t) { - m_is_truncated = t; - return *this; - } + void SetIsTruncated(bool t) { m_is_truncated = t; } bool GetIsTruncated() const { return m_is_truncated; } - - ReadBufferAndDumpToStreamOptions &SetLanguage(lldb::LanguageType l) { - m_language_type = l; - return *this; - } - - lldb::LanguageType GetLanguage() const - - { - return m_language_type; - } - private: DataExtractor m_data; - Stream *m_stream; - std::string m_prefix_token; - std::string m_suffix_token; - char m_quote; - uint32_t m_source_size; - bool m_escape_non_printables; - bool m_zero_is_terminator; - bool m_is_truncated; - lldb::LanguageType m_language_type; + bool m_is_truncated = false; }; // I can't use a std::unique_ptr for this because the Deleter is a template diff --git a/include/lldb/DataFormatters/TypeCategory.h b/include/lldb/DataFormatters/TypeCategory.h index bdb393abd848..a5438226bbbb 100644 --- a/include/lldb/DataFormatters/TypeCategory.h +++ b/include/lldb/DataFormatters/TypeCategory.h @@ -26,7 +26,7 @@ namespace lldb_private { template <typename FormatterImpl> class FormatterContainerPair { public: typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer; - typedef FormattersContainer<lldb::RegularExpressionSP, FormatterImpl> + typedef FormattersContainer<RegularExpression, FormatterImpl> RegexMatchContainer; typedef typename ExactMatchContainer::MapType ExactMatchMap; |