forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClangIndexer.h
221 lines (197 loc) · 7.66 KB
/
ClangIndexer.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
/* 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 ClangIndexer_h
#define ClangIndexer_h
#include <sys/stat.h>
#include "Token.h"
#include "IndexDataMessage.h"
#include "rct/Hash.h"
#include "rct/Path.h"
#include "rct/StopWatch.h"
#include "RTags.h"
#include "Server.h"
#include "Symbol.h"
#include <unordered_set>
struct Unit;
class ClangIndexer
{
public:
ClangIndexer();
~ClangIndexer();
bool exec(const String &data);
static Flags<Server::Option> serverOpts() { return sServerOpts; }
static const Path &serverSandboxRoot() { return sServerSandboxRoot; }
private:
bool diagnose();
bool visit();
bool parse();
void tokenize(CXFile file, uint32_t fileId, const Path &path);
bool writeFiles(const Path &root, String &error);
void addFileSymbol(uint32_t file);
int symbolLength(CXCursorKind kind, const CXCursor &cursor);
void extractArguments(List<Symbol::Argument> *arguments, const CXCursor &cursor);
CXCursor resolveTemplate(CXCursor cursor, Location location = Location(), bool *specialized = 0);
static CXCursor resolveTypedef(CXCursor cursor);
inline Location createLocation(const CXSourceLocation &location, bool *blocked = 0, unsigned *offset = 0)
{
CXString fileName;
unsigned int line, col;
CXFile file;
clang_getSpellingLocation(location, &file, &line, &col, offset);
if (file) {
fileName = clang_getFileName(file);
} else {
if (blocked)
*blocked = false;
return Location();
}
const char *fn = clang_getCString(fileName);
assert(fn);
if (!*fn || !strcmp("<built-in>", fn) || !strcmp("<command line>", fn)) {
if (blocked)
*blocked = false;
clang_disposeString(fileName);
return Location();
}
const Path path = RTags::eatString(fileName);
const Location ret = createLocation(path, line, col, blocked);
return ret;
}
Location createLocation(CXFile file, unsigned int line, unsigned int col, bool *blocked = 0)
{
if (blocked)
*blocked = false;
if (!file)
return Location();
CXString fn = clang_getFileName(file);
const char *cstr = clang_getCString(fn);
if (!cstr) {
clang_disposeString(fn);
return Location();
}
const Path p = Path::resolved(cstr);
clang_disposeString(fn);
return createLocation(p, line, col, blocked);
}
inline Location createLocation(const CXCursor &cursor, bool *blocked = 0, unsigned *offset = 0)
{
const CXSourceLocation location = clang_getCursorLocation(cursor);
if (!location)
return Location();
return createLocation(location, blocked, offset);
}
Location createLocation(const Path &file, unsigned int line, unsigned int col, bool *blocked = 0);
String addNamePermutations(const CXCursor &cursor,
Location location,
RTags::CursorType cursorType);
CXChildVisitResult handleCursor(const CXCursor &cursor, CXCursorKind kind,
Location location, Symbol **cursorPtr = 0);
bool handleReference(const CXCursor &cursor, CXCursorKind kind,
Location loc, CXCursor reference,
Symbol **cursorPtr = 0);
void handleBaseClassSpecifier(const CXCursor &cursor);
void handleInclude(const CXCursor &cursor, CXCursorKind kind, Location location);
void handleLiteral(const CXCursor &cursor, CXCursorKind kind, Location location);
CXChildVisitResult handleStatement(const CXCursor &cursor, CXCursorKind kind, Location location);
Location findByUSR(const CXCursor &cursor, CXCursorKind kind, Location loc) const;
std::unordered_set<CXCursor> addOverriddenCursors(const CXCursor &cursor, Location location);
bool superclassTemplateMemberFunctionUgleHack(const CXCursor &cursor, CXCursorKind kind,
Location location, const CXCursor &ref,
Symbol **cursorPtr = 0);
void visit(CXCursor cursor)
{
mParents.append(cursor);
clang_visitChildren(cursor, visitorHelper, this);
mParents.removeLast();
}
CXChildVisitResult indexVisitor(CXCursor cursor);
static CXChildVisitResult visitorHelper(CXCursor cursor, CXCursor, CXClientData userData);
static CXChildVisitResult verboseVisitor(CXCursor cursor, CXCursor, CXClientData userData);
static CXChildVisitResult resolveAutoTypeRefVisitor(CXCursor cursor, CXCursor, CXClientData data);
void onMessage(const std::shared_ptr<Message> &msg, const std::shared_ptr<Connection> &conn);
struct Unit {
Map<Location, Symbol> symbols;
Map<Location, Map<String, uint16_t> > targets;
Map<String, Set<Location> > usrs;
Map<String, Set<Location> > symbolNames;
Map<uint32_t, Token> tokens;
};
std::shared_ptr<Unit> &unit(uint32_t fileId)
{
std::shared_ptr<Unit> &unit = mUnits[fileId];
if (!unit) {
unit.reset(new Unit);
}
return unit;
}
std::shared_ptr<Unit> unit(Location loc) { return unit(loc.fileId()); }
enum FindResult {
Found,
NotIndexed,
NotFound
};
Symbol findSymbol(Location location, FindResult *result) const;
struct MacroLocationData {
Set<size_t> arguments;
List<Location> locations;
};
struct MacroData {
List<String> arguments;
Map<String, MacroLocationData> data;
};
Map<Location, MacroData> mMacroTokens;
Hash<uint32_t, std::shared_ptr<Unit> > mUnits;
Path mProject;
SourceList mSources;
Path mSourceFile;
IndexDataMessage mIndexDataMessage;
List<std::shared_ptr<RTags::TranslationUnit> > mTranslationUnits;
size_t mCurrentTranslationUnit;
CXCursor mLastCursor;
Symbol *mLastCallExprSymbol;
Location mLastClass;
uint32_t mVisitFileResponseMessageFileId;
bool mVisitFileResponseMessageVisit;
Path mSocketFile;
StopWatch mTimer;
int mParseDuration, mVisitDuration, mBlocked, mAllowed,
mIndexed, mVisitFileTimeout, mIndexDataMessageTimeout,
mFileIdsQueried, mFileIdsQueriedTime, mCursorsVisited;
UnsavedFiles mUnsavedFiles;
List<String> mDebugLocations;
FILE *mLogFile;
std::shared_ptr<Connection> mConnection;
Path mDataDir;
bool mUnionRecursion;
struct Scope {
enum ScopeType {
FunctionDefinition,
FunctionDeclaration,
Other
};
ScopeType type;
Symbol *symbol;
Location start, end;
};
List<Scope> mScopeStack;
struct Loop { // or switch
CXCursorKind kind;
Location start, end;
};
List<Loop> mLoopStack;
List<CXCursor> mParents;
std::unordered_set<CXCursor> mTemplateSpecializations;
size_t mInTemplateFunction;
static Flags<Server::Option> sServerOpts;
static Path sServerSandboxRoot;
};
#endif