forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cquery_call_hierarchy.cc
227 lines (205 loc) · 6.95 KB
/
cquery_call_hierarchy.cc
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
#include "message_handler.h"
#include "query_utils.h"
#include "queue_manager.h"
#include <loguru.hpp>
namespace {
MethodType kMethodType = "$cquery/callHierarchy";
enum class CallType : uint8_t {
Direct = 0,
Base = 1,
Derived = 2,
All = 1 | 2
};
MAKE_REFLECT_TYPE_PROXY(CallType);
bool operator&(CallType lhs, CallType rhs) {
return uint8_t(lhs) & uint8_t(rhs);
}
struct In_CqueryCallHierarchy : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
struct Params {
// If id is specified, expand a node; otherwise textDocument+position should
// be specified for building the root and |levels| of nodes below.
lsTextDocumentIdentifier textDocument;
lsPosition position;
Maybe<QueryId::Func> id;
// true: callee tree (functions called by this function); false: caller tree
// (where this function is called)
bool callee = false;
// Base: include base functions; All: include both base and derived
// functions.
CallType callType = CallType::All;
bool detailedName = false;
int levels = 1;
};
Params params;
};
MAKE_REFLECT_STRUCT(In_CqueryCallHierarchy::Params,
textDocument,
position,
id,
callee,
callType,
detailedName,
levels);
MAKE_REFLECT_STRUCT(In_CqueryCallHierarchy, id, params);
REGISTER_IN_MESSAGE(In_CqueryCallHierarchy);
struct Out_CqueryCallHierarchy : public lsOutMessage<Out_CqueryCallHierarchy> {
struct Entry {
QueryId::Func id;
std::string_view name;
lsLocation location;
CallType callType = CallType::Direct;
int numChildren;
// Empty if the |levels| limit is reached.
std::vector<Entry> children;
};
lsRequestId id;
optional<Entry> result;
};
MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy::Entry,
id,
name,
location,
callType,
numChildren,
children);
MAKE_REFLECT_STRUCT_OPTIONALS_MANDATORY(Out_CqueryCallHierarchy,
jsonrpc,
id,
result);
bool Expand(MessageHandler* m,
Out_CqueryCallHierarchy::Entry* entry,
bool callee,
CallType call_type,
bool detailed_name,
int levels) {
const QueryFunc& func = m->db->GetFunc(entry->id);
const QueryFunc::Def* def = func.AnyDef();
entry->numChildren = 0;
if (!def)
return false;
auto handle = [&](QueryId::LexicalRef ref, CallType call_type) {
entry->numChildren++;
if (levels > 0) {
Out_CqueryCallHierarchy::Entry entry1;
entry1.id = QueryId::Func(ref.id);
if (auto loc = GetLsLocation(m->db, m->working_files, ref))
entry1.location = *loc;
entry1.callType = call_type;
if (Expand(m, &entry1, callee, call_type, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1));
}
};
auto handle_uses = [&](const QueryFunc& func, CallType call_type) {
if (callee) {
if (const auto* def = func.AnyDef())
for (const QueryId::SymbolRef& ref : def->callees) {
if (ref.kind == SymbolKind::Func)
handle(QueryId::LexicalRef(ref.range, ref.id, ref.kind, ref.role,
def->file),
call_type);
}
} else {
for (QueryId::LexicalRef ref : func.uses)
if (ref.kind == SymbolKind::Func)
handle(ref, call_type);
}
};
std::unordered_set<Usr> seen;
seen.insert(func.usr);
std::vector<const QueryFunc*> stack;
if (detailed_name)
entry->name = def->detailed_name;
else
entry->name = def->ShortName();
handle_uses(func, CallType::Direct);
// Callers/callees of base functions.
if (call_type & CallType::Base) {
stack.push_back(&func);
while (stack.size()) {
const QueryFunc& func1 = *stack.back();
stack.pop_back();
if (auto* def1 = func1.AnyDef()) {
EachDefinedFunc(m->db, def1->bases, [&](QueryFunc& func2) {
if (!seen.count(func2.usr)) {
seen.insert(func2.usr);
stack.push_back(&func2);
handle_uses(func2, CallType::Base);
}
});
}
}
}
// Callers/callees of derived functions.
if (call_type & CallType::Derived) {
stack.push_back(&func);
while (stack.size()) {
const QueryFunc& func1 = *stack.back();
stack.pop_back();
EachDefinedFunc(m->db, func1.derived, [&](QueryFunc& func2) {
if (!seen.count(func2.usr)) {
seen.insert(func2.usr);
stack.push_back(&func2);
handle_uses(func2, CallType::Derived);
}
});
}
}
return true;
}
struct Handler_CqueryCallHierarchy
: BaseMessageHandler<In_CqueryCallHierarchy> {
MethodType GetMethodType() const override { return kMethodType; }
optional<Out_CqueryCallHierarchy::Entry> BuildInitial(QueryId::Func root_id,
bool callee,
CallType call_type,
bool detailed_name,
int levels) {
const auto* def = db->GetFunc(root_id).AnyDef();
if (!def)
return {};
Out_CqueryCallHierarchy::Entry entry;
entry.id = root_id;
entry.callType = CallType::Direct;
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
Expand(this, &entry, callee, call_type, detailed_name, levels);
return entry;
}
void Run(In_CqueryCallHierarchy* request) override {
const auto& params = request->params;
Out_CqueryCallHierarchy out;
out.id = request->id;
if (params.id) {
Out_CqueryCallHierarchy::Entry entry;
entry.id = *params.id;
entry.callType = CallType::Direct;
if (entry.id.id < db->funcs.size())
Expand(this, &entry, params.callee, params.callType,
params.detailedName, params.levels);
out.result = std::move(entry);
} else {
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetAbsolutePath(), &file))
return;
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
for (QueryId::SymbolRef sym :
FindSymbolsAtLocation(working_file, file, params.position)) {
if (sym.kind == SymbolKind::Func) {
out.result =
BuildInitial(QueryId::Func(sym.id), params.callee,
params.callType, params.detailedName, params.levels);
break;
}
}
}
QueueManager::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_CqueryCallHierarchy);
} // namespace