]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeSummary.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeSummary.cpp
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/lldb-python.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/Timer.h"
25 #include "lldb/DataFormatters/TypeSummary.h"
26 #include "lldb/DataFormatters/ValueObjectPrinter.h"
27 #include "lldb/Interpreter/CommandInterpreter.h"
28 #include "lldb/Symbol/ClangASTType.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31
32 #include "lldb/Host/Host.h"
33
34 using namespace lldb;
35 using namespace lldb_private;
36
37 TypeSummaryImpl::TypeSummaryImpl (const TypeSummaryImpl::Flags& flags) :
38 m_flags(flags)
39 {
40 }
41
42
43 StringSummaryFormat::StringSummaryFormat (const TypeSummaryImpl::Flags& flags,
44                                           const char *format_cstr) :
45 TypeSummaryImpl(flags),
46 m_format()
47 {
48     if (format_cstr)
49         m_format.assign(format_cstr);
50 }
51
52 bool
53 StringSummaryFormat::FormatObject (ValueObject *valobj,
54                                    std::string& retval)
55 {
56     if (!valobj)
57     {
58         retval.assign("NULL ValueObject");
59         return false;
60     }
61     
62     StreamString s;
63     ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
64     SymbolContext sc;
65     StackFrame *frame = exe_ctx.GetFramePtr();
66     if (frame)
67         sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
68     
69     if (IsOneLiner())
70     {
71         ValueObjectPrinter printer(valobj,&s,DumpValueObjectOptions());
72         printer.PrintChildrenOneLiner(HideNames());
73         retval.assign(s.GetData());
74         return true;
75     }
76     else
77     {
78         if (Debugger::FormatPrompt(m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, valobj))
79         {
80             retval.assign(s.GetString());
81             return true;
82         }
83         else
84         {
85             retval.assign("error: summary string parsing error");
86             return false;
87         }
88     }
89 }
90
91 std::string
92 StringSummaryFormat::GetDescription ()
93 {
94     StreamString sstr;
95     
96     sstr.Printf ("`%s`%s%s%s%s%s%s%s",      m_format.c_str(),
97                  Cascades() ? "" : " (not cascading)",
98                  !DoesPrintChildren() ? "" : " (show children)",
99                  !DoesPrintValue() ? " (hide value)" : "",
100                  IsOneLiner() ? " (one-line printout)" : "",
101                  SkipsPointers() ? " (skip pointers)" : "",
102                  SkipsReferences() ? " (skip references)" : "",
103                  HideNames() ? " (hide member names)" : "");
104     return sstr.GetString();
105 }
106
107 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat (const TypeSummaryImpl::Flags& flags,
108                                                     Callback impl,
109                                                     const char* description) :
110 TypeSummaryImpl(flags),
111 m_impl(impl),
112 m_description(description ? description : "")
113 {
114 }
115
116 bool
117 CXXFunctionSummaryFormat::FormatObject (ValueObject *valobj,
118                                         std::string& dest)
119 {
120     dest.clear();
121     StreamString stream;
122     if (!m_impl || m_impl(*valobj,stream) == false)
123         return false;
124     dest.assign(stream.GetData());
125     return true;
126 }
127
128 std::string
129 CXXFunctionSummaryFormat::GetDescription ()
130 {
131     StreamString sstr;
132     sstr.Printf ("`%s (%p) `%s%s%s%s%s%s%s",      m_description.c_str(),m_impl,
133                  Cascades() ? "" : " (not cascading)",
134                  !DoesPrintChildren() ? "" : " (show children)",
135                  !DoesPrintValue() ? " (hide value)" : "",
136                  IsOneLiner() ? " (one-line printout)" : "",
137                  SkipsPointers() ? " (skip pointers)" : "",
138                  SkipsReferences() ? " (skip references)" : "",
139                  HideNames() ? " (hide member names)" : "");
140     return sstr.GetString();
141 }
142
143 #ifndef LLDB_DISABLE_PYTHON
144
145
146 ScriptSummaryFormat::ScriptSummaryFormat (const TypeSummaryImpl::Flags& flags,
147                                           const char * function_name,
148                                           const char * python_script) :
149 TypeSummaryImpl(flags),
150 m_function_name(),
151 m_python_script(),
152 m_script_function_sp()
153 {
154     if (function_name)
155         m_function_name.assign(function_name);
156     if (python_script)
157         m_python_script.assign(python_script);
158 }
159
160 bool
161 ScriptSummaryFormat::FormatObject (ValueObject *valobj,
162                                    std::string& retval)
163 {
164     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
165     
166     if (!valobj)
167         return false;
168     
169     Host::SetCrashDescriptionWithFormat("[Python summary] Name: %s - Function: %s",
170                                         valobj->GetName().AsCString("unknown"),
171                                         m_function_name.c_str());
172
173     TargetSP target_sp(valobj->GetTargetSP());
174     
175     if (!target_sp)
176     {
177         retval.assign("error: no target");
178         return false;
179     }
180     
181     ScriptInterpreter *script_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
182     
183     if (!script_interpreter)
184     {
185         retval.assign("error: no ScriptInterpreter");
186         return false;
187     }
188     
189     return script_interpreter->GetScriptedSummary(m_function_name.c_str(),
190                                                   valobj->GetSP(),
191                                                   m_script_function_sp,
192                                                   retval);
193     
194 }
195
196 std::string
197 ScriptSummaryFormat::GetDescription ()
198 {
199     StreamString sstr;
200     sstr.Printf ("%s%s%s%s%s%s%s\n%s",       Cascades() ? "" : " (not cascading)",
201                  !DoesPrintChildren() ? "" : " (show children)",
202                  !DoesPrintValue() ? " (hide value)" : "",
203                  IsOneLiner() ? " (one-line printout)" : "",
204                  SkipsPointers() ? " (skip pointers)" : "",
205                  SkipsReferences() ? " (skip references)" : "",
206                  HideNames() ? " (hide member names)" : "",
207                  m_python_script.c_str());
208     return sstr.GetString();
209     
210 }
211
212 #endif // #ifndef LLDB_DISABLE_PYTHON