forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTagsClang.cpp
359 lines (318 loc) · 10.6 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
/* This file is part of RTags.
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 <iostream>
#include <zlib.h>
#include "Token.h"
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#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, unsigned 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 Reference:
ret += " r";
break;
case Cursor:
ret += " c";
break;
case Other:
ret += " o";
break;
case 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)
ret += " " + eatString(clang_getCursorUSR(cursor));
CXString file;
unsigned 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;
}
SymbolMap::const_iterator findCursorInfo(const SymbolMap &map, const Location &location)
{
SymbolMap::const_iterator it = map.lower_bound(location);
if (it != map.end() && it->first == location) {
return it;
} else if (it != map.begin()) {
--it;
if (it->first.fileId() == location.fileId() && location.line() == it->first.line()) {
const int off = location.column() - it->first.column();
if (it->second->symbolLength > off)
return it;
}
}
return map.end();
}
void parseTranslationUnit(const Path &sourceFile, const List<String> &args,
CXTranslationUnit &unit, CXIndex index,
CXUnsavedFile *unsaved, int unsavedCount,
unsigned int 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) {
String arg = args.at(j);
clangArgs[idx++] = args.at(j).constData();
if (clangLine) {
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, &unit);
if (error != CXError_Crashed)
break;
}
#else
unit = clang_parseTranslationUnit(index, sourceFile.constData(),
clangArgs.data(), idx, unsaved, unsavedCount,
translationUnitFlags);
#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;
}
}
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;
}
}