[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBTypeSummary.cpp
bloba5e87188c9f6306d4c81a2db231d98126edc40d4
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 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBTypeSummary.h"
10 #include "Utils.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"
18 using namespace lldb;
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);
49 if (IsValid())
50 return m_opaque_up->GetLanguage();
51 return lldb::eLanguageTypeUnknown;
54 lldb::TypeSummaryCapping SBTypeSummaryOptions::GetCapping() {
55 LLDB_INSTRUMENT_VA(this);
57 if (IsValid())
58 return m_opaque_up->GetCapping();
59 return eTypeSummaryCapped;
62 void SBTypeSummaryOptions::SetLanguage(lldb::LanguageType l) {
63 LLDB_INSTRUMENT_VA(this, l);
65 if (IsValid())
66 m_opaque_up->SetLanguage(l);
69 void SBTypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping c) {
70 LLDB_INSTRUMENT_VA(this, c);
72 if (IsValid())
73 m_opaque_up->SetCapping(c);
76 lldb_private::TypeSummaryOptions *SBTypeSummaryOptions::operator->() {
77 return m_opaque_up.get();
80 const lldb_private::TypeSummaryOptions *SBTypeSummaryOptions::
81 operator->() const {
82 return m_opaque_up.get();
85 lldb_private::TypeSummaryOptions *SBTypeSummaryOptions::get() {
86 return m_opaque_up.get();
89 lldb_private::TypeSummaryOptions &SBTypeSummaryOptions::ref() {
90 return *m_opaque_up;
93 const lldb_private::TypeSummaryOptions &SBTypeSummaryOptions::ref() const {
94 return *m_opaque_up;
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,
106 uint32_t options) {
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,
117 uint32_t options) {
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,
128 uint32_t options) {
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,
139 uint32_t options,
140 const char *description) {
141 LLDB_INSTRUMENT_VA(cb, options, description);
143 SBTypeSummary retval;
144 if (cb) {
145 retval.SetSP(TypeSummaryImplSP(new CXXFunctionSummaryFormat(
146 options,
147 [cb](ValueObject &valobj, Stream &stm,
148 const TypeSummaryOptions &opt) -> bool {
149 SBStream stream;
150 SBValue sb_value(valobj.GetSP());
151 SBTypeSummaryOptions options(opt);
152 if (!cb(sb_value, options, stream))
153 return false;
154 stm.Write(stream.GetData(), stream.GetSize());
155 return true;
157 description ? description : "callback summary formatter")));
160 return retval;
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);
183 if (!IsValid())
184 return false;
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);
190 return false;
193 bool SBTypeSummary::IsFunctionName() {
194 LLDB_INSTRUMENT_VA(this);
196 if (!IsValid())
197 return false;
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);
203 return false;
206 bool SBTypeSummary::IsSummaryString() {
207 LLDB_INSTRUMENT_VA(this);
209 if (!IsValid())
210 return false;
212 return m_opaque_sp->GetKind() == TypeSummaryImpl::Kind::eSummaryString;
215 const char *SBTypeSummary::GetData() {
216 LLDB_INSTRUMENT_VA(this);
218 if (!IsValid())
219 return nullptr;
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();
224 if (ftext && *ftext)
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();
230 return nullptr;
233 uint32_t SBTypeSummary::GetOptions() {
234 LLDB_INSTRUMENT_VA(this);
236 if (!IsValid())
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())
245 return;
246 m_opaque_sp->SetOptions(value);
249 void SBTypeSummary::SetSummaryString(const char *data) {
250 LLDB_INSTRUMENT_VA(this, data);
252 if (!IsValid())
253 return;
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);
264 if (!IsValid())
265 return;
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);
276 if (!IsValid())
277 return;
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())
290 return false;
291 else {
292 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
293 return true;
297 bool SBTypeSummary::DoesPrintValue(lldb::SBValue value) {
298 LLDB_INSTRUMENT_VA(this, value);
300 if (!IsValid())
301 return false;
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);
309 if (this != &rhs) {
310 m_opaque_sp = rhs.m_opaque_sp;
312 return *this;
315 bool SBTypeSummary::operator==(lldb::SBTypeSummary &rhs) {
316 LLDB_INSTRUMENT_VA(this, rhs);
318 if (!IsValid())
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);
326 if (IsValid()) {
327 // valid and invalid are different
328 if (!rhs.IsValid())
329 return false;
330 } else {
331 // invalid and valid are different
332 if (rhs.IsValid())
333 return false;
334 else
335 // both invalid are the same
336 return true;
339 if (m_opaque_sp->GetKind() != rhs.m_opaque_sp->GetKind())
340 return false;
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())
348 return false;
349 if (IsFunctionName() != rhs.IsFunctionName())
350 return false;
351 return GetOptions() == rhs.GetOptions();
352 case TypeSummaryImpl::Kind::eSummaryString:
353 if (IsSummaryString() != rhs.IsSummaryString())
354 return false;
355 return GetOptions() == rhs.GetOptions();
356 case TypeSummaryImpl::Kind::eInternal:
357 return (m_opaque_sp.get() == rhs.m_opaque_sp.get());
360 return false;
363 bool SBTypeSummary::operator!=(lldb::SBTypeSummary &rhs) {
364 LLDB_INSTRUMENT_VA(this, rhs);
366 if (!IsValid())
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() {
381 if (!IsValid())
382 return false;
384 if (m_opaque_sp.use_count() == 1)
385 return true;
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()));
405 SetSP(new_sp);
407 return nullptr != new_sp.get();
410 bool SBTypeSummary::ChangeSummaryType(bool want_script) {
411 if (!IsValid())
412 return false;
414 TypeSummaryImplSP new_sp;
416 if (want_script ==
417 (m_opaque_sp->GetKind() == TypeSummaryImpl::Kind::eScript)) {
418 if (m_opaque_sp->GetKind() ==
419 lldb_private::TypeSummaryImpl::Kind::eCallback &&
420 !want_script)
421 new_sp = TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
422 else
423 return CopyOnWrite_Impl();
426 if (!new_sp) {
427 if (want_script)
428 new_sp = TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
429 else
430 new_sp = TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
433 SetSP(new_sp);
435 return true;