forked from KDE/kdev-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zigparsejob.cpp
257 lines (221 loc) · 8.29 KB
/
zigparsejob.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
/*
* Copyright 2017 Emma Gospodinova <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zigparsejob.h"
#include <serialization/indexedstring.h>
#include <interfaces/icore.h>
#include <interfaces/iproject.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <language/interfaces/ilanguagesupport.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/backgroundparser/urlparselock.h>
#include <language/editor/documentrange.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/duchaindumper.h>
#include <project/projectmodel.h>
#include <util/path.h>
#include <QReadLocker>
#include "duchain/parsesession.h"
#include "duchain/kdevzigastparser.h"
#include "duchain/declarationbuilder.h"
#include "duchain/usebuilder.h"
#include "ziglanguagesupport.h"
#include "zigdebug.h"
#include <helpers.h>
#ifdef KDEV_ZIG_VALGRIND
#include <valgrind/callgrind.h>
#endif
using namespace KDevelop;
namespace Zig
{
#ifdef KDEV_ZIG_VALGRIND
class ValgrindTracer
{
public:
ValgrindTracer() {
CALLGRIND_START_INSTRUMENTATION;
CALLGRIND_TOGGLE_COLLECT;
qCDebug(KDEV_ZIG) << "Valgrind collection started";
}
~ValgrindTracer() {
CALLGRIND_TOGGLE_COLLECT;
CALLGRIND_STOP_INSTRUMENTATION;
qCDebug(KDEV_ZIG) << "Valgrind collection stopped";
}
};
#endif
ParseJob::ParseJob(const IndexedString &url, ILanguageSupport *languageSupport)
: KDevelop::ParseJob(url, languageSupport)
{
}
LanguageSupport *ParseJob::zig() const
{
return static_cast<LanguageSupport *>(languageSupport());
}
ParseSessionData::Ptr ParseJob::findParseSessionData(const IndexedString &url)
{
DUChainReadLocker lock;
auto context = KDevelop::DUChainUtils::standardContextForUrl(url.toUrl());
if (context) {
return ParseSessionData::Ptr(dynamic_cast<ParseSessionData *>(context->ast().data()));
}
return {};
}
ParseSessionData::Ptr ParseJob::createSessionData() const
{
return ParseSessionData::Ptr(new ParseSessionData(document(), contents().contents, this, parsePriority()));
}
void ParseJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
{
#ifdef KDEV_ZIG_VALGRIND
ValgrindTracer valgrind;
#endif
Q_UNUSED(self);
Q_UNUSED(thread);
QReadLocker parseLock(languageSupport()->parseLock());
if (abortRequested()) {
return;
}
qCDebug(KDEV_ZIG) << "Parse job starting for: " << document().toUrl();
{
UrlParseLock urlLock(document());
if (abortRequested() || !isUpdateRequired(ParseSession::languageString())) {
return;
}
ProblemPointer readProblem = readContents();
if (readProblem) {
return;
}
}
ParseSession session(findParseSessionData(document()));
if (!session.data()) {
session.setData(createSessionData());
}
session.parse();
if (abortRequested()) {
return;
}
ReferencedTopDUContext toUpdate = nullptr;
{
DUChainReadLocker lock;
toUpdate = DUChainUtils::standardContextForUrl(document().toUrl());
}
if (toUpdate) {
translateDUChainToRevision(toUpdate);
DUChainWriteLocker lock; // Must come after translateDUChainToRevision
toUpdate->setRange(RangeInRevision(0, 0, INT_MAX, INT_MAX));
toUpdate->clearProblems();
}
if (abortRequested()) {
return;
}
const auto num_errors = ast_error_count(session.ast());
ReferencedTopDUContext context;
if (num_errors == 0) {
ZigNode root = {session.ast(), 0};
qCDebug(KDEV_ZIG) << "Parsing succeeded for: " << document().toUrl();
DeclarationBuilder builder;
builder.setParseSession(&session);
// For reasons I don't understand when using setAst the highlighting
// breaks and the UpdateHighlighting hack used by the clangparsejob
// must be used
context = builder.build(document(), &root, toUpdate);
setDuChain(context);
if (abortRequested()) {
return;
}
UseBuilder uses(document());
uses.setParseSession(&session);
uses.buildUses(&root);
} else {
qCDebug(KDEV_ZIG) << "Parsing failed for: " << document().toUrl();
DUChainWriteLocker lock;
context = toUpdate.data();
if (context) {
ParsingEnvironmentFilePointer parsingEnvironmentFile = context->parsingEnvironmentFile();
parsingEnvironmentFile->setModificationRevision(contents().modification);
context->clearProblems();
} else {
ParsingEnvironmentFile *file = new ParsingEnvironmentFile(document());
file->setLanguage(ParseSession::languageString());
context = new TopDUContext(document(), RangeInRevision(0, 0, INT_MAX, INT_MAX), file);
DUChain::self()->addDocumentChain(context);
}
setDuChain(context);
}
if (abortRequested()) {
return;
}
if (num_errors > 0) {
DUChainWriteLocker lock;
for (uint32_t i=0; i < num_errors; i++) {
ZigError error = ZigError(ast_error_at(session.ast(), i));
if(error.data() != nullptr) {
ProblemPointer p = ProblemPointer(new Problem());
p->setFinalLocation(DocumentRange(document(), KTextEditor::Range(
error.data()->range.start.line,
error.data()->range.start.column,
error.data()->range.end.line,
error.data()->range.end.column)));
p->setSource(IProblem::Parser);
p->setSeverity(static_cast<IProblem::Severity>(error.data()->severity));
p->setDescription(QString::fromUtf8(error.data()->message));
context->addProblem(p);
}
}
}
// If some imports are still unresolved, reschedule it.
// Pulled from kdev-python
// TODO: This needs to somehow make sure that all imports have been resolved
// in the dependents because each dependency can still have unresolved
// imports...
// if (!session.unresolvedImports().isEmpty()) {
// DUChainWriteLocker lock;
//
// // If the dependencies were not scheduled for some reason
// // then reparsing will not do anything
// bool dependencyInQueue = false;
// for ( const IndexedString& url : session.unresolvedImports() ) {
// if (KDevelop::ICore::self()->languageController()->backgroundParser()->isQueued(url)
// || DUChain::self()->chainForDocument(url)) {
// dependencyInQueue= true;
// break;
// }
// }
//
// if (!(minimumFeatures() & Rescheduled) && dependencyInQueue) {
// constexpr TopDUContext::Features features{TopDUContext::ForceUpdate};
// KDevelop::ICore::self()->languageController()->backgroundParser()->addDocument(
// document(),
// static_cast<TopDUContext::Features>(features | Rescheduled), parsePriority(),
// nullptr, ParseJob::FullSequentialProcessing);
// }
// }
{
DUChainWriteLocker lock;
context->setFeatures(minimumFeatures());
ParsingEnvironmentFilePointer file = context->parsingEnvironmentFile();
Q_ASSERT(file);
file->setModificationRevision(contents().modification);
DUChain::self()->updateContextEnvironment(context->topContext(), file.data());
}
highlightDUChain();
DUChain::self()->emitUpdateReady(document(), duChain());
qCDebug(KDEV_ZIG) << "Parse job finished for: " << document().toUrl();
}
}