forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccls_member.cc
301 lines (285 loc) · 9.73 KB
/
ccls_member.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
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "clang_tu.hh"
#include "hierarchy.hh"
#include "message_handler.hh"
#include "pipeline.hh"
#include "query.hh"
#include <clang/AST/Type.h>
#include <llvm/ADT/DenseSet.h>
#include <unordered_set>
namespace ccls {
using namespace clang;
namespace {
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;
bool qualified = false;
int levels = 1;
// If Kind::Func and the point is at a type, list member functions instead of
// member variables.
Kind kind = Kind::Var;
bool hierarchy = false;
};
REFLECT_STRUCT(Param, textDocument, position, id, qualified, levels, kind,
hierarchy);
struct Out_cclsMember {
Usr usr;
std::string id;
std::string_view name;
std::string fieldName;
Location location;
// For unexpanded nodes, this is an upper bound because some entities may be
// undefined. If it is 0, there are no members.
int numChildren = 0;
// Empty if the |levels| limit is reached.
std::vector<Out_cclsMember> children;
};
REFLECT_STRUCT(Out_cclsMember, id, name, fieldName, location, numChildren,
children);
bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
int levels, Kind memberKind);
// Add a field to |entry| which is a Func/Type.
void DoField(MessageHandler *m, Out_cclsMember *entry, const QueryVar &var,
int64_t offset, bool qualified, int levels) {
const QueryVar::Def *def1 = var.AnyDef();
if (!def1)
return;
Out_cclsMember entry1;
// With multiple inheritance, the offset is incorrect.
if (offset >= 0) {
if (offset / 8 < 10)
entry1.fieldName += ' ';
entry1.fieldName += std::to_string(offset / 8);
if (offset % 8) {
entry1.fieldName += '.';
entry1.fieldName += std::to_string(offset % 8);
}
entry1.fieldName += ' ';
}
if (qualified)
entry1.fieldName += def1->detailed_name;
else {
entry1.fieldName +=
std::string_view(def1->detailed_name).substr(0, def1->qual_name_offset);
entry1.fieldName += def1->Name(false);
}
if (def1->spell) {
if (std::optional<Location> loc =
GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc;
}
if (def1->type) {
entry1.id = std::to_string(def1->type);
entry1.usr = def1->type;
if (Expand(m, &entry1, qualified, levels, Kind::Var))
entry->children.push_back(std::move(entry1));
} else {
entry1.id = "0";
entry1.usr = 0;
entry->children.push_back(std::move(entry1));
}
}
// Expand a type node by adding members recursively to it.
bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
int levels, Kind memberKind) {
if (0 < entry->usr && entry->usr <= BuiltinType::LastKind) {
entry->name = ClangBuiltinTypeName(int(entry->usr));
return true;
}
const QueryType *type = &m->db->Type(entry->usr);
const QueryType::Def *def = type->AnyDef();
// builtin types have no declaration and empty |qualified|.
if (!def)
return false;
entry->name = def->Name(qualified);
std::unordered_set<Usr> seen;
if (levels > 0) {
std::vector<const QueryType *> stack;
seen.insert(type->usr);
stack.push_back(type);
while (stack.size()) {
type = stack.back();
stack.pop_back();
const auto *def = type->AnyDef();
if (!def)
continue;
if (def->kind != SymbolKind::Namespace)
for (Usr usr : def->bases) {
auto &type1 = m->db->Type(usr);
if (type1.def.size()) {
seen.insert(type1.usr);
stack.push_back(&type1);
}
}
if (def->alias_of) {
const QueryType::Def *def1 = m->db->Type(def->alias_of).AnyDef();
Out_cclsMember entry1;
entry1.id = std::to_string(def->alias_of);
entry1.usr = def->alias_of;
if (def1 && def1->spell) {
// The declaration of target type.
if (std::optional<Location> loc =
GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc;
} else if (def->spell) {
// Builtin types have no declaration but the typedef declaration
// itself is useful.
if (std::optional<Location> loc =
GetLsLocation(m->db, m->wfiles, *def->spell))
entry1.location = *loc;
}
if (def1 && qualified)
entry1.fieldName = def1->detailed_name;
if (Expand(m, &entry1, qualified, levels - 1, memberKind)) {
// For builtin types |name| is set.
if (entry1.fieldName.empty())
entry1.fieldName = std::string(entry1.name);
entry->children.push_back(std::move(entry1));
}
} else if (memberKind == Kind::Func) {
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
for (auto &def : type->def)
for (Usr usr : def.funcs)
if (seen1.insert(usr).second) {
QueryFunc &func1 = m->db->Func(usr);
if (const QueryFunc::Def *def1 = func1.AnyDef()) {
Out_cclsMember entry1;
entry1.fieldName = def1->Name(false);
if (def1->spell) {
if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc;
} else if (func1.declarations.size()) {
if (auto loc = GetLsLocation(m->db, m->wfiles,
func1.declarations[0]))
entry1.location = *loc;
}
entry->children.push_back(std::move(entry1));
}
}
} else if (memberKind == Kind::Type) {
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
for (auto &def : type->def)
for (Usr usr : def.types)
if (seen1.insert(usr).second) {
QueryType &type1 = m->db->Type(usr);
if (const QueryType::Def *def1 = type1.AnyDef()) {
Out_cclsMember entry1;
entry1.fieldName = def1->Name(false);
if (def1->spell) {
if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc;
} else if (type1.declarations.size()) {
if (auto loc = GetLsLocation(m->db, m->wfiles,
type1.declarations[0]))
entry1.location = *loc;
}
entry->children.push_back(std::move(entry1));
}
}
} else {
llvm::DenseSet<Usr, DenseMapInfoForUsr> seen1;
for (auto &def : type->def)
for (auto it : def.vars)
if (seen1.insert(it.first).second) {
QueryVar &var = m->db->Var(it.first);
if (!var.def.empty())
DoField(m, entry, var, it.second, qualified, levels - 1);
}
}
}
entry->numChildren = int(entry->children.size());
} else
entry->numChildren = def->alias_of ? 1 : int(def->vars.size());
return true;
}
std::optional<Out_cclsMember> BuildInitial(MessageHandler *m, Kind kind,
Usr root_usr, bool qualified,
int levels, Kind memberKind) {
switch (kind) {
default:
return {};
case Kind::Func: {
const auto *def = m->db->Func(root_usr).AnyDef();
if (!def)
return {};
Out_cclsMember entry;
// Not type, |id| is invalid.
entry.name = def->Name(qualified);
if (def->spell) {
if (auto loc = GetLsLocation(m->db, m->wfiles, *def->spell))
entry.location = *loc;
}
for (Usr usr : def->vars) {
auto &var = m->db->Var(usr);
if (var.def.size())
DoField(m, &entry, var, -1, qualified, levels - 1);
}
return entry;
}
case Kind::Type: {
const auto *def = m->db->Type(root_usr).AnyDef();
if (!def)
return {};
Out_cclsMember entry;
entry.id = std::to_string(root_usr);
entry.usr = root_usr;
if (def->spell) {
if (auto loc = GetLsLocation(m->db, m->wfiles, *def->spell))
entry.location = *loc;
}
Expand(m, &entry, qualified, levels, memberKind);
return entry;
}
}
}
} // namespace
void MessageHandler::ccls_member(JsonReader &reader, ReplyOnce &reply) {
Param param;
Reflect(reader, param);
std::optional<Out_cclsMember> 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;
// entry.name is empty as it is known by the client.
if (!(db->HasType(param.usr) && Expand(this, &*result, param.qualified,
param.levels, param.kind)))
result.reset();
} else {
auto [file, wf] = FindOrFail(param.textDocument.uri.GetPath(), reply);
if (!wf)
return;
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
switch (sym.kind) {
case Kind::Func:
case Kind::Type:
result = BuildInitial(this, sym.kind, sym.usr, param.qualified,
param.levels, param.kind);
break;
case Kind::Var: {
const QueryVar::Def *def = db->GetVar(sym).AnyDef();
if (def && def->type)
result = BuildInitial(this, Kind::Type, def->type, param.qualified,
param.levels, param.kind);
break;
}
default:
continue;
}
break;
}
}
if (param.hierarchy)
reply(result);
else
reply(FlattenHierarchy(result));
}
} // namespace ccls