xref: /llvm-project/lldb/include/lldb/DataFormatters/FormattersHelpers.h (revision 87659a17d0703c1244211d9f8d1f0c21e816f0e1)
1 //===-- FormattersHelpers.h -------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H
10 #define LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H
11 
12 #include "lldb/lldb-enumerations.h"
13 #include "lldb/lldb-forward.h"
14 
15 #include "lldb/DataFormatters/TypeCategory.h"
16 #include "lldb/DataFormatters/TypeFormat.h"
17 #include "lldb/DataFormatters/TypeSummary.h"
18 #include "lldb/DataFormatters/TypeSynthetic.h"
19 
20 namespace lldb_private {
21 namespace formatters {
22 void AddFormat(TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
23                llvm::StringRef type_name, TypeFormatImpl::Flags flags,
24                bool regex = false);
25 
26 void AddSummary(TypeCategoryImpl::SharedPointer category_sp,
27                 lldb::TypeSummaryImplSP summary_sp, llvm::StringRef type_name,
28                 bool regex = false);
29 
30 void AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,
31                       const char *string, llvm::StringRef type_name,
32                       TypeSummaryImpl::Flags flags, bool regex = false);
33 
34 void AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,
35                        llvm::StringRef type_name, TypeSummaryImpl::Flags flags,
36                        bool regex = false);
37 
38 /// Add a summary that is implemented by a C++ callback.
39 void AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,
40                    CXXFunctionSummaryFormat::Callback funct,
41                    const char *description, llvm::StringRef type_name,
42                    TypeSummaryImpl::Flags flags, bool regex = false);
43 
44 /// Add a synthetic that is implemented by a C++ callback.
45 void AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,
46                      CXXSyntheticChildren::CreateFrontEndCallback generator,
47                      const char *description, llvm::StringRef type_name,
48                      ScriptedSyntheticChildren::Flags flags,
49                      bool regex = false);
50 
51 void AddFilter(TypeCategoryImpl::SharedPointer category_sp,
52                std::vector<std::string> children, const char *description,
53                llvm::StringRef type_name,
54                ScriptedSyntheticChildren::Flags flags, bool regex = false);
55 
56 size_t ExtractIndexFromString(const char *item_name);
57 
58 Address GetArrayAddressOrPointerValue(ValueObject &valobj);
59 
60 time_t GetOSXEpoch();
61 
62 struct InferiorSizedWord {
63 
64   InferiorSizedWord(const InferiorSizedWord &word) : ptr_size(word.ptr_size) {
65     if (ptr_size == 4)
66       thirty_two = word.thirty_two;
67     else
68       sixty_four = word.sixty_four;
69   }
70 
71   InferiorSizedWord operator=(const InferiorSizedWord &word) {
72     ptr_size = word.ptr_size;
73     if (ptr_size == 4)
74       thirty_two = word.thirty_two;
75     else
76       sixty_four = word.sixty_four;
77     return *this;
78   }
79 
80   InferiorSizedWord(uint64_t val, Process &process)
81       : ptr_size(process.GetAddressByteSize()) {
82     if (ptr_size == 4)
83       thirty_two = (uint32_t)val;
84     else if (ptr_size == 8)
85       sixty_four = val;
86     else
87       assert(false && "new pointer size is unknown");
88   }
89 
90   bool IsNegative() const {
91     if (ptr_size == 4)
92       return ((int32_t)thirty_two) < 0;
93     else
94       return ((int64_t)sixty_four) < 0;
95   }
96 
97   bool IsZero() const {
98     if (ptr_size == 4)
99       return thirty_two == 0;
100     else
101       return sixty_four == 0;
102   }
103 
104   static InferiorSizedWord GetMaximum(Process &process) {
105     if (process.GetAddressByteSize() == 4)
106       return InferiorSizedWord(UINT32_MAX, 4);
107     else
108       return InferiorSizedWord(UINT64_MAX, 8);
109   }
110 
111   InferiorSizedWord operator>>(int rhs) const {
112     if (ptr_size == 4)
113       return InferiorSizedWord(thirty_two >> rhs, 4);
114     return InferiorSizedWord(sixty_four >> rhs, 8);
115   }
116 
117   InferiorSizedWord operator<<(int rhs) const {
118     if (ptr_size == 4)
119       return InferiorSizedWord(thirty_two << rhs, 4);
120     return InferiorSizedWord(sixty_four << rhs, 8);
121   }
122 
123   InferiorSizedWord operator&(const InferiorSizedWord &word) const {
124     if (ptr_size != word.ptr_size)
125       return InferiorSizedWord(0, ptr_size);
126     if (ptr_size == 4)
127       return InferiorSizedWord(thirty_two & word.thirty_two, 4);
128     return InferiorSizedWord(sixty_four & word.sixty_four, 8);
129   }
130 
131   InferiorSizedWord operator&(int x) const {
132     if (ptr_size == 4)
133       return InferiorSizedWord(thirty_two & x, 4);
134     return InferiorSizedWord(sixty_four & x, 8);
135   }
136 
137   size_t GetBitSize() const { return ptr_size << 3; }
138 
139   size_t GetByteSize() const { return ptr_size; }
140 
141   uint64_t GetValue() const {
142     if (ptr_size == 4)
143       return (uint64_t)thirty_two;
144     return sixty_four;
145   }
146 
147   InferiorSizedWord SignExtend() const {
148     if (ptr_size == 4)
149       return InferiorSizedWord((int32_t)thirty_two, 4);
150     return InferiorSizedWord((int64_t)sixty_four, 8);
151   }
152 
153   uint8_t *CopyToBuffer(uint8_t *buffer) const {
154     if (ptr_size == 4) {
155       memcpy(buffer, &thirty_two, 4);
156       return buffer + 4;
157     } else {
158       memcpy(buffer, &sixty_four, 8);
159       return buffer + 8;
160     }
161   }
162 
163   DataExtractor
164   GetAsData(lldb::ByteOrder byte_order = lldb::eByteOrderInvalid) const {
165     if (ptr_size == 4)
166       return DataExtractor(&thirty_two, 4, byte_order, 4);
167     else
168       return DataExtractor(&sixty_four, 8, byte_order, 8);
169   }
170 
171 private:
172   InferiorSizedWord(uint64_t val, size_t psz) : ptr_size(psz) {
173     if (ptr_size == 4)
174       thirty_two = (uint32_t)val;
175     else
176       sixty_four = val;
177   }
178 
179   size_t ptr_size;
180   union {
181     uint32_t thirty_two;
182     uint64_t sixty_four;
183   };
184 };
185 } // namespace formatters
186 } // namespace lldb_private
187 
188 #endif // LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H
189