aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/clangbackend/source/clangjobrequest.cpp
blob: bbf2f726e6df2ba6be39d2a32a9b4daee6a5d0e2 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "clangjobrequest.h"

#include "clangcompletecodejob.h"
#include "clangfollowsymboljob.h"
#include "clangparsesupportivetranslationunitjob.h"
#include "clangrequestannotationsjob.h"
#include "clangrequestreferencesjob.h"
#include "clangrequesttooltipjob.h"
#include "clangresumedocumentjob.h"
#include "clangsuspenddocumentjob.h"
#include "clangupdateannotationsjob.h"
#include "clangupdateextraannotationsjob.h"

#include <clangsupport/clangcodemodelclientinterface.h>
#include <clangsupport/completionsmessage.h>
#include <clangsupport/followsymbolmessage.h>
#include <clangsupport/referencesmessage.h>
#include <clangsupport/tooltipmessage.h>

#include <utils/qtcassert.h>

#include <QFileInfo>

#include <ostream>

namespace ClangBackEnd {

#define RETURN_TEXT_FOR_CASE(enumValue) case JobRequest::Type::enumValue: return #enumValue
static const char *JobRequestTypeToText(JobRequest::Type type)
{
    switch (type) {
        RETURN_TEXT_FOR_CASE(Invalid);
        RETURN_TEXT_FOR_CASE(UpdateAnnotations);
        RETURN_TEXT_FOR_CASE(UpdateExtraAnnotations);
        RETURN_TEXT_FOR_CASE(ParseSupportiveTranslationUnit);
        RETURN_TEXT_FOR_CASE(RequestCompletions);
        RETURN_TEXT_FOR_CASE(RequestAnnotations);
        RETURN_TEXT_FOR_CASE(RequestReferences);
        RETURN_TEXT_FOR_CASE(RequestFollowSymbol);
        RETURN_TEXT_FOR_CASE(RequestToolTip);
        RETURN_TEXT_FOR_CASE(SuspendDocument);
        RETURN_TEXT_FOR_CASE(ResumeDocument);
    }

    return "UnhandledJobRequestType";
}
#undef RETURN_TEXT_FOR_CASE

#define RETURN_TEXT_FOR_CASE(enumValue) case PreferredTranslationUnit::enumValue: return #enumValue
const char *preferredTranslationUnitToText(PreferredTranslationUnit type)
{
    switch (type) {
        RETURN_TEXT_FOR_CASE(RecentlyParsed);
        RETURN_TEXT_FOR_CASE(PreviouslyParsed);
        RETURN_TEXT_FOR_CASE(LastUninitialized);
    }

    return "UnhandledPreferredTranslationUnitType";
}
#undef RETURN_TEXT_FOR_CASE

QDebug operator<<(QDebug debug, JobRequest::Type type)
{
    debug << JobRequestTypeToText(type);

    return debug;
}

std::ostream &operator<<(std::ostream &os, JobRequest::Type type)
{
    return os << JobRequestTypeToText(type);
}

std::ostream &operator<<(std::ostream &os, PreferredTranslationUnit preferredTranslationUnit)
{
    return os << preferredTranslationUnitToText(preferredTranslationUnit);
}

QDebug operator<<(QDebug debug, const JobRequest &jobRequest)
{
    debug.nospace() << "Job<"
                    << jobRequest.id
                    << ","
                    << QFileInfo(jobRequest.filePath).fileName()
                    << ","
                    << JobRequestTypeToText(jobRequest.type)
                    << ","
                    << preferredTranslationUnitToText(jobRequest.preferredTranslationUnit)
                    << ">";

    return debug.space();
}

static JobRequest::ExpirationConditions expirationConditionsForType(JobRequest::Type type)
{
    using Type = JobRequest::Type;
    using Condition = JobRequest::ExpirationCondition;
    using Conditions = JobRequest::ExpirationConditions;

    switch (type) {
    case Type::UpdateAnnotations:
    case Type::UpdateExtraAnnotations:
        return Conditions(Condition::AnythingChanged);
    case Type::RequestReferences:
    case Type::RequestAnnotations:
    case Type::RequestToolTip:
    case Type::RequestFollowSymbol:
        return Conditions(Condition::DocumentClosed)
             | Conditions(Condition::DocumentRevisionChanged);
    default:
        return Condition::DocumentClosed;
    }
}

static JobRequest::RunConditions conditionsForType(JobRequest::Type type)
{
    using Type = JobRequest::Type;
    using Condition = JobRequest::RunCondition;
    using Conditions = JobRequest::RunConditions;

    if (type == Type::SuspendDocument) {
        return Conditions(Condition::DocumentUnsuspended)
             | Conditions(Condition::DocumentNotVisible);
    }

    if (type == Type::ResumeDocument) {
        return Conditions(Condition::DocumentSuspended)
             | Conditions(Condition::DocumentVisible);
    }

    Conditions conditions = Conditions(Condition::DocumentUnsuspended)
                          | Conditions(Condition::DocumentVisible);

    if (type == Type::RequestReferences || type == Type::RequestFollowSymbol
        || type == Type::RequestToolTip || type == Type::UpdateExtraAnnotations) {
        conditions |= Condition::CurrentDocumentRevision;
    }

    if (type != Type::UpdateAnnotations && type != Type::ParseSupportiveTranslationUnit)
        conditions |= Condition::DocumentParsed;

    return conditions;
}

bool JobRequest::isTakeOverable() const
{
    // When new project information comes in and there are unprocessed jobs
    // in the queue, we need to decide what to do with them.

    switch (type) {
    // Never discard these as the client side might wait for a response.
    case Type::RequestCompletions:
    case Type::RequestReferences:
    case Type::RequestFollowSymbol:
    case Type::RequestToolTip:
        return true;

    // Discard this one as UpdateAnnotations will have the same effect.
    case Type::RequestAnnotations:

    // Discard Suspend because the document will be cleared anyway.
    // Discard Resume because a (re)parse will happen on demand.
    case Type::SuspendDocument:
    case Type::ResumeDocument:

    // Discard these as they are initial jobs that will be recreated on demand
    // anyway.
    case Type::UpdateAnnotations:
    case Type::UpdateExtraAnnotations:

    // Discard these as they only make sense in a row. Avoid splitting them up.
    case Type::ParseSupportiveTranslationUnit:

    case Type::Invalid:
        return false;
    }

    return false;
}

JobRequest::JobRequest(Type type)
{
    static quint64 idCounter = 0;

    id = ++idCounter;
    this->type = type;
    runConditions = conditionsForType(type);
    expirationConditions = expirationConditionsForType(type);
}

IAsyncJob *JobRequest::createJob() const
{
    switch (type) {
    case JobRequest::Type::Invalid:
        QTC_CHECK(false && "Cannot create job for invalid job request.");
        break;
    case JobRequest::Type::UpdateAnnotations:
        return new UpdateAnnotationsJob();
    case JobRequest::Type::UpdateExtraAnnotations:
        return new UpdateExtraAnnotationsJob();
    case JobRequest::Type::ParseSupportiveTranslationUnit:
        return new ParseSupportiveTranslationUnitJob();
    case JobRequest::Type::RequestCompletions:
        return new CompleteCodeJob();
    case JobRequest::Type::RequestAnnotations:
        return new RequestAnnotationsJob();
    case JobRequest::Type::RequestReferences:
        return new RequestReferencesJob();
    case JobRequest::Type::RequestToolTip:
         return new RequestToolTipJob();
    case JobRequest::Type::RequestFollowSymbol:
        return new FollowSymbolJob();
    case JobRequest::Type::SuspendDocument:
        return new SuspendDocumentJob();
    case JobRequest::Type::ResumeDocument:
        return new ResumeDocumentJob();
    }

    return nullptr;
}

void JobRequest::cancelJob(ClangCodeModelClientInterface &client) const
{
    // If a job request with a ticket number is cancelled, the plugin side
    // must get back some results in order to clean up the state there.

    switch (type) {
    case JobRequest::Type::Invalid:
    case JobRequest::Type::UpdateAnnotations:
    case JobRequest::Type::UpdateExtraAnnotations:
    case JobRequest::Type::ParseSupportiveTranslationUnit:
    case JobRequest::Type::RequestAnnotations:
    case JobRequest::Type::SuspendDocument:
    case JobRequest::Type::ResumeDocument:
        break;
    case JobRequest::Type::RequestReferences:
        client.references(ReferencesMessage(FileContainer(),
                                            QVector<SourceRangeContainer>(),
                                            false,
                                            ticketNumber));
        break;
    case JobRequest::Type::RequestToolTip:
        client.tooltip(ToolTipMessage(FileContainer(), ToolTipInfo(), ticketNumber));
        break;
    case JobRequest::Type::RequestCompletions:
        client.completions(CompletionsMessage(CodeCompletions(), ticketNumber));
        break;
    case JobRequest::Type::RequestFollowSymbol:
        client.followSymbol(
            FollowSymbolMessage(FileContainer(), SourceRangeContainer(), ticketNumber));
        break;
    }
}

bool JobRequest::operator==(const JobRequest &other) const
{
    return type == other.type
        && expirationConditions == other.expirationConditions
        && runConditions == other.runConditions

        && filePath == other.filePath
        && unsavedFilesChangeTimePoint == other.unsavedFilesChangeTimePoint
        && documentRevision == other.documentRevision
        && preferredTranslationUnit == other.preferredTranslationUnit

        && line == other.line
        && column == other.column
        && ticketNumber == other.ticketNumber;

        // Additional members that are not compared here explicitly are
        // supposed to depend on the already compared ones.
}

} // namespace ClangBackEnd