]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeSummary.cpp
Merge lld trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.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/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-enumerations.h"
20 #include "lldb/lldb-public.h"
21
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/ValueObject.h"
24 #include "lldb/DataFormatters/ValueObjectPrinter.h"
25 #include "lldb/Interpreter/CommandInterpreter.h"
26 #include "lldb/Symbol/CompilerType.h"
27 #include "lldb/Target/StackFrame.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Utility/StreamString.h"
30
31 using namespace lldb;
32 using namespace lldb_private;
33
34 TypeSummaryOptions::TypeSummaryOptions()
35     : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
36
37 TypeSummaryOptions::TypeSummaryOptions(const TypeSummaryOptions &rhs)
38     : m_lang(rhs.m_lang), m_capping(rhs.m_capping) {}
39
40 TypeSummaryOptions &TypeSummaryOptions::
41 operator=(const TypeSummaryOptions &rhs) {
42   m_lang = rhs.m_lang;
43   m_capping = rhs.m_capping;
44   return *this;
45 }
46
47 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
48
49 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
50   return m_capping;
51 }
52
53 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
54   m_lang = lang;
55   return *this;
56 }
57
58 TypeSummaryOptions &
59 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
60   m_capping = cap;
61   return *this;
62 }
63
64 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
65     : m_flags(flags), m_kind(kind) {}
66
67 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags &flags,
68                                          const char *format_cstr)
69     : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
70   SetSummaryString(format_cstr);
71 }
72
73 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
74   m_format.Clear();
75   if (format_cstr && format_cstr[0]) {
76     m_format_str = format_cstr;
77     m_error = FormatEntity::Parse(format_cstr, m_format);
78   } else {
79     m_format_str.clear();
80     m_error.Clear();
81   }
82 }
83
84 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
85                                        const TypeSummaryOptions &options) {
86   if (!valobj) {
87     retval.assign("NULL ValueObject");
88     return false;
89   }
90
91   StreamString s;
92   ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
93   SymbolContext sc;
94   StackFrame *frame = exe_ctx.GetFramePtr();
95   if (frame)
96     sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
97
98   if (IsOneLiner()) {
99     ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
100     printer.PrintChildrenOneLiner(HideNames(valobj));
101     retval = s.GetString();
102     return true;
103   } else {
104     if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
105                              &sc.line_entry.range.GetBaseAddress(), valobj,
106                              false, false)) {
107       retval.assign(s.GetString());
108       return true;
109     } else {
110       retval.assign("error: summary string parsing error");
111       return false;
112     }
113   }
114 }
115
116 std::string StringSummaryFormat::GetDescription() {
117   StreamString sstr;
118
119   sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
120               m_error.Fail() ? " error: " : "",
121               m_error.Fail() ? m_error.AsCString() : "",
122               Cascades() ? "" : " (not cascading)",
123               !DoesPrintChildren(nullptr) ? "" : " (show children)",
124               !DoesPrintValue(nullptr) ? " (hide value)" : "",
125               IsOneLiner() ? " (one-line printout)" : "",
126               SkipsPointers() ? " (skip pointers)" : "",
127               SkipsReferences() ? " (skip references)" : "",
128               HideNames(nullptr) ? " (hide member names)" : "");
129   return sstr.GetString();
130 }
131
132 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
133     const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
134     : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
135       m_description(description ? description : "") {}
136
137 bool CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
138                                             std::string &dest,
139                                             const TypeSummaryOptions &options) {
140   dest.clear();
141   StreamString stream;
142   if (!m_impl || m_impl(*valobj, stream, options) == false)
143     return false;
144   dest = stream.GetString();
145   return true;
146 }
147
148 std::string CXXFunctionSummaryFormat::GetDescription() {
149   StreamString sstr;
150   sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
151               !DoesPrintChildren(nullptr) ? "" : " (show children)",
152               !DoesPrintValue(nullptr) ? " (hide value)" : "",
153               IsOneLiner() ? " (one-line printout)" : "",
154               SkipsPointers() ? " (skip pointers)" : "",
155               SkipsReferences() ? " (skip references)" : "",
156               HideNames(nullptr) ? " (hide member names)" : "",
157               m_description.c_str());
158   return sstr.GetString();
159 }
160
161 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
162                                          const char *function_name,
163                                          const char *python_script)
164     : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
165       m_python_script(), m_script_function_sp() {
166   if (function_name)
167     m_function_name.assign(function_name);
168   if (python_script)
169     m_python_script.assign(python_script);
170 }
171
172 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
173                                        const TypeSummaryOptions &options) {
174   if (!valobj)
175     return false;
176
177   TargetSP target_sp(valobj->GetTargetSP());
178
179   if (!target_sp) {
180     retval.assign("error: no target");
181     return false;
182   }
183
184   ScriptInterpreter *script_interpreter =
185       target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
186
187   if (!script_interpreter) {
188     retval.assign("error: no ScriptInterpreter");
189     return false;
190   }
191
192   return script_interpreter->GetScriptedSummary(
193       m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
194       retval);
195 }
196
197 std::string ScriptSummaryFormat::GetDescription() {
198   StreamString sstr;
199   sstr.Printf("%s%s%s%s%s%s%s\n  ", Cascades() ? "" : " (not cascading)",
200               !DoesPrintChildren(nullptr) ? "" : " (show children)",
201               !DoesPrintValue(nullptr) ? " (hide value)" : "",
202               IsOneLiner() ? " (one-line printout)" : "",
203               SkipsPointers() ? " (skip pointers)" : "",
204               SkipsReferences() ? " (skip references)" : "",
205               HideNames(nullptr) ? " (hide member names)" : "");
206   if (m_python_script.empty()) {
207     if (m_function_name.empty()) {
208       sstr.PutCString("no backing script");
209     } else {
210       sstr.PutCString(m_function_name);
211     }
212   } else {
213     sstr.PutCString(m_python_script);
214   }
215   return sstr.GetString();
216 }