LLDB  mainline
SBTypeCategory.cpp
Go to the documentation of this file.
1 //===-- SBTypeCategory.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 
11 #include "SBReproducerPrivate.h"
12 
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBTypeFilter.h"
15 #include "lldb/API/SBTypeFormat.h"
17 #include "lldb/API/SBTypeSummary.h"
19 
20 #include "lldb/Core/Debugger.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 typedef std::pair<lldb::TypeCategoryImplSP, user_id_t> ImplType;
29 
30 SBTypeCategory::SBTypeCategory() : m_opaque_sp() {
32 }
33 
35  DataVisualization::Categories::GetCategory(ConstString(name), m_opaque_sp);
36 }
37 
39  : m_opaque_sp(rhs.m_opaque_sp) {
41 }
42 
44 
47  return this->operator bool();
48 }
49 SBTypeCategory::operator bool() const {
51 
52  return (m_opaque_sp.get() != NULL);
53 }
54 
57 
58  if (!IsValid())
59  return false;
60  return m_opaque_sp->IsEnabled();
61 }
62 
63 void SBTypeCategory::SetEnabled(bool enabled) {
64  LLDB_RECORD_METHOD(void, SBTypeCategory, SetEnabled, (bool), enabled);
65 
66  if (!IsValid())
67  return;
68  if (enabled)
69  DataVisualization::Categories::Enable(m_opaque_sp);
70  else
71  DataVisualization::Categories::Disable(m_opaque_sp);
72 }
73 
74 const char *SBTypeCategory::GetName() {
76 
77  if (!IsValid())
78  return NULL;
79  return m_opaque_sp->GetName();
80 }
81 
84  (uint32_t), idx);
85 
86  if (IsValid())
87  return m_opaque_sp->GetLanguageAtIndex(idx);
89 }
90 
93 
94  if (IsValid())
95  return m_opaque_sp->GetNumLanguages();
96  return 0;
97 }
98 
101  language);
102 
103  if (IsValid())
104  m_opaque_sp->AddLanguage(language);
105 }
106 
109 
110  if (!IsValid())
111  return 0;
112 
113  return m_opaque_sp->GetTypeFormatsContainer()->GetCount() +
114  m_opaque_sp->GetRegexTypeFormatsContainer()->GetCount();
115 }
116 
119 
120  if (!IsValid())
121  return 0;
122  return m_opaque_sp->GetTypeSummariesContainer()->GetCount() +
123  m_opaque_sp->GetRegexTypeSummariesContainer()->GetCount();
124 }
125 
128 
129  if (!IsValid())
130  return 0;
131  return m_opaque_sp->GetTypeFiltersContainer()->GetCount() +
132  m_opaque_sp->GetRegexTypeFiltersContainer()->GetCount();
133 }
134 
137 
138  if (!IsValid())
139  return 0;
140  return m_opaque_sp->GetTypeSyntheticsContainer()->GetCount() +
141  m_opaque_sp->GetRegexTypeSyntheticsContainer()->GetCount();
142 }
143 
148 
149  if (!IsValid())
152  m_opaque_sp->GetTypeNameSpecifierForFilterAtIndex(index)));
153 }
154 
159 
160  if (!IsValid())
163  m_opaque_sp->GetTypeNameSpecifierForFormatAtIndex(index)));
164 }
165 
170 
171  if (!IsValid())
174  m_opaque_sp->GetTypeNameSpecifierForSummaryAtIndex(index)));
175 }
176 
181  index);
182 
183  if (!IsValid())
186  m_opaque_sp->GetTypeNameSpecifierForSyntheticAtIndex(index)));
187 }
188 
191  (lldb::SBTypeNameSpecifier), spec);
192 
193  if (!IsValid())
195 
196  if (!spec.IsValid())
198 
199  lldb::TypeFilterImplSP children_sp;
200 
201  if (spec.IsRegex())
202  m_opaque_sp->GetRegexTypeFiltersContainer()->GetExact(
203  ConstString(spec.GetName()), children_sp);
204  else
205  m_opaque_sp->GetTypeFiltersContainer()->GetExact(
206  ConstString(spec.GetName()), children_sp);
207 
208  if (!children_sp)
210 
211  TypeFilterImplSP filter_sp =
213 
214  return LLDB_RECORD_RESULT(lldb::SBTypeFilter(filter_sp));
215 }
218  (lldb::SBTypeNameSpecifier), spec);
219 
220  if (!IsValid())
222 
223  if (!spec.IsValid())
225 
226  lldb::TypeFormatImplSP format_sp;
227 
228  if (spec.IsRegex())
229  m_opaque_sp->GetRegexTypeFormatsContainer()->GetExact(
230  ConstString(spec.GetName()), format_sp);
231  else
232  m_opaque_sp->GetTypeFormatsContainer()->GetExact(
233  ConstString(spec.GetName()), format_sp);
234 
235  if (!format_sp)
237 
238  return LLDB_RECORD_RESULT(lldb::SBTypeFormat(format_sp));
239 }
240 
243  (lldb::SBTypeNameSpecifier), spec);
244 
245  if (!IsValid())
247 
248  if (!spec.IsValid())
250 
251  lldb::TypeSummaryImplSP summary_sp;
252 
253  if (spec.IsRegex())
254  m_opaque_sp->GetRegexTypeSummariesContainer()->GetExact(
255  ConstString(spec.GetName()), summary_sp);
256  else
257  m_opaque_sp->GetTypeSummariesContainer()->GetExact(
258  ConstString(spec.GetName()), summary_sp);
259 
260  if (!summary_sp)
262 
263  return LLDB_RECORD_RESULT(lldb::SBTypeSummary(summary_sp));
264 }
265 
268  (lldb::SBTypeNameSpecifier), spec);
269 
270  if (!IsValid())
272 
273  if (!spec.IsValid())
275 
276  lldb::SyntheticChildrenSP children_sp;
277 
278  if (spec.IsRegex())
279  m_opaque_sp->GetRegexTypeSyntheticsContainer()->GetExact(
280  ConstString(spec.GetName()), children_sp);
281  else
282  m_opaque_sp->GetTypeSyntheticsContainer()->GetExact(
283  ConstString(spec.GetName()), children_sp);
284 
285  if (!children_sp)
287 
288  ScriptedSyntheticChildrenSP synth_sp =
290 
291  return LLDB_RECORD_RESULT(lldb::SBTypeSynthetic(synth_sp));
292 }
293 
296  (uint32_t), index);
297 
298  if (!IsValid())
300  lldb::SyntheticChildrenSP children_sp =
301  m_opaque_sp->GetSyntheticAtIndex((index));
302 
303  if (!children_sp.get())
305 
306  TypeFilterImplSP filter_sp =
308 
309  return LLDB_RECORD_RESULT(lldb::SBTypeFilter(filter_sp));
310 }
311 
314  (uint32_t), index);
315 
316  if (!IsValid())
318  return LLDB_RECORD_RESULT(
319  SBTypeFormat(m_opaque_sp->GetFormatAtIndex((index))));
320 }
321 
324  (uint32_t), index);
325 
326  if (!IsValid())
328  return LLDB_RECORD_RESULT(
329  SBTypeSummary(m_opaque_sp->GetSummaryAtIndex((index))));
330 }
331 
334  (uint32_t), index);
335 
336  if (!IsValid())
338  lldb::SyntheticChildrenSP children_sp =
339  m_opaque_sp->GetSyntheticAtIndex((index));
340 
341  if (!children_sp.get())
343 
344  ScriptedSyntheticChildrenSP synth_sp =
346 
347  return LLDB_RECORD_RESULT(lldb::SBTypeSynthetic(synth_sp));
348 }
349 
351  SBTypeFormat format) {
354  format);
355 
356  if (!IsValid())
357  return false;
358 
359  if (!type_name.IsValid())
360  return false;
361 
362  if (!format.IsValid())
363  return false;
364 
365  if (type_name.IsRegex())
366  m_opaque_sp->GetRegexTypeFormatsContainer()->Add(
367  lldb::RegularExpressionSP(new RegularExpression(
368  llvm::StringRef::withNullAsEmpty(type_name.GetName()))),
369  format.GetSP());
370  else
371  m_opaque_sp->GetTypeFormatsContainer()->Add(
372  ConstString(type_name.GetName()), format.GetSP());
373 
374  return true;
375 }
376 
379  (lldb::SBTypeNameSpecifier), type_name);
380 
381  if (!IsValid())
382  return false;
383 
384  if (!type_name.IsValid())
385  return false;
386 
387  if (type_name.IsRegex())
388  return m_opaque_sp->GetRegexTypeFormatsContainer()->Delete(
389  ConstString(type_name.GetName()));
390  else
391  return m_opaque_sp->GetTypeFormatsContainer()->Delete(
392  ConstString(type_name.GetName()));
393 }
394 
396  SBTypeSummary summary) {
399  type_name, summary);
400 
401  if (!IsValid())
402  return false;
403 
404  if (!type_name.IsValid())
405  return false;
406 
407  if (!summary.IsValid())
408  return false;
409 
410  // FIXME: we need to iterate over all the Debugger objects and have each of
411  // them contain a copy of the function
412  // since we currently have formatters live in a global space, while Python
413  // code lives in a specific Debugger-related environment this should
414  // eventually be fixed by deciding a final location in the LLDB object space
415  // for formatters
416  if (summary.IsFunctionCode()) {
417  const void *name_token =
418  (const void *)ConstString(type_name.GetName()).GetCString();
419  const char *script = summary.GetData();
420  StringList input;
421  input.SplitIntoLines(script, strlen(script));
423  bool need_set = true;
424  for (uint32_t j = 0; j < num_debuggers; j++) {
425  DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
426  if (debugger_sp) {
427  ScriptInterpreter *interpreter_ptr =
428  debugger_sp->GetScriptInterpreter();
429  if (interpreter_ptr) {
430  std::string output;
431  if (interpreter_ptr->GenerateTypeScriptFunction(input, output,
432  name_token) &&
433  !output.empty()) {
434  if (need_set) {
435  need_set = false;
436  summary.SetFunctionName(output.c_str());
437  }
438  }
439  }
440  }
441  }
442  }
443 
444  if (type_name.IsRegex())
445  m_opaque_sp->GetRegexTypeSummariesContainer()->Add(
446  lldb::RegularExpressionSP(new RegularExpression(
447  llvm::StringRef::withNullAsEmpty(type_name.GetName()))),
448  summary.GetSP());
449  else
450  m_opaque_sp->GetTypeSummariesContainer()->Add(
451  ConstString(type_name.GetName()), summary.GetSP());
452 
453  return true;
454 }
455 
458  (lldb::SBTypeNameSpecifier), type_name);
459 
460  if (!IsValid())
461  return false;
462 
463  if (!type_name.IsValid())
464  return false;
465 
466  if (type_name.IsRegex())
467  return m_opaque_sp->GetRegexTypeSummariesContainer()->Delete(
468  ConstString(type_name.GetName()));
469  else
470  return m_opaque_sp->GetTypeSummariesContainer()->Delete(
471  ConstString(type_name.GetName()));
472 }
473 
475  SBTypeFilter filter) {
478  filter);
479 
480  if (!IsValid())
481  return false;
482 
483  if (!type_name.IsValid())
484  return false;
485 
486  if (!filter.IsValid())
487  return false;
488 
489  if (type_name.IsRegex())
490  m_opaque_sp->GetRegexTypeFiltersContainer()->Add(
491  lldb::RegularExpressionSP(new RegularExpression(
492  llvm::StringRef::withNullAsEmpty(type_name.GetName()))),
493  filter.GetSP());
494  else
495  m_opaque_sp->GetTypeFiltersContainer()->Add(
496  ConstString(type_name.GetName()), filter.GetSP());
497 
498  return true;
499 }
500 
503  (lldb::SBTypeNameSpecifier), type_name);
504 
505  if (!IsValid())
506  return false;
507 
508  if (!type_name.IsValid())
509  return false;
510 
511  if (type_name.IsRegex())
512  return m_opaque_sp->GetRegexTypeFiltersContainer()->Delete(
513  ConstString(type_name.GetName()));
514  else
515  return m_opaque_sp->GetTypeFiltersContainer()->Delete(
516  ConstString(type_name.GetName()));
517 }
518 
520  SBTypeSynthetic synth) {
523  type_name, synth);
524 
525  if (!IsValid())
526  return false;
527 
528  if (!type_name.IsValid())
529  return false;
530 
531  if (!synth.IsValid())
532  return false;
533 
534  // FIXME: we need to iterate over all the Debugger objects and have each of
535  // them contain a copy of the function
536  // since we currently have formatters live in a global space, while Python
537  // code lives in a specific Debugger-related environment this should
538  // eventually be fixed by deciding a final location in the LLDB object space
539  // for formatters
540  if (synth.IsClassCode()) {
541  const void *name_token =
542  (const void *)ConstString(type_name.GetName()).GetCString();
543  const char *script = synth.GetData();
544  StringList input;
545  input.SplitIntoLines(script, strlen(script));
547  bool need_set = true;
548  for (uint32_t j = 0; j < num_debuggers; j++) {
549  DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
550  if (debugger_sp) {
551  ScriptInterpreter *interpreter_ptr =
552  debugger_sp->GetScriptInterpreter();
553  if (interpreter_ptr) {
554  std::string output;
555  if (interpreter_ptr->GenerateTypeSynthClass(input, output,
556  name_token) &&
557  !output.empty()) {
558  if (need_set) {
559  need_set = false;
560  synth.SetClassName(output.c_str());
561  }
562  }
563  }
564  }
565  }
566  }
567 
568  if (type_name.IsRegex())
569  m_opaque_sp->GetRegexTypeSyntheticsContainer()->Add(
570  lldb::RegularExpressionSP(new RegularExpression(
571  llvm::StringRef::withNullAsEmpty(type_name.GetName()))),
572  synth.GetSP());
573  else
574  m_opaque_sp->GetTypeSyntheticsContainer()->Add(
575  ConstString(type_name.GetName()), synth.GetSP());
576 
577  return true;
578 }
579 
582  (lldb::SBTypeNameSpecifier), type_name);
583 
584  if (!IsValid())
585  return false;
586 
587  if (!type_name.IsValid())
588  return false;
589 
590  if (type_name.IsRegex())
591  return m_opaque_sp->GetRegexTypeSyntheticsContainer()->Delete(
592  ConstString(type_name.GetName()));
593  else
594  return m_opaque_sp->GetTypeSyntheticsContainer()->Delete(
595  ConstString(type_name.GetName()));
596 }
597 
599  lldb::DescriptionLevel description_level) {
601  (lldb::SBStream &, lldb::DescriptionLevel), description,
602  description_level);
603 
604  if (!IsValid())
605  return false;
606  description.Printf("Category name: %s\n", GetName());
607  return true;
608 }
609 
613  SBTypeCategory, operator=,(const lldb::SBTypeCategory &),
614  rhs);
615 
616  if (this != &rhs) {
617  m_opaque_sp = rhs.m_opaque_sp;
618  }
619  return LLDB_RECORD_RESULT(*this);
620 }
621 
624  rhs);
625 
626  if (!IsValid())
627  return !rhs.IsValid();
628 
629  return m_opaque_sp.get() == rhs.m_opaque_sp.get();
630 }
631 
634  rhs);
635 
636  if (!IsValid())
637  return rhs.IsValid();
638 
639  return m_opaque_sp.get() != rhs.m_opaque_sp.get();
640 }
641 
642 lldb::TypeCategoryImplSP SBTypeCategory::GetSP() {
643  if (!IsValid())
644  return lldb::TypeCategoryImplSP();
645  return m_opaque_sp;
646 }
647 
649  const lldb::TypeCategoryImplSP &typecategory_impl_sp) {
650  m_opaque_sp = typecategory_impl_sp;
651 }
652 
654  const lldb::TypeCategoryImplSP &typecategory_impl_sp)
655  : m_opaque_sp(typecategory_impl_sp) {}
656 
658  if (!IsValid())
659  return false;
660 
661  return (strcmp(m_opaque_sp->GetName(), "default") == 0);
662 }
663 
664 namespace lldb_private {
665 namespace repro {
666 
667 template <>
672  LLDB_REGISTER_METHOD_CONST(bool, SBTypeCategory, operator bool, ());
675  LLDB_REGISTER_METHOD(const char *, SBTypeCategory, GetName, ());
677  (uint32_t));
692  (uint32_t));
694  (uint32_t));
714  (uint32_t));
729  SBTypeCategory, operator=,(const lldb::SBTypeCategory &));
731  SBTypeCategory, operator==,(lldb::SBTypeCategory &));
733  SBTypeCategory, operator!=,(lldb::SBTypeCategory &));
734 }
735 
736 }
737 }
void AddLanguage(lldb::LanguageType language)
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)
lldb::LanguageType GetLanguageAtIndex(uint32_t idx)
bool DeleteTypeFormat(SBTypeNameSpecifier)
virtual bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, const void *name_token=nullptr)
lldb::TypeFormatImplSP GetSP()
bool operator==(lldb::SBTypeCategory &rhs)
bool AddTypeFilter(SBTypeNameSpecifier, SBTypeFilter)
bool AddTypeSynthetic(SBTypeNameSpecifier, SBTypeSynthetic)
void RegisterMethods< SBTypeCategory >(Registry &R)
"lldb/Utility/RegularExpression.h" A C++ wrapper class for regex.
SBTypeNameSpecifier GetTypeNameSpecifierForFormatAtIndex(uint32_t)
virtual bool GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token=nullptr)
SBTypeNameSpecifier GetTypeNameSpecifierForFilterAtIndex(uint32_t)
static size_t GetNumDebuggers()
Definition: Debugger.cpp:1157
void SetClassName(const char *data)
SBTypeFormat GetFormatForType(SBTypeNameSpecifier)
lldb::SBTypeCategory & operator=(const lldb::SBTypeCategory &rhs)
SBTypeNameSpecifier GetTypeNameSpecifierForSummaryAtIndex(uint32_t)
bool operator!=(lldb::SBTypeCategory &rhs)
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
LanguageType
Programming language type.
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:62
lldb::TypeSummaryImplSP GetSP()
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
bool DeleteTypeSynthetic(SBTypeNameSpecifier)
void SetSP(const lldb::TypeCategoryImplSP &typecategory_impl_sp)
bool DeleteTypeFilter(SBTypeNameSpecifier)
class LLDB_API SBTypeFilter
Definition: SBDefines.h:82
bool IsValid() const
SBTypeFormat GetFormatAtIndex(uint32_t)
class LLDB_API SBTypeSummary
Definition: SBDefines.h:86
const char * GetName()
SBTypeSynthetic GetSyntheticAtIndex(uint32_t)
bool DeleteTypeSummary(SBTypeNameSpecifier)
lldb::ScriptedSyntheticChildrenSP GetSP()
class LLDB_API SBTypeFormat
Definition: SBDefines.h:83
class LLDB_API SBTypeSynthetic
Definition: SBDefines.h:88
TypeCategoryImplSP m_opaque_sp
SBTypeSynthetic GetSyntheticForType(SBTypeNameSpecifier)
A uniqued constant string class.
Definition: ConstString.h:38
Unknown or invalid language value.
Definition: SBAddress.h:15
class LLDB_API SBTypeNameSpecifier
Definition: SBDefines.h:85
SharingPtr< T > static_pointer_cast(const SharingPtr< U > &r)
Definition: SharingPtr.h:355
lldb::TypeCategoryImplSP GetSP()
void SetFunctionName(const char *data)
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
SBTypeFilter GetFilterAtIndex(uint32_t)
bool GetDescription(lldb::SBStream &description, lldb::DescriptionLevel description_level)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
std::pair< lldb::TypeCategoryImplSP, user_id_t > ImplType
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:149
const char * GetData()
SBTypeFilter GetFilterForType(SBTypeNameSpecifier)
bool IsValid() const
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
Definition: Debugger.cpp:1165
bool AddTypeSummary(SBTypeNameSpecifier, SBTypeSummary)
bool AddTypeFormat(SBTypeNameSpecifier, SBTypeFormat)
#define LLDB_RECORD_RESULT(Result)
SBTypeSummary GetSummaryAtIndex(uint32_t)
lldb::TypeFilterImplSP GetSP()
SBTypeNameSpecifier GetTypeNameSpecifierForSyntheticAtIndex(uint32_t)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
SBTypeSummary GetSummaryForType(SBTypeNameSpecifier)