1 //===-- SBTypeSummary.cpp -----------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBTypeSummary.h"
11 #include "SBReproducerPrivate.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBValue.h"
15 #include "lldb/DataFormatters/DataVisualization.h"
17 #include "llvm/Support/Casting.h"
20 using namespace lldb_private
;
22 SBTypeSummaryOptions::SBTypeSummaryOptions() {
23 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeSummaryOptions
);
25 m_opaque_up
.reset(new TypeSummaryOptions());
28 SBTypeSummaryOptions::SBTypeSummaryOptions(
29 const lldb::SBTypeSummaryOptions
&rhs
) {
30 LLDB_RECORD_CONSTRUCTOR(SBTypeSummaryOptions
,
31 (const lldb::SBTypeSummaryOptions
&), rhs
);
33 m_opaque_up
= clone(rhs
.m_opaque_up
);
36 SBTypeSummaryOptions::~SBTypeSummaryOptions() {}
38 bool SBTypeSummaryOptions::IsValid() {
39 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSummaryOptions
, IsValid
);
40 return this->operator bool();
42 SBTypeSummaryOptions::operator bool() const {
43 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSummaryOptions
, operator bool);
45 return m_opaque_up
.get();
48 lldb::LanguageType
SBTypeSummaryOptions::GetLanguage() {
49 LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType
, SBTypeSummaryOptions
,
53 return m_opaque_up
->GetLanguage();
54 return lldb::eLanguageTypeUnknown
;
57 lldb::TypeSummaryCapping
SBTypeSummaryOptions::GetCapping() {
58 LLDB_RECORD_METHOD_NO_ARGS(lldb::TypeSummaryCapping
, SBTypeSummaryOptions
,
62 return m_opaque_up
->GetCapping();
63 return eTypeSummaryCapped
;
66 void SBTypeSummaryOptions::SetLanguage(lldb::LanguageType l
) {
67 LLDB_RECORD_METHOD(void, SBTypeSummaryOptions
, SetLanguage
,
68 (lldb::LanguageType
), l
);
71 m_opaque_up
->SetLanguage(l
);
74 void SBTypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping c
) {
75 LLDB_RECORD_METHOD(void, SBTypeSummaryOptions
, SetCapping
,
76 (lldb::TypeSummaryCapping
), c
);
79 m_opaque_up
->SetCapping(c
);
82 lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::operator->() {
83 return m_opaque_up
.get();
86 const lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::
88 return m_opaque_up
.get();
91 lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::get() {
92 return m_opaque_up
.get();
95 lldb_private::TypeSummaryOptions
&SBTypeSummaryOptions::ref() {
99 const lldb_private::TypeSummaryOptions
&SBTypeSummaryOptions::ref() const {
103 SBTypeSummaryOptions::SBTypeSummaryOptions(
104 const lldb_private::TypeSummaryOptions
*lldb_object_ptr
) {
105 LLDB_RECORD_CONSTRUCTOR(SBTypeSummaryOptions
,
106 (const lldb_private::TypeSummaryOptions
*),
109 SetOptions(lldb_object_ptr
);
112 void SBTypeSummaryOptions::SetOptions(
113 const lldb_private::TypeSummaryOptions
*lldb_object_ptr
) {
115 m_opaque_up
.reset(new TypeSummaryOptions(*lldb_object_ptr
));
117 m_opaque_up
.reset(new TypeSummaryOptions());
120 SBTypeSummary::SBTypeSummary() : m_opaque_sp() {
121 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeSummary
);
124 SBTypeSummary
SBTypeSummary::CreateWithSummaryString(const char *data
,
126 LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
127 CreateWithSummaryString
, (const char *, uint32_t),
130 if (!data
|| data
[0] == 0)
131 return LLDB_RECORD_RESULT(SBTypeSummary());
133 return LLDB_RECORD_RESULT(
134 SBTypeSummary(TypeSummaryImplSP(new StringSummaryFormat(options
, data
))));
137 SBTypeSummary
SBTypeSummary::CreateWithFunctionName(const char *data
,
139 LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
140 CreateWithFunctionName
, (const char *, uint32_t),
143 if (!data
|| data
[0] == 0)
144 return LLDB_RECORD_RESULT(SBTypeSummary());
146 return LLDB_RECORD_RESULT(
147 SBTypeSummary(TypeSummaryImplSP(new ScriptSummaryFormat(options
, data
))));
150 SBTypeSummary
SBTypeSummary::CreateWithScriptCode(const char *data
,
152 LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
153 CreateWithScriptCode
, (const char *, uint32_t),
156 if (!data
|| data
[0] == 0)
157 return LLDB_RECORD_RESULT(SBTypeSummary());
159 return LLDB_RECORD_RESULT(SBTypeSummary(
160 TypeSummaryImplSP(new ScriptSummaryFormat(options
, "", data
))));
163 SBTypeSummary
SBTypeSummary::CreateWithCallback(FormatCallback cb
,
165 const char *description
) {
167 lldb::SBTypeSummary
, SBTypeSummary
, CreateWithCallback
,
168 (lldb::SBTypeSummary::FormatCallback
, uint32_t, const char *), cb
,
169 options
, description
);
171 SBTypeSummary retval
;
173 retval
.SetSP(TypeSummaryImplSP(new CXXFunctionSummaryFormat(
175 [cb
](ValueObject
&valobj
, Stream
&stm
,
176 const TypeSummaryOptions
&opt
) -> bool {
178 SBValue
sb_value(valobj
.GetSP());
179 SBTypeSummaryOptions
options(&opt
);
180 if (!cb(sb_value
, options
, stream
))
182 stm
.Write(stream
.GetData(), stream
.GetSize());
185 description
? description
: "callback summary formatter")));
191 SBTypeSummary::SBTypeSummary(const lldb::SBTypeSummary
&rhs
)
192 : m_opaque_sp(rhs
.m_opaque_sp
) {
193 LLDB_RECORD_CONSTRUCTOR(SBTypeSummary
, (const lldb::SBTypeSummary
&), rhs
);
196 SBTypeSummary::~SBTypeSummary() {}
198 bool SBTypeSummary::IsValid() const {
199 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSummary
, IsValid
);
200 return this->operator bool();
202 SBTypeSummary::operator bool() const {
203 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSummary
, operator bool);
205 return m_opaque_sp
.get() != nullptr;
208 bool SBTypeSummary::IsFunctionCode() {
209 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSummary
, IsFunctionCode
);
213 if (ScriptSummaryFormat
*script_summary_ptr
=
214 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
215 const char *ftext
= script_summary_ptr
->GetPythonScript();
216 return (ftext
&& *ftext
!= 0);
221 bool SBTypeSummary::IsFunctionName() {
222 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSummary
, IsFunctionName
);
226 if (ScriptSummaryFormat
*script_summary_ptr
=
227 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
228 const char *ftext
= script_summary_ptr
->GetPythonScript();
229 return (!ftext
|| *ftext
== 0);
234 bool SBTypeSummary::IsSummaryString() {
235 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSummary
, IsSummaryString
);
240 return m_opaque_sp
->GetKind() == TypeSummaryImpl::Kind::eSummaryString
;
243 const char *SBTypeSummary::GetData() {
244 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeSummary
, GetData
);
248 if (ScriptSummaryFormat
*script_summary_ptr
=
249 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
250 const char *fname
= script_summary_ptr
->GetFunctionName();
251 const char *ftext
= script_summary_ptr
->GetPythonScript();
255 } else if (StringSummaryFormat
*string_summary_ptr
=
256 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get()))
257 return string_summary_ptr
->GetSummaryString();
261 uint32_t SBTypeSummary::GetOptions() {
262 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeSummary
, GetOptions
);
265 return lldb::eTypeOptionNone
;
266 return m_opaque_sp
->GetOptions();
269 void SBTypeSummary::SetOptions(uint32_t value
) {
270 LLDB_RECORD_METHOD(void, SBTypeSummary
, SetOptions
, (uint32_t), value
);
272 if (!CopyOnWrite_Impl())
274 m_opaque_sp
->SetOptions(value
);
277 void SBTypeSummary::SetSummaryString(const char *data
) {
278 LLDB_RECORD_METHOD(void, SBTypeSummary
, SetSummaryString
, (const char *),
283 if (!llvm::isa
<StringSummaryFormat
>(m_opaque_sp
.get()))
284 ChangeSummaryType(false);
285 if (StringSummaryFormat
*string_summary_ptr
=
286 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get()))
287 string_summary_ptr
->SetSummaryString(data
);
290 void SBTypeSummary::SetFunctionName(const char *data
) {
291 LLDB_RECORD_METHOD(void, SBTypeSummary
, SetFunctionName
, (const char *),
296 if (!llvm::isa
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
297 ChangeSummaryType(true);
298 if (ScriptSummaryFormat
*script_summary_ptr
=
299 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
300 script_summary_ptr
->SetFunctionName(data
);
303 void SBTypeSummary::SetFunctionCode(const char *data
) {
304 LLDB_RECORD_METHOD(void, SBTypeSummary
, SetFunctionCode
, (const char *),
309 if (!llvm::isa
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
310 ChangeSummaryType(true);
311 if (ScriptSummaryFormat
*script_summary_ptr
=
312 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
313 script_summary_ptr
->SetPythonScript(data
);
316 bool SBTypeSummary::GetDescription(lldb::SBStream
&description
,
317 lldb::DescriptionLevel description_level
) {
318 LLDB_RECORD_METHOD(bool, SBTypeSummary
, GetDescription
,
319 (lldb::SBStream
&, lldb::DescriptionLevel
), description
,
322 if (!CopyOnWrite_Impl())
325 description
.Printf("%s\n", m_opaque_sp
->GetDescription().c_str());
330 bool SBTypeSummary::DoesPrintValue(lldb::SBValue value
) {
331 LLDB_RECORD_METHOD(bool, SBTypeSummary
, DoesPrintValue
, (lldb::SBValue
),
336 lldb::ValueObjectSP value_sp
= value
.GetSP();
337 return m_opaque_sp
->DoesPrintValue(value_sp
.get());
340 lldb::SBTypeSummary
&SBTypeSummary::operator=(const lldb::SBTypeSummary
&rhs
) {
341 LLDB_RECORD_METHOD(lldb::SBTypeSummary
&,
342 SBTypeSummary
, operator=,(const lldb::SBTypeSummary
&),
346 m_opaque_sp
= rhs
.m_opaque_sp
;
348 return LLDB_RECORD_RESULT(*this);
351 bool SBTypeSummary::operator==(lldb::SBTypeSummary
&rhs
) {
352 LLDB_RECORD_METHOD(bool, SBTypeSummary
, operator==,(lldb::SBTypeSummary
&),
356 return !rhs
.IsValid();
357 return m_opaque_sp
== rhs
.m_opaque_sp
;
360 bool SBTypeSummary::IsEqualTo(lldb::SBTypeSummary
&rhs
) {
361 LLDB_RECORD_METHOD(bool, SBTypeSummary
, IsEqualTo
, (lldb::SBTypeSummary
&),
365 // valid and invalid are different
369 // invalid and valid are different
373 // both invalid are the same
377 if (m_opaque_sp
->GetKind() != rhs
.m_opaque_sp
->GetKind())
380 switch (m_opaque_sp
->GetKind()) {
381 case TypeSummaryImpl::Kind::eCallback
:
382 return llvm::dyn_cast
<CXXFunctionSummaryFormat
>(m_opaque_sp
.get()) ==
383 llvm::dyn_cast
<CXXFunctionSummaryFormat
>(rhs
.m_opaque_sp
.get());
384 case TypeSummaryImpl::Kind::eScript
:
385 if (IsFunctionCode() != rhs
.IsFunctionCode())
387 if (IsFunctionName() != rhs
.IsFunctionName())
389 return GetOptions() == rhs
.GetOptions();
390 case TypeSummaryImpl::Kind::eSummaryString
:
391 if (IsSummaryString() != rhs
.IsSummaryString())
393 return GetOptions() == rhs
.GetOptions();
394 case TypeSummaryImpl::Kind::eInternal
:
395 return (m_opaque_sp
.get() == rhs
.m_opaque_sp
.get());
401 bool SBTypeSummary::operator!=(lldb::SBTypeSummary
&rhs
) {
402 LLDB_RECORD_METHOD(bool, SBTypeSummary
, operator!=,(lldb::SBTypeSummary
&),
406 return !rhs
.IsValid();
407 return m_opaque_sp
!= rhs
.m_opaque_sp
;
410 lldb::TypeSummaryImplSP
SBTypeSummary::GetSP() { return m_opaque_sp
; }
412 void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP
&typesummary_impl_sp
) {
413 m_opaque_sp
= typesummary_impl_sp
;
416 SBTypeSummary::SBTypeSummary(const lldb::TypeSummaryImplSP
&typesummary_impl_sp
)
417 : m_opaque_sp(typesummary_impl_sp
) {}
419 bool SBTypeSummary::CopyOnWrite_Impl() {
423 if (m_opaque_sp
.unique())
426 TypeSummaryImplSP new_sp
;
428 if (CXXFunctionSummaryFormat
*current_summary_ptr
=
429 llvm::dyn_cast
<CXXFunctionSummaryFormat
>(m_opaque_sp
.get())) {
430 new_sp
= TypeSummaryImplSP(new CXXFunctionSummaryFormat(
431 GetOptions(), current_summary_ptr
->m_impl
,
432 current_summary_ptr
->m_description
.c_str()));
433 } else if (ScriptSummaryFormat
*current_summary_ptr
=
434 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
435 new_sp
= TypeSummaryImplSP(new ScriptSummaryFormat(
436 GetOptions(), current_summary_ptr
->GetFunctionName(),
437 current_summary_ptr
->GetPythonScript()));
438 } else if (StringSummaryFormat
*current_summary_ptr
=
439 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get())) {
440 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(
441 GetOptions(), current_summary_ptr
->GetSummaryString()));
446 return nullptr != new_sp
.get();
449 bool SBTypeSummary::ChangeSummaryType(bool want_script
) {
453 TypeSummaryImplSP new_sp
;
456 (m_opaque_sp
->GetKind() == TypeSummaryImpl::Kind::eScript
)) {
457 if (m_opaque_sp
->GetKind() ==
458 lldb_private::TypeSummaryImpl::Kind::eCallback
&&
460 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
462 return CopyOnWrite_Impl();
467 new_sp
= TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
469 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
477 namespace lldb_private
{
481 void RegisterMethods
<SBTypeSummaryOptions
>(Registry
&R
) {
482 LLDB_REGISTER_CONSTRUCTOR(SBTypeSummaryOptions
, ());
483 LLDB_REGISTER_CONSTRUCTOR(SBTypeSummaryOptions
,
484 (const lldb::SBTypeSummaryOptions
&));
485 LLDB_REGISTER_METHOD(bool, SBTypeSummaryOptions
, IsValid
, ());
486 LLDB_REGISTER_METHOD_CONST(bool, SBTypeSummaryOptions
, operator bool, ());
487 LLDB_REGISTER_METHOD(lldb::LanguageType
, SBTypeSummaryOptions
, GetLanguage
,
489 LLDB_REGISTER_METHOD(lldb::TypeSummaryCapping
, SBTypeSummaryOptions
,
491 LLDB_REGISTER_METHOD(void, SBTypeSummaryOptions
, SetLanguage
,
492 (lldb::LanguageType
));
493 LLDB_REGISTER_METHOD(void, SBTypeSummaryOptions
, SetCapping
,
494 (lldb::TypeSummaryCapping
));
495 LLDB_REGISTER_CONSTRUCTOR(SBTypeSummaryOptions
,
496 (const lldb_private::TypeSummaryOptions
*));
500 void RegisterMethods
<SBTypeSummary
>(Registry
&R
) {
501 LLDB_REGISTER_CONSTRUCTOR(SBTypeSummary
, ());
502 LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
503 CreateWithSummaryString
,
504 (const char *, uint32_t));
505 LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
506 CreateWithFunctionName
,
507 (const char *, uint32_t));
508 LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSummary
, SBTypeSummary
,
509 CreateWithScriptCode
, (const char *, uint32_t));
510 LLDB_REGISTER_CONSTRUCTOR(SBTypeSummary
, (const lldb::SBTypeSummary
&));
511 LLDB_REGISTER_METHOD_CONST(bool, SBTypeSummary
, IsValid
, ());
512 LLDB_REGISTER_METHOD_CONST(bool, SBTypeSummary
, operator bool, ());
513 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, IsFunctionCode
, ());
514 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, IsFunctionName
, ());
515 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, IsSummaryString
, ());
516 LLDB_REGISTER_METHOD(const char *, SBTypeSummary
, GetData
, ());
517 LLDB_REGISTER_METHOD(uint32_t, SBTypeSummary
, GetOptions
, ());
518 LLDB_REGISTER_METHOD(void, SBTypeSummary
, SetOptions
, (uint32_t));
519 LLDB_REGISTER_METHOD(void, SBTypeSummary
, SetSummaryString
, (const char *));
520 LLDB_REGISTER_METHOD(void, SBTypeSummary
, SetFunctionName
, (const char *));
521 LLDB_REGISTER_METHOD(void, SBTypeSummary
, SetFunctionCode
, (const char *));
522 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, GetDescription
,
523 (lldb::SBStream
&, lldb::DescriptionLevel
));
524 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, DoesPrintValue
, (lldb::SBValue
));
525 LLDB_REGISTER_METHOD(
526 lldb::SBTypeSummary
&,
527 SBTypeSummary
, operator=,(const lldb::SBTypeSummary
&));
528 LLDB_REGISTER_METHOD(bool,
529 SBTypeSummary
, operator==,(lldb::SBTypeSummary
&));
530 LLDB_REGISTER_METHOD(bool, SBTypeSummary
, IsEqualTo
,
531 (lldb::SBTypeSummary
&));
532 LLDB_REGISTER_METHOD(bool,
533 SBTypeSummary
, operator!=,(lldb::SBTypeSummary
&));