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