forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTags.cpp
414 lines (390 loc) · 11.4 KB
/
RTags.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
#include "RTags.h"
#include "CursorInfo.h"
#include "Server.h"
#include <rct/StopWatch.h>
#include "Str.h"
#include <dirent.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <rct/Rct.h>
#include <rct/Messages.h>
#ifdef OS_FreeBSD
#include <sys/sysctl.h>
#endif
#ifdef OS_Darwin
#include <mach-o/dyld.h>
#endif
namespace RTags {
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#include <cxxabi.h>
static inline char *demangle(const char *str)
{
if (!str)
return 0;
int status;
#ifdef OS_Darwin
char paren[1024];
sscanf(str, "%*d %*s %*s %s %*s %*d", paren);
#else
const char *paren = strchr(str, '(');
if (!paren) {
paren = str;
} else {
++paren;
}
#endif
size_t l;
if (const char *plus = strchr(paren, '+')) {
l = plus - paren;
} else {
l = strlen(paren);
}
char buf[1024];
size_t len = sizeof(buf);
if (l >= len)
return 0;
memcpy(buf, paren, l + 1);
buf[l] = '\0';
char *ret = abi::__cxa_demangle(buf, 0, 0, &status);
if (status != 0) {
if (ret)
free(ret);
#ifdef OS_Darwin
return strdup(paren);
#else
return 0;
#endif
}
return ret;
}
String backtrace(int maxFrames)
{
enum { SIZE = 1024 };
void *stack[SIZE];
int frameCount = backtrace(stack, sizeof(stack) / sizeof(void*));
if (frameCount <= 0)
return String("Couldn't get stack trace");
String ret;
char **symbols = backtrace_symbols(stack, frameCount);
if (symbols) {
char frame[1024];
for (int i=1; i<frameCount && (maxFrames < 0 || i - 1 < maxFrames); ++i) {
char *demangled = demangle(symbols[i]);
snprintf(frame, sizeof(frame), "%d/%d %s\n", i, frameCount - 1, demangled ? demangled : symbols[i]);
ret += frame;
if (demangled)
free(demangled);
}
free(symbols);
}
return ret;
}
#else
String backtrace(int)
{
return String();
}
#endif
void dirtySymbolNames(SymbolNameMap &map, const Set<uint32_t> &dirty)
{
SymbolNameMap::iterator it = map.begin();
while (it != map.end()) {
Set<Location> &locations = it->second;
Set<Location>::iterator i = locations.begin();
while (i != locations.end()) {
if (dirty.contains(i->fileId())) {
locations.erase(i++);
} else {
++i;
}
}
if (locations.isEmpty()) {
map.erase(it++);
} else {
++it;
}
}
}
void dirtySymbols(SymbolMap &map, const Set<uint32_t> &dirty)
{
SymbolMap::iterator it = map.begin();
while (it != map.end()) {
if (dirty.contains(it->first.fileId())) {
map.erase(it++);
} else {
CursorInfo &cursorInfo = it->second;
cursorInfo.dirty(dirty);
++it;
}
}
}
void dirtyUsr(UsrMap &map, const Set<uint32_t> &dirty)
{
UsrMap::iterator it = map.begin();
while (it != map.end()) {
Set<Location> &locations = it->second;
Set<Location>::iterator i = locations.begin();
while (i != locations.end()) {
if (dirty.contains(i->fileId())) {
locations.erase(i++);
} else {
++i;
}
}
if (locations.isEmpty()) {
map.erase(it++);
} else {
++it;
}
}
}
/* Same behavior as rtags-default-current-project() */
enum FindAncestorFlag {
Shallow = 0x1,
Wildcard = 0x2
};
static inline Path findAncestor(Path path, const char *fn, unsigned flags)
{
Path ret;
int slash = path.size();
const int len = strlen(fn) + 1;
struct stat st;
char buf[PATH_MAX + sizeof(dirent) + 1];
dirent *direntBuf = 0, *entry = 0;
if (flags & Wildcard)
direntBuf = reinterpret_cast<struct dirent *>(malloc(sizeof(buf)));
memcpy(buf, path.constData(), path.size() + 1);
while ((slash = path.lastIndexOf('/', slash - 1)) > 0) { // We don't want to search in /
if (!(flags & Wildcard)) {
memcpy(buf + slash + 1, fn, len);
if (!stat(buf, &st)) {
buf[slash + 1] = '\0';
ret = buf;
if (flags & Shallow) {
break;
}
}
} else {
buf[slash + 1] = '\0';
DIR *dir = opendir(buf);
bool found = false;
if (dir) {
while (!readdir_r(dir, direntBuf, &entry) && entry) {
const int l = strlen(entry->d_name) + 1;
switch (l - 1) {
case 1:
if (entry->d_name[0] == '.')
continue;
break;
case 2:
if (entry->d_name[0] == '.' && entry->d_name[1] == '.')
continue;
break;
}
assert(buf[slash] == '/');
assert(l + slash + 1 < static_cast<int>(sizeof(buf)));
memcpy(buf + slash + 1, entry->d_name, l);
if (!fnmatch(fn, buf, 0)) {
ret = buf;
ret.truncate(slash + 1);
found = true;
break;
}
}
}
closedir(dir);
if (found && flags & Shallow)
break;
}
}
if (flags & Wildcard)
free(direntBuf);
if (!ret.isEmpty() && !ret.endsWith('/'))
ret.append('/');
return ret;
}
struct Entry {
const char *name;
const unsigned flags;
};
static inline Path checkEntry(const Entry *entries, const Path &path, const Path &home)
{
for (int i=0; entries[i].name; ++i) {
Path p = findAncestor(path, entries[i].name, entries[i].flags);
if ((p.isEmpty() || p == home) && (entries[i].flags & Wildcard)) {
const int len = strlen(entries[i].name);
if (entries[i].name[len - 1] == '*') {
const String name(entries[i].name, len - 1);
p = findAncestor(path, name.constData(), entries[i].flags & ~Wildcard);
}
}
if (!p.isEmpty() && p != home) {
if (!p.compare("./") || !p.compare("."))
error() << "1" << path << "=>" << p << entries[i].name;
return p;
}
}
return Path();
}
Path findProjectRoot(const Path &path)
{
assert(path.isAbsolute());
static const Path home = Path::home();
const Entry before[] = {
{ "GTAGS", 0 },
{ "CMakeLists.txt", 0 },
{ "configure", 0 },
{ ".git", 0 },
{ ".svn", 0 },
{ "*.pro", Wildcard },
{ "scons.1", 0 },
{ "*.scons", Wildcard },
{ "SConstruct", 0 },
{ "autogen.*", Wildcard },
{ "GNUMakefile*", Wildcard },
{ "INSTALL*", Wildcard },
{ "README*", Wildcard },
{ 0, 0 }
};
{
const Path ret = checkEntry(before, path, home);
if (!ret.isEmpty())
return ret;
}
{
const Path configStatus = findAncestor(path, "config.status", 0);
if (!configStatus.isEmpty()) {
FILE *f = fopen((configStatus + "config.status").constData(), "r");
Path ret;
if (f) {
char line[1024];
enum { MaxLines = 10 };
for (int i=0; i<MaxLines; ++i) {
int r = Rct::readLine(f, line, sizeof(line));
if (r == -1)
break;
char *configure = strstr(line, "/configure");
if (configure) {
char *end = configure + 10;
while (--configure >= line) {
Path conf(configure, end - configure);
if (!conf.isAbsolute())
conf.resolve();
if (conf.isFile()) {
ret = conf.parentDir();
if (ret == home)
ret.clear();
break;
}
}
}
if (!ret.isEmpty())
break;
}
fclose(f);
if (!ret.isEmpty())
return ret;
}
}
}
{
const Path cmakeCache = findAncestor(path, "CMakeCache.txt", 0);
if (!cmakeCache.isEmpty()) {
FILE *f = fopen((cmakeCache + "Makefile").constData(), "r");
if (f) {
Path ret;
char line[1024];
enum { MaxLines = 256 };
for (int i=0; i<MaxLines; ++i) {
int r = Rct::readLine(f, line, sizeof(line));
if (r == -1) {
break;
}
if (!strncmp(line, "CMAKE_SOURCE_DIR", 16)) {
char *dir = line + 16;
while (*dir && (*dir == ' ' || *dir == '='))
++dir;
if (dir != home) {
ret = dir;
ret += '/';
if (!Path(ret + "CMakeLists.txt").isFile())
ret.clear();
}
break;
}
}
fclose(f);
if (!ret.isEmpty())
return ret;
}
}
}
const Entry after[] = {
{ "Makefile*", Wildcard },
{ 0, 0 }
};
{
const Path ret = checkEntry(after, path, home);
if (!ret.isEmpty())
return ret;
}
return Path();
}
String filterPreprocessor(const Path &path)
{
String ret;
FILE *f = fopen(path.constData(), "r");
if (f) {
char line[1026];
int r;
while ((r = Rct::readLine(f, line, sizeof(line) - 1)) != -1) {
int start = 0;
while (start < r && isspace(line[start]))
++start;
if (start == r || line[start] != '#')
continue;
line[r] = '\n';
ret.append(line, r + 1);
int end = r - 1;
while (end >= start && isspace(line[end]))
--end;
while ((r = Rct::readLine(f, line, sizeof(line) - 1)) != -1) {
line[r] = '\n';
ret.append(line, r + 1);
end = r - 1;
while (end >= 0 && isspace(line[end]))
--end;
if (end < 0 || line[end] != '\\') {
break;
}
}
}
fclose(f);
}
return ret;
}
void initMessages()
{
#ifndef GRTAGS
Messages::registerMessage<QueryMessage>();
Messages::registerMessage<CompletionMessage>();
Messages::registerMessage<CompileMessage>();
Messages::registerMessage<CreateOutputMessage>();
#endif
}
}
#ifdef RTAGS_DEBUG_MUTEX
void Mutex::lock()
{
Timer timer;
while (!tryLock()) {
usleep(10000);
if (timer.elapsed() >= 10000) {
error("Couldn't acquire lock in 10 seconds\n%s", RTags::backtrace().constData());
timer.restart();
}
}
}
#endif