builtineditordocumentparser.cpp 10 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(const InMemoryInfo &info)
48
{
49 50 51
    if (filePath().isEmpty())
        return;

52 53 54 55 56
    const Configuration baseConfig = configuration();
    const bool releaseSourceAndAST_ = releaseSourceAndAST();

    State baseState = state();
    ExtraState state = extraState();
57
    WorkingCopy workingCopy = info.workingCopy;
58

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

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

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

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

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

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

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

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

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

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

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

    Snapshot globalSnapshot = modelManager->snapshot();

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

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

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

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

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

    setState(baseState);
    setExtraState(state);

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

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

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

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

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

229
BuiltinEditorDocumentParser::Ptr BuiltinEditorDocumentParser::get(const QString &filePath)
230
{
231 232 233
    if (BaseEditorDocumentParser::Ptr b = BaseEditorDocumentParser::get(filePath))
        return b.objectCast<BuiltinEditorDocumentParser>();
    return BuiltinEditorDocumentParser::Ptr();
234 235
}

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

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

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