forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.h
394 lines (345 loc) · 14 KB
/
Project.h
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
/* 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/>. */
#ifndef Project_h
#define Project_h
#include "IndexerJob.h"
#include "Match.h"
#include "QueryMessage.h"
#include "RTags.h"
#include "RTagsClang.h"
#include <cstdint>
#include <memory>
#include <mutex>
#include <rct/FileSystemWatcher.h>
#include <rct/EmbeddedLinkedList.h>
#include <rct/LinkedList.h>
#include <rct/Path.h>
#include <regex>
#include <rct/Timer.h>
#include <rct/Flags.h>
class IndexDataMessage;
class FileManager;
class IndexerJob;
class RestoreThread;
class Connection;
class Dirty;
struct DependencyNode
{
DependencyNode(uint32_t f)
: fileId(f)
{}
void include(DependencyNode *dependee)
{
assert(!includes.contains(dependee->fileId) || includes.value(dependee->fileId) == dependee);
includes[dependee->fileId] = dependee;
assert(!dependee->dependents.contains(fileId) || dependee->dependents.value(fileId) == this);
dependee->dependents[fileId] = this;
}
Dependencies dependents, includes;
uint32_t fileId;
};
class Project : public std::enable_shared_from_this<Project>
{
public:
Project(const Path &path);
~Project();
bool init();
std::shared_ptr<FileManager> fileManager() const { return mFileManager; }
Path path() const { return mPath; }
bool match(const Match &match, bool *indexed = 0) const;
enum FileMapType {
Symbols,
SymbolNames,
Targets,
Usrs
};
static const char *fileMapName(FileMapType type)
{
switch (type) {
case Symbols:
return "symbols";
case SymbolNames:
return "symnames";
case Targets:
return "targets";
case Usrs:
return "usrs";
}
return 0;
}
std::shared_ptr<FileMap<String, Set<Location> > > openSymbolNames(uint32_t fileId, String *err = 0)
{
assert(mFileMapScope);
return mFileMapScope->openFileMap<String, Set<Location> >(SymbolNames, fileId, mFileMapScope->symbolNames, err);
}
std::shared_ptr<FileMap<Location, Symbol> > openSymbols(uint32_t fileId, String *err = 0)
{
assert(mFileMapScope);
return mFileMapScope->openFileMap<Location, Symbol>(Symbols, fileId, mFileMapScope->symbols, err);
}
std::shared_ptr<FileMap<String, Set<Location> > > openTargets(uint32_t fileId, String *err = 0)
{
assert(mFileMapScope);
return mFileMapScope->openFileMap<String, Set<Location> >(Targets, fileId, mFileMapScope->targets, err);
}
std::shared_ptr<FileMap<String, Set<Location> > > openUsrs(uint32_t fileId, String *err = 0)
{
assert(mFileMapScope);
return mFileMapScope->openFileMap<String, Set<Location> >(Usrs, fileId, mFileMapScope->usrs, err);
}
enum DependencyMode {
DependsOnArg,
ArgDependsOn
};
Set<uint32_t> dependencies(uint32_t fileId, DependencyMode mode) const;
String dumpDependencies(uint32_t fileId) const;
const Hash<uint32_t, DependencyNode*> &dependencies() const { return mDependencies; }
const Declarations &declarations() const { return mDeclarations; }
bool isDeclaration(const String &usr) const { return mDeclarations.contains(usr); }
static bool readSources(const Path &path, Sources &sources, String *error);
enum SymbolMatchType {
Exact,
Wildcard,
StartsWith
};
void findSymbols(const String &symbolName,
const std::function<void(SymbolMatchType, const String &, const Set<Location> &)> &func,
Flags<QueryMessage::Flag> queryFlags,
uint32_t fileFilter = 0);
static bool matchSymbolName(const String &pattern, const String &symbolName, String::CaseSensitivity cs)
{
return Rct::wildCmp(pattern.constData(), symbolName.constData(), cs);
}
Symbol findSymbol(const Location &location, int *index = 0);
Set<Symbol> findTargets(const Location &location) { return findTargets(findSymbol(location)); }
Set<Symbol> findTargets(const Symbol &symbol);
Symbol findTarget(const Location &location) { return RTags::bestTarget(findTargets(location)); }
Symbol findTarget(const Symbol &symbol) { return RTags::bestTarget(findTargets(symbol)); }
Set<Symbol> findAllReferences(const Location &location) { return findAllReferences(findSymbol(location)); }
Set<Symbol> findAllReferences(const Symbol &symbol);
Set<Symbol> findCallers(const Location &location) { return findCallers(findSymbol(location)); }
Set<Symbol> findCallers(const Symbol &symbol);
Set<Symbol> findVirtuals(const Location &location) { return findVirtuals(findSymbol(location)); }
Set<Symbol> findVirtuals(const Symbol &symbol);
Set<String> findTargetUsrs(const Location &loc);
Set<Symbol> findSubclasses(const Symbol &symbol);
Set<Symbol> findByUsr(const String &usr, uint32_t fileId, DependencyMode mode);
Path sourceFilePath(uint32_t fileId, const char *path = "") const;
List<RTags::SortedSymbol> sort(const Set<Symbol> &symbols,
Flags<QueryMessage::Flag> flags = Flags<QueryMessage::Flag>());
const Files &files() const { return mFiles; }
Files &files() { return mFiles; }
const Set<uint32_t> &suspendedFiles() const;
bool toggleSuspendFile(uint32_t file);
bool isSuspended(uint32_t file) const;
void clearSuspendedFiles();
bool isIndexed(uint32_t fileId) const;
void index(const std::shared_ptr<IndexerJob> &job);
List<Source> sources(uint32_t fileId) const;
bool hasSource(uint32_t fileId) const;
bool isActiveJob(uint64_t key) { return !key || mActiveJobs.contains(key); }
inline bool visitFile(uint32_t fileId, const Path &path, uint64_t id);
inline void releaseFileIds(const Set<uint32_t> &fileIds);
String fixIts(uint32_t fileId) const;
int reindex(const Match &match,
const std::shared_ptr<QueryMessage> &query,
const std::shared_ptr<Connection> &wait);
int remove(const Match &match);
void onJobFinished(const std::shared_ptr<IndexerJob> &job, const std::shared_ptr<IndexDataMessage> &msg);
Sources sources() const { return mSources; }
String toCompilationDatabase() const;
enum WatchMode {
Watch_FileManager = 0x1,
Watch_SourceFile = 0x2
};
void watch(const Path &dir, WatchMode mode);
void unwatch(const Path &dir, WatchMode mode);
void clearWatch(WatchMode mode);
Hash<Path, Flags<WatchMode> > watchedPaths() const { return mWatchedPaths; }
bool isIndexing() const { return !mActiveJobs.isEmpty(); }
void onFileAdded(const Path &path);
void onFileModified(const Path &path);
void onFileRemoved(const Path &path);
void dumpFileMaps(const std::shared_ptr<QueryMessage> &msg, const std::shared_ptr<Connection> &conn);
Hash<uint32_t, Path> visitedFiles() const
{
std::lock_guard<std::mutex> lock(mMutex);
return mVisitedFiles;
}
void encodeVisitedFiles(Serializer &serializer)
{
std::lock_guard<std::mutex> lock(mMutex);
serializer << mVisitedFiles;
}
void beginScope();
void endScope();
void dirty(uint32_t fileId);
bool save();
void prepare(uint32_t fileId);
private:
void onFileAddedOrModified(const Path &path);
void watchFile(uint32_t fileId);
bool validate(uint32_t fileId, String *error = 0) const;
void removeDependencies(uint32_t fileId);
void updateDependencies(const std::shared_ptr<IndexDataMessage> &msg);
void updateDeclarations(const Set<uint32_t> &visited, Declarations &declarations);
void loadFailed(uint32_t fileId);
void updateFixIts(const Set<uint32_t> &visited, FixIts &fixIts);
int startDirtyJobs(Dirty *dirty,
const UnsavedFiles &unsavedFiles = UnsavedFiles(),
const std::shared_ptr<Connection> &wait = std::shared_ptr<Connection>());
void onDirtyTimeout(Timer *);
struct FileMapScope {
FileMapScope(const std::shared_ptr<Project> &proj, int m)
: project(proj), openedFiles(0), max(m)
{}
struct LRUKey {
FileMapType type;
uint32_t fileId;
bool operator<(const LRUKey &other) const
{
return fileId < other.fileId || (fileId == other.fileId && type < other.type);
}
};
struct LRUEntry {
LRUEntry(FileMapType t, uint32_t f)
: key({ t, f })
{}
const LRUKey key;
std::shared_ptr<LRUEntry> next, prev;
};
void poke(FileMapType t, uint32_t f)
{
const LRUKey key = { t, f };
auto ptr = entryMap.value(key);
assert(ptr);
entryList.remove(ptr);
entryList.append(ptr);
}
template <typename Key, typename Value>
std::shared_ptr<FileMap<Key, Value> > openFileMap(FileMapType type, uint32_t fileId,
Hash<uint32_t, std::shared_ptr<FileMap<Key, Value> > > &cache,
String *errPtr)
{
auto it = cache.find(fileId);
if (it != cache.end()) {
poke(type, fileId);
return it->second;
}
const Path path = project->sourceFilePath(fileId, Project::fileMapName(type));
std::shared_ptr<FileMap<Key, Value> > fileMap(new FileMap<Key, Value>);
String err;
if (fileMap->load(path, &err)) {
cache[fileId] = fileMap;
std::shared_ptr<LRUEntry> entry(new LRUEntry(type, fileId));
entryList.append(entry);
entryMap[entry->key] = entry;
if (++openedFiles > max) {
const std::shared_ptr<LRUEntry> e = entryList.takeFirst();
assert(e);
entryMap.remove(e->key);
switch (e->key.type) {
case SymbolNames:
assert(symbolNames.contains(e->key.fileId));
symbolNames.remove(e->key.fileId);
break;
case Symbols:
assert(symbols.contains(e->key.fileId));
symbols.remove(e->key.fileId);
break;
case Targets:
assert(targets.contains(e->key.fileId));
targets.remove(e->key.fileId);
break;
case Usrs:
assert(usrs.contains(e->key.fileId));
usrs.remove(e->key.fileId);
break;
}
--openedFiles;
}
assert(openedFiles <= max);
} else {
if (errPtr) {
*errPtr = "Failed to open: " + path + " " + Location::path(fileId) + ": " + err;
} else {
error() << "Failed to open" << path << Location::path(fileId) << err;
}
project->loadFailed(fileId);
fileMap.reset();
}
return fileMap;
}
Hash<uint32_t, std::shared_ptr<FileMap<String, Set<Location> > > > symbolNames;
Hash<uint32_t, std::shared_ptr<FileMap<Location, Symbol> > > symbols;
Hash<uint32_t, std::shared_ptr<FileMap<String, Set<Location> > > > targets, usrs;
std::shared_ptr<Project> project;
int openedFiles;
const int max;
EmbeddedLinkedList<std::shared_ptr<LRUEntry> > entryList;
Map<LRUKey, std::shared_ptr<LRUEntry> > entryMap;
};
std::shared_ptr<FileMapScope> mFileMapScope;
const Path mPath, mSourceFilePathBase;
Path mProjectFilePath, mSourcesFilePath;
Files mFiles;
Hash<uint32_t, Path> mVisitedFiles;
int mJobCounter, mJobsStarted;
Set<uint32_t> mHadDiagnostics;
// key'ed on Source::key()
Hash<uint64_t, std::shared_ptr<IndexerJob> > mActiveJobs;
Timer mDirtyTimer;
Set<uint32_t> mPendingDirtyFiles;
StopWatch mTimer;
FileSystemWatcher mWatcher;
Declarations mDeclarations;
Sources mSources;
Hash<Path, Flags<WatchMode> > mWatchedPaths;
std::shared_ptr<FileManager> mFileManager;
FixIts mFixIts;
Hash<uint32_t, DependencyNode*> mDependencies;
Set<uint32_t> mSuspendedFiles;
mutable std::mutex mMutex;
};
RCT_FLAGS(Project::WatchMode);
inline bool Project::visitFile(uint32_t visitFileId, const Path &path, uint64_t key)
{
std::lock_guard<std::mutex> lock(mMutex);
assert(visitFileId);
Path &p = mVisitedFiles[visitFileId];
if (p.isEmpty()) {
p = path;
if (key) {
assert(mActiveJobs.contains(key));
std::shared_ptr<IndexerJob> &job = mActiveJobs[key];
assert(job);
job->visited.insert(visitFileId);
}
return true;
}
return false;
}
inline void Project::releaseFileIds(const Set<uint32_t> &fileIds)
{
if (!fileIds.isEmpty()) {
std::lock_guard<std::mutex> lock(mMutex);
for (const auto &f : fileIds) {
// error() << "Returning files" << Location::path(f);
mVisitedFiles.remove(f);
}
}
}
inline Path Project::sourceFilePath(uint32_t fileId, const char *type) const
{
return String::format<1024>("%s%d/%s", mSourceFilePathBase.constData(), fileId, type);
}
#endif