1 //===-- TypeSummary.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/TypeSummary.h"
16 // Other libraries and framework includes
19 #include "lldb/lldb-enumerations.h"
20 #include "lldb/lldb-public.h"
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Core/ValueObject.h"
25 #include "lldb/DataFormatters/ValueObjectPrinter.h"
26 #include "lldb/Interpreter/CommandInterpreter.h"
27 #include "lldb/Symbol/CompilerType.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
32 using namespace lldb_private;
34 TypeSummaryOptions::TypeSummaryOptions()
35 : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
37 TypeSummaryOptions::TypeSummaryOptions(const TypeSummaryOptions &rhs)
38 : m_lang(rhs.m_lang), m_capping(rhs.m_capping) {}
40 TypeSummaryOptions &TypeSummaryOptions::
41 operator=(const TypeSummaryOptions &rhs) {
43 m_capping = rhs.m_capping;
47 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
49 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
53 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
59 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
64 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
65 : m_flags(flags), m_kind(kind) {}
67 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags &flags,
68 const char *format_cstr)
69 : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
70 SetSummaryString(format_cstr);
73 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
75 if (format_cstr && format_cstr[0]) {
76 m_format_str = format_cstr;
77 m_error = FormatEntity::Parse(format_cstr, m_format);
84 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
85 const TypeSummaryOptions &options) {
87 retval.assign("NULL ValueObject");
92 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
94 StackFrame *frame = exe_ctx.GetFramePtr();
96 sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
99 ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
100 printer.PrintChildrenOneLiner(HideNames(valobj));
101 retval = s.GetString();
104 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
105 &sc.line_entry.range.GetBaseAddress(), valobj,
107 retval.assign(s.GetString());
110 retval.assign("error: summary string parsing error");
116 std::string StringSummaryFormat::GetDescription() {
119 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
120 m_error.Fail() ? " error: " : "",
121 m_error.Fail() ? m_error.AsCString() : "",
122 Cascades() ? "" : " (not cascading)",
123 !DoesPrintChildren(nullptr) ? "" : " (show children)",
124 !DoesPrintValue(nullptr) ? " (hide value)" : "",
125 IsOneLiner() ? " (one-line printout)" : "",
126 SkipsPointers() ? " (skip pointers)" : "",
127 SkipsReferences() ? " (skip references)" : "",
128 HideNames(nullptr) ? " (hide member names)" : "");
129 return sstr.GetString();
132 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
133 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
134 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
135 m_description(description ? description : "") {}
137 bool CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
139 const TypeSummaryOptions &options) {
142 if (!m_impl || m_impl(*valobj, stream, options) == false)
144 dest = stream.GetString();
148 std::string CXXFunctionSummaryFormat::GetDescription() {
150 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
151 !DoesPrintChildren(nullptr) ? "" : " (show children)",
152 !DoesPrintValue(nullptr) ? " (hide value)" : "",
153 IsOneLiner() ? " (one-line printout)" : "",
154 SkipsPointers() ? " (skip pointers)" : "",
155 SkipsReferences() ? " (skip references)" : "",
156 HideNames(nullptr) ? " (hide member names)" : "",
157 m_description.c_str());
158 return sstr.GetString();
161 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
162 const char *function_name,
163 const char *python_script)
164 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
165 m_python_script(), m_script_function_sp() {
167 m_function_name.assign(function_name);
169 m_python_script.assign(python_script);
172 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
173 const TypeSummaryOptions &options) {
177 TargetSP target_sp(valobj->GetTargetSP());
180 retval.assign("error: no target");
184 ScriptInterpreter *script_interpreter =
185 target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
187 if (!script_interpreter) {
188 retval.assign("error: no ScriptInterpreter");
192 return script_interpreter->GetScriptedSummary(
193 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
197 std::string ScriptSummaryFormat::GetDescription() {
199 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
200 !DoesPrintChildren(nullptr) ? "" : " (show children)",
201 !DoesPrintValue(nullptr) ? " (hide value)" : "",
202 IsOneLiner() ? " (one-line printout)" : "",
203 SkipsPointers() ? " (skip pointers)" : "",
204 SkipsReferences() ? " (skip references)" : "",
205 HideNames(nullptr) ? " (hide member names)" : "");
206 if (m_python_script.empty()) {
207 if (m_function_name.empty()) {
208 sstr.PutCString("no backing script");
210 sstr.PutCString(m_function_name);
213 sstr.PutCString(m_python_script);
215 return sstr.GetString();