builtineditordocumentparser.cpp 9.96 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

31
#include "builtineditordocumentparser.h"
32
#include "cppsourceprocessor.h"
33
#include "editordocumenthandle.h"
34 35 36 37 38 39 40

#include <utils/qtcassert.h>

using namespace CPlusPlus;
using namespace CppTools;
using namespace CppTools::Internal;

41
BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const QString &filePath)
42
    : BaseEditorDocumentParser(filePath)
43
{
44
    qRegisterMetaType<CPlusPlus::Snapshot>("CPlusPlus::Snapshot");
45 46
}

47
void BuiltinEditorDocumentParser::updateHelper(WorkingCopy workingCopy)
48
{
49 50 51 52 53
    const Configuration baseConfig = configuration();
    const bool releaseSourceAndAST_ = releaseSourceAndAST();

    State baseState = state();
    ExtraState state = extraState();
54

55
    if (filePath().isEmpty())
56 57
        return;

58
    bool invalidateSnapshot = false, invalidateConfig = false, editorDefinesChanged_ = false;
59

60
    CppModelManager *modelManager = CppModelManager::instance();
61
    QByteArray configFile = modelManager->codeModelConfiguration();
62
    ProjectPart::HeaderPaths headerPaths;
63
    QStringList precompiledHeaders;
64
    QString projectConfigFile;
65
    LanguageFeatures features = LanguageFeatures::defaultFeatures();
66

67
    baseState.projectPart = determineProjectPart(filePath(), baseConfig, baseState);
68

69
    if (state.forceSnapshotInvalidation) {
70
        invalidateSnapshot = true;
71
        state.forceSnapshotInvalidation = false;
72 73
    }

74
    if (const ProjectPart::Ptr part = baseState.projectPart) {
75 76 77 78
        configFile += part->toolchainDefines;
        configFile += part->projectDefines;
        headerPaths = part->headerPaths;
        projectConfigFile = part->projectConfigFile;
79
        if (baseConfig.usePrecompiledHeaders)
80
            precompiledHeaders = part->precompiledHeaders;
81
        features = part->languageFeatures;
82 83
    }

84 85
    if (configFile != state.configFile) {
        state.configFile = configFile;
86 87 88 89
        invalidateSnapshot = true;
        invalidateConfig = true;
    }

90 91
    if (baseConfig.editorDefines != baseState.editorDefines) {
        baseState.editorDefines = baseConfig.editorDefines;
92
        invalidateSnapshot = true;
93
        editorDefinesChanged_ = true;
94 95
    }

96 97
    if (headerPaths != state.headerPaths) {
        state.headerPaths = headerPaths;
98 99 100
        invalidateSnapshot = true;
    }

101 102
    if (projectConfigFile != state.projectConfigFile) {
        state.projectConfigFile = projectConfigFile;
103 104 105
        invalidateSnapshot = true;
    }

106 107
    if (precompiledHeaders != state.precompiledHeaders) {
        state.precompiledHeaders = precompiledHeaders;
108 109 110
        invalidateSnapshot = true;
    }

111
    unsigned rev = 0;
112
    if (Document::Ptr doc = state.snapshot.document(filePath()))
113 114 115 116 117 118 119
        rev = doc->revision();
    else
        invalidateSnapshot = true;

    Snapshot globalSnapshot = modelManager->snapshot();

    if (invalidateSnapshot) {
120
        state.snapshot = Snapshot();
121 122
    } else {
        // Remove changed files from the snapshot
123
        QSet<Utils::FileName> toRemove;
124
        foreach (const Document::Ptr &doc, state.snapshot) {
125
            const Utils::FileName fileName = Utils::FileName::fromString(doc->fileName());
126 127
            if (workingCopy.contains(fileName)) {
                if (workingCopy.get(fileName).second != doc->editorRevision())
128
                    addFileAndDependencies(&state.snapshot, &toRemove, fileName);
129 130 131 132
                continue;
            }
            Document::Ptr otherDoc = globalSnapshot.document(fileName);
            if (!otherDoc.isNull() && otherDoc->revision() != doc->revision())
133
                addFileAndDependencies(&state.snapshot, &toRemove, fileName);
134 135 136 137
        }

        if (!toRemove.isEmpty()) {
            invalidateSnapshot = true;
138
            foreach (const Utils::FileName &fileName, toRemove)
139
                state.snapshot.remove(fileName);
140 141 142 143 144 145 146
        }
    }

    // Update the snapshot
    if (invalidateSnapshot) {
        const QString configurationFileName = modelManager->configurationFileName();
        if (invalidateConfig)
147 148 149 150
            state.snapshot.remove(configurationFileName);
        if (!state.snapshot.contains(configurationFileName))
            workingCopy.insert(configurationFileName, state.configFile);
        state.snapshot.remove(filePath());
151

152
        static const QString editorDefinesFileName
153
            = CppModelManager::editorConfigurationFileName();
154
        if (editorDefinesChanged_) {
155 156
            state.snapshot.remove(editorDefinesFileName);
            workingCopy.insert(editorDefinesFileName, baseState.editorDefines);
157 158
        }

159
        CppSourceProcessor sourceProcessor(state.snapshot, [&](const Document::Ptr &doc) {
160
            const QString fileName = doc->fileName();
161
            const bool isInEditor = fileName == filePath();
162 163 164 165 166 167
            Document::Ptr otherDoc = modelManager->document(fileName);
            unsigned newRev = otherDoc.isNull() ? 1U : otherDoc->revision() + 1;
            if (isInEditor)
                newRev = qMax(rev + 1, newRev);
            doc->setRevision(newRev);
            modelManager->emitDocumentUpdated(doc);
168
            if (releaseSourceAndAST_)
169
                doc->releaseSourceAndAST();
170
        });
171
        Snapshot globalSnapshot = modelManager->snapshot();
172
        globalSnapshot.remove(filePath());
173 174
        sourceProcessor.setGlobalSnapshot(globalSnapshot);
        sourceProcessor.setWorkingCopy(workingCopy);
175
        sourceProcessor.setHeaderPaths(state.headerPaths);
176
        sourceProcessor.setLanguageFeatures(features);
177
        sourceProcessor.run(configurationFileName);
178 179 180 181
        if (!state.projectConfigFile.isEmpty())
            sourceProcessor.run(state.projectConfigFile);
        if (baseConfig.usePrecompiledHeaders) {
            foreach (const QString &precompiledHeader, state.precompiledHeaders)
182
                sourceProcessor.run(precompiledHeader);
183
        }
184
        if (!baseState.editorDefines.isEmpty())
185
            sourceProcessor.run(editorDefinesFileName);
186 187 188 189 190
        sourceProcessor.run(filePath(), baseConfig.usePrecompiledHeaders ? state.precompiledHeaders
                                                                         : QStringList());
        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) {
191
            if (Client::isInjectedFile(i.key().toString()))
192 193
                newSnapshot.insert(i.value());
        }
194 195
        state.snapshot = newSnapshot;
        state.snapshot.updateDependencyTable();
196
    }
197 198 199 200 201 202

    setState(baseState);
    setExtraState(state);

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

205
void BuiltinEditorDocumentParser::releaseResources()
206
{
207 208 209 210
    ExtraState s = extraState();
    s.snapshot = Snapshot();
    s.forceSnapshotInvalidation = true;
    setExtraState(s);
211 212
}

213
Document::Ptr BuiltinEditorDocumentParser::document() const
214
{
215
    return extraState().snapshot.document(filePath());
216 217
}

218
Snapshot BuiltinEditorDocumentParser::snapshot() const
219
{
220
    return extraState().snapshot;
221 222
}

223
ProjectPart::HeaderPaths BuiltinEditorDocumentParser::headerPaths() const
224
{
225
    return extraState().headerPaths;
226 227
}

228 229 230 231 232 233 234
BuiltinEditorDocumentParser *BuiltinEditorDocumentParser::get(const QString &filePath)
{
    if (BaseEditorDocumentParser *b = BaseEditorDocumentParser::get(filePath))
        return qobject_cast<BuiltinEditorDocumentParser *>(b);
    return 0;
}

235 236
void BuiltinEditorDocumentParser::addFileAndDependencies(Snapshot *snapshot,
                                                         QSet<Utils::FileName> *toRemove,
237
                                                         const Utils::FileName &fileName) const
238
{
239 240
    QTC_ASSERT(snapshot, return);

241
    toRemove->insert(fileName);
242
    if (fileName != Utils::FileName::fromString(filePath())) {
243
        Utils::FileNameList deps = snapshot->filesDependingOn(fileName);
244
        toRemove->unite(QSet<Utils::FileName>::fromList(deps));
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

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;
}