1 //===-- TypeSummary.cpp ---------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/DataFormatters/TypeSummary.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-public.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/DataFormatters/ValueObjectPrinter.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 #include "lldb/Symbol/CompilerType.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/StreamString.h"
27 using namespace lldb_private
;
29 TypeSummaryOptions::TypeSummaryOptions() = default;
31 lldb::LanguageType
TypeSummaryOptions::GetLanguage() const { return m_lang
; }
33 lldb::TypeSummaryCapping
TypeSummaryOptions::GetCapping() const {
37 TypeSummaryOptions
&TypeSummaryOptions::SetLanguage(lldb::LanguageType lang
) {
43 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap
) {
48 TypeSummaryImpl::TypeSummaryImpl(Kind kind
, const TypeSummaryImpl::Flags
&flags
)
49 : m_flags(flags
), m_kind(kind
) {}
51 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags
&flags
,
52 const char *format_cstr
)
53 : TypeSummaryImpl(Kind::eSummaryString
, flags
), m_format_str() {
54 SetSummaryString(format_cstr
);
57 void StringSummaryFormat::SetSummaryString(const char *format_cstr
) {
59 if (format_cstr
&& format_cstr
[0]) {
60 m_format_str
= format_cstr
;
61 m_error
= FormatEntity::Parse(format_cstr
, m_format
);
68 bool StringSummaryFormat::FormatObject(ValueObject
*valobj
, std::string
&retval
,
69 const TypeSummaryOptions
&options
) {
71 retval
.assign("NULL ValueObject");
76 ExecutionContext
exe_ctx(valobj
->GetExecutionContextRef());
78 StackFrame
*frame
= exe_ctx
.GetFramePtr();
80 sc
= frame
->GetSymbolContext(lldb::eSymbolContextEverything
);
83 ValueObjectPrinter
printer(valobj
, &s
, DumpValueObjectOptions());
84 printer
.PrintChildrenOneLiner(HideNames(valobj
));
85 retval
= std::string(s
.GetString());
88 if (FormatEntity::Format(m_format
, s
, &sc
, &exe_ctx
,
89 &sc
.line_entry
.range
.GetBaseAddress(), valobj
,
91 retval
.assign(std::string(s
.GetString()));
94 retval
.assign("error: summary string parsing error");
100 std::string
StringSummaryFormat::GetDescription() {
103 sstr
.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str
.c_str(),
104 m_error
.Fail() ? " error: " : "",
105 m_error
.Fail() ? m_error
.AsCString() : "",
106 Cascades() ? "" : " (not cascading)",
107 !DoesPrintChildren(nullptr) ? "" : " (show children)",
108 !DoesPrintValue(nullptr) ? " (hide value)" : "",
109 IsOneLiner() ? " (one-line printout)" : "",
110 SkipsPointers() ? " (skip pointers)" : "",
111 SkipsReferences() ? " (skip references)" : "",
112 HideNames(nullptr) ? " (hide member names)" : "");
113 return std::string(sstr
.GetString());
116 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
117 const TypeSummaryImpl::Flags
&flags
, Callback impl
, const char *description
)
118 : TypeSummaryImpl(Kind::eCallback
, flags
), m_impl(impl
),
119 m_description(description
? description
: "") {}
121 bool CXXFunctionSummaryFormat::FormatObject(ValueObject
*valobj
,
123 const TypeSummaryOptions
&options
) {
126 if (!m_impl
|| !m_impl(*valobj
, stream
, options
))
128 dest
= std::string(stream
.GetString());
132 std::string
CXXFunctionSummaryFormat::GetDescription() {
134 sstr
.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
135 !DoesPrintChildren(nullptr) ? "" : " (show children)",
136 !DoesPrintValue(nullptr) ? " (hide value)" : "",
137 IsOneLiner() ? " (one-line printout)" : "",
138 SkipsPointers() ? " (skip pointers)" : "",
139 SkipsReferences() ? " (skip references)" : "",
140 HideNames(nullptr) ? " (hide member names)" : "",
141 m_description
.c_str());
142 return std::string(sstr
.GetString());
145 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags
&flags
,
146 const char *function_name
,
147 const char *python_script
)
148 : TypeSummaryImpl(Kind::eScript
, flags
), m_function_name(),
149 m_python_script(), m_script_function_sp() {
151 m_function_name
.assign(function_name
);
153 m_python_script
.assign(python_script
);
156 bool ScriptSummaryFormat::FormatObject(ValueObject
*valobj
, std::string
&retval
,
157 const TypeSummaryOptions
&options
) {
161 TargetSP
target_sp(valobj
->GetTargetSP());
164 retval
.assign("error: no target");
168 ScriptInterpreter
*script_interpreter
=
169 target_sp
->GetDebugger().GetScriptInterpreter();
171 if (!script_interpreter
) {
172 retval
.assign("error: no ScriptInterpreter");
176 return script_interpreter
->GetScriptedSummary(
177 m_function_name
.c_str(), valobj
->GetSP(), m_script_function_sp
, options
,
181 std::string
ScriptSummaryFormat::GetDescription() {
183 sstr
.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
184 !DoesPrintChildren(nullptr) ? "" : " (show children)",
185 !DoesPrintValue(nullptr) ? " (hide value)" : "",
186 IsOneLiner() ? " (one-line printout)" : "",
187 SkipsPointers() ? " (skip pointers)" : "",
188 SkipsReferences() ? " (skip references)" : "",
189 HideNames(nullptr) ? " (hide member names)" : "");
190 if (m_python_script
.empty()) {
191 if (m_function_name
.empty()) {
192 sstr
.PutCString("no backing script");
194 sstr
.PutCString(m_function_name
);
197 sstr
.PutCString(m_python_script
);
199 return std::string(sstr
.GetString());