LLDB  mainline
SBTypeSummary.cpp
Go to the documentation of this file.
1 //===-- SBTypeSummary.cpp -----------------------------------------*- C++
2 //-*-===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBTypeSummary.h"
11 #include "SBReproducerPrivate.h"
12 #include "Utils.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBValue.h"
16 
17 #include "llvm/Support/Casting.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
24 
25  m_opaque_up.reset(new TypeSummaryOptions());
26 }
27 
29  const lldb::SBTypeSummaryOptions &rhs) {
31  (const lldb::SBTypeSummaryOptions &), rhs);
32 
33  m_opaque_up = clone(rhs.m_opaque_up);
34 }
35 
37 
40  return this->operator bool();
41 }
42 SBTypeSummaryOptions::operator bool() const {
44 
45  return m_opaque_up.get();
46 }
47 
50  GetLanguage);
51 
52  if (IsValid())
53  return m_opaque_up->GetLanguage();
55 }
56 
59  GetCapping);
60 
61  if (IsValid())
62  return m_opaque_up->GetCapping();
63  return eTypeSummaryCapped;
64 }
65 
67  LLDB_RECORD_METHOD(void, SBTypeSummaryOptions, SetLanguage,
68  (lldb::LanguageType), l);
69 
70  if (IsValid())
71  m_opaque_up->SetLanguage(l);
72 }
73 
75  LLDB_RECORD_METHOD(void, SBTypeSummaryOptions, SetCapping,
77 
78  if (IsValid())
79  m_opaque_up->SetCapping(c);
80 }
81 
83  return m_opaque_up.get();
84 }
85 
87 operator->() const {
88  return m_opaque_up.get();
89 }
90 
92  return m_opaque_up.get();
93 }
94 
96  return *m_opaque_up;
97 }
98 
100  return *m_opaque_up;
101 }
102 
104  const lldb_private::TypeSummaryOptions *lldb_object_ptr) {
107  lldb_object_ptr);
108 
109  SetOptions(lldb_object_ptr);
110 }
111 
113  const lldb_private::TypeSummaryOptions *lldb_object_ptr) {
114  if (lldb_object_ptr)
115  m_opaque_up.reset(new TypeSummaryOptions(*lldb_object_ptr));
116  else
117  m_opaque_up.reset(new TypeSummaryOptions());
118 }
119 
120 SBTypeSummary::SBTypeSummary() : m_opaque_sp() {
122 }
123 
125  uint32_t options) {
127  CreateWithSummaryString, (const char *, uint32_t),
128  data, options);
129 
130  if (!data || data[0] == 0)
132 
133  return LLDB_RECORD_RESULT(
134  SBTypeSummary(TypeSummaryImplSP(new StringSummaryFormat(options, data))));
135 }
136 
138  uint32_t options) {
140  CreateWithFunctionName, (const char *, uint32_t),
141  data, options);
142 
143  if (!data || data[0] == 0)
145 
146  return LLDB_RECORD_RESULT(
147  SBTypeSummary(TypeSummaryImplSP(new ScriptSummaryFormat(options, data))));
148 }
149 
151  uint32_t options) {
153  CreateWithScriptCode, (const char *, uint32_t),
154  data, options);
155 
156  if (!data || data[0] == 0)
158 
160  TypeSummaryImplSP(new ScriptSummaryFormat(options, "", data))));
161 }
162 
164  uint32_t options,
165  const char *description) {
168  (lldb::SBTypeSummary::FormatCallback, uint32_t, const char *), cb,
169  options, description);
170 
171  SBTypeSummary retval;
172  if (cb) {
173  retval.SetSP(TypeSummaryImplSP(new CXXFunctionSummaryFormat(
174  options,
175  [cb](ValueObject &valobj, Stream &stm,
176  const TypeSummaryOptions &opt) -> bool {
177  SBStream stream;
178  SBValue sb_value(valobj.GetSP());
179  SBTypeSummaryOptions options(&opt);
180  if (!cb(sb_value, options, stream))
181  return false;
182  stm.Write(stream.GetData(), stream.GetSize());
183  return true;
184  },
185  description ? description : "callback summary formatter")));
186  }
187 
188  return retval;
189 }
190 
192  : m_opaque_sp(rhs.m_opaque_sp) {
194 }
195 
197 
200  return this->operator bool();
201 }
202 SBTypeSummary::operator bool() const {
203  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSummary, operator bool);
204 
205  return m_opaque_sp.get() != NULL;
206 }
207 
210 
211  if (!IsValid())
212  return false;
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);
217  }
218  return false;
219 }
220 
223 
224  if (!IsValid())
225  return false;
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);
230  }
231  return false;
232 }
233 
236 
237  if (!IsValid())
238  return false;
239 
240  return m_opaque_sp->GetKind() == TypeSummaryImpl::Kind::eSummaryString;
241 }
242 
243 const char *SBTypeSummary::GetData() {
245 
246  if (!IsValid())
247  return NULL;
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();
252  if (ftext && *ftext)
253  return ftext;
254  return fname;
255  } else if (StringSummaryFormat *string_summary_ptr =
256  llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
257  return string_summary_ptr->GetSummaryString();
258  return nullptr;
259 }
260 
263 
264  if (!IsValid())
265  return lldb::eTypeOptionNone;
266  return m_opaque_sp->GetOptions();
267 }
268 
271 
272  if (!CopyOnWrite_Impl())
273  return;
274  m_opaque_sp->SetOptions(value);
275 }
276 
277 void SBTypeSummary::SetSummaryString(const char *data) {
278  LLDB_RECORD_METHOD(void, SBTypeSummary, SetSummaryString, (const char *),
279  data);
280 
281  if (!IsValid())
282  return;
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);
288 }
289 
290 void SBTypeSummary::SetFunctionName(const char *data) {
291  LLDB_RECORD_METHOD(void, SBTypeSummary, SetFunctionName, (const char *),
292  data);
293 
294  if (!IsValid())
295  return;
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);
301 }
302 
303 void SBTypeSummary::SetFunctionCode(const char *data) {
304  LLDB_RECORD_METHOD(void, SBTypeSummary, SetFunctionCode, (const char *),
305  data);
306 
307  if (!IsValid())
308  return;
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);
314 }
315 
317  lldb::DescriptionLevel description_level) {
319  (lldb::SBStream &, lldb::DescriptionLevel), description,
320  description_level);
321 
322  if (!CopyOnWrite_Impl())
323  return false;
324  else {
325  description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
326  return true;
327  }
328 }
329 
332  value);
333 
334  if (!IsValid())
335  return false;
336  lldb::ValueObjectSP value_sp = value.GetSP();
337  return m_opaque_sp->DoesPrintValue(value_sp.get());
338 }
339 
342  SBTypeSummary, operator=,(const lldb::SBTypeSummary &),
343  rhs);
344 
345  if (this != &rhs) {
346  m_opaque_sp = rhs.m_opaque_sp;
347  }
348  return LLDB_RECORD_RESULT(*this);
349 }
350 
353  rhs);
354 
355  if (!IsValid())
356  return !rhs.IsValid();
357  return m_opaque_sp == rhs.m_opaque_sp;
358 }
359 
362  rhs);
363 
364  if (IsValid()) {
365  // valid and invalid are different
366  if (!rhs.IsValid())
367  return false;
368  } else {
369  // invalid and valid are different
370  if (rhs.IsValid())
371  return false;
372  else
373  // both invalid are the same
374  return true;
375  }
376 
377  if (m_opaque_sp->GetKind() != rhs.m_opaque_sp->GetKind())
378  return false;
379 
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())
386  return false;
387  if (IsFunctionName() != rhs.IsFunctionName())
388  return false;
389  return GetOptions() == rhs.GetOptions();
390  case TypeSummaryImpl::Kind::eSummaryString:
391  if (IsSummaryString() != rhs.IsSummaryString())
392  return false;
393  return GetOptions() == rhs.GetOptions();
394  case TypeSummaryImpl::Kind::eInternal:
395  return (m_opaque_sp.get() == rhs.m_opaque_sp.get());
396  }
397 
398  return false;
399 }
400 
403  rhs);
404 
405  if (!IsValid())
406  return !rhs.IsValid();
407  return m_opaque_sp != rhs.m_opaque_sp;
408 }
409 
410 lldb::TypeSummaryImplSP SBTypeSummary::GetSP() { return m_opaque_sp; }
411 
412 void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP &typesummary_impl_sp) {
413  m_opaque_sp = typesummary_impl_sp;
414 }
415 
416 SBTypeSummary::SBTypeSummary(const lldb::TypeSummaryImplSP &typesummary_impl_sp)
417  : m_opaque_sp(typesummary_impl_sp) {}
418 
420  if (!IsValid())
421  return false;
422 
423  if (m_opaque_sp.unique())
424  return true;
425 
426  TypeSummaryImplSP new_sp;
427 
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()));
442  }
443 
444  SetSP(new_sp);
445 
446  return nullptr != new_sp.get();
447 }
448 
449 bool SBTypeSummary::ChangeSummaryType(bool want_script) {
450  if (!IsValid())
451  return false;
452 
453  TypeSummaryImplSP new_sp;
454 
455  if (want_script ==
456  (m_opaque_sp->GetKind() == TypeSummaryImpl::Kind::eScript)) {
457  if (m_opaque_sp->GetKind() ==
459  !want_script)
460  new_sp = TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
461  else
462  return CopyOnWrite_Impl();
463  }
464 
465  if (!new_sp) {
466  if (want_script)
467  new_sp = TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
468  else
469  new_sp = TypeSummaryImplSP(new StringSummaryFormat(GetOptions(), ""));
470  }
471 
472  SetSP(new_sp);
473 
474  return true;
475 }
476 
477 namespace lldb_private {
478 namespace repro {
479 
480 template <>
484  (const lldb::SBTypeSummaryOptions &));
486  LLDB_REGISTER_METHOD_CONST(bool, SBTypeSummaryOptions, operator bool, ());
488  ());
490  GetCapping, ());
491  LLDB_REGISTER_METHOD(void, SBTypeSummaryOptions, SetLanguage,
493  LLDB_REGISTER_METHOD(void, SBTypeSummaryOptions, SetCapping,
497 }
498 
499 template <>
504  (const char *, uint32_t));
507  (const char *, uint32_t));
509  CreateWithScriptCode, (const char *, uint32_t));
512  LLDB_REGISTER_METHOD_CONST(bool, SBTypeSummary, operator bool, ());
516  LLDB_REGISTER_METHOD(const char *, SBTypeSummary, GetData, ());
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 *));
527  SBTypeSummary, operator=,(const lldb::SBTypeSummary &));
529  SBTypeSummary, operator==,(lldb::SBTypeSummary &));
531  (lldb::SBTypeSummary &));
533  SBTypeSummary, operator!=,(lldb::SBTypeSummary &));
534 }
535 
536 }
537 }
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static SBTypeSummary CreateWithCallback(FormatCallback cb, uint32_t options=0, const char *description=nullptr)
lldb_private::TypeSummaryOptions & ref()
#define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature,...)
#define LLDB_RECORD_DUMMY(Result, Class, Method, Signature,...)
The LLDB_RECORD_DUMMY macro is special because it doesn&#39;t actually record anything.
bool operator!=(lldb::SBTypeSummary &rhs)
void SetFunctionCode(const char *data)
void SetCapping(lldb::TypeSummaryCapping)
lldb::SBTypeSummary & operator=(const lldb::SBTypeSummary &rhs)
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:100
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
lldb::TypeSummaryCapping GetCapping()
void RegisterMethods< SBTypeSummaryOptions >(Registry &R)
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:1108
void SetOptions(const lldb_private::TypeSummaryOptions *lldb_object_ptr)
bool DoesPrintValue(lldb::SBValue value)
bool operator==(lldb::SBTypeSummary &rhs)
const char * GetData()
Definition: SBStream.cpp:42
bool ChangeSummaryType(bool want_script)
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
LanguageType
Programming language type.
lldb_private::TypeSummaryOptions * get()
lldb::LanguageType GetLanguage()
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:62
void SetLanguage(lldb::LanguageType)
static SBTypeSummary CreateWithSummaryString(const char *data, uint32_t options=0)
void SetSummaryString(const char *data)
lldb::TypeSummaryImplSP GetSP()
void RegisterMethods< SBTypeSummary >(Registry &R)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
size_t GetSize()
Definition: SBStream.cpp:53
void SetSP(const lldb::TypeSummaryImplSP &typefilter_impl_sp)
bool(* FormatCallback)(SBValue, SBTypeSummaryOptions, SBStream &)
Definition: SBTypeSummary.h:62
lldb::TypeSummaryImplSP m_opaque_sp
void SetOptions(uint32_t)
Unknown or invalid language value.
Definition: SBAddress.h:15
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:565
void SetFunctionName(const char *data)
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
bool GetDescription(lldb::SBStream &description, lldb::DescriptionLevel description_level)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
lldb_private::TypeSummaryOptions * operator->()
const char * GetData()
bool IsEqualTo(lldb::SBTypeSummary &rhs)
#define LLDB_RECORD_RESULT(Result)
static SBTypeSummary CreateWithFunctionName(const char *data, uint32_t options=0)
static SBTypeSummary CreateWithScriptCode(const char *data, uint32_t options=0)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)