forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DumpThread.cpp
288 lines (256 loc) · 11 KB
/
DumpThread.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
/* 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 "DumpThread.h"
#include "rct/Connection.h"
#include "RTags.h"
#include "Server.h"
struct Dep : public DependencyNode
{
Dep(uint32_t f)
: DependencyNode(f)
{}
Hash<uint32_t, Map<Location, Location> > references;
};
DumpThread::DumpThread(const std::shared_ptr<QueryMessage> &queryMessage, const Source &source, const std::shared_ptr<Connection> &conn)
: Thread(), mQueryFlags(queryMessage->flags()), mSource(source), mConnection(conn), mIndentLevel(0), mAborted(false)
{
setAutoDelete(true);
}
static const CXSourceLocation nullLocation = clang_getNullLocation();
static const CXCursor nullCursor = clang_getNullCursor();
CXChildVisitResult DumpThread::visitor(CXCursor cursor, CXCursor, CXClientData userData)
{
DumpThread *that = reinterpret_cast<DumpThread*>(userData);
assert(that);
return that->visit(cursor);
}
CXChildVisitResult DumpThread::visit(const CXCursor &cursor)
{
if (isAborted())
return CXChildVisit_Break;
const Location location = createLocation(cursor);
if (!location.isNull()) {
if (mQueryFlags & QueryMessage::DumpCheckIncludes) {
checkIncludes(location, cursor);
return CXChildVisit_Recurse;
} else {
Flags<Location::ToStringFlag> locationFlags;
if (mQueryFlags & QueryMessage::NoColor)
locationFlags |= Location::NoColor;
CXSourceRange range = clang_getCursorExtent(cursor);
CXSourceLocation rangeEnd = clang_getRangeEnd(range);
unsigned int endLine, endColumn;
clang_getPresumedLocation(rangeEnd, 0, &endLine, &endColumn);
if (!(mQueryFlags & QueryMessage::DumpIncludeHeaders) && location.fileId() != mSource.fileId) {
return CXChildVisit_Continue;
}
String message;
message.reserve(256);
if (!(mQueryFlags & QueryMessage::NoContext)) {
message = location.context(locationFlags, &mContextCache);
}
if (endLine == location.line()) {
message += String::format<32>(" // %d-%d, %d: ", location.column(), endColumn, mIndentLevel);
} else {
message += String::format<32>(" // %d-%d:%d, %d: ", location.column(), endLine, endColumn, mIndentLevel);
}
message += RTags::cursorToString(cursor, RTags::AllCursorToStringFlags);
message.append(" " + RTags::typeName(cursor));;
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) {
const std::shared_ptr<RTags::Auto> autoResolved = RTags::resolveAuto(cursor);
if (autoResolved && !clang_equalCursors(autoResolved->cursor, nullCursor)) {
message += "auto resolves to " + RTags::cursorToString(autoResolved->cursor, RTags::AllCursorToStringFlags);
}
}
CXCursor ref = clang_getCursorReferenced(cursor);
if (clang_equalCursors(ref, cursor)) {
message.append("refs self");
} else if (!clang_equalCursors(ref, nullCursor)) {
message.append("refs ");
message.append(RTags::cursorToString(ref, RTags::AllCursorToStringFlags));
}
CXCursor canonical = clang_getCanonicalCursor(cursor);
if (!clang_equalCursors(canonical, cursor) && !clang_equalCursors(canonical, nullCursor)) {
message.append("canonical ");
message.append(RTags::cursorToString(canonical, RTags::AllCursorToStringFlags));
}
CXCursor specialized = clang_getSpecializedCursorTemplate(cursor);
if (!clang_equalCursors(specialized, cursor) && !clang_equalCursors(specialized, nullCursor)) {
message.append("specialized ");
message.append(RTags::cursorToString(specialized, RTags::AllCursorToStringFlags));
}
writeToConnetion(message);
}
}
++mIndentLevel;
clang_visitChildren(cursor, DumpThread::visitor, this);
if (isAborted())
return CXChildVisit_Break;
--mIndentLevel;
return CXChildVisit_Continue;
}
void DumpThread::run()
{
const auto key = mConnection->disconnected().connect([this](const std::shared_ptr<Connection> &) { abort(); });
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit translationUnit = 0;
String clangLine;
RTags::parseTranslationUnit(mSource.sourceFile(), mSource.toCommandLine(Source::Default), translationUnit,
index, 0, 0, CXTranslationUnit_DetailedPreprocessingRecord, &clangLine);
if (!(mQueryFlags & QueryMessage::DumpCheckIncludes))
writeToConnetion(String::format<128>("Indexed: %s => %s", clangLine.constData(), translationUnit ? "success" : "failure"));
if (translationUnit) {
clang_visitChildren(clang_getTranslationUnitCursor(translationUnit), DumpThread::visitor, this);
clang_disposeTranslationUnit(translationUnit);
}
clang_disposeIndex(index);
mConnection->disconnected().disconnect(key);
std::weak_ptr<Connection> conn = mConnection;
if (mQueryFlags & QueryMessage::DumpCheckIncludes) {
checkIncludes();
}
EventLoop::mainEventLoop()->callLater([conn]() {
if (auto c = conn.lock())
c->finish();
});
}
void DumpThread::writeToConnetion(const String &message)
{
std::weak_ptr<Connection> conn = mConnection;
EventLoop::mainEventLoop()->callLater([conn, message]() {
if (auto c = conn.lock()) {
c->write(message);
}
});
}
void DumpThread::handleInclude(const Location &loc, const CXCursor &cursor)
{
CXFile includedFile = clang_getIncludedFile(cursor);
if (includedFile) {
CXStringScope fn = clang_getFileName(includedFile);
const char *cstr = clang_getCString(fn);
if (!cstr) {
clang_disposeString(fn);
return;
}
const Path p = Path::resolved(cstr);
clang_disposeString(fn);
const uint32_t fileId = Location::insertFile(p);
Dep *&source = mDependencies[loc.fileId()];
if (!source)
source = new Dep(loc.fileId());
Dep *&include = mDependencies[fileId];
if (!include)
include = new Dep(fileId);
source->include(include);
}
}
void DumpThread::handleReference(const Location &loc, const CXCursor &ref)
{
if (clang_getCursorKind(ref) == CXCursor_Namespace)
return;
const Location refLoc = createLocation(ref);
if (refLoc.isNull() || refLoc.fileId() == loc.fileId())
return;
Dep *dep = mDependencies[loc.fileId()];
assert(dep);
Dep *refDep = mDependencies[refLoc.fileId()];
assert(refDep);
auto &refs = dep->references[refDep->fileId];
refs[loc] = refLoc;
}
void DumpThread::checkIncludes(const Location &location, const CXCursor &cursor)
{
if (clang_getCursorKind(cursor) == CXCursor_InclusionDirective) {
handleInclude(location, cursor);
} else {
const CXCursor ref = clang_getCursorReferenced(cursor);
if (!clang_equalCursors(cursor, nullCursor) && !clang_equalCursors(cursor, ref)) {
handleReference(location, ref);
}
}
}
static bool validateHasInclude(uint32_t ref, const Dep *cur, Set<uint32_t> &seen)
{
assert(ref);
assert(cur);
if (cur->includes.contains(ref))
return true;
if (!seen.insert(ref))
return false;
for (const auto &pair : cur->includes) {
if (validateHasInclude(ref, static_cast<const Dep*>(pair.second), seen))
return true;
}
return false;
}
static bool validateNeedsInclude(const Dep *source, const Dep *header, Set<uint32_t> &seen)
{
if (!seen.insert(header->fileId)) {
// error() << "already seen" << Location::path(source->fileId);
return false;
}
if (source->references.contains(header->fileId)) {
// error() << "Got ref" << Location::path(header->fileId);
return true;
}
for (const auto &child : header->includes) {
// error() << "Checking child" << Location::path(child.second->fileId);
if (validateNeedsInclude(source, static_cast<const Dep*>(child.second), seen)) {
return true;
}
}
// error() << "Checking" << Location::path(source->fileId) << "doesn't seem to need" << Location::path(header->fileId) << depth;
return false;
}
void DumpThread::checkIncludes()
{
for (const auto &it : mDependencies) {
const Path path = Location::path(it.first);
if (path.isSystem())
continue;
for (const auto &dep : it.second->includes) {
Set<uint32_t> seen;
if (!validateNeedsInclude(it.second, static_cast<Dep*>(dep.second), seen)) {
writeToConnetion(String::format<128>("%s includes %s for no reason",
path.constData(),
Location::path(dep.second->fileId).constData()));
}
}
for (const auto &ref : it.second->references) {
const Path refPath = Location::path(ref.first);
if (refPath.startsWith("/usr/include/sys/_types/_") || refPath.startsWith("/usr/include/_types/_"))
continue;
Set<uint32_t> seen;
if (!validateHasInclude(ref.first, it.second, seen)) {
List<String> reasons;
for (const auto &r : ref.second) {
String reason;
Log log(&reason);
log << r.first << "=>" << r.second;
reasons << reason;
}
writeToConnetion(String::format<128>("%s should include %s (%s)",
Location::path(it.first).constData(),
Location::path(ref.first).constData(),
String::join(reasons, " ").constData()));
// for (const auto &incs : mDependencies[ref.first]->dependents) {
// writeToConnetion(String::format<128>("GOT INCLUDER %s:%d", Location::path(incs.first).constData(),
// incs.first));
// }
}
}
}
for (auto it : mDependencies) {
delete it.second;
}
}