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"
15 #include "lldb/lldb-enumerations.h"
16 #include "lldb/lldb-public.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/DataFormatters/ValueObjectPrinter.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Target/StackFrame.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/StreamString.h"
28 using namespace lldb_private;
30 TypeSummaryOptions::TypeSummaryOptions()
31 : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
33 TypeSummaryOptions::TypeSummaryOptions(const TypeSummaryOptions &rhs)
34 : m_lang(rhs.m_lang), m_capping(rhs.m_capping) {}
36 TypeSummaryOptions &TypeSummaryOptions::
37 operator=(const TypeSummaryOptions &rhs) {
39 m_capping = rhs.m_capping;
43 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
45 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
49 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
55 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
60 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
61 : m_flags(flags), m_kind(kind) {}
63 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags &flags,
64 const char *format_cstr)
65 : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
66 SetSummaryString(format_cstr);
69 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
71 if (format_cstr && format_cstr[0]) {
72 m_format_str = format_cstr;
73 m_error = FormatEntity::Parse(format_cstr, m_format);
80 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
81 const TypeSummaryOptions &options) {
83 retval.assign("NULL ValueObject");
88 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
90 StackFrame *frame = exe_ctx.GetFramePtr();
92 sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
95 ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
96 printer.PrintChildrenOneLiner(HideNames(valobj));
97 retval = s.GetString();
100 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
101 &sc.line_entry.range.GetBaseAddress(), valobj,
103 retval.assign(s.GetString());
106 retval.assign("error: summary string parsing error");
112 std::string StringSummaryFormat::GetDescription() {
115 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
116 m_error.Fail() ? " error: " : "",
117 m_error.Fail() ? m_error.AsCString() : "",
118 Cascades() ? "" : " (not cascading)",
119 !DoesPrintChildren(nullptr) ? "" : " (show children)",
120 !DoesPrintValue(nullptr) ? " (hide value)" : "",
121 IsOneLiner() ? " (one-line printout)" : "",
122 SkipsPointers() ? " (skip pointers)" : "",
123 SkipsReferences() ? " (skip references)" : "",
124 HideNames(nullptr) ? " (hide member names)" : "");
125 return sstr.GetString();
128 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
129 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
130 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
131 m_description(description ? description : "") {}
133 bool CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
135 const TypeSummaryOptions &options) {
138 if (!m_impl || !m_impl(*valobj, stream, options))
140 dest = stream.GetString();
144 std::string CXXFunctionSummaryFormat::GetDescription() {
146 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
147 !DoesPrintChildren(nullptr) ? "" : " (show children)",
148 !DoesPrintValue(nullptr) ? " (hide value)" : "",
149 IsOneLiner() ? " (one-line printout)" : "",
150 SkipsPointers() ? " (skip pointers)" : "",
151 SkipsReferences() ? " (skip references)" : "",
152 HideNames(nullptr) ? " (hide member names)" : "",
153 m_description.c_str());
154 return sstr.GetString();
157 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
158 const char *function_name,
159 const char *python_script)
160 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
161 m_python_script(), m_script_function_sp() {
163 m_function_name.assign(function_name);
165 m_python_script.assign(python_script);
168 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
169 const TypeSummaryOptions &options) {
173 TargetSP target_sp(valobj->GetTargetSP());
176 retval.assign("error: no target");
180 ScriptInterpreter *script_interpreter =
181 target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
183 if (!script_interpreter) {
184 retval.assign("error: no ScriptInterpreter");
188 return script_interpreter->GetScriptedSummary(
189 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
193 std::string ScriptSummaryFormat::GetDescription() {
195 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
196 !DoesPrintChildren(nullptr) ? "" : " (show children)",
197 !DoesPrintValue(nullptr) ? " (hide value)" : "",
198 IsOneLiner() ? " (one-line printout)" : "",
199 SkipsPointers() ? " (skip pointers)" : "",
200 SkipsReferences() ? " (skip references)" : "",
201 HideNames(nullptr) ? " (hide member names)" : "");
202 if (m_python_script.empty()) {
203 if (m_function_name.empty()) {
204 sstr.PutCString("no backing script");
206 sstr.PutCString(m_function_name);
209 sstr.PutCString(m_python_script);
211 return sstr.GetString();