LLDB mainline
SBTypeSummary.cpp
Go to the documentation of this file.
1//===-- SBTypeSummary.cpp -------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "Utils.h"
11#include "lldb/API/SBStream.h"
12#include "lldb/API/SBValue.h"
15
16#include "llvm/Support/Casting.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
23
24 m_opaque_up = std::make_unique<TypeSummaryOptions>();
25}
26
33
35
38 return this->operator bool();
39}
40SBTypeSummaryOptions::operator bool() const {
42
43 return m_opaque_up.get();
44}
45
53
61
63 LLDB_INSTRUMENT_VA(this, l);
64
65 if (IsValid())
66 m_opaque_up->SetLanguage(l);
67}
68
70 LLDB_INSTRUMENT_VA(this, c);
71
72 if (IsValid())
73 m_opaque_up->SetCapping(c);
74}
75
79
84
88
92
96
98 const lldb_private::TypeSummaryOptions &lldb_object)
99 : m_opaque_up(std::make_unique<TypeSummaryOptions>(lldb_object)) {
100 LLDB_INSTRUMENT_VA(this, lldb_object);
101}
102
104
106 uint32_t options) {
107 LLDB_INSTRUMENT_VA(data, options);
108
109 if (!data || data[0] == 0)
110 return SBTypeSummary();
111
112 return SBTypeSummary(
113 TypeSummaryImplSP(new StringSummaryFormat(options, data)));
114}
115
117 uint32_t options) {
118 LLDB_INSTRUMENT_VA(data, options);
119
120 if (!data || data[0] == 0)
121 return SBTypeSummary();
122
123 return SBTypeSummary(
124 TypeSummaryImplSP(new ScriptSummaryFormat(options, data)));
125}
126
128 uint32_t options) {
129 LLDB_INSTRUMENT_VA(data, options);
130
131 if (!data || data[0] == 0)
132 return SBTypeSummary();
133
134 return SBTypeSummary(
135 TypeSummaryImplSP(new ScriptSummaryFormat(options, "", data)));
136}
137
139 uint32_t options) {
140 LLDB_INSTRUMENT_VA(data, options);
141
142 if (!data || data[0] == 0)
143 return SBTypeSummary();
144
145 return SBTypeSummary(
146 TypeSummaryImplSP(new ScriptedSummaryFormat(options, data)));
147}
148
150 uint32_t options,
151 const char *description) {
152 LLDB_INSTRUMENT_VA(cb, options, description);
153
154 SBTypeSummary retval;
155 if (cb) {
157 options,
158 [cb](ValueObject &valobj, Stream &stm,
159 const TypeSummaryOptions &opt) -> bool {
160 SBStream stream;
161 SBValue sb_value(valobj.GetSP());
162 SBTypeSummaryOptions options(opt);
163 if (!cb(sb_value, options, stream))
164 return false;
165 stm.Write(stream.GetData(), stream.GetSize());
166 return true;
167 },
168 description ? description : "callback summary formatter")));
169 }
170
171 return retval;
172}
173
178
180
182 LLDB_INSTRUMENT_VA(this);
183 return this->operator bool();
184}
185SBTypeSummary::operator bool() const {
186 LLDB_INSTRUMENT_VA(this);
187
188 return m_opaque_sp.get() != nullptr;
189}
190
192 LLDB_INSTRUMENT_VA(this);
193
194 if (!IsValid())
195 return false;
196 if (ScriptSummaryFormat *script_summary_ptr =
197 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
198 const char *ftext = script_summary_ptr->GetPythonScript();
199 return (ftext && *ftext != 0);
200 }
201 return false;
202}
203
205 LLDB_INSTRUMENT_VA(this);
206
207 if (!IsValid())
208 return false;
209 if (ScriptSummaryFormat *script_summary_ptr =
210 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
211 const char *ftext = script_summary_ptr->GetPythonScript();
212 return (!ftext || *ftext == 0);
213 }
214 return false;
215}
216
218 LLDB_INSTRUMENT_VA(this);
219
220 if (!IsValid())
221 return false;
222
224}
225
227 LLDB_INSTRUMENT_VA(this);
228
229 if (!IsValid())
230 return nullptr;
231 if (ScriptSummaryFormat *script_summary_ptr =
232 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
233 const char *fname = script_summary_ptr->GetFunctionName();
234 const char *ftext = script_summary_ptr->GetPythonScript();
235 if (ftext && *ftext)
236 return ConstString(ftext).GetCString();
237 return ConstString(fname).GetCString();
238 } else if (StringSummaryFormat *string_summary_ptr =
239 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
240 return ConstString(string_summary_ptr->GetSummaryString()).GetCString();
241 return nullptr;
242}
243
245 LLDB_INSTRUMENT_VA(this);
246
247 if (!IsValid())
248 return 0;
249 return m_opaque_sp->GetPtrMatchDepth();
250}
251
252void SBTypeSummary::SetPtrMatchDepth(uint32_t ptr_match_depth) {
253 LLDB_INSTRUMENT_VA(this);
254
255 if (!IsValid())
256 return;
257 return m_opaque_sp->SetPtrMatchDepth(ptr_match_depth);
258}
259
261 LLDB_INSTRUMENT_VA(this);
262
263 if (!IsValid())
264 return lldb::eTypeOptionNone;
265 return m_opaque_sp->GetOptions();
266}
267
268void SBTypeSummary::SetOptions(uint32_t value) {
269 LLDB_INSTRUMENT_VA(this, value);
270
271 if (!CopyOnWrite_Impl())
272 return;
273 m_opaque_sp->SetOptions(value);
274}
275
276void SBTypeSummary::SetSummaryString(const char *data) {
277 LLDB_INSTRUMENT_VA(this, data);
278
279 if (!IsValid())
280 return;
281 if (!llvm::isa<StringSummaryFormat>(m_opaque_sp.get()))
282 ChangeSummaryType(false);
283 if (StringSummaryFormat *string_summary_ptr =
284 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
285 string_summary_ptr->SetSummaryString(data);
286}
287
288void SBTypeSummary::SetFunctionName(const char *data) {
289 LLDB_INSTRUMENT_VA(this, data);
290
291 if (!IsValid())
292 return;
293 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
294 ChangeSummaryType(true);
295 if (ScriptSummaryFormat *script_summary_ptr =
296 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
297 script_summary_ptr->SetFunctionName(data);
298}
299
300void SBTypeSummary::SetFunctionCode(const char *data) {
301 LLDB_INSTRUMENT_VA(this, data);
302
303 if (!IsValid())
304 return;
305 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
306 ChangeSummaryType(true);
307 if (ScriptSummaryFormat *script_summary_ptr =
308 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
309 script_summary_ptr->SetPythonScript(data);
310}
311
313 lldb::DescriptionLevel description_level) {
314 LLDB_INSTRUMENT_VA(this, description, description_level);
315
316 if (!CopyOnWrite_Impl())
317 return false;
318 else {
319 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
320 return true;
321 }
322}
323
325 LLDB_INSTRUMENT_VA(this, value);
326
327 if (!IsValid())
328 return false;
329 lldb::ValueObjectSP value_sp = value.GetSP();
330 return m_opaque_sp->DoesPrintValue(value_sp.get());
331}
332
334 LLDB_INSTRUMENT_VA(this, rhs);
335
336 if (this != &rhs) {
338 }
339 return *this;
340}
341
343 LLDB_INSTRUMENT_VA(this, rhs);
344
345 if (!IsValid())
346 return !rhs.IsValid();
347 return m_opaque_sp == rhs.m_opaque_sp;
348}
349
351 LLDB_INSTRUMENT_VA(this, rhs);
352
353 if (IsValid()) {
354 // valid and invalid are different
355 if (!rhs.IsValid())
356 return false;
357 } else {
358 // invalid and valid are different
359 if (rhs.IsValid())
360 return false;
361 else
362 // both invalid are the same
363 return true;
364 }
365
366 if (m_opaque_sp->GetKind() != rhs.m_opaque_sp->GetKind())
367 return false;
368
369 switch (m_opaque_sp->GetKind()) {
371 return llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get()) ==
372 llvm::dyn_cast<CXXFunctionSummaryFormat>(rhs.m_opaque_sp.get());
375 if (IsFunctionCode() != rhs.IsFunctionCode())
376 return false;
377 if (IsFunctionName() != rhs.IsFunctionName())
378 return false;
379 return GetOptions() == rhs.GetOptions();
381 if (IsSummaryString() != rhs.IsSummaryString())
382 return false;
383 return GetOptions() == rhs.GetOptions();
385 return (m_opaque_sp.get() == rhs.m_opaque_sp.get());
387 ScriptedSummaryFormat *lhs_ptr =
388 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get());
389 ScriptedSummaryFormat *rhs_ptr =
390 llvm::dyn_cast<ScriptedSummaryFormat>(rhs.m_opaque_sp.get());
391 if (!lhs_ptr || !rhs_ptr)
392 return false;
393 return strcmp(lhs_ptr->GetClassName(), rhs_ptr->GetClassName()) == 0 &&
394 GetOptions() == rhs.GetOptions();
395 }
396 }
397
398 return false;
399}
400
402 LLDB_INSTRUMENT_VA(this, rhs);
403
404 if (!IsValid())
405 return !rhs.IsValid();
406 return m_opaque_sp != rhs.m_opaque_sp;
407}
408
410
411void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP &typesummary_impl_sp) {
412 m_opaque_sp = typesummary_impl_sp;
413}
414
416 : m_opaque_sp(typesummary_impl_sp) {}
417
419 if (!IsValid())
420 return false;
421
422 if (m_opaque_sp.use_count() == 1)
423 return true;
424
425 TypeSummaryImplSP new_sp;
426
427 if (CXXFunctionSummaryFormat *current_summary_ptr =
428 llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get())) {
430 GetOptions(), current_summary_ptr->m_impl,
431 current_summary_ptr->m_description.c_str()));
432 } else if (ScriptSummaryFormat *current_summary_ptr =
433 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
435 GetOptions(), current_summary_ptr->GetFunctionName(),
436 current_summary_ptr->GetPythonScript()));
437 } else if (StringSummaryFormat *current_summary_ptr =
438 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get())) {
440 GetOptions(), current_summary_ptr->GetSummaryString()));
441 } else if (ScriptedSummaryFormat *current_summary_ptr =
442 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get())) {
444 GetOptions(), current_summary_ptr->GetClassName()));
445 }
446
447 SetSP(new_sp);
448
449 return nullptr != new_sp.get();
450}
451
452bool SBTypeSummary::ChangeSummaryType(bool want_script) {
453 if (!IsValid())
454 return false;
455
456 TypeSummaryImplSP new_sp;
457
458 if (want_script ==
460 if (m_opaque_sp->GetKind() ==
462 !want_script)
464 else
465 return CopyOnWrite_Impl();
466 }
467
468 if (!new_sp) {
469 if (want_script)
470 new_sp = TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
471 else
473 }
474
475 SetSP(new_sp);
476
477 return true;
478}
#define LLDB_INSTRUMENT_VA(...)
size_t GetSize()
Definition SBStream.cpp:56
const char * GetData()
Definition SBStream.cpp:44
lldb::LanguageType GetLanguage()
void SetLanguage(lldb::LanguageType)
lldb_private::TypeSummaryOptions & ref()
lldb::TypeSummaryCapping GetCapping()
void SetCapping(lldb::TypeSummaryCapping)
std::unique_ptr< lldb_private::TypeSummaryOptions > m_opaque_up
lldb_private::TypeSummaryOptions * get()
lldb_private::TypeSummaryOptions * operator->()
static SBTypeSummary CreateWithFunctionName(const char *data, uint32_t options=0)
void SetSummaryString(const char *data)
void SetOptions(uint32_t)
lldb::SBTypeSummary & operator=(const lldb::SBTypeSummary &rhs)
static SBTypeSummary CreateWithCallback(FormatCallback cb, uint32_t options=0, const char *description=nullptr)
static SBTypeSummary CreateWithSummaryString(const char *data, uint32_t options=0)
bool operator==(lldb::SBTypeSummary &rhs)
bool ChangeSummaryType(bool want_script)
void SetSP(const lldb::TypeSummaryImplSP &typefilter_impl_sp)
bool(* FormatCallback)(SBValue, SBTypeSummaryOptions, SBStream &)
void SetPtrMatchDepth(uint32_t ptr_match_depth)
void SetFunctionCode(const char *data)
static SBTypeSummary CreateWithClassName(const char *data, uint32_t options=0)
static SBTypeSummary CreateWithScriptCode(const char *data, uint32_t options=0)
bool operator!=(lldb::SBTypeSummary &rhs)
bool IsEqualTo(lldb::SBTypeSummary &rhs)
lldb::TypeSummaryImplSP m_opaque_sp
const char * GetData()
bool DoesPrintValue(lldb::SBValue value)
void SetFunctionName(const char *data)
bool GetDescription(lldb::SBStream &description, lldb::DescriptionLevel description_level)
lldb::TypeSummaryImplSP GetSP()
lldb::ValueObjectSP GetSP() const
Same as the protected version of GetSP that takes a locker, except that we make the locker locally in...
Definition SBValue.cpp:1011
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition Stream.h:111
lldb::ValueObjectSP GetSP()
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition Utils.h:17
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
std::shared_ptr< lldb_private::TypeSummaryImpl > TypeSummaryImplSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
const char * GetClassName() const