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 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
17 #include "lldb/lldb-public.h"
18 #include "lldb/lldb-enumerations.h"
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Core/Timer.h"
23 #include "lldb/DataFormatters/FormatManager.h"
24 #include "lldb/DataFormatters/TypeFormat.h"
25 #include "lldb/Interpreter/CommandInterpreter.h"
26 #include "lldb/Symbol/ClangASTType.h"
27 #include "lldb/Symbol/TypeList.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
32 using namespace lldb_private;
34 TypeFormatImpl::TypeFormatImpl (const Flags& flags) :
40 TypeFormatImpl::~TypeFormatImpl ()
44 TypeFormatImpl_Format::TypeFormatImpl_Format (lldb::Format f,
45 const TypeFormatImpl::Flags& flags) :
46 TypeFormatImpl(flags),
51 TypeFormatImpl_Format::~TypeFormatImpl_Format ()
56 TypeFormatImpl_Format::FormatObject (ValueObject *valobj,
57 std::string& dest) const
61 if (valobj->CanProvideValue())
63 Value& value(valobj->GetValue());
64 const Value::ContextType context_type = value.GetContextType();
65 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
68 if (context_type == Value::eContextTypeRegisterInfo)
70 const RegisterInfo *reg_info = value.GetRegisterInfo();
74 valobj->GetData(data, error);
78 StreamString reg_sstr;
88 exe_ctx.GetBestExecutionContextScope());
89 dest.swap(reg_sstr.GetString());
94 ClangASTType clang_type = value.GetClangType ();
97 // put custom bytes to display in the DataExtractor to override the default value logic
98 if (GetFormat() == eFormatCString)
100 lldb_private::Flags type_flags(clang_type.GetTypeInfo(NULL)); // disambiguate w.r.t. TypeFormatImpl::Flags
101 if (type_flags.Test(eTypeIsPointer) && !type_flags.Test(eTypeIsObjC))
103 // if we are dumping a pointer as a c-string, get the pointee data as a string
104 TargetSP target_sp(valobj->GetTargetSP());
107 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
109 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
110 Address address(valobj->GetPointerValue());
111 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
112 data.SetData(buffer_sp);
119 valobj->GetData(data, error);
125 clang_type.DumpTypeValue (&sstr, // The stream to use for display
126 GetFormat(), // Format to display this type with
127 data, // Data to extract from
128 0, // Byte offset into "m_data"
129 valobj->GetByteSize(), // Byte size of item in "m_data"
130 valobj->GetBitfieldBitSize(), // Bitfield bit size
131 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
132 exe_ctx.GetBestExecutionContextScope());
133 // Given that we do not want to set the ValueObject's m_error
134 // for a formatting error (or else we wouldn't be able to reformat
135 // until a next update), an empty string is treated as a "false"
136 // return from here, but that's about as severe as we get
137 // ClangASTType::DumpTypeValue() should always return
138 // something, even if that something is an error message
139 if (sstr.GetString().empty())
142 dest.swap(sstr.GetString());
145 return !dest.empty();
152 TypeFormatImpl_Format::GetDescription()
155 sstr.Printf ("%s%s%s%s",
156 FormatManager::GetFormatAsCString (GetFormat()),
157 Cascades() ? "" : " (not cascading)",
158 SkipsPointers() ? " (skip pointers)" : "",
159 SkipsReferences() ? " (skip references)" : "");
160 return sstr.GetString();
163 TypeFormatImpl_EnumType::TypeFormatImpl_EnumType (ConstString type_name,
164 const TypeFormatImpl::Flags& flags) :
165 TypeFormatImpl(flags),
166 m_enum_type(type_name),
171 TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType ()
176 TypeFormatImpl_EnumType::FormatObject (ValueObject *valobj,
177 std::string& dest) const
182 if (!valobj->CanProvideValue())
184 ProcessSP process_sp;
186 void* valobj_key = (process_sp = valobj->GetProcessSP()).get();
188 valobj_key = (target_sp = valobj->GetTargetSP()).get();
190 target_sp = process_sp->GetTarget().shared_from_this();
193 auto iter = m_types.find(valobj_key),
195 ClangASTType valobj_enum_type;
198 // probably a redundant check
201 const ModuleList& images(target_sp->GetImages());
204 images.FindTypes(sc, m_enum_type, false, UINT32_MAX, types);
205 if (types.GetSize() == 0)
207 for (lldb::TypeSP type_sp : types.Types())
211 if ( (type_sp->GetClangForwardType().GetTypeInfo() & eTypeIsEnumeration) == eTypeIsEnumeration)
213 valobj_enum_type = type_sp->GetClangFullType();
214 m_types.emplace(valobj_key,valobj_enum_type);
220 valobj_enum_type = iter->second;
221 if (valobj_enum_type.IsValid() == false)
225 valobj->GetData(data, error);
228 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
230 valobj_enum_type.DumpTypeValue(&sstr,
237 exe_ctx.GetBestExecutionContextScope());
238 if (!sstr.GetString().empty())
239 dest.swap(sstr.GetString());
240 return !dest.empty();
244 TypeFormatImpl_EnumType::GetDescription()
247 sstr.Printf ("as type %s%s%s%s",
248 m_enum_type.AsCString("<invalid type>"),
249 Cascades() ? "" : " (not cascading)",
250 SkipsPointers() ? " (skip pointers)" : "",
251 SkipsReferences() ? " (skip references)" : "");
252 return sstr.GetString();