[Lldb-commits] [lldb] r247189 - Preparatory work for letting language plugins help the StringPrinter with formatting special characters

Enrico Granata via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 9 13:59:49 PDT 2015


Author: enrico
Date: Wed Sep  9 15:59:49 2015
New Revision: 247189

URL: http://llvm.org/viewvc/llvm-project?rev=247189&view=rev
Log:
Preparatory work for letting language plugins help the StringPrinter with formatting special characters


Modified:
    lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
    lldb/trunk/source/Core/ValueObject.cpp
    lldb/trunk/source/DataFormatters/Cocoa.cpp
    lldb/trunk/source/DataFormatters/StringPrinter.cpp
    lldb/trunk/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp
    lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp

Modified: lldb/trunk/include/lldb/DataFormatters/StringPrinter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/StringPrinter.h?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/StringPrinter.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/StringPrinter.h Wed Sep  9 15:59:49 2015
@@ -14,6 +14,8 @@
 
 #include "lldb/Core/DataExtractor.h"
 
+#include <functional>
+
 namespace lldb_private {
     namespace formatters
     {
@@ -308,13 +310,99 @@ namespace lldb_private {
             bool m_zero_is_terminator;
         };
         
-        template <StringElementType element_type>
-        bool
-        ReadStringAndDumpToStream (const ReadStringAndDumpToStreamOptions& options);
-        
-        template <StringElementType element_type>
-        bool
-        ReadBufferAndDumpToStream (const ReadBufferAndDumpToStreamOptions& options);
+        class StringPrinter
+        {
+        public:
+            // I can't use a std::unique_ptr for this because the Deleter is a template argument there
+            // and I want the same type to represent both pointers I want to free and pointers I don't need
+            // to free - which is what this class essentially is
+            // It's very specialized to the needs of this file, and not suggested for general use
+            template <typename T = uint8_t, typename U = char, typename S = size_t>
+            struct StringPrinterBufferPointer
+            {
+            public:
+                
+                typedef std::function<void(const T*)> Deleter;
+                
+                StringPrinterBufferPointer (std::nullptr_t ptr) :
+                m_data(nullptr),
+                m_size(0),
+                m_deleter()
+                {}
+                
+                StringPrinterBufferPointer(const T* bytes, S size, Deleter deleter = nullptr) :
+                m_data(bytes),
+                m_size(size),
+                m_deleter(deleter)
+                {}
+                
+                StringPrinterBufferPointer(const U* bytes, S size, Deleter deleter = nullptr) :
+                m_data((T*)bytes),
+                m_size(size),
+                m_deleter(deleter)
+                {}
+                
+                StringPrinterBufferPointer(StringPrinterBufferPointer&& rhs) :
+                m_data(rhs.m_data),
+                m_size(rhs.m_size),
+                m_deleter(rhs.m_deleter)
+                {
+                    rhs.m_data = nullptr;
+                }
+                
+                StringPrinterBufferPointer(const StringPrinterBufferPointer& rhs) :
+                m_data(rhs.m_data),
+                m_size(rhs.m_size),
+                m_deleter(rhs.m_deleter)
+                {
+                    rhs.m_data = nullptr; // this is why m_data has to be mutable
+                }
+                
+                const T*
+                GetBytes () const
+                {
+                    return m_data;
+                }
+                
+                const S
+                GetSize () const
+                {
+                    return m_size;
+                }
+                
+                ~StringPrinterBufferPointer ()
+                {
+                    if (m_data && m_deleter)
+                        m_deleter(m_data);
+                    m_data = nullptr;
+                }
+                
+                StringPrinterBufferPointer&
+                operator = (const StringPrinterBufferPointer& rhs)
+                {
+                    if (m_data && m_deleter)
+                        m_deleter(m_data);
+                    m_data = rhs.m_data;
+                    m_size = rhs.m_size;
+                    m_deleter = rhs.m_deleter;
+                    rhs.m_data = nullptr;
+                    return *this;
+                }
+                
+            private:
+                mutable const T* m_data;
+                size_t m_size;
+                Deleter m_deleter;
+            };
+            
+            template <StringElementType element_type>
+            static bool
+            ReadStringAndDumpToStream (const ReadStringAndDumpToStreamOptions& options);
+            
+            template <StringElementType element_type>
+            static bool
+            ReadBufferAndDumpToStream (const ReadBufferAndDumpToStreamOptions& options);
+        };
         
     } // namespace formatters
 } // namespace lldb_private

Modified: lldb/trunk/source/Core/ValueObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObject.cpp?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObject.cpp (original)
+++ lldb/trunk/source/Core/ValueObject.cpp Wed Sep  9 15:59:49 2015
@@ -1601,7 +1601,7 @@ ValueObject::DumpPrintableRepresentation
                 options.SetPrefixToken(0);
                 options.SetQuote('"');
                 options.SetSourceSize(buffer_sp->GetByteSize());
-                lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
+                formatters::StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
                 return !error.Fail();
             }
             

Modified: lldb/trunk/source/DataFormatters/Cocoa.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/Cocoa.cpp?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/Cocoa.cpp (original)
+++ lldb/trunk/source/DataFormatters/Cocoa.cpp Wed Sep  9 15:59:49 2015
@@ -876,7 +876,7 @@ lldb_private::formatters::NSStringSummar
             options.SetNeedsZeroTermination(false);
             options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
             options.SetBinaryZeroIsTerminator(false);
-            return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(options);
         }
         else
         {
@@ -889,7 +889,7 @@ lldb_private::formatters::NSStringSummar
             options.SetNeedsZeroTermination(false);
             options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
             options.SetBinaryZeroIsTerminator(false);
-            return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(options);
         }
     }
     else if (is_inline && has_explicit_length && !is_unicode && !is_path_store && !is_mutable)
@@ -904,7 +904,7 @@ lldb_private::formatters::NSStringSummar
         options.SetQuote('"');
         options.SetSourceSize(explicit_length);
         options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
-        return ReadStringAndDumpToStream<StringElementType::ASCII> (options);
+        return StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII> (options);
     }
     else if (is_unicode)
     {
@@ -935,7 +935,7 @@ lldb_private::formatters::NSStringSummar
         options.SetNeedsZeroTermination(has_explicit_length == false);
         options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
         options.SetBinaryZeroIsTerminator(has_explicit_length == false);
-        return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
+        return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16> (options);
     }
     else if (is_path_store)
     {
@@ -953,7 +953,7 @@ lldb_private::formatters::NSStringSummar
         options.SetNeedsZeroTermination(has_explicit_length == false);
         options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
         options.SetBinaryZeroIsTerminator(has_explicit_length == false);
-        return ReadStringAndDumpToStream<StringElementType::UTF16> (options);
+        return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16> (options);
     }
     else if (is_inline)
     {
@@ -980,9 +980,9 @@ lldb_private::formatters::NSStringSummar
         options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
         options.SetBinaryZeroIsTerminator(!has_explicit_length);
         if (has_explicit_length)
-            return ReadStringAndDumpToStream<StringElementType::UTF8>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(options);
         else
-            return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(options);
     }
     else
     {
@@ -999,7 +999,7 @@ lldb_private::formatters::NSStringSummar
         options.SetStream(&stream);
         options.SetSourceSize(explicit_length);
         options.SetIgnoreMaxLength(summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryUncapped);
-        return ReadStringAndDumpToStream<StringElementType::ASCII>(options);
+        return StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(options);
     }
 }
 

Modified: lldb/trunk/source/DataFormatters/StringPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/StringPrinter.cpp?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/StringPrinter.cpp (original)
+++ lldb/trunk/source/DataFormatters/StringPrinter.cpp Wed Sep  9 15:59:49 2015
@@ -19,99 +19,16 @@
 #include "llvm/Support/ConvertUTF.h"
 
 #include <ctype.h>
-#include <functional>
 #include <locale>
 
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::formatters;
 
-// I can't use a std::unique_ptr for this because the Deleter is a template argument there
-// and I want the same type to represent both pointers I want to free and pointers I don't need
-// to free - which is what this class essentially is
-// It's very specialized to the needs of this file, and not suggested for general use
-template <typename T = uint8_t, typename U = char, typename S = size_t>
-struct StringPrinterBufferPointer
-{
-public:
-    
-    typedef std::function<void(const T*)> Deleter;
-    
-    StringPrinterBufferPointer (std::nullptr_t ptr) :
-    m_data(nullptr),
-    m_size(0),
-    m_deleter()
-    {}
-    
-    StringPrinterBufferPointer(const T* bytes, S size, Deleter deleter = nullptr) :
-    m_data(bytes),
-    m_size(size),
-    m_deleter(deleter)
-    {}
-    
-    StringPrinterBufferPointer(const U* bytes, S size, Deleter deleter = nullptr) :
-    m_data((T*)bytes),
-    m_size(size),
-    m_deleter(deleter)
-    {}
-    
-    StringPrinterBufferPointer(StringPrinterBufferPointer&& rhs) :
-    m_data(rhs.m_data),
-    m_size(rhs.m_size),
-    m_deleter(rhs.m_deleter)
-    {
-        rhs.m_data = nullptr;
-    }
-    
-    StringPrinterBufferPointer(const StringPrinterBufferPointer& rhs) :
-    m_data(rhs.m_data),
-    m_size(rhs.m_size),
-    m_deleter(rhs.m_deleter)
-    {
-        rhs.m_data = nullptr; // this is why m_data has to be mutable
-    }
-    
-    const T*
-    GetBytes () const
-    {
-        return m_data;
-    }
-    
-    const S
-    GetSize () const
-    {
-        return m_size;
-    }
-    
-    ~StringPrinterBufferPointer ()
-    {
-        if (m_data && m_deleter)
-            m_deleter(m_data);
-        m_data = nullptr;
-    }
-    
-    StringPrinterBufferPointer&
-    operator = (const StringPrinterBufferPointer& rhs)
-    {
-        if (m_data && m_deleter)
-            m_deleter(m_data);
-        m_data = rhs.m_data;
-        m_size = rhs.m_size;
-        m_deleter = rhs.m_deleter;
-        rhs.m_data = nullptr;
-        return *this;
-    }
-    
-private:
-    mutable const T* m_data;
-    size_t m_size;
-    Deleter m_deleter;
-};
-
 // we define this for all values of type but only implement it for those we care about
 // that's good because we get linker errors for any unsupported type
 template <StringElementType type>
-static StringPrinterBufferPointer<>
+static StringPrinter::StringPrinterBufferPointer<>
 GetPrintableImpl(uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next);
 
 // mimic isprint() for Unicode codepoints
@@ -142,10 +59,10 @@ isprint(char32_t codepoint)
 }
 
 template <>
-StringPrinterBufferPointer<>
+StringPrinter::StringPrinterBufferPointer<>
 GetPrintableImpl<StringElementType::ASCII> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
 {
-    StringPrinterBufferPointer<> retval = {nullptr};
+    StringPrinter::StringPrinterBufferPointer<> retval = {nullptr};
     
     switch (*buffer)
     {
@@ -212,10 +129,10 @@ ConvertUTF8ToCodePoint (unsigned char c0
 }
 
 template <>
-StringPrinterBufferPointer<>
+StringPrinter::StringPrinterBufferPointer<>
 GetPrintableImpl<StringElementType::UTF8> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
 {
-    StringPrinterBufferPointer<> retval {nullptr};
+    StringPrinter::StringPrinterBufferPointer<> retval {nullptr};
     
     unsigned utf8_encoded_len = getNumBytesForUTF8(*buffer);
     
@@ -309,7 +226,7 @@ GetPrintableImpl<StringElementType::UTF8
 // Given a sequence of bytes, this function returns:
 // a sequence of bytes to actually print out + a length
 // the following unscanned position of the buffer is in next
-static StringPrinterBufferPointer<>
+static StringPrinter::StringPrinterBufferPointer<>
 GetPrintable(StringElementType type, uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
 {
     if (!buffer)
@@ -464,7 +381,7 @@ namespace formatters
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options)
+StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
     Error my_error;
@@ -608,7 +525,7 @@ ReadUTFBufferAndDumpToStream (const Read
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options)
+StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF8>(options,
                                               nullptr);
@@ -616,7 +533,7 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options)
+StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF16>(options,
                                                ConvertUTF16toUTF8);
@@ -624,7 +541,7 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options)
+StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options)
 {
     return ReadUTFBufferAndDumpToStream<UTF32>(options,
                                                ConvertUTF32toUTF8);
@@ -632,7 +549,7 @@ ReadStringAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options)
+StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 
@@ -641,7 +558,7 @@ ReadBufferAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options)
+StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options)
 {
     // treat ASCII the same as UTF8
     // FIXME: can we optimize ASCII some more?
@@ -650,7 +567,7 @@ ReadBufferAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options)
+StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 
@@ -659,7 +576,7 @@ ReadBufferAndDumpToStream<StringElementT
 
 template <>
 bool
-ReadBufferAndDumpToStream<StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options)
+StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options)
 {
     assert(options.GetStream() && "need a Stream to print the string to");
 

Modified: lldb/trunk/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp (original)
+++ lldb/trunk/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp Wed Sep  9 15:59:49 2015
@@ -55,7 +55,7 @@ lldb_private::formatters::Char16StringSu
     options.SetStream(&stream);
     options.SetPrefixToken('u');
     
-    if (!ReadStringAndDumpToStream<StringElementType::UTF16>(options))
+    if (!StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(options))
     {
         stream.Printf("Summary Unavailable");
         return true;
@@ -82,7 +82,7 @@ lldb_private::formatters::Char32StringSu
     options.SetStream(&stream);
     options.SetPrefixToken('U');
     
-    if (!ReadStringAndDumpToStream<StringElementType::UTF32>(options))
+    if (!StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(options))
     {
         stream.Printf("Summary Unavailable");
         return true;
@@ -125,11 +125,11 @@ lldb_private::formatters::WCharStringSum
     switch (wchar_size)
     {
         case 8:
-            return ReadStringAndDumpToStream<StringElementType::UTF8>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(options);
         case 16:
-            return ReadStringAndDumpToStream<StringElementType::UTF16>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(options);
         case 32:
-            return ReadStringAndDumpToStream<StringElementType::UTF32>(options);
+            return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(options);
         default:
             stream.Printf("size for wchar_t is not valid");
             return true;
@@ -160,7 +160,7 @@ lldb_private::formatters::Char16SummaryP
     options.SetSourceSize(1);
     options.SetBinaryZeroIsTerminator(false);
     
-    return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
+    return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
 }
 
 bool
@@ -186,7 +186,7 @@ lldb_private::formatters::Char32SummaryP
     options.SetSourceSize(1);
     options.SetBinaryZeroIsTerminator(false);
     
-    return ReadBufferAndDumpToStream<StringElementType::UTF32>(options);
+    return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(options);
 }
 
 bool
@@ -207,5 +207,5 @@ lldb_private::formatters::WCharSummaryPr
     options.SetSourceSize(1);
     options.SetBinaryZeroIsTerminator(false);
     
-    return ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
+    return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(options);
 }

Modified: lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp?rev=247189&r1=247188&r2=247189&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp (original)
+++ lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp Wed Sep  9 15:59:49 2015
@@ -581,15 +581,15 @@ lldb_private::formatters::LibcxxWStringS
     switch (wchar_t_size)
     {
         case 1:
-            lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF8>(options);
+            StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF8>(options);
             break;
             
         case 2:
-            lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF16>(options);
+            lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF16>(options);
             break;
             
         case 4:
-            lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF32>(options);
+            lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::UTF32>(options);
             break;
             
         default:
@@ -630,7 +630,7 @@ lldb_private::formatters::LibcxxStringSu
     options.SetQuote('"');
     options.SetSourceSize(size);
     options.SetBinaryZeroIsTerminator(false);
-    lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
+    StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
     
     return true;
 }




More information about the lldb-commits mailing list