2013-08-19 16:05:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2013-08-19 16:05:29 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-08-19 16:05:29 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** 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.
|
2013-08-19 16:05:29 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-08-19 16:05:29 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
#include "builtineditordocumentparser.h"
|
2014-05-16 15:51:04 -04:00
|
|
|
#include "cppsourceprocessor.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "editordocumenthandle.h"
|
2013-08-19 16:05:29 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const QString &filePath)
|
2014-07-30 17:37:17 +02:00
|
|
|
: BaseEditorDocumentParser(filePath)
|
2013-10-10 10:26:39 +02:00
|
|
|
, m_forceSnapshotInvalidation(false)
|
2014-08-05 11:34:52 +02:00
|
|
|
, m_releaseSourceAndAST(true)
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
2014-08-26 14:26:28 +02:00
|
|
|
qRegisterMetaType<CPlusPlus::Snapshot>("CPlusPlus::Snapshot");
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
|
2014-07-30 17:37:17 +02:00
|
|
|
if (filePath().isEmpty())
|
2013-08-19 16:05:29 +02:00
|
|
|
return;
|
|
|
|
|
|
2014-07-30 17:37:17 +02:00
|
|
|
bool invalidateSnapshot = false, invalidateConfig = false, editorDefinesChanged_ = false;
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2014-09-15 00:12:27 +02:00
|
|
|
CppModelManager *modelManager = CppModelManager::instance();
|
2013-08-19 16:05:29 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 17:37:17 +02:00
|
|
|
if (const ProjectPart::Ptr part = projectPart()) {
|
|
|
|
|
configFile += part->toolchainDefines;
|
|
|
|
|
configFile += part->projectDefines;
|
|
|
|
|
headerPaths = part->headerPaths;
|
|
|
|
|
projectConfigFile = part->projectConfigFile;
|
|
|
|
|
if (usePrecompiledHeaders())
|
|
|
|
|
precompiledHeaders = part->precompiledHeaders;
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configFile != m_configFile) {
|
|
|
|
|
m_configFile = configFile;
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
invalidateConfig = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 17:37:17 +02:00
|
|
|
if (editorDefinesChanged()) {
|
2013-09-30 14:37:50 +02:00
|
|
|
invalidateSnapshot = true;
|
2014-07-30 17:37:17 +02:00
|
|
|
editorDefinesChanged_ = true;
|
|
|
|
|
resetEditorDefinesChanged();
|
2013-09-30 14:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
2014-11-27 12:11:46 +01:00
|
|
|
QSet<Utils::FileName> toRemove;
|
2013-08-19 16:05:29 +02:00
|
|
|
foreach (const Document::Ptr &doc, m_snapshot) {
|
2014-11-27 12:11:46 +01:00
|
|
|
const Utils::FileName fileName = Utils::FileName::fromString(doc->fileName());
|
2013-08-19 16:05:29 +02:00
|
|
|
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;
|
2014-11-27 12:11:46 +01:00
|
|
|
foreach (const Utils::FileName &fileName, toRemove)
|
2013-08-19 16:05:29 +02:00
|
|
|
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);
|
2014-07-30 17:37:17 +02:00
|
|
|
m_snapshot.remove(filePath());
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2013-10-21 12:59:48 +02:00
|
|
|
static const QString editorDefinesFileName
|
2014-09-15 00:12:27 +02:00
|
|
|
= CppModelManager::editorConfigurationFileName();
|
2014-07-30 17:37:17 +02:00
|
|
|
if (editorDefinesChanged_) {
|
2013-09-30 14:37:50 +02:00
|
|
|
m_snapshot.remove(editorDefinesFileName);
|
2014-07-30 17:37:17 +02:00
|
|
|
workingCopy.insert(editorDefinesFileName, editorDefines());
|
2013-09-30 14:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-06 14:41:19 +02:00
|
|
|
CppSourceProcessor sourceProcessor(m_snapshot, [&](const Document::Ptr &doc) {
|
|
|
|
|
const QString fileName = doc->fileName();
|
2014-07-30 17:37:17 +02:00
|
|
|
const bool isInEditor = fileName == filePath();
|
2014-06-06 14:41:19 +02:00
|
|
|
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);
|
2014-08-05 11:34:52 +02:00
|
|
|
if (m_releaseSourceAndAST)
|
|
|
|
|
doc->releaseSourceAndAST();
|
2014-06-06 14:41:19 +02:00
|
|
|
});
|
2013-08-19 16:05:29 +02:00
|
|
|
Snapshot globalSnapshot = modelManager->snapshot();
|
2014-07-30 17:37:17 +02:00
|
|
|
globalSnapshot.remove(filePath());
|
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);
|
2014-07-30 17:37:17 +02:00
|
|
|
if (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
|
|
|
}
|
2014-07-30 17:37:17 +02:00
|
|
|
if (!editorDefines().isEmpty())
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(editorDefinesFileName);
|
2014-07-30 17:37:17 +02:00
|
|
|
sourceProcessor.run(filePath(), usePrecompiledHeaders() ? m_precompiledHeaders
|
2014-07-16 11:25:15 +02:00
|
|
|
: QStringList());
|
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) {
|
2014-11-27 12:11:46 +01:00
|
|
|
if (Client::isInjectedFile(i.key().toString()))
|
2013-10-01 14:48:31 +02:00
|
|
|
newSnapshot.insert(i.value());
|
|
|
|
|
}
|
|
|
|
|
m_snapshot = newSnapshot;
|
2014-09-11 13:15:44 +02:00
|
|
|
m_snapshot.updateDependencyTable();
|
2014-08-26 14:26:28 +02:00
|
|
|
|
|
|
|
|
emit finished(document(), m_snapshot);
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
void BuiltinEditorDocumentParser::releaseResources()
|
2013-10-10 10:26:39 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_snapshot = Snapshot();
|
|
|
|
|
m_forceSnapshotInvalidation = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
Document::Ptr BuiltinEditorDocumentParser::document() const
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
2014-07-30 17:37:17 +02:00
|
|
|
return m_snapshot.document(filePath());
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
Snapshot BuiltinEditorDocumentParser::snapshot() const
|
2013-10-07 11:44:50 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
return m_snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
ProjectPart::HeaderPaths BuiltinEditorDocumentParser::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
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
void BuiltinEditorDocumentParser::setReleaseSourceAndAST(bool onoff)
|
2014-08-05 11:34:52 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
|
m_releaseSourceAndAST = onoff;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
BuiltinEditorDocumentParser *BuiltinEditorDocumentParser::get(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
if (BaseEditorDocumentParser *b = BaseEditorDocumentParser::get(filePath))
|
|
|
|
|
return qobject_cast<BuiltinEditorDocumentParser *>(b);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 12:11:46 +01:00
|
|
|
void BuiltinEditorDocumentParser::addFileAndDependencies(QSet<Utils::FileName> *toRemove,
|
|
|
|
|
const Utils::FileName &fileName) const
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
|
|
|
|
toRemove->insert(fileName);
|
2014-11-27 12:11:46 +01:00
|
|
|
if (fileName != Utils::FileName::fromString(filePath())) {
|
|
|
|
|
Utils::FileNameList deps = m_snapshot.filesDependingOn(fileName);
|
|
|
|
|
toRemove->unite(QSet<Utils::FileName>::fromList(deps));
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
}
|