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