-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIVisitor.h
258 lines (202 loc) · 6.21 KB
/
DIVisitor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// llvm-meta-meetup library
// Copyright (c) 2023 llvm-meta-meetup authors
// Distributed under the BSD 3-Clause License license.
// (See accompanying file LICENSE)
// SPDX-License-Identifier: BSD-3-Clause
#ifndef META_DIVISITOR_H
#define META_DIVISITOR_H
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/Debug.h"
#include <functional>
namespace meta {
namespace visitor {
namespace detail {
template <typename Fn>
class ScopeExit {
private:
Fn exit_fn_;
public:
explicit ScopeExit(Fn&& exit_fn) : exit_fn_(std::forward<Fn>(exit_fn)) {
}
ScopeExit(const ScopeExit&) = delete;
ScopeExit& operator=(const ScopeExit&) = delete;
~ScopeExit() {
std::invoke(exit_fn_);
}
};
template <typename Fn>
ScopeExit<Fn> create_scope_exit(Fn&& exit_fn) {
return ScopeExit<Fn>(std::forward<Fn>(exit_fn));
}
} // namespace detail
template <typename SubClass>
class DINodeVisitor {
private:
llvm::SmallPtrSet<const llvm::DINode*, 8> visited_dinodes_;
template <typename TySink, typename TySource, typename VisitFn>
bool invoke_if(VisitFn&& visitor, TySource&& type) {
if (auto* base_t = llvm::dyn_cast<TySink>(type)) {
return std::invoke(std::forward<VisitFn>(visitor), *this, base_t);
}
return false;
}
template <typename T>
bool invoke_if_any(T&& type) {
using namespace llvm;
return invoke_if<DIBasicType>(&DINodeVisitor::traverseBasicType, std::forward<T>(type)) ||
invoke_if<DIDerivedType>(&DINodeVisitor::traverseDerivedType, std::forward<T>(type)) ||
invoke_if<DICompositeType>(&DINodeVisitor::traverseCompositeType, std::forward<T>(type)) ||
invoke_if<DILocalVariable>(&DINodeVisitor::traverseLocalVariable, std::forward<T>(type));
}
protected:
unsigned depth_composite_{0};
unsigned depth_derived_{0};
unsigned depth_var_{0};
[[nodiscard]] inline unsigned depth() const {
return depth_composite_ + depth_derived_ + depth_var_;
}
inline bool visited_node(const llvm::DINode* node) {
return visited_dinodes_.contains(node);
}
public:
[[nodiscard]] SubClass& get() {
return static_cast<SubClass&>(*this);
}
[[nodiscard]] const SubClass& get() const {
return static_cast<const SubClass&>(*this);
}
bool traverseLocalVariable(const llvm::DIVariable* var) {
const bool ret = get().visitLocalVariable(var);
if (!ret) {
return false;
}
const auto* type = var->getType();
const auto ret_v = get().traverseType(type);
return ret_v;
}
bool visitLocalVariable(const llvm::DIVariable*) {
return true;
}
bool traverseType(const llvm::DIType* type) {
if (!type) {
return true; // FIXME special case, to be observed
}
if (!get().visitType(type)) {
return false;
}
return invoke_if_any(type);
}
bool visitType(const llvm::DIType*) {
return true;
}
bool traverseBasicType(const llvm::DIBasicType* basic_type) {
++depth_var_;
const auto exit = detail::create_scope_exit([&]() {
assert(depth_var_ > 0);
--depth_var_;
});
return get().visitBasicType(basic_type);
}
bool visitBasicType(const llvm::DIBasicType*) {
return true;
}
bool traverseDerivedType(const llvm::DIDerivedType* derived_type) {
++depth_derived_;
const auto exit = detail::create_scope_exit([&]() {
assert(depth_derived_ > 0);
--depth_derived_;
});
const bool ret = get().visitDerivedType(derived_type);
if (!ret) {
return false;
}
const bool ret_v = get().traverseType(derived_type->getBaseType());
return ret_v;
}
bool visitDerivedType(const llvm::DIDerivedType*) {
return true;
}
bool traverseCompositeType(const llvm::DICompositeType* composite_type) {
// Make sure, we do not infinitely recurse
if (visited_node(composite_type)) {
return true;
}
++depth_composite_;
const auto exit = detail::create_scope_exit([&]() {
assert(depth_composite_ > 0);
--depth_composite_;
});
const bool ret = get().visitCompositeType(composite_type);
visited_dinodes_.insert(composite_type);
if (!ret) {
return false;
}
const bool ret_b = get().traverseType(composite_type->getBaseType());
if (!ret_b) {
return false;
}
for (auto* eleme : composite_type->getElements()) {
invoke_if_any(eleme);
}
return true;
}
bool visitCompositeType(const llvm::DICompositeType*) {
return true;
}
};
} // namespace visitor
namespace util {
class DIPrinter : public visitor::DINodeVisitor<DIPrinter> {
private:
llvm::raw_ostream& outp_;
llvm::Optional<const llvm::Module*> module_;
std::string no_pointer_str(const llvm::Metadata& type) {
std::string view;
llvm::raw_string_ostream rso(view);
#if LLVM_VERSION_MAJOR > 14
type.print(rso, module_.value_or(nullptr));
if (module_) {
return rso.str();
}
#else
type.print(rso, module_.getValueOr(nullptr));
if (module_) {
return rso.str();
}
#endif
const llvm::StringRef ref(rso.str());
const auto a_pos = ref.find("=");
if (a_pos == llvm::StringRef::npos || (a_pos + 2) > ref.size()) {
return ref.str();
}
return std::string{ref.substr(a_pos + 2)};
}
[[nodiscard]] unsigned width() const {
return depth() == 1 ? 0 : depth();
}
public:
explicit DIPrinter(llvm::raw_ostream& outp, const llvm::Module* mod = nullptr) : outp_(outp), module_(mod) {
}
bool visitLocalVariable(llvm::DIVariable const* var) {
outp_ << llvm::left_justify("", width()) << no_pointer_str(*var) << "\n";
return true;
}
bool visitBasicType(const llvm::DIBasicType* basic_type) {
outp_ << llvm::left_justify("", width() + 3) << no_pointer_str(*basic_type) << "\n";
return true;
}
bool visitDerivedType(const llvm::DIDerivedType* derived_type) {
outp_ << llvm::left_justify("", width()) << no_pointer_str(*derived_type) << "\n";
return true;
}
bool visitCompositeType(const llvm::DICompositeType* composite_type) {
outp_ << llvm::left_justify("", width()) << no_pointer_str(*composite_type) << "\n";
return true;
}
};
} // namespace util
} // namespace meta
#endif // DIMETA_DIVISITOR_H