[Lldb-commits] [lldb] r197874 - Centralize the code for GetValueAsCString() in TypeFormatImpl (the implementing class of "type format ...")

Enrico Granata egranata at apple.com
Sun Dec 22 01:24:24 PST 2013


Author: enrico
Date: Sun Dec 22 03:24:22 2013
New Revision: 197874

URL: http://llvm.org/viewvc/llvm-project?rev=197874&view=rev
Log:
Centralize the code for GetValueAsCString() in TypeFormatImpl (the implementing class of "type format ...")
TypeFormatImpl used to just wrap a Format (and Flags for matching), and then ValueObject itself would do the printing deed
With this checkin, the responsibility of generating a value string is centralized in the data formatter (as it should, and already is for summaries) 

This change is good practice per se, and should also enable us to extend the type format mechanism in a cleaner way


Modified:
    lldb/trunk/include/lldb/Core/ValueObject.h
    lldb/trunk/include/lldb/DataFormatters/TypeFormat.h
    lldb/trunk/source/Core/ValueObject.cpp
    lldb/trunk/source/DataFormatters/TypeFormat.cpp

Modified: lldb/trunk/include/lldb/Core/ValueObject.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ValueObject.h?rev=197874&r1=197873&r2=197874&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ValueObject.h (original)
+++ lldb/trunk/include/lldb/Core/ValueObject.h Sun Dec 22 03:24:22 2013
@@ -499,6 +499,10 @@ public:
     GetValueAsCString ();
     
     virtual bool
+    GetValueAsCString (const lldb_private::TypeFormatImpl& format,
+                       std::string& destination);
+
+    bool
     GetValueAsCString (lldb::Format format,
                        std::string& destination);
     

Modified: lldb/trunk/include/lldb/DataFormatters/TypeFormat.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/TypeFormat.h?rev=197874&r1=197873&r2=197874&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/TypeFormat.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/TypeFormat.h Sun Dec 22 03:24:22 2013
@@ -204,6 +204,13 @@ namespace lldb_private {
             return m_my_revision;
         }
         
+        // we are using a ValueObject* instead of a ValueObjectSP because we do not need to hold on to this for
+        // extended periods of time and we trust the ValueObject to stay around for as long as it is required
+        // for us to generate its value
+        bool
+        FormatObject (ValueObject *valobj,
+                      std::string& dest) const;
+        
         std::string
         GetDescription();
         

Modified: lldb/trunk/source/Core/ValueObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObject.cpp?rev=197874&r1=197873&r2=197874&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObject.cpp (original)
+++ lldb/trunk/source/Core/ValueObject.cpp Sun Dec 22 03:24:22 2013
@@ -1389,99 +1389,33 @@ ValueObject::GetObjectDescription ()
 }
 
 bool
-ValueObject::GetValueAsCString (lldb::Format format,
+ValueObject::GetValueAsCString (const lldb_private::TypeFormatImpl& format,
                                 std::string& destination)
 {
-    if (GetClangType().IsAggregateType () == false && UpdateValueIfNeeded(false))
-    {
-        const Value::ContextType context_type = m_value.GetContextType();
-        
-        if (context_type == Value::eContextTypeRegisterInfo)
-        {
-            const RegisterInfo *reg_info = m_value.GetRegisterInfo();
-            if (reg_info)
-            {
-                ExecutionContext exe_ctx (GetExecutionContextRef());
-                
-                StreamString reg_sstr;
-                m_data.Dump (&reg_sstr,
-                             0,
-                             format,
-                             reg_info->byte_size,
-                             1,
-                             UINT32_MAX,
-                             LLDB_INVALID_ADDRESS,
-                             0,
-                             0,
-                             exe_ctx.GetBestExecutionContextScope());
-                destination.swap(reg_sstr.GetString());
-            }
-        }
-        else
-        {
-            ClangASTType clang_type = GetClangType ();
-            if (clang_type)
-            {
-                 // put custom bytes to display in this DataExtractor to override the default value logic
-                lldb_private::DataExtractor special_format_data;
-                if (format == eFormatCString)
-                {
-                    Flags type_flags(clang_type.GetTypeInfo(NULL));
-                    if (type_flags.Test(ClangASTType::eTypeIsPointer) && !type_flags.Test(ClangASTType::eTypeIsObjC))
-                    {
-                        // if we are dumping a pointer as a c-string, get the pointee data as a string
-                        TargetSP target_sp(GetTargetSP());
-                        if (target_sp)
-                        {
-                            size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
-                            Error error;
-                            DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
-                            Address address(GetPointerValue());
-                            if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
-                                special_format_data.SetData(buffer_sp);
-                        }
-                    }
-                }
-                
-                StreamString sstr;
-                ExecutionContext exe_ctx (GetExecutionContextRef());
-                clang_type.DumpTypeValue (&sstr,                         // The stream to use for display
-                                          format,                        // Format to display this type with
-                                          special_format_data.GetByteSize() ?
-                                          special_format_data: m_data,   // Data to extract from
-                                          0,                             // Byte offset into "m_data"
-                                          GetByteSize(),                 // Byte size of item in "m_data"
-                                          GetBitfieldBitSize(),          // Bitfield bit size
-                                          GetBitfieldBitOffset(),        // Bitfield bit offset
-                                          exe_ctx.GetBestExecutionContextScope());
-                // Don't set the m_error to anything here otherwise
-                // we won't be able to re-format as anything else. The
-                // code for ClangASTType::DumpTypeValue() should always
-                // return something, even if that something contains
-                // an error messsage. "m_error" is used to detect errors
-                // when reading the valid object, not for formatting errors.
-                if (sstr.GetString().empty())
-                    destination.clear();
-                else
-                    destination.swap(sstr.GetString());
-            }
-        }
-        return !destination.empty();
-    }
+    if (UpdateValueIfNeeded(false))
+        return format.FormatObject(this,destination);
     else
         return false;
 }
 
+bool
+ValueObject::GetValueAsCString (lldb::Format format,
+                                std::string& destination)
+{
+    return GetValueAsCString(TypeFormatImpl(format),destination);
+}
+
 const char *
 ValueObject::GetValueAsCString ()
 {
     if (UpdateValueIfNeeded(true))
     {
+        lldb::TypeFormatImplSP format_sp;
         lldb::Format my_format = GetFormat();
         if (my_format == lldb::eFormatDefault)
         {
             if (m_type_format_sp)
-                my_format = m_type_format_sp->GetFormat();
+                format_sp = m_type_format_sp;
             else
             {
                 if (m_is_bitfield_for_scalar)
@@ -1504,7 +1438,9 @@ ValueObject::GetValueAsCString ()
         if (my_format != m_last_format || m_value_str.empty())
         {
             m_last_format = my_format;
-            if (GetValueAsCString(my_format, m_value_str))
+            if (!format_sp)
+                format_sp.reset(new TypeFormatImpl(my_format));
+            if (GetValueAsCString(*format_sp.get(), m_value_str))
             {
                 if (!m_value_did_change && m_old_value_valid)
                 {

Modified: lldb/trunk/source/DataFormatters/TypeFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/TypeFormat.cpp?rev=197874&r1=197873&r2=197874&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/TypeFormat.cpp (original)
+++ lldb/trunk/source/DataFormatters/TypeFormat.cpp Sun Dec 22 03:24:22 2013
@@ -38,6 +38,94 @@ m_format (f)
 {
 }
 
+bool
+TypeFormatImpl::FormatObject (ValueObject *valobj,
+                              std::string& dest) const
+{
+    if (!valobj)
+        return false;
+    if (valobj->GetClangType().IsAggregateType () == false)
+    {
+        const Value& value(valobj->GetValue());
+        const Value::ContextType context_type = value.GetContextType();
+        ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
+        DataExtractor data;
+        
+        if (context_type == Value::eContextTypeRegisterInfo)
+        {
+            const RegisterInfo *reg_info = value.GetRegisterInfo();
+            if (reg_info)
+            {
+                valobj->GetData(data);
+                
+                StreamString reg_sstr;
+                data.Dump (&reg_sstr,
+                           0,
+                           GetFormat(),
+                           reg_info->byte_size,
+                           1,
+                           UINT32_MAX,
+                           LLDB_INVALID_ADDRESS,
+                           0,
+                           0,
+                           exe_ctx.GetBestExecutionContextScope());
+                dest.swap(reg_sstr.GetString());
+            }
+        }
+        else
+        {
+            ClangASTType clang_type = valobj->GetClangType ();
+            if (clang_type)
+            {
+                // put custom bytes to display in the DataExtractor to override the default value logic
+                if (GetFormat() == eFormatCString)
+                {
+                    lldb_private::Flags type_flags(clang_type.GetTypeInfo(NULL)); // disambiguate w.r.t. TypeFormatImpl::Flags
+                    if (type_flags.Test(ClangASTType::eTypeIsPointer) && !type_flags.Test(ClangASTType::eTypeIsObjC))
+                    {
+                        // if we are dumping a pointer as a c-string, get the pointee data as a string
+                        TargetSP target_sp(valobj->GetTargetSP());
+                        if (target_sp)
+                        {
+                            size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
+                            Error error;
+                            DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
+                            Address address(valobj->GetPointerValue());
+                            if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
+                                data.SetData(buffer_sp);
+                        }
+                    }
+                }
+                else
+                    valobj->GetData(data);
+                
+                StreamString sstr;
+                clang_type.DumpTypeValue (&sstr,                         // The stream to use for display
+                                          GetFormat(),                  // Format to display this type with
+                                          data,                         // Data to extract from
+                                          0,                             // Byte offset into "m_data"
+                                          valobj->GetByteSize(),                 // Byte size of item in "m_data"
+                                          valobj->GetBitfieldBitSize(),          // Bitfield bit size
+                                          valobj->GetBitfieldBitOffset(),        // Bitfield bit offset
+                                          exe_ctx.GetBestExecutionContextScope());
+                // Given that we do not want to set the ValueObject's m_error
+                // for a formatting error (or else we wouldn't be able to reformat
+                // until a next update), an empty string is treated as a "false"
+                // return from here, but that's about as severe as we get
+                // ClangASTType::DumpTypeValue() should always return
+                // something, even if that something is an error message
+                if (sstr.GetString().empty())
+                    dest.clear();
+                else
+                    dest.swap(sstr.GetString());
+            }
+        }
+        return !dest.empty();
+    }
+    else
+        return false;
+}
+
 std::string
 TypeFormatImpl::GetDescription()
 {





More information about the lldb-commits mailing list