forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTagsClang.cpp
460 lines (417 loc) · 14.4 KB
/
RTagsClang.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* This file is part of RTags (http://rtags.net).
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include "RTagsClang.h"
#include "Server.h"
#include <rct/StopWatch.h>
#include "Project.h"
#include <rct/Hash.h>
#include <iostream>
#include <zlib.h>
#include "Token.h"
#ifdef HAVE_BACKTRACE
#undef HAVE_BACKTRACE
#endif
namespace RTags {
String eatString(CXString str)
{
const String ret(clang_getCString(str));
clang_disposeString(str);
return ret;
}
String cursorToString(CXCursor cursor, Flags<CursorToStringFlags> flags)
{
const CXCursorKind kind = clang_getCursorKind(cursor);
String ret;
ret.reserve(256);
ret += eatString(clang_getCursorKindSpelling(kind));
if (clang_isInvalid(kind))
return ret;
switch (RTags::cursorType(kind)) {
case Type_Reference:
ret += " r";
break;
case Type_Cursor:
ret += " c";
break;
case Type_Other:
ret += " o";
break;
case Type_Include:
ret += " i";
break;
}
const String name = eatString(clang_getCursorDisplayName(cursor));
const String other = eatString(clang_getCursorSpelling(cursor));
if (!name.isEmpty())
ret += " " + name;
if (other != name && !other.isEmpty())
ret += " " + other;
if (clang_isCursorDefinition(cursor))
ret += " def";
if (flags & IncludeUSR) {
const String usr = eatString(clang_getCursorUSR(clang_getCanonicalCursor(cursor)));
if (!usr.isEmpty()) {
ret += " " + usr;
}
}
if (flags & IncludeSpecializedUsr) {
const CXCursor general = clang_getSpecializedCursorTemplate(cursor);
if (!clang_Cursor_isNull(general)) {
const String usr = eatString(clang_getCursorUSR(clang_getCanonicalCursor(general)));
if (!usr.isEmpty()) {
ret += " " + usr;
}
}
}
CXString file;
unsigned int line, col;
for (int pieceIndex = 0; true; ++pieceIndex) {
CXSourceRange range = clang_Cursor_getSpellingNameRange(cursor, pieceIndex, 0);
if (clang_Range_isNull(range))
break;
CXSourceLocation rangeStart = clang_getRangeStart(range);
clang_getPresumedLocation(rangeStart, &file, &line, &col);
const char *data = clang_getCString(file);
if (data && *data) {
ret += ' ';
ret += data;
ret += ':';
ret += String::number(line);
ret += ':';
ret += String::number(col);
}
clang_disposeString(file);
}
return ret;
}
void parseTranslationUnit(const Path &sourceFile, const List<String> &args,
CXTranslationUnit &unit, CXIndex index,
CXUnsavedFile *unsaved, int unsavedCount,
Flags<CXTranslationUnit_Flags> translationUnitFlags,
String *clangLine)
{
if (clangLine)
*clangLine = "clang ";
int idx = 0;
List<const char*> clangArgs(args.size() + 2, 0);
const int count = args.size();
for (int j=0; j<count; ++j) {
clangArgs[idx++] = args.at(j).constData();
if (clangLine) {
String arg = args.at(j);
arg.replace("\"", "\\\"");
*clangLine += arg;
*clangLine += ' ';
}
}
// clangArgs[idx++] = "-disable-free";
// clangArgs[idx++] = "-disable-llvm-verifier";
if (clangLine)
*clangLine += sourceFile;
// StopWatch sw;
#if CINDEX_VERSION_MINOR >= 23
for (int i=0; i<3; ++i) {
auto error = clang_parseTranslationUnit2(index, sourceFile.constData(),
clangArgs.data(), idx, unsaved, unsavedCount,
translationUnitFlags.cast<unsigned int>(), &unit);
if (error != CXError_Crashed)
break;
usleep(100000);
}
#else
unit = clang_parseTranslationUnit(index, sourceFile.constData(),
clangArgs.data(), idx, unsaved, unsavedCount,
translationUnitFlags.cast<unsigned int>());
#endif
// error() << sourceFile << sw.elapsed();
}
void reparseTranslationUnit(CXTranslationUnit &unit, CXUnsavedFile *unsaved, int unsavedCount)
{
assert(unit);
if (clang_reparseTranslationUnit(unit, unsavedCount, unsaved, clang_defaultReparseOptions(unit)) != 0) {
clang_disposeTranslationUnit(unit);
unit = 0;
}
}
struct ResolveAutoTypeRefUserData
{
// CXCursor orig;
CXCursor ref;
int index;
bool ok;
Hash<CXCursor, bool> *seen;
};
static CXChildVisitResult resolveAutoTypeRefVisitor(CXCursor cursor, CXCursor, CXClientData data)
{
ResolveAutoTypeRefUserData *userData = reinterpret_cast<ResolveAutoTypeRefUserData*>(data);
++userData->index;
// error() << "Got cursor" << cursor << clang_getCursorReferenced(cursor);
const CXCursorKind kind = clang_getCursorKind(cursor);
if (!userData->seen->insert(cursor, true)) {
// error() << "I've seen" << cursor << "before";
return CXChildVisit_Break;
}
// userData->chain.append(kind);
// error() << "Got here" << cursor << userData->chain;
switch (kind) {
case CXCursor_CStyleCastExpr:
case CXCursor_CXXStaticCastExpr:
case CXCursor_CXXDynamicCastExpr:
case CXCursor_CXXReinterpretCastExpr:
case CXCursor_CXXNewExpr:
// error() << "Setting to ok";
userData->ok = true;
break;
case CXCursor_TypeRef:
case CXCursor_TemplateRef:
// error() << userData->orig << "Found typeRef" << cursor << userData->index;
userData->ref = cursor;
return CXChildVisit_Break;
case CXCursor_DeclRefExpr:
case CXCursor_CallExpr:
case CXCursor_UnexposedExpr: {
CXCursor ref = clang_getCursorReferenced(cursor);
// error() << userData->orig << "got ref" << ref << cursor;
switch (clang_getCursorKind(ref)) {
case CXCursor_FunctionTemplate:
case CXCursor_CXXMethod:
ref = clang_getSpecializedCursorTemplate(ref);
// fall through
case CXCursor_VarDecl:
case CXCursor_ParmDecl:
case CXCursor_FieldDecl:
case CXCursor_TemplateTypeParameter:
case CXCursor_TemplateTemplateParameter:
case CXCursor_FunctionDecl:
case CXCursor_Constructor: {
// error() << "Recursing for ref" << ref;
userData->ok = true;
ResolveAutoTypeRefUserData u = { /* userData->orig, */clang_getNullCursor(), 0, true, userData->seen };
clang_visitChildren(ref, resolveAutoTypeRefVisitor, &u);
// error() << "Visited for typeRef" << u.ref << clang_isInvalid(clang_getCursorKind(u.ref));
if (!clang_equalCursors(u.ref, clang_getNullCursor())) {
// error() << "Found a good cursor" << u.ref;
++userData->index;
userData->ref = u.ref;
return CXChildVisit_Break;
}
if (userData->index + u.index > 10) {
// error() << "Gone too far";
return CXChildVisit_Break;
}
break; }
default:
// error() << "Nothing reffed";
break;
}
break; }
case CXCursor_ParmDecl:
// error() << "Breaking due to ParmDecl";
// nothing to find here
return CXChildVisit_Break;
default:
break;
}
return CXChildVisit_Recurse;
}
CXCursor resolveAutoTypeRef(const CXCursor &cursor, bool *isAuto)
{
CXType type = clang_getCursorType(cursor);
while (type.kind == CXType_Pointer)
type = clang_getPointeeType(type);
if (type.kind == CXType_Unexposed) {
if (isAuto)
*isAuto = true;
// error() << "resolving" << cursor << clang_getCursorType(cursor).kind;
assert(clang_getCursorKind(cursor) == CXCursor_VarDecl);
Hash<CXCursor, bool> seen;
ResolveAutoTypeRefUserData userData = { /* cursor, */clang_getNullCursor(), 0, false, &seen }; //, List<CXCursorKind>() };
clang_visitChildren(cursor, resolveAutoTypeRefVisitor, &userData);
if (userData.ok)
return userData.ref;
} else if (isAuto) {
*isAuto = false;
}
return clang_getNullCursor();
}
static CXChildVisitResult findFirstChildVisitor(CXCursor cursor, CXCursor, CXClientData data)
{
*reinterpret_cast<CXCursor*>(data) = cursor;
return CXChildVisit_Break;
}
CXCursor findFirstChild(CXCursor parent)
{
CXCursor ret = clang_getNullCursor();
if (!clang_isInvalid(clang_getCursorKind(parent)))
clang_visitChildren(parent, findFirstChildVisitor, &ret);
return ret;
}
struct FindChildVisitor
{
CXCursorKind kind;
String name;
CXCursor cursor;
};
static CXChildVisitResult findChildVisitor(CXCursor cursor, CXCursor, CXClientData data)
{
FindChildVisitor *u = reinterpret_cast<FindChildVisitor*>(data);
if (u->name.isEmpty()) {
if (clang_getCursorKind(cursor) == u->kind) {
u->cursor = cursor;
return CXChildVisit_Break;
}
} else {
CXStringScope str = clang_getCursorSpelling(cursor);
if (str.data() && u->name == str.data()) {
u->cursor = cursor;
return CXChildVisit_Break;
}
}
return CXChildVisit_Continue;
}
CXCursor findChild(CXCursor parent, CXCursorKind kind)
{
FindChildVisitor u = { kind, String(), clang_getNullCursor() };
if (!clang_isInvalid(clang_getCursorKind(parent)))
clang_visitChildren(parent, findChildVisitor, &u);
return u.cursor;
}
CXCursor findChild(CXCursor parent, const String &name)
{
FindChildVisitor u = { CXCursor_FirstInvalid, name, clang_getNullCursor() };
if (!clang_isInvalid(clang_getCursorKind(parent)))
clang_visitChildren(parent, findChildVisitor, &u);
return u.cursor;
}
struct ChildrenVisitor
{
const Filter ∈
const Filter &out;
List<CXCursor> children;
};
static CXChildVisitResult childrenVisitor(CXCursor cursor, CXCursor, CXClientData data)
{
ChildrenVisitor *u = reinterpret_cast<ChildrenVisitor*>(data);
if ((u->out.isNull() || !u->out.match(cursor)) && (u->in.isNull() || u->in.match(cursor))) {
u->children.append(cursor);
}
return CXChildVisit_Continue;
}
List<CXCursor> children(CXCursor parent, const Filter &in, const Filter &out)
{
ChildrenVisitor userData = { in, out, List<CXCursor>() };
if (!clang_isInvalid(clang_getCursorKind(parent)))
clang_visitChildren(parent, childrenVisitor, &userData);
return userData.children;
}
struct FindChainVisitor
{
const List<CXCursorKind> &kinds;
List<CXCursor> ret;
};
static CXChildVisitResult findChainVisitor(CXCursor cursor, CXCursor, CXClientData data)
{
FindChainVisitor *u = reinterpret_cast<FindChainVisitor*>(data);
if (clang_getCursorKind(cursor) == u->kinds.at(u->ret.size())) {
u->ret.append(cursor);
if (u->ret.size() < u->kinds.size())
return CXChildVisit_Recurse;
return CXChildVisit_Break;
}
return CXChildVisit_Break;
}
List<CXCursor> findChain(CXCursor parent, const List<CXCursorKind> &kinds)
{
assert(!kinds.isEmpty());
FindChainVisitor userData = { kinds, List<CXCursor>() };
if (!clang_isInvalid(clang_getCursorKind(parent)))
clang_visitChildren(parent, findChainVisitor, &userData);
if (userData.ret.size() != kinds.size()) {
userData.ret.clear();
}
return userData.ret;
}
String typeName(const CXCursor &cursor)
{
String ret;
switch (clang_getCursorKind(cursor)) {
case CXCursor_FunctionTemplate:
// ### If the return value is a template type we get an empty string here
case CXCursor_FunctionDecl:
case CXCursor_CXXMethod:
ret = typeString(clang_getResultType(clang_getCursorType(cursor)));
break;
case CXCursor_ClassTemplate:
case CXCursor_ClassDecl:
case CXCursor_StructDecl:
case CXCursor_UnionDecl:
case CXCursor_TypedefDecl:
case CXCursor_EnumDecl:
ret = RTags::eatString(clang_getCursorSpelling(cursor));
break;
case CXCursor_VarDecl: {
const CXCursor initType = RTags::findFirstChild(cursor);
if (clang_getCursorKind(initType) == CXCursor_InitListExpr) {
ret = typeString(clang_getCursorType(initType));
} else {
ret = typeString(clang_getCursorType(cursor));
}
break; }
case CXCursor_FieldDecl: // ### If the return value is a template type we get an empty string here
case CXCursor_ParmDecl:
ret = typeString(clang_getCursorType(cursor));
break;
default:
return String();
}
if (!ret.isEmpty() && !ret.endsWith('*') && !ret.endsWith('&'))
ret.append(' ');
return ret;
}
String typeString(const CXType &type)
{
String ret;
if (clang_isConstQualifiedType(type))
ret = "const ";
const char *builtIn = builtinTypeName(type.kind);
if (builtIn) {
ret += builtIn;
return ret;
}
if (char pointer = (type.kind == CXType_Pointer ? '*' : (type.kind == CXType_LValueReference ? '&' : 0))) {
const CXType pointee = clang_getPointeeType(type);
ret += typeString(pointee);
if (ret.endsWith('*') || ret.endsWith('&')) {
ret += pointer;
} else {
ret += ' ';
ret += pointer;
}
return ret;
}
if (type.kind == CXType_ConstantArray) {
ret += typeString(clang_getArrayElementType(type));
#if CLANG_VERSION_MAJOR > 3 || (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR >= 1)
const int64_t count = clang_getNumElements(type);
ret += '[';
if (count >= 0)
ret += String::number(count);
ret += ']';
#endif
return ret;
}
ret += typeName(clang_getTypeDeclaration(type));
if (ret.endsWith(' '))
ret.chop(1);
return ret;
}
}