1//===-- TypeSummary.cpp ----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/DataFormatters/TypeSummary.h"
11
12// C Includes
13
14// C++ Includes
15
16// Other libraries and framework includes
17
18// Project includes
19#include "lldb/lldb-public.h"
20#include "lldb/lldb-enumerations.h"
21
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"
30
31using namespace lldb;
32using namespace lldb_private;
33
34TypeSummaryOptions::TypeSummaryOptions () :
35    m_lang(eLanguageTypeUnknown),
36    m_capping(eTypeSummaryCapped)
37{}
38
39TypeSummaryOptions::TypeSummaryOptions (const TypeSummaryOptions& rhs) :
40    m_lang(rhs.m_lang),
41    m_capping(rhs.m_capping)
42{}
43
44TypeSummaryOptions&
45TypeSummaryOptions::operator = (const TypeSummaryOptions& rhs)
46{
47    m_lang = rhs.m_lang;
48    m_capping = rhs.m_capping;
49    return *this;
50}
51
52lldb::LanguageType
53TypeSummaryOptions::GetLanguage () const
54{
55    return m_lang;
56}
57
58lldb::TypeSummaryCapping
59TypeSummaryOptions::GetCapping () const
60{
61    return m_capping;
62}
63
64TypeSummaryOptions&
65TypeSummaryOptions::SetLanguage (lldb::LanguageType lang)
66{
67    m_lang = lang;
68    return *this;
69}
70
71TypeSummaryOptions&
72TypeSummaryOptions::SetCapping (lldb::TypeSummaryCapping cap)
73{
74    m_capping = cap;
75    return *this;
76}
77
78TypeSummaryImpl::TypeSummaryImpl (Kind kind,
79                                  const TypeSummaryImpl::Flags& flags) :
80    m_flags(flags),
81    m_kind(kind)
82{
83}
84
85
86StringSummaryFormat::StringSummaryFormat (const TypeSummaryImpl::Flags& flags,
87                                          const char *format_cstr) :
88    TypeSummaryImpl(Kind::eSummaryString,flags),
89    m_format_str()
90{
91    SetSummaryString (format_cstr);
92}
93
94void
95StringSummaryFormat::SetSummaryString (const char* format_cstr)
96{
97    m_format.Clear();
98    if (format_cstr && format_cstr[0])
99    {
100        m_format_str = format_cstr;
101        m_error = FormatEntity::Parse(format_cstr, m_format);
102    }
103    else
104    {
105        m_format_str.clear();
106        m_error.Clear();
107    }
108}
109
110
111
112bool
113StringSummaryFormat::FormatObject (ValueObject *valobj,
114                                   std::string& retval,
115                                   const TypeSummaryOptions& options)
116{
117    if (!valobj)
118    {
119        retval.assign("NULL ValueObject");
120        return false;
121    }
122
123    StreamString s;
124    ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
125    SymbolContext sc;
126    StackFrame *frame = exe_ctx.GetFramePtr();
127    if (frame)
128        sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
129
130    if (IsOneLiner())
131    {
132        ValueObjectPrinter printer(valobj,&s,DumpValueObjectOptions());
133        printer.PrintChildrenOneLiner(HideNames(valobj));
134        retval.assign(s.GetData());
135        return true;
136    }
137    else
138    {
139        if (FormatEntity::Format(m_format, s, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), valobj, false, false))
140        {
141            retval.assign(s.GetString());
142            return true;
143        }
144        else
145        {
146            retval.assign("error: summary string parsing error");
147            return false;
148        }
149    }
150}
151
152std::string
153StringSummaryFormat::GetDescription ()
154{
155    StreamString sstr;
156
157    sstr.Printf ("`%s`%s%s%s%s%s%s%s%s%s",
158                 m_format_str.c_str(),
159                 m_error.Fail() ? " error: " : "",
160                 m_error.Fail() ? m_error.AsCString() : "",
161                 Cascades() ? "" : " (not cascading)",
162                 !DoesPrintChildren(nullptr) ? "" : " (show children)",
163                 !DoesPrintValue(nullptr) ? " (hide value)" : "",
164                 IsOneLiner() ? " (one-line printout)" : "",
165                 SkipsPointers() ? " (skip pointers)" : "",
166                 SkipsReferences() ? " (skip references)" : "",
167                 HideNames(nullptr) ? " (hide member names)" : "");
168    return sstr.GetString();
169}
170
171CXXFunctionSummaryFormat::CXXFunctionSummaryFormat (const TypeSummaryImpl::Flags& flags,
172                                                    Callback impl,
173                                                    const char* description) :
174    TypeSummaryImpl(Kind::eCallback,flags),
175    m_impl(impl),
176    m_description(description ? description : "")
177{
178}
179
180bool
181CXXFunctionSummaryFormat::FormatObject (ValueObject *valobj,
182                                        std::string& dest,
183                                        const TypeSummaryOptions& options)
184{
185    dest.clear();
186    StreamString stream;
187    if (!m_impl || m_impl(*valobj,stream,options) == false)
188        return false;
189    dest.assign(stream.GetData());
190    return true;
191}
192
193std::string
194CXXFunctionSummaryFormat::GetDescription ()
195{
196    StreamString sstr;
197    sstr.Printf ("%s%s%s%s%s%s%s %s",
198                 Cascades() ? "" : " (not cascading)",
199                 !DoesPrintChildren(nullptr) ? "" : " (show children)",
200                 !DoesPrintValue(nullptr) ? " (hide value)" : "",
201                 IsOneLiner() ? " (one-line printout)" : "",
202                 SkipsPointers() ? " (skip pointers)" : "",
203                 SkipsReferences() ? " (skip references)" : "",
204                 HideNames(nullptr) ? " (hide member names)" : "",
205                 m_description.c_str());
206    return sstr.GetString();
207}
208
209ScriptSummaryFormat::ScriptSummaryFormat (const TypeSummaryImpl::Flags& flags,
210                                          const char * function_name,
211                                          const char * python_script) :
212    TypeSummaryImpl(Kind::eScript,flags),
213    m_function_name(),
214    m_python_script(),
215    m_script_function_sp()
216{
217    if (function_name)
218        m_function_name.assign(function_name);
219    if (python_script)
220        m_python_script.assign(python_script);
221}
222
223bool
224ScriptSummaryFormat::FormatObject (ValueObject *valobj,
225                                   std::string& retval,
226                                   const TypeSummaryOptions& options)
227{
228    if (!valobj)
229        return false;
230
231    TargetSP target_sp(valobj->GetTargetSP());
232
233    if (!target_sp)
234    {
235        retval.assign("error: no target");
236        return false;
237    }
238
239    ScriptInterpreter *script_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
240
241    if (!script_interpreter)
242    {
243        retval.assign("error: no ScriptInterpreter");
244        return false;
245    }
246
247    return script_interpreter->GetScriptedSummary(m_function_name.c_str(),
248                                                  valobj->GetSP(),
249                                                  m_script_function_sp,
250                                                  options,
251                                                  retval);
252
253}
254
255std::string
256ScriptSummaryFormat::GetDescription ()
257{
258    StreamString sstr;
259    sstr.Printf ("%s%s%s%s%s%s%s\n%s",       Cascades() ? "" : " (not cascading)",
260                 !DoesPrintChildren(nullptr) ? "" : " (show children)",
261                 !DoesPrintValue(nullptr) ? " (hide value)" : "",
262                 IsOneLiner() ? " (one-line printout)" : "",
263                 SkipsPointers() ? " (skip pointers)" : "",
264                 SkipsReferences() ? " (skip references)" : "",
265                 HideNames(nullptr) ? " (hide member names)" : "",
266                 m_python_script.c_str());
267    return sstr.GetString();
268
269}
270