1 //===-- TypeSummary.h -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef lldb_TypeSummary_h_ 11 #define lldb_TypeSummary_h_ 12 13 #include <stdint.h> 14 15 #include <functional> 16 #include <memory> 17 #include <string> 18 19 #include "lldb/lldb-enumerations.h" 20 #include "lldb/lldb-public.h" 21 22 #include "lldb/Core/FormatEntity.h" 23 #include "lldb/Utility/Status.h" 24 #include "lldb/Utility/StructuredData.h" 25 26 namespace lldb_private { 27 class TypeSummaryOptions { 28 public: 29 TypeSummaryOptions(); 30 TypeSummaryOptions(const TypeSummaryOptions &rhs); 31 32 ~TypeSummaryOptions() = default; 33 34 TypeSummaryOptions &operator=(const TypeSummaryOptions &rhs); 35 36 lldb::LanguageType GetLanguage() const; 37 38 lldb::TypeSummaryCapping GetCapping() const; 39 40 TypeSummaryOptions &SetLanguage(lldb::LanguageType); 41 42 TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping); 43 44 private: 45 lldb::LanguageType m_lang; 46 lldb::TypeSummaryCapping m_capping; 47 }; 48 49 class TypeSummaryImpl { 50 public: 51 enum class Kind { eSummaryString, eScript, eCallback, eInternal }; 52 53 virtual ~TypeSummaryImpl() = default; 54 GetKind()55 Kind GetKind() const { return m_kind; } 56 57 class Flags { 58 public: Flags()59 Flags() : m_flags(lldb::eTypeOptionCascade) {} 60 Flags(const Flags & other)61 Flags(const Flags &other) : m_flags(other.m_flags) {} 62 Flags(uint32_t value)63 Flags(uint32_t value) : m_flags(value) {} 64 65 Flags &operator=(const Flags &rhs) { 66 if (&rhs != this) 67 m_flags = rhs.m_flags; 68 69 return *this; 70 } 71 72 Flags &operator=(const uint32_t &rhs) { 73 m_flags = rhs; 74 return *this; 75 } 76 Clear()77 Flags &Clear() { 78 m_flags = 0; 79 return *this; 80 } 81 GetCascades()82 bool GetCascades() const { 83 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade; 84 } 85 86 Flags &SetCascades(bool value = true) { 87 if (value) 88 m_flags |= lldb::eTypeOptionCascade; 89 else 90 m_flags &= ~lldb::eTypeOptionCascade; 91 return *this; 92 } 93 GetSkipPointers()94 bool GetSkipPointers() const { 95 return (m_flags & lldb::eTypeOptionSkipPointers) == 96 lldb::eTypeOptionSkipPointers; 97 } 98 99 Flags &SetSkipPointers(bool value = true) { 100 if (value) 101 m_flags |= lldb::eTypeOptionSkipPointers; 102 else 103 m_flags &= ~lldb::eTypeOptionSkipPointers; 104 return *this; 105 } 106 GetSkipReferences()107 bool GetSkipReferences() const { 108 return (m_flags & lldb::eTypeOptionSkipReferences) == 109 lldb::eTypeOptionSkipReferences; 110 } 111 112 Flags &SetSkipReferences(bool value = true) { 113 if (value) 114 m_flags |= lldb::eTypeOptionSkipReferences; 115 else 116 m_flags &= ~lldb::eTypeOptionSkipReferences; 117 return *this; 118 } 119 GetDontShowChildren()120 bool GetDontShowChildren() const { 121 return (m_flags & lldb::eTypeOptionHideChildren) == 122 lldb::eTypeOptionHideChildren; 123 } 124 125 Flags &SetDontShowChildren(bool value = true) { 126 if (value) 127 m_flags |= lldb::eTypeOptionHideChildren; 128 else 129 m_flags &= ~lldb::eTypeOptionHideChildren; 130 return *this; 131 } 132 GetHideEmptyAggregates()133 bool GetHideEmptyAggregates() const { 134 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) == 135 lldb::eTypeOptionHideEmptyAggregates; 136 } 137 138 Flags &SetHideEmptyAggregates(bool value = true) { 139 if (value) 140 m_flags |= lldb::eTypeOptionHideEmptyAggregates; 141 else 142 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates; 143 return *this; 144 } 145 GetDontShowValue()146 bool GetDontShowValue() const { 147 return (m_flags & lldb::eTypeOptionHideValue) == 148 lldb::eTypeOptionHideValue; 149 } 150 151 Flags &SetDontShowValue(bool value = true) { 152 if (value) 153 m_flags |= lldb::eTypeOptionHideValue; 154 else 155 m_flags &= ~lldb::eTypeOptionHideValue; 156 return *this; 157 } 158 GetShowMembersOneLiner()159 bool GetShowMembersOneLiner() const { 160 return (m_flags & lldb::eTypeOptionShowOneLiner) == 161 lldb::eTypeOptionShowOneLiner; 162 } 163 164 Flags &SetShowMembersOneLiner(bool value = true) { 165 if (value) 166 m_flags |= lldb::eTypeOptionShowOneLiner; 167 else 168 m_flags &= ~lldb::eTypeOptionShowOneLiner; 169 return *this; 170 } 171 GetHideItemNames()172 bool GetHideItemNames() const { 173 return (m_flags & lldb::eTypeOptionHideNames) == 174 lldb::eTypeOptionHideNames; 175 } 176 177 Flags &SetHideItemNames(bool value = true) { 178 if (value) 179 m_flags |= lldb::eTypeOptionHideNames; 180 else 181 m_flags &= ~lldb::eTypeOptionHideNames; 182 return *this; 183 } 184 GetNonCacheable()185 bool GetNonCacheable() const { 186 return (m_flags & lldb::eTypeOptionNonCacheable) == 187 lldb::eTypeOptionNonCacheable; 188 } 189 190 Flags &SetNonCacheable(bool value = true) { 191 if (value) 192 m_flags |= lldb::eTypeOptionNonCacheable; 193 else 194 m_flags &= ~lldb::eTypeOptionNonCacheable; 195 return *this; 196 } 197 GetValue()198 uint32_t GetValue() { return m_flags; } 199 SetValue(uint32_t value)200 void SetValue(uint32_t value) { m_flags = value; } 201 202 private: 203 uint32_t m_flags; 204 }; 205 Cascades()206 bool Cascades() const { return m_flags.GetCascades(); } 207 SkipsPointers()208 bool SkipsPointers() const { return m_flags.GetSkipPointers(); } 209 SkipsReferences()210 bool SkipsReferences() const { return m_flags.GetSkipReferences(); } 211 NonCacheable()212 bool NonCacheable() const { return m_flags.GetNonCacheable(); } 213 DoesPrintChildren(ValueObject * valobj)214 virtual bool DoesPrintChildren(ValueObject *valobj) const { 215 return !m_flags.GetDontShowChildren(); 216 } 217 DoesPrintEmptyAggregates()218 virtual bool DoesPrintEmptyAggregates() const { 219 return !m_flags.GetHideEmptyAggregates(); 220 } 221 DoesPrintValue(ValueObject * valobj)222 virtual bool DoesPrintValue(ValueObject *valobj) const { 223 return !m_flags.GetDontShowValue(); 224 } 225 IsOneLiner()226 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); } 227 HideNames(ValueObject * valobj)228 virtual bool HideNames(ValueObject *valobj) const { 229 return m_flags.GetHideItemNames(); 230 } 231 SetCascades(bool value)232 void SetCascades(bool value) { m_flags.SetCascades(value); } 233 SetSkipsPointers(bool value)234 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); } 235 SetSkipsReferences(bool value)236 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); } 237 SetDoesPrintChildren(bool value)238 virtual void SetDoesPrintChildren(bool value) { 239 m_flags.SetDontShowChildren(!value); 240 } 241 SetDoesPrintValue(bool value)242 virtual void SetDoesPrintValue(bool value) { 243 m_flags.SetDontShowValue(!value); 244 } 245 SetIsOneLiner(bool value)246 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); } 247 SetHideNames(bool value)248 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); } 249 SetNonCacheable(bool value)250 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); } 251 GetOptions()252 uint32_t GetOptions() { return m_flags.GetValue(); } 253 SetOptions(uint32_t value)254 void SetOptions(uint32_t value) { m_flags.SetValue(value); } 255 256 // we are using a ValueObject* instead of a ValueObjectSP because we do not 257 // need to hold on to this for extended periods of time and we trust the 258 // ValueObject to stay around for as long as it is required for us to 259 // generate its summary 260 virtual bool FormatObject(ValueObject *valobj, std::string &dest, 261 const TypeSummaryOptions &options) = 0; 262 263 virtual std::string GetDescription() = 0; 264 GetRevision()265 uint32_t &GetRevision() { return m_my_revision; } 266 267 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer; 268 269 protected: 270 uint32_t m_my_revision; 271 Flags m_flags; 272 273 TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags); 274 275 private: 276 Kind m_kind; 277 DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl); 278 }; 279 280 // simple string-based summaries, using ${var to show data 281 struct StringSummaryFormat : public TypeSummaryImpl { 282 std::string m_format_str; 283 FormatEntity::Entry m_format; 284 Status m_error; 285 286 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f); 287 288 ~StringSummaryFormat() override = default; 289 GetSummaryStringStringSummaryFormat290 const char *GetSummaryString() const { return m_format_str.c_str(); } 291 292 void SetSummaryString(const char *f); 293 294 bool FormatObject(ValueObject *valobj, std::string &dest, 295 const TypeSummaryOptions &options) override; 296 297 std::string GetDescription() override; 298 classofStringSummaryFormat299 static bool classof(const TypeSummaryImpl *S) { 300 return S->GetKind() == Kind::eSummaryString; 301 } 302 303 private: 304 DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat); 305 }; 306 307 // summaries implemented via a C++ function 308 struct CXXFunctionSummaryFormat : public TypeSummaryImpl { 309 // we should convert these to SBValue and SBStream if we ever cross the 310 // boundary towards the external world 311 typedef std::function<bool(ValueObject &, Stream &, 312 const TypeSummaryOptions &)> 313 Callback; 314 315 Callback m_impl; 316 std::string m_description; 317 318 CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl, 319 const char *description); 320 321 ~CXXFunctionSummaryFormat() override = default; 322 GetBackendFunctionCXXFunctionSummaryFormat323 Callback GetBackendFunction() const { return m_impl; } 324 GetTextualInfoCXXFunctionSummaryFormat325 const char *GetTextualInfo() const { return m_description.c_str(); } 326 SetBackendFunctionCXXFunctionSummaryFormat327 void SetBackendFunction(Callback cb_func) { m_impl = cb_func; } 328 SetTextualInfoCXXFunctionSummaryFormat329 void SetTextualInfo(const char *descr) { 330 if (descr) 331 m_description.assign(descr); 332 else 333 m_description.clear(); 334 } 335 336 bool FormatObject(ValueObject *valobj, std::string &dest, 337 const TypeSummaryOptions &options) override; 338 339 std::string GetDescription() override; 340 classofCXXFunctionSummaryFormat341 static bool classof(const TypeSummaryImpl *S) { 342 return S->GetKind() == Kind::eCallback; 343 } 344 345 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer; 346 347 private: 348 DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat); 349 }; 350 351 // Python-based summaries, running script code to show data 352 struct ScriptSummaryFormat : public TypeSummaryImpl { 353 std::string m_function_name; 354 std::string m_python_script; 355 StructuredData::ObjectSP m_script_function_sp; 356 357 ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags, 358 const char *function_name, 359 const char *python_script = nullptr); 360 361 ~ScriptSummaryFormat() override = default; 362 GetFunctionNameScriptSummaryFormat363 const char *GetFunctionName() const { return m_function_name.c_str(); } 364 GetPythonScriptScriptSummaryFormat365 const char *GetPythonScript() const { return m_python_script.c_str(); } 366 SetFunctionNameScriptSummaryFormat367 void SetFunctionName(const char *function_name) { 368 if (function_name) 369 m_function_name.assign(function_name); 370 else 371 m_function_name.clear(); 372 m_python_script.clear(); 373 } 374 SetPythonScriptScriptSummaryFormat375 void SetPythonScript(const char *script) { 376 if (script) 377 m_python_script.assign(script); 378 else 379 m_python_script.clear(); 380 } 381 382 bool FormatObject(ValueObject *valobj, std::string &dest, 383 const TypeSummaryOptions &options) override; 384 385 std::string GetDescription() override; 386 classofScriptSummaryFormat387 static bool classof(const TypeSummaryImpl *S) { 388 return S->GetKind() == Kind::eScript; 389 } 390 391 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer; 392 393 private: 394 DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat); 395 }; 396 } // namespace lldb_private 397 398 #endif // lldb_TypeSummary_h_ 399