forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexerJob.cpp
203 lines (185 loc) · 6.79 KB
/
IndexerJob.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
/* 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 "IndexerJob.h"
#include "CompilerManager.h"
#include "Project.h"
#include "rct/Process.h"
#include "RTags.h"
#include "Server.h"
#include "RTagsVersion.h"
uint64_t IndexerJob::sNextId = 1;
IndexerJob::IndexerJob(const SourceList &s,
Flags<Flag> f,
const std::shared_ptr<Project> &p,
const UnsavedFiles &u)
: id(0), flags(f),
project(p->path()), priority(0), unsavedFiles(u), crashCount(0)
{
sources.append(s.front());
for (size_t i=1; i<s.size(); ++i) {
const Source &src = s.at(i);
bool found = false;
for (size_t j=0; j<sources.size(); ++j) {
if (src.compareArguments(sources.at(j))) {
found = true;
break;
}
}
if (!found)
sources.append(src);
}
assert(!sources.isEmpty());
sourceFile = s.begin()->sourceFile();
acquireId();
if (flags & Dirty) {
++priority;
} else if (flags & Reindex) {
priority += 4;
}
Server *server = Server::instance();
assert(server);
if (server->isActiveBuffer(sources.begin()->fileId)) {
priority += 8;
} else if (DependencyNode *node = p->dependencyNode(sources.begin()->fileId)) {
Set<DependencyNode*> seen;
seen.insert(node);
std::function<bool(const DependencyNode *node)> func = [&seen, server, &func](const DependencyNode *n) {
for (const auto &inc : n->includes) {
if (seen.insert(inc.second)
&& !Location::path(n->fileId).isSystem()
&& (server->isActiveBuffer(n->fileId) || func(inc.second))) {
return true;
}
}
return false;
};
if (func(node))
priority += 2;
}
visited.insert(sources.begin()->fileId);
}
IndexerJob::~IndexerJob()
{
destroyed(this);
}
void IndexerJob::acquireId()
{
id = sNextId++;
}
String IndexerJob::encode() const
{
String ret;
{
Serializer serializer(ret);
serializer.write("1234", sizeof(int)); // for size
std::shared_ptr<Project> proj = Server::instance()->project(project);
const Server::Options &options = Server::instance()->options();
serializer << static_cast<uint16_t>(RTags::DatabaseVersion)
<< options.sandboxRoot
<< id
<< options.socketFile
<< project
<< static_cast<uint32_t>(sources.size());
for (Source copy : sources) {
if (!(options.options & Server::AllowWErrorAndWFatalErrors)) {
int idx = copy.arguments.indexOf("-Werror");
if (idx != -1)
copy.arguments.removeAt(idx);
idx = copy.arguments.indexOf("-Wfatal-errors");
if (idx != -1)
copy.arguments.removeAt(idx);
}
copy.arguments << options.defaultArguments;
if (!(options.options & Server::AllowPedantic)) {
const int idx = copy.arguments.indexOf("-Wpedantic");
if (idx != -1) {
copy.arguments.removeAt(idx);
}
}
if (options.options & Server::EnableCompilerManager) {
CompilerManager::applyToSource(copy, CompilerManager::IncludeIncludePaths);
}
for (const String &blocked : options.blockedArguments) {
if (blocked.endsWith("=")) {
size_t i = 0;
while (i<copy.arguments.size()) {
if (copy.arguments.at(i).startsWith(blocked)) {
// error() << "Removing" << copy.arguments.at(i);
copy.arguments.remove(i, 1);
} else if (!strncmp(blocked.constData(), copy.arguments.at(i).constData(), blocked.size() - 1)) {
const size_t count = (i + 1 < copy.arguments.size()) ? 2 : 1;
// error() << "Removing" << copy.arguments.mid(i, count);
copy.arguments.remove(i, count);
} else {
++i;
}
}
} else {
copy.arguments.remove(blocked);
}
}
for (const auto &inc : options.includePaths) {
copy.includePaths << inc;
}
if (Server::instance()->options().options & Server::PCHEnabled)
proj->fixPCH(copy);
copy.defines << options.defines;
if (!(options.options & Server::EnableNDEBUG)) {
copy.defines.remove(Source::Define("NDEBUG"));
}
assert(!sourceFile.isEmpty());
copy.encode(serializer, Source::IgnoreSandbox);
}
assert(proj);
serializer << sourceFile
<< flags
<< static_cast<uint32_t>(options.rpVisitFileTimeout)
<< static_cast<uint32_t>(options.rpIndexDataMessageTimeout)
<< static_cast<uint32_t>(options.rpConnectTimeout)
<< static_cast<uint32_t>(options.rpConnectAttempts)
<< static_cast<int32_t>(options.rpNiceValue)
<< options.options
<< unsavedFiles
<< options.dataDir
<< options.debugLocations;
proj->encodeVisitedFiles(serializer);
}
const uint32_t size = ret.size() - sizeof(int);
memcpy(&ret[0], &size, sizeof(size));
return ret;
}
String IndexerJob::dumpFlags(Flags<Flag> flags)
{
List<String> ret;
if (flags & Dirty) {
ret += "Dirty";
}
if (flags & Reindex) {
ret += "Reindex";
}
if (flags & Compile) {
ret += "Compile";
}
if (flags & Running) {
ret += "Running";
}
if (flags & Crashed) {
ret += "Crashed";
}
if (flags & Aborted) {
ret += "Aborted";
}
if (flags & Complete) {
ret += "Complete";
}
return String::join(ret, ", ");
}