1 //===-- SBTypeSummary.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBTypeSummary.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBValue.h"
13 #include "lldb/DataFormatters/DataVisualization.h"
14 #include "lldb/Utility/Instrumentation.h"
16 #include "llvm/Support/Casting.h"
19 using namespace lldb_private
;
21 SBTypeSummaryOptions::SBTypeSummaryOptions() {
22 LLDB_INSTRUMENT_VA(this);
24 m_opaque_up
= std::make_unique
<TypeSummaryOptions
>();
27 SBTypeSummaryOptions::SBTypeSummaryOptions(
28 const lldb::SBTypeSummaryOptions
&rhs
) {
29 LLDB_INSTRUMENT_VA(this, rhs
);
31 m_opaque_up
= clone(rhs
.m_opaque_up
);
34 SBTypeSummaryOptions::~SBTypeSummaryOptions() = default;
36 bool SBTypeSummaryOptions::IsValid() {
37 LLDB_INSTRUMENT_VA(this);
38 return this->operator bool();
40 SBTypeSummaryOptions::operator bool() const {
41 LLDB_INSTRUMENT_VA(this);
43 return m_opaque_up
.get();
46 lldb::LanguageType
SBTypeSummaryOptions::GetLanguage() {
47 LLDB_INSTRUMENT_VA(this);
50 return m_opaque_up
->GetLanguage();
51 return lldb::eLanguageTypeUnknown
;
54 lldb::TypeSummaryCapping
SBTypeSummaryOptions::GetCapping() {
55 LLDB_INSTRUMENT_VA(this);
58 return m_opaque_up
->GetCapping();
59 return eTypeSummaryCapped
;
62 void SBTypeSummaryOptions::SetLanguage(lldb::LanguageType l
) {
63 LLDB_INSTRUMENT_VA(this, l
);
66 m_opaque_up
->SetLanguage(l
);
69 void SBTypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping c
) {
70 LLDB_INSTRUMENT_VA(this, c
);
73 m_opaque_up
->SetCapping(c
);
76 lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::operator->() {
77 return m_opaque_up
.get();
80 const lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::
82 return m_opaque_up
.get();
85 lldb_private::TypeSummaryOptions
*SBTypeSummaryOptions::get() {
86 return m_opaque_up
.get();
89 lldb_private::TypeSummaryOptions
&SBTypeSummaryOptions::ref() {
93 const lldb_private::TypeSummaryOptions
&SBTypeSummaryOptions::ref() const {
97 SBTypeSummaryOptions::SBTypeSummaryOptions(
98 const lldb_private::TypeSummaryOptions
&lldb_object
)
99 : m_opaque_up(std::make_unique
<TypeSummaryOptions
>(lldb_object
)) {
100 LLDB_INSTRUMENT_VA(this, lldb_object
);
103 SBTypeSummary::SBTypeSummary() { LLDB_INSTRUMENT_VA(this); }
105 SBTypeSummary
SBTypeSummary::CreateWithSummaryString(const char *data
,
107 LLDB_INSTRUMENT_VA(data
, options
);
109 if (!data
|| data
[0] == 0)
110 return SBTypeSummary();
112 return SBTypeSummary(
113 TypeSummaryImplSP(new StringSummaryFormat(options
, data
)));
116 SBTypeSummary
SBTypeSummary::CreateWithFunctionName(const char *data
,
118 LLDB_INSTRUMENT_VA(data
, options
);
120 if (!data
|| data
[0] == 0)
121 return SBTypeSummary();
123 return SBTypeSummary(
124 TypeSummaryImplSP(new ScriptSummaryFormat(options
, data
)));
127 SBTypeSummary
SBTypeSummary::CreateWithScriptCode(const char *data
,
129 LLDB_INSTRUMENT_VA(data
, options
);
131 if (!data
|| data
[0] == 0)
132 return SBTypeSummary();
134 return SBTypeSummary(
135 TypeSummaryImplSP(new ScriptSummaryFormat(options
, "", data
)));
138 SBTypeSummary
SBTypeSummary::CreateWithCallback(FormatCallback cb
,
140 const char *description
) {
141 LLDB_INSTRUMENT_VA(cb
, options
, description
);
143 SBTypeSummary retval
;
145 retval
.SetSP(TypeSummaryImplSP(new CXXFunctionSummaryFormat(
147 [cb
](ValueObject
&valobj
, Stream
&stm
,
148 const TypeSummaryOptions
&opt
) -> bool {
150 SBValue
sb_value(valobj
.GetSP());
151 SBTypeSummaryOptions
options(opt
);
152 if (!cb(sb_value
, options
, stream
))
154 stm
.Write(stream
.GetData(), stream
.GetSize());
157 description
? description
: "callback summary formatter")));
163 SBTypeSummary::SBTypeSummary(const lldb::SBTypeSummary
&rhs
)
164 : m_opaque_sp(rhs
.m_opaque_sp
) {
165 LLDB_INSTRUMENT_VA(this, rhs
);
168 SBTypeSummary::~SBTypeSummary() = default;
170 bool SBTypeSummary::IsValid() const {
171 LLDB_INSTRUMENT_VA(this);
172 return this->operator bool();
174 SBTypeSummary::operator bool() const {
175 LLDB_INSTRUMENT_VA(this);
177 return m_opaque_sp
.get() != nullptr;
180 bool SBTypeSummary::IsFunctionCode() {
181 LLDB_INSTRUMENT_VA(this);
185 if (ScriptSummaryFormat
*script_summary_ptr
=
186 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
187 const char *ftext
= script_summary_ptr
->GetPythonScript();
188 return (ftext
&& *ftext
!= 0);
193 bool SBTypeSummary::IsFunctionName() {
194 LLDB_INSTRUMENT_VA(this);
198 if (ScriptSummaryFormat
*script_summary_ptr
=
199 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
200 const char *ftext
= script_summary_ptr
->GetPythonScript();
201 return (!ftext
|| *ftext
== 0);
206 bool SBTypeSummary::IsSummaryString() {
207 LLDB_INSTRUMENT_VA(this);
212 return m_opaque_sp
->GetKind() == TypeSummaryImpl::Kind::eSummaryString
;
215 const char *SBTypeSummary::GetData() {
216 LLDB_INSTRUMENT_VA(this);
220 if (ScriptSummaryFormat
*script_summary_ptr
=
221 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
222 const char *fname
= script_summary_ptr
->GetFunctionName();
223 const char *ftext
= script_summary_ptr
->GetPythonScript();
225 return ConstString(ftext
).GetCString();
226 return ConstString(fname
).GetCString();
227 } else if (StringSummaryFormat
*string_summary_ptr
=
228 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get()))
229 return ConstString(string_summary_ptr
->GetSummaryString()).GetCString();
233 uint32_t SBTypeSummary::GetOptions() {
234 LLDB_INSTRUMENT_VA(this);
237 return lldb::eTypeOptionNone
;
238 return m_opaque_sp
->GetOptions();
241 void SBTypeSummary::SetOptions(uint32_t value
) {
242 LLDB_INSTRUMENT_VA(this, value
);
244 if (!CopyOnWrite_Impl())
246 m_opaque_sp
->SetOptions(value
);
249 void SBTypeSummary::SetSummaryString(const char *data
) {
250 LLDB_INSTRUMENT_VA(this, data
);
254 if (!llvm::isa
<StringSummaryFormat
>(m_opaque_sp
.get()))
255 ChangeSummaryType(false);
256 if (StringSummaryFormat
*string_summary_ptr
=
257 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get()))
258 string_summary_ptr
->SetSummaryString(data
);
261 void SBTypeSummary::SetFunctionName(const char *data
) {
262 LLDB_INSTRUMENT_VA(this, data
);
266 if (!llvm::isa
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
267 ChangeSummaryType(true);
268 if (ScriptSummaryFormat
*script_summary_ptr
=
269 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
270 script_summary_ptr
->SetFunctionName(data
);
273 void SBTypeSummary::SetFunctionCode(const char *data
) {
274 LLDB_INSTRUMENT_VA(this, data
);
278 if (!llvm::isa
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
279 ChangeSummaryType(true);
280 if (ScriptSummaryFormat
*script_summary_ptr
=
281 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get()))
282 script_summary_ptr
->SetPythonScript(data
);
285 bool SBTypeSummary::GetDescription(lldb::SBStream
&description
,
286 lldb::DescriptionLevel description_level
) {
287 LLDB_INSTRUMENT_VA(this, description
, description_level
);
289 if (!CopyOnWrite_Impl())
292 description
.Printf("%s\n", m_opaque_sp
->GetDescription().c_str());
297 bool SBTypeSummary::DoesPrintValue(lldb::SBValue value
) {
298 LLDB_INSTRUMENT_VA(this, value
);
302 lldb::ValueObjectSP value_sp
= value
.GetSP();
303 return m_opaque_sp
->DoesPrintValue(value_sp
.get());
306 lldb::SBTypeSummary
&SBTypeSummary::operator=(const lldb::SBTypeSummary
&rhs
) {
307 LLDB_INSTRUMENT_VA(this, rhs
);
310 m_opaque_sp
= rhs
.m_opaque_sp
;
315 bool SBTypeSummary::operator==(lldb::SBTypeSummary
&rhs
) {
316 LLDB_INSTRUMENT_VA(this, rhs
);
319 return !rhs
.IsValid();
320 return m_opaque_sp
== rhs
.m_opaque_sp
;
323 bool SBTypeSummary::IsEqualTo(lldb::SBTypeSummary
&rhs
) {
324 LLDB_INSTRUMENT_VA(this, rhs
);
327 // valid and invalid are different
331 // invalid and valid are different
335 // both invalid are the same
339 if (m_opaque_sp
->GetKind() != rhs
.m_opaque_sp
->GetKind())
342 switch (m_opaque_sp
->GetKind()) {
343 case TypeSummaryImpl::Kind::eCallback
:
344 return llvm::dyn_cast
<CXXFunctionSummaryFormat
>(m_opaque_sp
.get()) ==
345 llvm::dyn_cast
<CXXFunctionSummaryFormat
>(rhs
.m_opaque_sp
.get());
346 case TypeSummaryImpl::Kind::eScript
:
347 if (IsFunctionCode() != rhs
.IsFunctionCode())
349 if (IsFunctionName() != rhs
.IsFunctionName())
351 return GetOptions() == rhs
.GetOptions();
352 case TypeSummaryImpl::Kind::eSummaryString
:
353 if (IsSummaryString() != rhs
.IsSummaryString())
355 return GetOptions() == rhs
.GetOptions();
356 case TypeSummaryImpl::Kind::eInternal
:
357 return (m_opaque_sp
.get() == rhs
.m_opaque_sp
.get());
363 bool SBTypeSummary::operator!=(lldb::SBTypeSummary
&rhs
) {
364 LLDB_INSTRUMENT_VA(this, rhs
);
367 return !rhs
.IsValid();
368 return m_opaque_sp
!= rhs
.m_opaque_sp
;
371 lldb::TypeSummaryImplSP
SBTypeSummary::GetSP() { return m_opaque_sp
; }
373 void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP
&typesummary_impl_sp
) {
374 m_opaque_sp
= typesummary_impl_sp
;
377 SBTypeSummary::SBTypeSummary(const lldb::TypeSummaryImplSP
&typesummary_impl_sp
)
378 : m_opaque_sp(typesummary_impl_sp
) {}
380 bool SBTypeSummary::CopyOnWrite_Impl() {
384 if (m_opaque_sp
.unique())
387 TypeSummaryImplSP new_sp
;
389 if (CXXFunctionSummaryFormat
*current_summary_ptr
=
390 llvm::dyn_cast
<CXXFunctionSummaryFormat
>(m_opaque_sp
.get())) {
391 new_sp
= TypeSummaryImplSP(new CXXFunctionSummaryFormat(
392 GetOptions(), current_summary_ptr
->m_impl
,
393 current_summary_ptr
->m_description
.c_str()));
394 } else if (ScriptSummaryFormat
*current_summary_ptr
=
395 llvm::dyn_cast
<ScriptSummaryFormat
>(m_opaque_sp
.get())) {
396 new_sp
= TypeSummaryImplSP(new ScriptSummaryFormat(
397 GetOptions(), current_summary_ptr
->GetFunctionName(),
398 current_summary_ptr
->GetPythonScript()));
399 } else if (StringSummaryFormat
*current_summary_ptr
=
400 llvm::dyn_cast
<StringSummaryFormat
>(m_opaque_sp
.get())) {
401 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(
402 GetOptions(), current_summary_ptr
->GetSummaryString()));
407 return nullptr != new_sp
.get();
410 bool SBTypeSummary::ChangeSummaryType(bool want_script
) {
414 TypeSummaryImplSP new_sp
;
417 (m_opaque_sp
->GetKind() == TypeSummaryImpl::Kind::eScript
)) {
418 if (m_opaque_sp
->GetKind() ==
419 lldb_private::TypeSummaryImpl::Kind::eCallback
&&
421 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
423 return CopyOnWrite_Impl();
428 new_sp
= TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
430 new_sp
= TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));