2014-08-19 15:59:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** 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
|
2014-10-15 14:46:23 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2014-08-19 15:59:29 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-15 14:46:23 +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.
|
2014-08-19 15:59:29 +02:00
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppsemanticinfoupdater.h"
|
|
|
|
|
|
2014-08-26 14:26:28 +02:00
|
|
|
#include "cpplocalsymbols.h"
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/runextensions.h>
|
|
|
|
|
|
|
|
|
|
#include <cplusplus/Control.h>
|
|
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
#include <cplusplus/TranslationUnit.h>
|
|
|
|
|
|
2014-10-23 12:39:59 +02:00
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
|
2014-10-23 12:39:59 +02:00
|
|
|
static Q_LOGGING_CATEGORY(log, "qtc.cpptools.semanticinfoupdater")
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
namespace CppTools {
|
|
|
|
|
|
|
|
|
|
class SemanticInfoUpdaterPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-02-04 17:01:07 +02:00
|
|
|
class FuturizedTopLevelDeclarationProcessor: public TopLevelDeclarationProcessor
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FuturizedTopLevelDeclarationProcessor(QFutureInterface<void> &future): m_future(future) {}
|
2015-02-04 17:01:07 +02:00
|
|
|
bool processDeclaration(DeclarationAST *) { return !isCanceled(); }
|
2014-08-19 15:59:29 +02:00
|
|
|
bool isCanceled() { return m_future.isCanceled(); }
|
|
|
|
|
private:
|
|
|
|
|
QFutureInterface<void> m_future;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2014-08-26 14:26:28 +02:00
|
|
|
SemanticInfoUpdaterPrivate(SemanticInfoUpdater *q);
|
2014-08-19 15:59:29 +02:00
|
|
|
~SemanticInfoUpdaterPrivate();
|
|
|
|
|
|
|
|
|
|
SemanticInfo semanticInfo() const;
|
|
|
|
|
void setSemanticInfo(const SemanticInfo &semanticInfo, bool emitSignal);
|
|
|
|
|
|
|
|
|
|
SemanticInfo update(const SemanticInfo::Source &source,
|
|
|
|
|
bool emitSignalWhenFinished,
|
|
|
|
|
FuturizedTopLevelDeclarationProcessor *processor);
|
|
|
|
|
|
|
|
|
|
bool reuseCurrentSemanticInfo(const SemanticInfo::Source &source, bool emitSignalWhenFinished);
|
|
|
|
|
|
|
|
|
|
void update_helper(QFutureInterface<void> &future, const SemanticInfo::Source source);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SemanticInfoUpdater *q;
|
|
|
|
|
mutable QMutex m_lock;
|
|
|
|
|
SemanticInfo m_semanticInfo;
|
|
|
|
|
QFuture<void> m_future;
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-26 14:26:28 +02:00
|
|
|
SemanticInfoUpdaterPrivate::SemanticInfoUpdaterPrivate(SemanticInfoUpdater *q)
|
2014-08-19 15:59:29 +02:00
|
|
|
: q(q)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfoUpdaterPrivate::~SemanticInfoUpdaterPrivate()
|
|
|
|
|
{
|
|
|
|
|
m_future.cancel();
|
|
|
|
|
m_future.waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfo SemanticInfoUpdaterPrivate::semanticInfo() const
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lock);
|
|
|
|
|
return m_semanticInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticInfoUpdaterPrivate::setSemanticInfo(const SemanticInfo &semanticInfo, bool emitSignal)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lock);
|
|
|
|
|
m_semanticInfo = semanticInfo;
|
|
|
|
|
}
|
|
|
|
|
if (emitSignal) {
|
2014-10-23 12:39:59 +02:00
|
|
|
qCDebug(log) << "emiting new info";
|
2014-08-19 15:59:29 +02:00
|
|
|
emit q->updated(semanticInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfo SemanticInfoUpdaterPrivate::update(const SemanticInfo::Source &source,
|
|
|
|
|
bool emitSignalWhenFinished,
|
|
|
|
|
FuturizedTopLevelDeclarationProcessor *processor)
|
|
|
|
|
{
|
|
|
|
|
SemanticInfo newSemanticInfo;
|
|
|
|
|
newSemanticInfo.revision = source.revision;
|
2014-08-26 14:26:28 +02:00
|
|
|
newSemanticInfo.snapshot = source.snapshot;
|
2014-08-19 15:59:29 +02:00
|
|
|
|
|
|
|
|
Document::Ptr doc = newSemanticInfo.snapshot.preprocessedDocument(source.code, source.fileName);
|
|
|
|
|
if (processor)
|
|
|
|
|
doc->control()->setTopLevelDeclarationProcessor(processor);
|
2014-11-19 12:07:29 +01:00
|
|
|
doc->setRetryHarderToParseDeclarations(true);
|
2014-08-19 15:59:29 +02:00
|
|
|
doc->check();
|
|
|
|
|
if (processor && processor->isCanceled())
|
|
|
|
|
newSemanticInfo.complete = false;
|
|
|
|
|
newSemanticInfo.doc = doc;
|
|
|
|
|
|
2014-10-23 12:39:59 +02:00
|
|
|
qCDebug(log) << "update() for source revision:" << source.revision
|
|
|
|
|
<< "canceled:" << !newSemanticInfo.complete;
|
2014-08-19 15:59:29 +02:00
|
|
|
|
|
|
|
|
setSemanticInfo(newSemanticInfo, emitSignalWhenFinished);
|
|
|
|
|
return newSemanticInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SemanticInfoUpdaterPrivate::reuseCurrentSemanticInfo(const SemanticInfo::Source &source,
|
|
|
|
|
bool emitSignalWhenFinished)
|
|
|
|
|
{
|
|
|
|
|
const SemanticInfo currentSemanticInfo = semanticInfo();
|
|
|
|
|
|
|
|
|
|
if (!source.force
|
|
|
|
|
&& currentSemanticInfo.complete
|
|
|
|
|
&& currentSemanticInfo.revision == source.revision
|
|
|
|
|
&& currentSemanticInfo.doc
|
|
|
|
|
&& currentSemanticInfo.doc->translationUnit()->ast()
|
2014-08-26 14:26:28 +02:00
|
|
|
&& currentSemanticInfo.doc->fileName() == source.fileName
|
2014-10-14 11:37:40 +02:00
|
|
|
&& !currentSemanticInfo.snapshot.isEmpty()
|
|
|
|
|
&& currentSemanticInfo.snapshot == source.snapshot) {
|
2014-08-19 15:59:29 +02:00
|
|
|
SemanticInfo newSemanticInfo;
|
|
|
|
|
newSemanticInfo.revision = source.revision;
|
2014-08-26 14:26:28 +02:00
|
|
|
newSemanticInfo.snapshot = source.snapshot;
|
2014-08-19 15:59:29 +02:00
|
|
|
newSemanticInfo.doc = currentSemanticInfo.doc;
|
|
|
|
|
setSemanticInfo(newSemanticInfo, emitSignalWhenFinished);
|
2014-10-23 12:39:59 +02:00
|
|
|
qCDebug(log) << "re-using current semantic info, source revision:" << source.revision;
|
2014-08-19 15:59:29 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticInfoUpdaterPrivate::update_helper(QFutureInterface<void> &future,
|
|
|
|
|
const SemanticInfo::Source source)
|
|
|
|
|
{
|
|
|
|
|
FuturizedTopLevelDeclarationProcessor processor(future);
|
|
|
|
|
update(source, true, &processor);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 14:26:28 +02:00
|
|
|
SemanticInfoUpdater::SemanticInfoUpdater()
|
|
|
|
|
: d(new SemanticInfoUpdaterPrivate(this))
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfoUpdater::~SemanticInfoUpdater()
|
|
|
|
|
{
|
|
|
|
|
d->m_future.cancel();
|
|
|
|
|
d->m_future.waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfo SemanticInfoUpdater::update(const SemanticInfo::Source &source)
|
|
|
|
|
{
|
2014-10-23 12:39:59 +02:00
|
|
|
qCDebug(log) << "update() - synchronous";
|
2014-08-19 15:59:29 +02:00
|
|
|
d->m_future.cancel();
|
|
|
|
|
|
|
|
|
|
const bool emitSignalWhenFinished = false;
|
|
|
|
|
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished)) {
|
|
|
|
|
d->m_future = QFuture<void>();
|
|
|
|
|
return semanticInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d->update(source, emitSignalWhenFinished, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticInfoUpdater::updateDetached(const SemanticInfo::Source source)
|
|
|
|
|
{
|
2014-10-23 12:39:59 +02:00
|
|
|
qCDebug(log) << "updateDetached() - asynchronous";
|
2014-08-19 15:59:29 +02:00
|
|
|
d->m_future.cancel();
|
|
|
|
|
|
|
|
|
|
const bool emitSignalWhenFinished = true;
|
|
|
|
|
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished)) {
|
|
|
|
|
d->m_future = QFuture<void>();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d->m_future = QtConcurrent::run<SemanticInfoUpdaterPrivate, void, const SemanticInfo::Source>
|
|
|
|
|
(&SemanticInfoUpdaterPrivate::update_helper, d.data(), source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemanticInfo SemanticInfoUpdater::semanticInfo() const
|
|
|
|
|
{
|
|
|
|
|
return d->semanticInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace CppTools
|