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"
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2015-12-02 10:17:08 +01:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
|
2015-12-02 10:17:08 +01:00
|
|
|
static QByteArray overwrittenToolchainDefines(const ProjectPart &projectPart)
|
|
|
|
|
{
|
|
|
|
|
QByteArray defines;
|
|
|
|
|
|
|
|
|
|
// MSVC's predefined macros like __FUNCSIG__ expand to itself.
|
|
|
|
|
// We can't parse this, so redefine to the empty string literal.
|
|
|
|
|
if (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
|
|
|
|
|
defines += "#define __FUNCSIG__ \"\"\n"
|
|
|
|
|
"#define __FUNCDNAME__ \"\"\n"
|
|
|
|
|
"#define __FUNCTION__ \"\"\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defines;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const QString &filePath)
|
2014-07-30 17:37:17 +02:00
|
|
|
: BaseEditorDocumentParser(filePath)
|
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
|
|
|
}
|
|
|
|
|
|
2015-12-15 12:32:01 +01:00
|
|
|
void BuiltinEditorDocumentParser::updateHelper(const WorkingCopy &theWorkingCopy)
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
2015-07-10 14:57:42 +02:00
|
|
|
if (filePath().isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
const Configuration baseConfig = configuration();
|
|
|
|
|
const bool releaseSourceAndAST_ = releaseSourceAndAST();
|
|
|
|
|
|
|
|
|
|
State baseState = state();
|
|
|
|
|
ExtraState state = extraState();
|
2015-12-15 12:32:01 +01:00
|
|
|
WorkingCopy workingCopy = theWorkingCopy;
|
2013-08-19 16:05:29 +02:00
|
|
|
|
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;
|
2015-02-15 23:43:26 +02:00
|
|
|
LanguageFeatures features = LanguageFeatures::defaultFeatures();
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
baseState.projectPart = determineProjectPart(filePath(), baseConfig, baseState);
|
2013-08-19 16:05:29 +02:00
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (state.forceSnapshotInvalidation) {
|
2013-10-10 10:26:39 +02:00
|
|
|
invalidateSnapshot = true;
|
2015-07-07 15:02:14 +02:00
|
|
|
state.forceSnapshotInvalidation = false;
|
2013-10-10 10:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (const ProjectPart::Ptr part = baseState.projectPart) {
|
2014-07-30 17:37:17 +02:00
|
|
|
configFile += part->toolchainDefines;
|
2015-12-02 10:17:08 +01:00
|
|
|
configFile += overwrittenToolchainDefines(*part.data());
|
2014-07-30 17:37:17 +02:00
|
|
|
configFile += part->projectDefines;
|
|
|
|
|
headerPaths = part->headerPaths;
|
|
|
|
|
projectConfigFile = part->projectConfigFile;
|
2015-07-07 15:02:14 +02:00
|
|
|
if (baseConfig.usePrecompiledHeaders)
|
2014-07-30 17:37:17 +02:00
|
|
|
precompiledHeaders = part->precompiledHeaders;
|
2015-02-15 23:43:26 +02:00
|
|
|
features = part->languageFeatures;
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (configFile != state.configFile) {
|
|
|
|
|
state.configFile = configFile;
|
2013-08-19 16:05:29 +02:00
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
invalidateConfig = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (baseConfig.editorDefines != baseState.editorDefines) {
|
|
|
|
|
baseState.editorDefines = baseConfig.editorDefines;
|
2013-09-30 14:37:50 +02:00
|
|
|
invalidateSnapshot = true;
|
2014-07-30 17:37:17 +02:00
|
|
|
editorDefinesChanged_ = true;
|
2013-09-30 14:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (headerPaths != state.headerPaths) {
|
|
|
|
|
state.headerPaths = headerPaths;
|
2013-08-19 16:05:29 +02:00
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (projectConfigFile != state.projectConfigFile) {
|
|
|
|
|
state.projectConfigFile = projectConfigFile;
|
2014-02-05 16:44:35 +01:00
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
if (precompiledHeaders != state.precompiledHeaders) {
|
|
|
|
|
state.precompiledHeaders = precompiledHeaders;
|
2013-09-30 13:36:01 +02:00
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
unsigned rev = 0;
|
2015-07-07 15:02:14 +02:00
|
|
|
if (Document::Ptr doc = state.snapshot.document(filePath()))
|
2013-08-19 16:05:29 +02:00
|
|
|
rev = doc->revision();
|
|
|
|
|
else
|
|
|
|
|
invalidateSnapshot = true;
|
|
|
|
|
|
|
|
|
|
Snapshot globalSnapshot = modelManager->snapshot();
|
|
|
|
|
|
|
|
|
|
if (invalidateSnapshot) {
|
2015-07-07 15:02:14 +02:00
|
|
|
state.snapshot = Snapshot();
|
2013-08-19 16:05:29 +02:00
|
|
|
} else {
|
|
|
|
|
// Remove changed files from the snapshot
|
2014-11-27 12:11:46 +01:00
|
|
|
QSet<Utils::FileName> toRemove;
|
2015-07-07 15:02:14 +02:00
|
|
|
foreach (const Document::Ptr &doc, state.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())
|
2015-07-07 15:02:14 +02:00
|
|
|
addFileAndDependencies(&state.snapshot, &toRemove, fileName);
|
2013-08-19 16:05:29 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Document::Ptr otherDoc = globalSnapshot.document(fileName);
|
|
|
|
|
if (!otherDoc.isNull() && otherDoc->revision() != doc->revision())
|
2015-07-07 15:02:14 +02:00
|
|
|
addFileAndDependencies(&state.snapshot, &toRemove, fileName);
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!toRemove.isEmpty()) {
|
|
|
|
|
invalidateSnapshot = true;
|
2014-11-27 12:11:46 +01:00
|
|
|
foreach (const Utils::FileName &fileName, toRemove)
|
2015-07-07 15:02:14 +02:00
|
|
|
state.snapshot.remove(fileName);
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the snapshot
|
|
|
|
|
if (invalidateSnapshot) {
|
|
|
|
|
const QString configurationFileName = modelManager->configurationFileName();
|
|
|
|
|
if (invalidateConfig)
|
2015-07-07 15:02:14 +02:00
|
|
|
state.snapshot.remove(configurationFileName);
|
|
|
|
|
if (!state.snapshot.contains(configurationFileName))
|
|
|
|
|
workingCopy.insert(configurationFileName, state.configFile);
|
|
|
|
|
state.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_) {
|
2015-07-07 15:02:14 +02:00
|
|
|
state.snapshot.remove(editorDefinesFileName);
|
|
|
|
|
workingCopy.insert(editorDefinesFileName, baseState.editorDefines);
|
2013-09-30 14:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
CppSourceProcessor sourceProcessor(state.snapshot, [&](const Document::Ptr &doc) {
|
2014-06-06 14:41:19 +02:00
|
|
|
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);
|
2015-07-07 15:02:14 +02:00
|
|
|
if (releaseSourceAndAST_)
|
2014-08-05 11:34:52 +02:00
|
|
|
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);
|
2015-07-07 15:02:14 +02:00
|
|
|
sourceProcessor.setHeaderPaths(state.headerPaths);
|
2015-02-15 23:43:26 +02:00
|
|
|
sourceProcessor.setLanguageFeatures(features);
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(configurationFileName);
|
2015-07-07 15:02:14 +02:00
|
|
|
if (!state.projectConfigFile.isEmpty())
|
|
|
|
|
sourceProcessor.run(state.projectConfigFile);
|
|
|
|
|
if (baseConfig.usePrecompiledHeaders) {
|
|
|
|
|
foreach (const QString &precompiledHeader, state.precompiledHeaders)
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(precompiledHeader);
|
2013-10-01 14:48:31 +02:00
|
|
|
}
|
2015-07-07 15:02:14 +02:00
|
|
|
if (!baseState.editorDefines.isEmpty())
|
2014-05-16 15:51:04 -04:00
|
|
|
sourceProcessor.run(editorDefinesFileName);
|
2015-07-07 15:02:14 +02:00
|
|
|
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) {
|
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());
|
|
|
|
|
}
|
2015-07-07 15:02:14 +02:00
|
|
|
state.snapshot = newSnapshot;
|
|
|
|
|
state.snapshot.updateDependencyTable();
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
2015-07-07 15:02:14 +02:00
|
|
|
|
|
|
|
|
setState(baseState);
|
|
|
|
|
setExtraState(state);
|
|
|
|
|
|
|
|
|
|
if (invalidateSnapshot)
|
|
|
|
|
emit finished(state.snapshot.document(filePath()), state.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
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
ExtraState s = extraState();
|
|
|
|
|
s.snapshot = Snapshot();
|
|
|
|
|
s.forceSnapshotInvalidation = true;
|
|
|
|
|
setExtraState(s);
|
2013-10-10 10:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
Document::Ptr BuiltinEditorDocumentParser::document() const
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
return extraState().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
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
return extraState().snapshot;
|
2013-10-07 11:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
ProjectPart::HeaderPaths BuiltinEditorDocumentParser::headerPaths() const
|
2013-10-01 14:25:26 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
return extraState().headerPaths;
|
2014-08-05 11:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 17:34:07 +02:00
|
|
|
BuiltinEditorDocumentParser::Ptr BuiltinEditorDocumentParser::get(const QString &filePath)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2015-09-01 17:34:07 +02:00
|
|
|
if (BaseEditorDocumentParser::Ptr b = BaseEditorDocumentParser::get(filePath))
|
|
|
|
|
return b.objectCast<BuiltinEditorDocumentParser>();
|
|
|
|
|
return BuiltinEditorDocumentParser::Ptr();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 15:02:14 +02:00
|
|
|
void BuiltinEditorDocumentParser::addFileAndDependencies(Snapshot *snapshot,
|
|
|
|
|
QSet<Utils::FileName> *toRemove,
|
2014-11-27 12:11:46 +01:00
|
|
|
const Utils::FileName &fileName) const
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
2015-07-07 15:02:14 +02:00
|
|
|
QTC_ASSERT(snapshot, return);
|
|
|
|
|
|
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())) {
|
2015-07-07 15:02:14 +02:00
|
|
|
Utils::FileNameList deps = snapshot->filesDependingOn(fileName);
|
2014-11-27 12:11:46 +01:00
|
|
|
toRemove->unite(QSet<Utils::FileName>::fromList(deps));
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-07 15:02:14 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|