1 //===-- TypeFormat.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/TypeFormat.h"
16 // Other libraries and framework includes
19 #include "lldb/lldb-public.h"
20 #include "lldb/lldb-enumerations.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/DataFormatters/FormatManager.h"
24 #include "lldb/Symbol/CompilerType.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/TypeList.h"
27 #include "lldb/Target/Target.h"
30 using namespace lldb_private;
32 TypeFormatImpl::TypeFormatImpl (const Flags& flags) :
38 TypeFormatImpl::~TypeFormatImpl ()
42 TypeFormatImpl_Format::TypeFormatImpl_Format (lldb::Format f,
43 const TypeFormatImpl::Flags& flags) :
44 TypeFormatImpl(flags),
49 TypeFormatImpl_Format::~TypeFormatImpl_Format ()
54 TypeFormatImpl_Format::FormatObject (ValueObject *valobj,
55 std::string& dest) const
59 if (valobj->CanProvideValue())
61 Value& value(valobj->GetValue());
62 const Value::ContextType context_type = value.GetContextType();
63 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
66 if (context_type == Value::eContextTypeRegisterInfo)
68 const RegisterInfo *reg_info = value.GetRegisterInfo();
72 valobj->GetData(data, error);
76 StreamString reg_sstr;
86 exe_ctx.GetBestExecutionContextScope());
87 dest.swap(reg_sstr.GetString());
92 CompilerType compiler_type = value.GetCompilerType ();
95 // put custom bytes to display in the DataExtractor to override the default value logic
96 if (GetFormat() == eFormatCString)
98 lldb_private::Flags type_flags(compiler_type.GetTypeInfo(NULL)); // disambiguate w.r.t. TypeFormatImpl::Flags
99 if (type_flags.Test(eTypeIsPointer) && !type_flags.Test(eTypeIsObjC))
101 // if we are dumping a pointer as a c-string, get the pointee data as a string
102 TargetSP target_sp(valobj->GetTargetSP());
105 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
107 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
108 Address address(valobj->GetPointerValue());
109 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
110 data.SetData(buffer_sp);
117 valobj->GetData(data, error);
123 compiler_type.DumpTypeValue (&sstr, // The stream to use for display
124 GetFormat(), // Format to display this type with
125 data, // Data to extract from
126 0, // Byte offset into "m_data"
127 valobj->GetByteSize(), // Byte size of item in "m_data"
128 valobj->GetBitfieldBitSize(), // Bitfield bit size
129 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
130 exe_ctx.GetBestExecutionContextScope());
131 // Given that we do not want to set the ValueObject's m_error
132 // for a formatting error (or else we wouldn't be able to reformat
133 // until a next update), an empty string is treated as a "false"
134 // return from here, but that's about as severe as we get
135 // CompilerType::DumpTypeValue() should always return
136 // something, even if that something is an error message
137 if (sstr.GetString().empty())
140 dest.swap(sstr.GetString());
143 return !dest.empty();
150 TypeFormatImpl_Format::GetDescription()
153 sstr.Printf ("%s%s%s%s",
154 FormatManager::GetFormatAsCString (GetFormat()),
155 Cascades() ? "" : " (not cascading)",
156 SkipsPointers() ? " (skip pointers)" : "",
157 SkipsReferences() ? " (skip references)" : "");
158 return sstr.GetString();
161 TypeFormatImpl_EnumType::TypeFormatImpl_EnumType (ConstString type_name,
162 const TypeFormatImpl::Flags& flags) :
163 TypeFormatImpl(flags),
164 m_enum_type(type_name),
169 TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType ()
174 TypeFormatImpl_EnumType::FormatObject (ValueObject *valobj,
175 std::string& dest) const
180 if (!valobj->CanProvideValue())
182 ProcessSP process_sp;
184 void* valobj_key = (process_sp = valobj->GetProcessSP()).get();
186 valobj_key = (target_sp = valobj->GetTargetSP()).get();
188 target_sp = process_sp->GetTarget().shared_from_this();
191 auto iter = m_types.find(valobj_key),
193 CompilerType valobj_enum_type;
196 // probably a redundant check
199 const ModuleList& images(target_sp->GetImages());
202 images.FindTypes(sc, m_enum_type, false, UINT32_MAX, types);
203 if (types.GetSize() == 0)
205 for (lldb::TypeSP type_sp : types.Types())
209 if ( (type_sp->GetForwardCompilerType().GetTypeInfo() & eTypeIsEnumeration) == eTypeIsEnumeration)
211 valobj_enum_type = type_sp->GetFullCompilerType ();
212 m_types.emplace(valobj_key,valobj_enum_type);
218 valobj_enum_type = iter->second;
219 if (valobj_enum_type.IsValid() == false)
223 valobj->GetData(data, error);
226 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
228 valobj_enum_type.DumpTypeValue(&sstr,
235 exe_ctx.GetBestExecutionContextScope());
236 if (!sstr.GetString().empty())
237 dest.swap(sstr.GetString());
238 return !dest.empty();
242 TypeFormatImpl_EnumType::GetDescription()
245 sstr.Printf ("as type %s%s%s%s",
246 m_enum_type.AsCString("<invalid type>"),
247 Cascades() ? "" : " (not cascading)",
248 SkipsPointers() ? " (skip pointers)" : "",
249 SkipsReferences() ? " (skip references)" : "");
250 return sstr.GetString();