1 //===-- TypeSummary.cpp ----------------------------------------*- C++ -*-===//
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()
30 : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
32 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
34 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
38 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
44 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
49 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
50 : m_flags(flags), m_kind(kind) {}
52 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags &flags,
53 const char *format_cstr)
54 : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
55 SetSummaryString(format_cstr);
58 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
60 if (format_cstr && format_cstr[0]) {
61 m_format_str = format_cstr;
62 m_error = FormatEntity::Parse(format_cstr, m_format);
69 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
70 const TypeSummaryOptions &options) {
72 retval.assign("NULL ValueObject");
77 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
79 StackFrame *frame = exe_ctx.GetFramePtr();
81 sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
84 ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
85 printer.PrintChildrenOneLiner(HideNames(valobj));
86 retval = s.GetString();
89 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
90 &sc.line_entry.range.GetBaseAddress(), valobj,
92 retval.assign(s.GetString());
95 retval.assign("error: summary string parsing error");
101 std::string StringSummaryFormat::GetDescription() {
104 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
105 m_error.Fail() ? " error: " : "",
106 m_error.Fail() ? m_error.AsCString() : "",
107 Cascades() ? "" : " (not cascading)",
108 !DoesPrintChildren(nullptr) ? "" : " (show children)",
109 !DoesPrintValue(nullptr) ? " (hide value)" : "",
110 IsOneLiner() ? " (one-line printout)" : "",
111 SkipsPointers() ? " (skip pointers)" : "",
112 SkipsReferences() ? " (skip references)" : "",
113 HideNames(nullptr) ? " (hide member names)" : "");
114 return sstr.GetString();
117 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
118 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
119 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
120 m_description(description ? description : "") {}
122 bool CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
124 const TypeSummaryOptions &options) {
127 if (!m_impl || !m_impl(*valobj, stream, options))
129 dest = stream.GetString();
133 std::string CXXFunctionSummaryFormat::GetDescription() {
135 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
136 !DoesPrintChildren(nullptr) ? "" : " (show children)",
137 !DoesPrintValue(nullptr) ? " (hide value)" : "",
138 IsOneLiner() ? " (one-line printout)" : "",
139 SkipsPointers() ? " (skip pointers)" : "",
140 SkipsReferences() ? " (skip references)" : "",
141 HideNames(nullptr) ? " (hide member names)" : "",
142 m_description.c_str());
143 return sstr.GetString();
146 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
147 const char *function_name,
148 const char *python_script)
149 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
150 m_python_script(), m_script_function_sp() {
152 m_function_name.assign(function_name);
154 m_python_script.assign(python_script);
157 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
158 const TypeSummaryOptions &options) {
162 TargetSP target_sp(valobj->GetTargetSP());
165 retval.assign("error: no target");
169 ScriptInterpreter *script_interpreter =
170 target_sp->GetDebugger().GetScriptInterpreter();
172 if (!script_interpreter) {
173 retval.assign("error: no ScriptInterpreter");
177 return script_interpreter->GetScriptedSummary(
178 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
182 std::string ScriptSummaryFormat::GetDescription() {
184 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
185 !DoesPrintChildren(nullptr) ? "" : " (show children)",
186 !DoesPrintValue(nullptr) ? " (hide value)" : "",
187 IsOneLiner() ? " (one-line printout)" : "",
188 SkipsPointers() ? " (skip pointers)" : "",
189 SkipsReferences() ? " (skip references)" : "",
190 HideNames(nullptr) ? " (hide member names)" : "");
191 if (m_python_script.empty()) {
192 if (m_function_name.empty()) {
193 sstr.PutCString("no backing script");
195 sstr.PutCString(m_function_name);
198 sstr.PutCString(m_python_script);
200 return sstr.GetString();