cppsnapshotupdater.cpp 10.1 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
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
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

30
#include "cppsourceprocessor.h"
31 32 33 34 35 36 37 38 39 40 41
#include "cppsnapshotupdater.h"

#include <utils/qtcassert.h>

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

SnapshotUpdater::SnapshotUpdater(const QString &fileInEditor)
    : m_mutex(QMutex::Recursive)
    , m_fileInEditor(fileInEditor)
42
    , m_editorDefinesChangedSinceLastUpdate(false)
43
    , m_usePrecompiledHeaders(false)
44
    , m_forceSnapshotInvalidation(false)
45 46 47 48 49 50 51 52 53 54
{
}

void SnapshotUpdater::update(CppModelManager::WorkingCopy workingCopy)
{
    QMutexLocker locker(&m_mutex);

    if (m_fileInEditor.isEmpty())
        return;

55
    bool invalidateSnapshot = false, invalidateConfig = false, editorDefinesChanged = false;
56 57 58 59 60 61

    CppModelManager *modelManager
        = dynamic_cast<CppModelManager *>(CppModelManagerInterface::instance());
    QByteArray configFile = modelManager->codeModelConfiguration();
    QStringList includePaths;
    QStringList frameworkPaths;
62
    QStringList precompiledHeaders;
63
    QString projectConfigFile;
64 65 66

    updateProjectPart();

67 68 69 70 71
    if (m_forceSnapshotInvalidation) {
        invalidateSnapshot = true;
        m_forceSnapshotInvalidation = false;
    }

72
    if (m_projectPart) {
73 74
        configFile += m_projectPart->toolchainDefines;
        configFile += m_projectPart->projectDefines;
75 76
        includePaths = m_projectPart->includePaths;
        frameworkPaths = m_projectPart->frameworkPaths;
77
        projectConfigFile = m_projectPart->projectConfigFile;
78 79
        if (m_usePrecompiledHeaders)
            precompiledHeaders = m_projectPart->precompiledHeaders;
80 81 82 83 84 85 86 87
    }

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

88 89 90 91 92 93
    if (m_editorDefinesChangedSinceLastUpdate) {
        invalidateSnapshot = true;
        editorDefinesChanged = true;
        m_editorDefinesChangedSinceLastUpdate = false;
    }

94 95 96 97 98 99 100 101 102 103
    if (includePaths != m_includePaths) {
        m_includePaths = includePaths;
        invalidateSnapshot = true;
    }

    if (frameworkPaths != m_frameworkPaths) {
        m_frameworkPaths = frameworkPaths;
        invalidateSnapshot = true;
    }

104 105 106 107 108
    if (projectConfigFile != m_projectConfigFile) {
        m_projectConfigFile = projectConfigFile;
        invalidateSnapshot = true;
    }

109 110 111 112 113
    if (precompiledHeaders != m_precompiledHeaders) {
        m_precompiledHeaders = precompiledHeaders;
        invalidateSnapshot = true;
    }

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
    unsigned rev = 0;
    if (Document::Ptr doc = document())
        rev = doc->revision();
    else
        invalidateSnapshot = true;

    Snapshot globalSnapshot = modelManager->snapshot();

    if (invalidateSnapshot) {
        m_snapshot = Snapshot();
    } else {
        // Remove changed files from the snapshot
        QSet<QString> toRemove;
        foreach (const Document::Ptr &doc, m_snapshot) {
            QString fileName = doc->fileName();
            if (workingCopy.contains(fileName)) {
                if (workingCopy.get(fileName).second != doc->editorRevision())
                    addFileAndDependencies(&toRemove, fileName);
                continue;
            }
            Document::Ptr otherDoc = globalSnapshot.document(fileName);
            if (!otherDoc.isNull() && otherDoc->revision() != doc->revision())
                addFileAndDependencies(&toRemove, fileName);
        }

        if (!toRemove.isEmpty()) {
            invalidateSnapshot = true;
            foreach (const QString &fileName, toRemove)
                m_snapshot.remove(fileName);
        }
    }

    // Update the snapshot
    if (invalidateSnapshot) {
        const QString configurationFileName = modelManager->configurationFileName();
        if (invalidateConfig)
            m_snapshot.remove(configurationFileName);
        if (!m_snapshot.contains(configurationFileName))
            workingCopy.insert(configurationFileName, m_configFile);
        m_snapshot.remove(m_fileInEditor);

155 156
        static const QString editorDefinesFileName
            = CppModelManagerInterface::editorConfigurationFileName();
157 158 159 160 161
        if (editorDefinesChanged) {
            m_snapshot.remove(editorDefinesFileName);
            workingCopy.insert(editorDefinesFileName, m_editorDefines);
        }

162
        CppSourceProcessor sourceProcessor(modelManager, m_snapshot);
163 164
        Snapshot globalSnapshot = modelManager->snapshot();
        globalSnapshot.remove(fileInEditor());
165 166 167 168 169
        sourceProcessor.setGlobalSnapshot(globalSnapshot);
        sourceProcessor.setWorkingCopy(workingCopy);
        sourceProcessor.setIncludePaths(m_includePaths);
        sourceProcessor.setFrameworkPaths(m_frameworkPaths);
        sourceProcessor.run(configurationFileName);
170
        if (!m_projectConfigFile.isEmpty())
171
            sourceProcessor.run(m_projectConfigFile);
172
        if (m_usePrecompiledHeaders) {
173
            foreach (const QString &precompiledHeader, m_precompiledHeaders)
174
                sourceProcessor.run(precompiledHeader);
175
        }
176
        if (!m_editorDefines.isEmpty())
177 178
            sourceProcessor.run(editorDefinesFileName);
        sourceProcessor.run(m_fileInEditor);
179

180
        m_snapshot = sourceProcessor.snapshot();
181 182 183 184 185 186
        Snapshot newSnapshot = m_snapshot.simplified(document());
        for (Snapshot::const_iterator i = m_snapshot.begin(), ei = m_snapshot.end(); i != ei; ++i) {
            if (Client::isInjectedFile(i.key()))
                newSnapshot.insert(i.value());
        }
        m_snapshot = newSnapshot;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        m_deps.build(m_snapshot);

        foreach (Document::Ptr doc, m_snapshot) {
            QString fileName = doc->fileName();
            if (doc->revision() == 0) {
                Document::Ptr otherDoc = globalSnapshot.document(fileName);
                doc->setRevision(otherDoc.isNull() ? 0 : otherDoc->revision());
            }
            if (fileName != fileInEditor())
                doc->releaseSourceAndAST();
        }

        QTC_CHECK(document());
        if (Document::Ptr doc = document())
            doc->setRevision(rev + 1);
    }
}

205 206 207 208 209 210 211 212
void SnapshotUpdater::releaseSnapshot()
{
    QMutexLocker locker(&m_mutex);
    m_snapshot = Snapshot();
    m_deps = DependencyTable();
    m_forceSnapshotInvalidation = true;
}

213 214 215 216 217 218
Document::Ptr SnapshotUpdater::document() const
{
    QMutexLocker locker(&m_mutex);
    return m_snapshot.document(m_fileInEditor);
}

219 220 221 222 223 224 225 226 227 228 229 230 231
Snapshot SnapshotUpdater::snapshot() const
{
    QMutexLocker locker(&m_mutex);
    return m_snapshot;
}

QStringList SnapshotUpdater::includePaths() const
{
    QMutexLocker locker(&m_mutex);
    return m_includePaths;
}

QStringList SnapshotUpdater::frameworkPaths() const
232 233
{
    QMutexLocker locker(&m_mutex);
234 235
    return m_frameworkPaths;
}
236

237 238 239
ProjectPart::Ptr SnapshotUpdater::currentProjectPart() const
{
    QMutexLocker locker(&m_mutex);
240 241 242 243 244 245 246 247 248
    return m_projectPart;
}

void SnapshotUpdater::setProjectPart(ProjectPart::Ptr projectPart)
{
    QMutexLocker locker(&m_mutex);
    m_manuallySetProjectPart = projectPart;
}

249 250 251 252 253 254
void SnapshotUpdater::setUsePrecompiledHeaders(bool usePrecompiledHeaders)
{
    QMutexLocker locker(&m_mutex);
    m_usePrecompiledHeaders = usePrecompiledHeaders;
}

255 256 257 258 259 260 261 262 263 264
void SnapshotUpdater::setEditorDefines(const QByteArray &editorDefines)
{
    QMutexLocker locker(&m_mutex);

    if (editorDefines != m_editorDefines) {
        m_editorDefines = editorDefines;
        m_editorDefinesChangedSinceLastUpdate = true;
    }
}

265 266
void SnapshotUpdater::updateProjectPart()
{
267 268 269 270 271
    if (m_manuallySetProjectPart) {
        m_projectPart = m_manuallySetProjectPart;
        return;
    }

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 300 301 302
    CppModelManager *cmm = dynamic_cast<CppModelManager *>(CppModelManagerInterface::instance());
    QList<ProjectPart::Ptr> pParts = cmm->projectPart(m_fileInEditor);
    if (pParts.isEmpty()) {
        if (m_projectPart)
            // File is not directly part of any project, but we got one before. We will re-use it,
            // because re-calculating this can be expensive when the dependency table is big.
            return;

        // Fall-back step 1: Get some parts through the dependency table:
        pParts = cmm->projectPartFromDependencies(m_fileInEditor);
        if (pParts.isEmpty())
            // Fall-back step 2: Use fall-back part from the model manager:
            m_projectPart = cmm->fallbackProjectPart();
        else
            m_projectPart = pParts.first();
    } else {
        if (!pParts.contains(m_projectPart))
            // Apparently the project file changed, so update our project part.
            m_projectPart = pParts.first();
    }
}

void SnapshotUpdater::addFileAndDependencies(QSet<QString> *toRemove, const QString &fileName) const
{
    toRemove->insert(fileName);
    if (fileName != m_fileInEditor) {
        QStringList deps = m_deps.filesDependingOn(fileName);
        toRemove->unite(QSet<QString>::fromList(deps));
    }
}