aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/builtineditordocumentparser.cpp
blob: 627e36d3b3d452e56507e6dc3ae21acab11578ed (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "builtineditordocumentparser.h"

#include "cppsourceprocessor.h"

#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectmacro.h>

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

#include <QPromise>

using namespace CPlusPlus;
using namespace Utils;

namespace CppEditor {

static QByteArray overwrittenToolchainDefines(const ProjectPart &projectPart)
{
    QByteArray defines;

    // MSVC's predefined macros like __FUNCSIG__ expand to itself.
    // We can't parse this, so redefine to the empty string literal.
    if (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
        defines += "#define __FUNCSIG__ \"void __cdecl "
                   "someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580(void)\"\n"
                   "#define __FUNCDNAME__ "
                   "\"?someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580@@YAXXZ\"\n"
                   "#define __FUNCTION__ "
                   "\"someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580\"\n";
    }

    return defines;
}

BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const FilePath &filePath,
                                                         int fileSizeLimitInMb)
    : BaseEditorDocumentParser(filePath)
    , m_fileSizeLimitInMb(fileSizeLimitInMb)
{
    qRegisterMetaType<CPlusPlus::Snapshot>("CPlusPlus::Snapshot");
}

void BuiltinEditorDocumentParser::updateImpl(const QPromise<void> &promise,
                                             const UpdateParams &updateParams)
{
    if (filePath().isEmpty())
        return;

    const Configuration baseConfig = configuration();
    const bool releaseSourceAndAST_ = releaseSourceAndAST();

    State baseState = state();
    ExtraState state = extraState();
    WorkingCopy workingCopy = updateParams.workingCopy;

    bool invalidateSnapshot = false, invalidateConfig = false;

    QByteArray configFile = CppModelManager::codeModelConfiguration();
    ProjectExplorer::HeaderPaths headerPaths;
    FilePaths includedFiles;
    FilePaths precompiledHeaders;
    QString projectConfigFile;
    LanguageFeatures features = LanguageFeatures::defaultFeatures();

    baseState.projectPartInfo = determineProjectPart(filePath().toString(),
                                                     baseConfig.preferredProjectPartId,
                                                     baseState.projectPartInfo,
                                                     updateParams.activeProject,
                                                     updateParams.languagePreference,
                                                     updateParams.projectsUpdated);
    emit projectPartInfoUpdated(baseState.projectPartInfo);

    if (state.forceSnapshotInvalidation) {
        invalidateSnapshot = true;
        state.forceSnapshotInvalidation = false;
    }

    if (const ProjectPart::ConstPtr part = baseState.projectPartInfo.projectPart) {
        configFile += ProjectExplorer::Macro::toByteArray(part->toolchainMacros);
        configFile += overwrittenToolchainDefines(*part.data());
        configFile += ProjectExplorer::Macro::toByteArray(part->projectMacros);
        if (!part->projectConfigFile.isEmpty())
            configFile += ProjectPart::readProjectConfigFile(part->projectConfigFile);
        headerPaths = part->headerPaths;
        projectConfigFile = part->projectConfigFile;
        includedFiles = Utils::transform(part->includedFiles, &FilePath::fromString);
        if (baseConfig.usePrecompiledHeaders)
            precompiledHeaders = Utils::transform(part->precompiledHeaders, &FilePath::fromString);
        features = part->languageFeatures;
    }

    if (configFile != state.configFile) {
        state.configFile = configFile;
        invalidateSnapshot = true;
        invalidateConfig = true;
    }

    if (baseConfig.editorDefines != baseState.editorDefines) {
        baseState.editorDefines = baseConfig.editorDefines;
        invalidateSnapshot = true;
    }

    if (headerPaths != state.headerPaths) {
        state.headerPaths = headerPaths;
        invalidateSnapshot = true;
    }

    if (projectConfigFile != state.projectConfigFile) {
        state.projectConfigFile = projectConfigFile;
        invalidateSnapshot = true;
    }

    if (includedFiles != state.includedFiles) {
        state.includedFiles = includedFiles;
        invalidateSnapshot = true;
    }

    if (precompiledHeaders != state.precompiledHeaders) {
        state.precompiledHeaders = precompiledHeaders;
        invalidateSnapshot = true;
    }

    unsigned rev = 0;
    if (Document::Ptr doc = state.snapshot.document(filePath()))
        rev = doc->revision();
    else
        invalidateSnapshot = true;

    Snapshot globalSnapshot = CppModelManager::snapshot();

    if (invalidateSnapshot) {
        state.snapshot = Snapshot();
        if (!baseState.editorDefines.isEmpty()) {
            workingCopy.insert(CppModelManager::editorConfigurationFileName(),
                               baseState.editorDefines);
        }
    } else {
        // Remove changed files from the snapshot
        QSet<Utils::FilePath> toRemove;
        for (const Document::Ptr &doc : std::as_const(state.snapshot)) {
            const Utils::FilePath filePath = doc->filePath();
            if (const auto entry = workingCopy.get(filePath)) {
                if (entry->second != doc->editorRevision())
                    addFileAndDependencies(&state.snapshot, &toRemove, filePath);
                continue;
            }
            Document::Ptr otherDoc = globalSnapshot.document(filePath);
            if (!otherDoc.isNull() && otherDoc->revision() != doc->revision())
                addFileAndDependencies(&state.snapshot, &toRemove, filePath);
        }

        if (!toRemove.isEmpty()) {
            invalidateSnapshot = true;
            for (const Utils::FilePath &fileName : std::as_const(toRemove))
                state.snapshot.remove(fileName);
        }
    }

    // Update the snapshot
    if (invalidateSnapshot) {
        const FilePath configurationFileName = CppModelManager::configurationFileName();
        if (invalidateConfig)
            state.snapshot.remove(configurationFileName);
        if (!state.snapshot.contains(configurationFileName))
            workingCopy.insert(configurationFileName, state.configFile);
        state.snapshot.remove(filePath());

        Internal::CppSourceProcessor sourceProcessor(state.snapshot, [&](const Document::Ptr &doc) {
            const bool isInEditor = doc->filePath() == filePath();
            Document::Ptr otherDoc = CppModelManager::document(doc->filePath());
            unsigned newRev = otherDoc.isNull() ? 1U : otherDoc->revision() + 1;
            if (isInEditor)
                newRev = qMax(rev + 1, newRev);
            doc->setRevision(newRev);
            CppModelManager::emitDocumentUpdated(doc);
            if (releaseSourceAndAST_)
                doc->releaseSourceAndAST();
        });
        sourceProcessor.setFileSizeLimitInMb(m_fileSizeLimitInMb);
        sourceProcessor.setCancelChecker([&promise] { return promise.isCanceled(); });

        Snapshot globalSnapshot = CppModelManager::snapshot();
        globalSnapshot.remove(filePath());
        sourceProcessor.setGlobalSnapshot(globalSnapshot);
        sourceProcessor.setWorkingCopy(workingCopy);
        sourceProcessor.setHeaderPaths(state.headerPaths);
        sourceProcessor.setLanguageFeatures(features);
        sourceProcessor.run(configurationFileName);
        if (baseConfig.usePrecompiledHeaders) {
            for (const FilePath &precompiledHeader : std::as_const(state.precompiledHeaders))
                sourceProcessor.run(precompiledHeader);
        }
        if (!baseState.editorDefines.isEmpty())
            sourceProcessor.run(CppModelManager::editorConfigurationFileName());
        FilePaths includedFiles = state.includedFiles;
        if (baseConfig.usePrecompiledHeaders)
            includedFiles << state.precompiledHeaders;
        FilePath::removeDuplicates(includedFiles);
        sourceProcessor.run(filePath(), includedFiles);
        state.snapshot = sourceProcessor.snapshot();
        Snapshot newSnapshot = state.snapshot.simplified(state.snapshot.document(filePath()));
        for (Snapshot::const_iterator i = state.snapshot.begin(), ei = state.snapshot.end();
             i != ei;
             ++i) {
            if (Client::isInjectedFile(i.key().toString()))
                newSnapshot.insert(i.value());
        }
        state.snapshot = newSnapshot;
        state.snapshot.updateDependencyTable();
    }

    setState(baseState);
    setExtraState(state);

    if (invalidateSnapshot)
        emit finished(state.snapshot.document(filePath()), state.snapshot);
}

void BuiltinEditorDocumentParser::releaseResources()
{
    ExtraState s = extraState();
    s.snapshot = Snapshot();
    s.forceSnapshotInvalidation = true;
    setExtraState(s);
}

Document::Ptr BuiltinEditorDocumentParser::document() const
{
    return extraState().snapshot.document(filePath());
}

Snapshot BuiltinEditorDocumentParser::snapshot() const
{
    return extraState().snapshot;
}

ProjectExplorer::HeaderPaths BuiltinEditorDocumentParser::headerPaths() const
{
    return extraState().headerPaths;
}

BuiltinEditorDocumentParser::Ptr BuiltinEditorDocumentParser::get(const FilePath &filePath)
{
    if (BaseEditorDocumentParser::Ptr b = BaseEditorDocumentParser::get(filePath))
        return b.objectCast<BuiltinEditorDocumentParser>();
    return BuiltinEditorDocumentParser::Ptr();
}

void BuiltinEditorDocumentParser::addFileAndDependencies(Snapshot *snapshot,
                                                         QSet<Utils::FilePath> *toRemove,
                                                         const Utils::FilePath &fileName) const
{
    QTC_ASSERT(snapshot, return);

    toRemove->insert(fileName);
    if (fileName != filePath()) {
        Utils::FilePaths deps = snapshot->filesDependingOn(fileName);
        toRemove->unite(Utils::toSet(deps));
    }
}

BuiltinEditorDocumentParser::ExtraState BuiltinEditorDocumentParser::extraState() const
{
    QMutexLocker locker(&m_stateAndConfigurationMutex);
    return m_extraState;
}

void BuiltinEditorDocumentParser::setExtraState(const ExtraState &extraState)
{
    QMutexLocker locker(&m_stateAndConfigurationMutex);
    m_extraState = extraState;
}

bool BuiltinEditorDocumentParser::releaseSourceAndAST() const
{
    QMutexLocker locker(&m_stateAndConfigurationMutex);
    return m_releaseSourceAndAST;
}

void BuiltinEditorDocumentParser::setReleaseSourceAndAST(bool release)
{
    QMutexLocker locker(&m_stateAndConfigurationMutex);
    m_releaseSourceAndAST = release;
}

} // namespace CppEditor