forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccls_call.cc
347 lines (319 loc) · 10.4 KB
/
ccls_call.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "hierarchy.hh"
#include "message_handler.hh"
#include "pipeline.hh"
#include "query.hh"
#include <map>
#include <unordered_set>
namespace ccls {
namespace {
enum class CallType : uint8_t {
Direct = 0,
Base = 1,
Derived = 2,
All = 1 | 2
};
REFLECT_UNDERLYING(CallType);
bool operator&(CallType lhs, CallType rhs) {
return uint8_t(lhs) & uint8_t(rhs);
}
struct Param : TextDocumentPositionParam {
// If id is specified, expand a node; otherwise textDocument+position should
// be specified for building the root and |levels| of nodes below.
Usr usr;
std::string 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 qualified = true;
int levels = 1;
bool hierarchy = false;
};
REFLECT_STRUCT(Param, textDocument, position, id, callee, callType, qualified,
levels, hierarchy);
struct Out_cclsCall {
Usr usr;
std::string id;
std::string_view name;
Location location;
lsRange useRange;
CallType callType = CallType::Direct;
int numChildren;
// Empty if the |levels| limit is reached.
std::vector<Out_cclsCall> children;
bool operator==(const Out_cclsCall &o) const {
return (location == o.location) && (useRange == o.useRange);
}
bool operator<(const Out_cclsCall &o) const
{
return location == o.location ? useRange < o.useRange
: location < o.location;
}
};
REFLECT_STRUCT(Out_cclsCall, id, name, location, useRange, callType, numChildren,
children);
struct Out_incomingCall {
CallHierarchyItem from;
std::vector<lsRange> fromRanges;
};
REFLECT_STRUCT(Out_incomingCall, from, fromRanges);
struct Out_outgoingCall {
CallHierarchyItem to;
std::vector<lsRange> fromRanges;
};
REFLECT_STRUCT(Out_outgoingCall, to, fromRanges);
bool expand(MessageHandler *m, Out_cclsCall *entry, bool callee,
CallType call_type, bool qualified, int levels) {
const QueryFunc &func = m->db->getFunc(entry->usr);
const QueryFunc::Def *def = func.anyDef();
entry->numChildren = 0;
if (!def)
return false;
auto handle = [&](SymbolRef sym, int file_id, CallType call_type1, Range useRange) {
entry->numChildren++;
if (levels > 0) {
Out_cclsCall entry1;
entry1.id = std::to_string(sym.usr);
entry1.usr = sym.usr;
if (auto loc = getLsLocation(m->db, m->wfiles,
Use{{sym.range, sym.role}, file_id}))
entry1.location = *loc;
if (auto loc = getLsLocation(m->db, m->wfiles,
Use{{useRange, sym.role}, file_id}))
entry1.useRange = loc->range;
entry1.callType = call_type1;
if (expand(m, &entry1, callee, call_type, qualified, 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 (SymbolRef sym : def->callees)
if (sym.kind == Kind::Func)
handle(sym, def->file_id, call_type, sym.range);
} else {
for (Use use : func.uses) {
const QueryFile &file1 = m->db->files[use.file_id];
Maybe<ExtentRef> best;
for (auto [sym, refcnt] : file1.symbol2refcnt)
if (refcnt > 0 && sym.extent.valid() && sym.kind == Kind::Func &&
sym.extent.start <= use.range.start &&
use.range.end <= sym.extent.end &&
(!best || best->extent.start < sym.extent.start))
best = sym;
if (best)
handle(*best, use.file_id, call_type, use.range);
}
}
};
std::unordered_set<Usr> seen;
seen.insert(func.usr);
std::vector<const QueryFunc *> stack;
entry->name = def->name(qualified);
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);
}
});
}
}
std::sort(entry->children.begin(), entry->children.end());
entry->children.erase(
std::unique(entry->children.begin(), entry->children.end()),
entry->children.end());
return true;
}
std::optional<Out_cclsCall> buildInitial(MessageHandler *m, Usr root_usr,
bool callee, CallType call_type,
bool qualified, int levels) {
const auto *def = m->db->getFunc(root_usr).anyDef();
if (!def)
return {};
Out_cclsCall entry;
entry.id = std::to_string(root_usr);
entry.usr = root_usr;
entry.callType = CallType::Direct;
if (def->spell) {
if (auto loc = getLsLocation(m->db, m->wfiles, *def->spell))
entry.location = *loc;
}
expand(m, &entry, callee, call_type, qualified, levels);
return entry;
}
} // namespace
void MessageHandler::ccls_call(JsonReader &reader, ReplyOnce &reply) {
Param param;
reflect(reader, param);
std::optional<Out_cclsCall> result;
if (param.id.size()) {
try {
param.usr = std::stoull(param.id);
} catch (...) {
return;
}
result.emplace();
result->id = std::to_string(param.usr);
result->usr = param.usr;
result->callType = CallType::Direct;
if (db->hasFunc(param.usr))
expand(this, &*result, param.callee, param.callType, param.qualified,
param.levels);
} else {
auto [file, wf] = findOrFail(param.textDocument.uri.getPath(), reply);
if (!wf)
return;
for (SymbolRef sym : findSymbolsAtLocation(wf, file, param.position)) {
if (sym.kind == Kind::Func) {
result = buildInitial(this, sym.usr, param.callee, param.callType,
param.qualified, param.levels);
break;
}
}
}
if (param.hierarchy)
reply(result);
else
reply(flattenHierarchy(result));
}
void MessageHandler::textDocument_prepareCallHierarchy(
TextDocumentPositionParam ¶m, ReplyOnce &reply) {
std::string path = param.textDocument.uri.getPath();
auto [file, wf] = findOrFail(path, reply);
if (!file)
return;
std::vector<CallHierarchyItem> result;
for (SymbolRef sym : findSymbolsAtLocation(wf, file, param.position)) {
if (sym.kind != Kind::Func)
continue;
const auto *def = db->getFunc(sym.usr).anyDef();
if (!def)
continue;
auto r = getLsRange(wf, sym.range);
if (!r)
continue;
CallHierarchyItem &item = result.emplace_back();
item.name = def->name(false);
item.kind = def->kind;
item.detail = def->name(true);
item.uri = DocumentUri::fromPath(path);
item.range = item.selectionRange = *r;
item.data = std::to_string(sym.usr);
}
reply(result);
}
static lsRange toLsRange(Range r) {
return {{r.start.line, r.start.column}, {r.end.line, r.end.column}};
}
static void
add(std::map<SymbolIdx, std::pair<int, std::vector<lsRange>>> &sym2ranges,
SymbolRef sym, int file_id) {
auto [it, inserted] = sym2ranges.try_emplace(SymbolIdx{sym.usr, sym.kind});
if (inserted)
it->second.first = file_id;
if (it->second.first == file_id)
it->second.second.push_back(toLsRange(sym.range));
}
template <typename Out>
static std::vector<Out> toCallResult(
DB *db,
const std::map<SymbolIdx, std::pair<int, std::vector<lsRange>>> &sym2ranges) {
std::vector<Out> result;
for (auto &[sym, ranges] : sym2ranges) {
CallHierarchyItem item;
item.uri = getLsDocumentUri(db, ranges.first);
auto r = ranges.second[0];
item.range = {{uint16_t(r.start.line), int16_t(r.start.character)},
{uint16_t(r.end.line), int16_t(r.end.character)}};
item.selectionRange = item.range;
switch (sym.kind) {
default:
continue;
case Kind::Func: {
auto idx = db->func_usr[sym.usr];
const QueryFunc &func = db->funcs[idx];
const QueryFunc::Def *def = func.anyDef();
if (!def)
continue;
item.name = def->name(false);
item.kind = def->kind;
item.detail = def->name(true);
item.data = std::to_string(sym.usr);
}
}
result.push_back({std::move(item), std::move(ranges.second)});
}
return result;
}
void MessageHandler::callHierarchy_incomingCalls(CallsParam ¶m,
ReplyOnce &reply) {
Usr usr;
try {
usr = std::stoull(param.item.data);
} catch (...) {
return;
}
const QueryFunc &func = db->getFunc(usr);
std::map<SymbolIdx, std::pair<int, std::vector<lsRange>>> sym2ranges;
for (Use use : func.uses) {
const QueryFile &file = db->files[use.file_id];
Maybe<ExtentRef> best;
for (auto [sym, refcnt] : file.symbol2refcnt)
if (refcnt > 0 && sym.extent.valid() && sym.kind == Kind::Func &&
sym.extent.start <= use.range.start &&
use.range.end <= sym.extent.end &&
(!best || best->extent.start < sym.extent.start))
best = sym;
if (best)
add(sym2ranges, *best, use.file_id);
}
reply(toCallResult<Out_incomingCall>(db, sym2ranges));
}
void MessageHandler::callHierarchy_outgoingCalls(CallsParam ¶m,
ReplyOnce &reply) {
Usr usr;
try {
usr = std::stoull(param.item.data);
} catch (...) {
return;
}
const QueryFunc &func = db->getFunc(usr);
std::map<SymbolIdx, std::pair<int, std::vector<lsRange>>> sym2ranges;
if (const auto *def = func.anyDef())
for (SymbolRef sym : def->callees)
if (sym.kind == Kind::Func) {
add(sym2ranges, sym, def->file_id);
}
reply(toCallResult<Out_outgoingCall>(db, sym2ranges));
}
} // namespace ccls