2013-08-19 16:05:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-08-19 16:05:29 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-05-16 15:51:04 -04:00
|
|
|
#include "cppsourceprocessor.h"
|
2013-08-19 16:05:29 +02:00
|
|
|
#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)
|
2013-09-30 14:37:50 +02:00
|
|
|
, m_editorDefinesChangedSinceLastUpdate(false)
|
2013-09-30 13:36:01 +02:00
|
|
|
, m_usePrecompiledHeaders(false)
|
2013-10-10 10:26:39 +02:00
|
|
|
, m_forceSnapshotInvalidation(false)
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnapshotUpdater::update(CppModelManager::WorkingCopy workingCopy)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
|
|
|
|
if (m_fileInEditor.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-09-30 14:37:50 +02:00
|
|
|
bool invalidateSnapshot = false, invalidateConfig = false, editorDefinesChanged = false;
|
2013-08-19 16:05:29 +02:00
|
|
|
|
|
|
|
|
CppModelManager *modelManager
|
|
|
|
|
= dynamic_cast<CppModelManager *>(CppModelManagerInterface::instance());
|
|
|
|
|
QByteArray configFile = modelManager->codeModelConfiguration();
|
2014-06-25 17:23:19 +02:00
|
|
|
ProjectPart::HeaderPaths headerPaths;
|
2013-09-30 13:36:01 +02:00
|
|
|
QStringList precompiledHeaders;
|
2014-02-05 16:44:35 +01:00
|
|
|
QString projectConfigFile;
|
2013-08-19 16:05:29 +02:00
|
|
|
|
|
|
|
|
updateProjectPart();
|
|
|
|
|
|
2013-10-10 10:26:39 +02:00
|
|
|
if (m_forceSnapshotInvalidation) {
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
m_forceSnapshotInvalidation = false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
if (m_projectPart) {
|
2013-11-27 15:17:51 +01:00
|
|
|
configFile += m_projectPart->toolchainDefines;
|
|
|
|
|
configFile += m_projectPart->projectDefines;
|
2014-06-25 17:23:19 +02:00
|
|
|
headerPaths = m_projectPart->headerPaths;
|
2014-02-05 16:44:35 +01:00
|
|
|
projectConfigFile = m_projectPart->projectConfigFile;
|
2013-09-30 13:36:01 +02:00
|
|
|
if (m_usePrecompiledHeaders)
|
|
|
|
|
precompiledHeaders = m_projectPart->precompiledHeaders;
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configFile != m_configFile) {
|
|
|
|
|
m_configFile = configFile;
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
invalidateConfig = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 14:37:50 +02:00
|
|
|
if (m_editorDefinesChangedSinceLastUpdate) {
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
editorDefinesChanged = true;
|
|
|
|
|
m_editorDefinesChangedSinceLastUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 17:23:19 +02:00
|
|
|
if (headerPaths != m_headerPaths) {
|
|
|
|
|
m_headerPaths = headerPaths;
|
2013-08-19 16:05:29 +02:00
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 16:44:35 +01:00
|
|
|
if (projectConfigFile != m_projectConfigFile) {
|
|
|
|
|
m_projectConfigFile = projectConfigFile;
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 13:36:01 +02:00
|
|
|
if (precompiledHeaders != m_precompiledHeaders) {
|
|
|
|
|
m_precompiledHeaders = precompiledHeaders;
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
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);
|
|
|
|
|
|
2013-10-21 12:59:48 +02:00
|
|
|
static const QString editorDefinesFileName
|
|
|
|
|
= CppModelManagerInterface::editorConfigurationFileName();
|
2013-09-30 14:37:50 +02:00
|
|
|
if (editorDefinesChanged) {
|
|
|
|
|
m_snapshot.remove(editorDefinesFileName);
|
|
|
|
|
workingCopy.insert(editorDefinesFileName, m_editorDefines);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 14:41:19 +02:00
|
|
|
CppSourceProcessor sourceProcessor(m_snapshot, [&](const Document::Ptr &doc) {
|
|
|
|
|
const QString fileName = doc->fileName();
|
|
|
|
|
const bool isInEditor = fileName == fileInEditor();
|
|
|
|
|
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);
|
|
|
|
|
doc->releaseSourceAndAST();
|
|
|
|
|
});
|
2013-08-19 16:05:29 +02:00
|
|
|
Snapshot globalSnapshot = modelManager->snapshot();
|
|
|
|
|
globalSnapshot.remove(fileInEditor());
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.setGlobalSnapshot(globalSnapshot);
|
|
|
|
|
sourceProcessor.setWorkingCopy(workingCopy);
|
2014-06-25 17:23:19 +02:00
|
|
|
sourceProcessor.setHeaderPaths(m_headerPaths);
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(configurationFileName);
|
2014-02-05 16:44:35 +01:00
|
|
|
if (!m_projectConfigFile.isEmpty())
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(m_projectConfigFile);
|
2013-10-01 14:48:31 +02:00
|
|
|
if (m_usePrecompiledHeaders) {
|
2013-09-30 13:36:01 +02:00
|
|
|
foreach (const QString &precompiledHeader, m_precompiledHeaders)
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(precompiledHeader);
|
2013-10-01 14:48:31 +02:00
|
|
|
}
|
2013-09-30 14:37:50 +02:00
|
|
|
if (!m_editorDefines.isEmpty())
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(editorDefinesFileName);
|
|
|
|
|
sourceProcessor.run(m_fileInEditor);
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2014-05-16 15:51:04 -04:00
|
|
|
m_snapshot = sourceProcessor.snapshot();
|
2013-10-01 14:48:31 +02:00
|
|
|
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;
|
2013-08-19 16:05:29 +02:00
|
|
|
m_deps.build(m_snapshot);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 10:26:39 +02:00
|
|
|
void SnapshotUpdater::releaseSnapshot()
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_snapshot = Snapshot();
|
|
|
|
|
m_deps = DependencyTable();
|
|
|
|
|
m_forceSnapshotInvalidation = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
Document::Ptr SnapshotUpdater::document() const
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
return m_snapshot.document(m_fileInEditor);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 11:44:50 +02:00
|
|
|
Snapshot SnapshotUpdater::snapshot() const
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
return m_snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 17:23:19 +02:00
|
|
|
ProjectPart::HeaderPaths SnapshotUpdater::headerPaths() const
|
2013-10-01 14:25:26 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
2014-06-25 17:23:19 +02:00
|
|
|
return m_headerPaths;
|
2013-10-07 11:44:50 +02:00
|
|
|
}
|
2013-10-01 14:25:26 +02:00
|
|
|
|
2013-10-07 11:44:50 +02:00
|
|
|
ProjectPart::Ptr SnapshotUpdater::currentProjectPart() const
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
2013-10-01 14:25:26 +02:00
|
|
|
return m_projectPart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnapshotUpdater::setProjectPart(ProjectPart::Ptr projectPart)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_manuallySetProjectPart = projectPart;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 13:36:01 +02:00
|
|
|
void SnapshotUpdater::setUsePrecompiledHeaders(bool usePrecompiledHeaders)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_usePrecompiledHeaders = usePrecompiledHeaders;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 14:37:50 +02:00
|
|
|
void SnapshotUpdater::setEditorDefines(const QByteArray &editorDefines)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
|
|
|
|
if (editorDefines != m_editorDefines) {
|
|
|
|
|
m_editorDefines = editorDefines;
|
|
|
|
|
m_editorDefinesChangedSinceLastUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
void SnapshotUpdater::updateProjectPart()
|
|
|
|
|
{
|
2013-10-01 14:25:26 +02:00
|
|
|
if (m_manuallySetProjectPart) {
|
|
|
|
|
m_projectPart = m_manuallySetProjectPart;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|