Commit 442bdbde authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

CppTools: Avoid unnecessary blocking of main thread



Among others, BaseEditorDocumentParser::projectPart() was a blocking
operation in case the parser was running. This led to noticeable GUI
freezes for the ClangCodeModel since the function was called from the
main thread.

Rework *EditorDocumentParser to clearly separate the configuration data
(input) from the actual object state. Querying/setting configuration or
(last) state does not block anymore. update() is supposed to get the
necessary configuration and the last state at the beginning and to set
the new state at end.

Change-Id: Ib4b534fa6ff373c3059826726b3f10ece95acc21
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
parent 91c497b1
......@@ -91,21 +91,25 @@ ClangEditorDocumentParser::ClangEditorDocumentParser(const QString &filePath)
void ClangEditorDocumentParser::update(CppTools::WorkingCopy workingCopy)
{
QTC_ASSERT(m_marker, return);
QMutexLocker lock(m_marker->mutex());
QMutexLocker lock2(&m_mutex);
QMutexLocker locker(&m_updateIsRunning);
m_projectPart = determineProjectPart();
const QStringList options = createOptions(filePath(), projectPart(), true);
// Determine project part
State state_ = state();
state_.projectPart = determineProjectPart(filePath(), configuration(), state_);
setState(state_);
// Determine command line arguments
const QStringList options = createOptions(filePath(), state_.projectPart, true);
qCDebug(log, "Reparse options (cmd line equivalent): %s",
commandLine(options, filePath()).toUtf8().constData());
QTime t; t.start();
// Run
QTime t; t.start();
QMutexLocker lock(m_marker->mutex());
m_marker->setFileName(filePath());
m_marker->setCompilationOptions(options);
const Internal::UnsavedFiles unsavedFiles = Utils::createUnsavedFiles(workingCopy);
m_marker->reparse(unsavedFiles);
qCDebug(log) << "Reparse took" << t.elapsed() << "ms.";
}
......
......@@ -268,9 +268,11 @@ void CppEditorDocument::setPreprocessorSettings(const CppTools::ProjectPart::Ptr
{
CppTools::BaseEditorDocumentParser *parser = processor()->parser();
QTC_ASSERT(parser, return);
if (parser->projectPart() != projectPart || parser->editorDefines() != defines) {
parser->setProjectPart(projectPart);
parser->setEditorDefines(defines);
if (parser->projectPart() != projectPart || parser->configuration().editorDefines != defines) {
CppTools::BaseEditorDocumentParser::Configuration config = parser->configuration();
config.manuallySetProjectPart = projectPart;
config.editorDefines = defines;
parser->setConfiguration(config);
emit preprocessorSettingsChanged(!defines.trimmed().isEmpty());
}
......
......@@ -44,15 +44,19 @@ namespace CppTools {
It's meant to be used in the C++ editor to get precise results by using
the "best" project part for a file.
Derived classes are expected to implement update() by using the protected
mutex, determineProjectPart() and by respecting the options set by the client.
Derived classes are expected to implement update() this way:
\list
\li Get a copy of the configuration and the last state.
\li Acquire the protected m_updateIsRunning for the duration of update().
\li Work on the data and do whatever is necessary. At least, update
the project part with the help of determineProjectPart().
\li Ensure the new state is set before update() returns.
\endlist
*/
BaseEditorDocumentParser::BaseEditorDocumentParser(const QString &filePath)
: m_mutex(QMutex::Recursive)
, m_filePath(filePath)
, m_usePrecompiledHeaders(false)
, m_editorDefinesChangedSinceLastUpdate(false)
: m_filePath(filePath)
{
}
......@@ -65,44 +69,33 @@ QString BaseEditorDocumentParser::filePath() const
return m_filePath;
}
ProjectPart::Ptr BaseEditorDocumentParser::projectPart() const
{
QMutexLocker locker(&m_mutex);
return m_projectPart;
}
void BaseEditorDocumentParser::setProjectPart(ProjectPart::Ptr projectPart)
BaseEditorDocumentParser::Configuration BaseEditorDocumentParser::configuration() const
{
QMutexLocker locker(&m_mutex);
m_manuallySetProjectPart = projectPart;
QMutexLocker locker(&m_stateAndConfigurationMutex);
return m_configuration;
}
bool BaseEditorDocumentParser::usePrecompiledHeaders() const
void BaseEditorDocumentParser::setConfiguration(const Configuration &configuration)
{
QMutexLocker locker(&m_mutex);
return m_usePrecompiledHeaders;
QMutexLocker locker(&m_stateAndConfigurationMutex);
m_configuration = configuration;
}
void BaseEditorDocumentParser::setUsePrecompiledHeaders(bool usePrecompiledHeaders)
BaseEditorDocumentParser::State BaseEditorDocumentParser::state() const
{
QMutexLocker locker(&m_mutex);
m_usePrecompiledHeaders = usePrecompiledHeaders;
QMutexLocker locker(&m_stateAndConfigurationMutex);
return m_state;
}
QByteArray BaseEditorDocumentParser::editorDefines() const
void BaseEditorDocumentParser::setState(const State &state)
{
QMutexLocker locker(&m_mutex);
return m_editorDefines;
QMutexLocker locker(&m_stateAndConfigurationMutex);
m_state = state;
}
void BaseEditorDocumentParser::setEditorDefines(const QByteArray &editorDefines)
ProjectPart::Ptr BaseEditorDocumentParser::projectPart() const
{
QMutexLocker locker(&m_mutex);
if (editorDefines != m_editorDefines) {
m_editorDefines = editorDefines;
m_editorDefinesChangedSinceLastUpdate = true;
}
return state().projectPart;
}
BaseEditorDocumentParser *BaseEditorDocumentParser::get(const QString &filePath)
......@@ -115,15 +108,17 @@ BaseEditorDocumentParser *BaseEditorDocumentParser::get(const QString &filePath)
return 0;
}
ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart() const
ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart(const QString &filePath,
const Configuration &config,
const State &state)
{
if (m_manuallySetProjectPart)
return m_manuallySetProjectPart;
if (config.manuallySetProjectPart)
return config.manuallySetProjectPart;
ProjectPart::Ptr projectPart = m_projectPart;
ProjectPart::Ptr projectPart = state.projectPart;
CppModelManager *cmm = CppModelManager::instance();
QList<ProjectPart::Ptr> projectParts = cmm->projectPart(m_filePath);
QList<ProjectPart::Ptr> projectParts = cmm->projectPart(filePath);
if (projectParts.isEmpty()) {
if (projectPart)
// File is not directly part of any project, but we got one before. We will re-use it,
......@@ -131,7 +126,7 @@ ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart() const
return projectPart;
// Fall-back step 1: Get some parts through the dependency table:
projectParts = cmm->projectPartFromDependencies(Utils::FileName::fromString(m_filePath));
projectParts = cmm->projectPartFromDependencies(Utils::FileName::fromString(filePath));
if (projectParts.isEmpty())
// Fall-back step 2: Use fall-back part from the model manager:
projectPart = cmm->fallbackProjectPart();
......@@ -146,14 +141,4 @@ ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart() const
return projectPart;
}
bool BaseEditorDocumentParser::editorDefinesChanged() const
{
return m_editorDefinesChangedSinceLastUpdate;
}
void BaseEditorDocumentParser::resetEditorDefinesChanged()
{
m_editorDefinesChangedSinceLastUpdate = false;
}
} // namespace CppTools
......@@ -42,45 +42,49 @@ class CPPTOOLS_EXPORT BaseEditorDocumentParser : public QObject
{
Q_OBJECT
public:
static BaseEditorDocumentParser *get(const QString &filePath);
struct Configuration {
bool usePrecompiledHeaders = false;
QByteArray editorDefines;
ProjectPart::Ptr manuallySetProjectPart;
};
public:
BaseEditorDocumentParser(const QString &filePath);
virtual ~BaseEditorDocumentParser();
QString filePath() const;
Configuration configuration() const;
void setConfiguration(const Configuration &configuration);
virtual void update(WorkingCopy workingCopy) = 0;
ProjectPart::Ptr projectPart() const;
void setProjectPart(ProjectPart::Ptr projectPart);
bool usePrecompiledHeaders() const;
void setUsePrecompiledHeaders(bool usePrecompiledHeaders);
QByteArray editorDefines() const;
void setEditorDefines(const QByteArray &editorDefines);
public:
static BaseEditorDocumentParser *get(const QString &filePath);
protected:
ProjectPart::Ptr determineProjectPart() const;
struct State {
QByteArray editorDefines;
ProjectPart::Ptr projectPart;
};
bool editorDefinesChanged() const;
void resetEditorDefinesChanged();
State state() const;
void setState(const State &state);
protected:
mutable QMutex m_mutex;
ProjectPart::Ptr m_projectPart;
static ProjectPart::Ptr determineProjectPart(const QString &filePath,
const Configuration &config,
const State &state);
mutable QMutex m_updateIsRunning;
mutable QMutex m_stateAndConfigurationMutex;
private:
const QString m_filePath;
ProjectPart::Ptr m_manuallySetProjectPart;
bool m_usePrecompiledHeaders;
QByteArray m_editorDefines;
bool m_editorDefinesChangedSinceLastUpdate;
Configuration m_configuration;
State m_state;
};
} // namespace CppTools
......
......@@ -40,15 +40,19 @@ using namespace CppTools::Internal;
BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const QString &filePath)
: BaseEditorDocumentParser(filePath)
, m_forceSnapshotInvalidation(false)
, m_releaseSourceAndAST(true)
{
qRegisterMetaType<CPlusPlus::Snapshot>("CPlusPlus::Snapshot");
}
void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
{
QMutexLocker locker(&m_mutex);
QMutexLocker locker(&m_updateIsRunning);
const Configuration baseConfig = configuration();
const bool releaseSourceAndAST_ = releaseSourceAndAST();
State baseState = state();
ExtraState state = extraState();
if (filePath().isEmpty())
return;
......@@ -62,52 +66,52 @@ void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
QString projectConfigFile;
LanguageFeatures features = LanguageFeatures::defaultFeatures();
m_projectPart = determineProjectPart();
baseState.projectPart = determineProjectPart(filePath(), baseConfig, baseState);
if (m_forceSnapshotInvalidation) {
if (state.forceSnapshotInvalidation) {
invalidateSnapshot = true;
m_forceSnapshotInvalidation = false;
state.forceSnapshotInvalidation = false;
}
if (const ProjectPart::Ptr part = projectPart()) {
if (const ProjectPart::Ptr part = baseState.projectPart) {
configFile += part->toolchainDefines;
configFile += part->projectDefines;
headerPaths = part->headerPaths;
projectConfigFile = part->projectConfigFile;
if (usePrecompiledHeaders())
if (baseConfig.usePrecompiledHeaders)
precompiledHeaders = part->precompiledHeaders;
features = part->languageFeatures;
}
if (configFile != m_configFile) {
m_configFile = configFile;
if (configFile != state.configFile) {
state.configFile = configFile;
invalidateSnapshot = true;
invalidateConfig = true;
}
if (editorDefinesChanged()) {
if (baseConfig.editorDefines != baseState.editorDefines) {
baseState.editorDefines = baseConfig.editorDefines;
invalidateSnapshot = true;
editorDefinesChanged_ = true;
resetEditorDefinesChanged();
}
if (headerPaths != m_headerPaths) {
m_headerPaths = headerPaths;
if (headerPaths != state.headerPaths) {
state.headerPaths = headerPaths;
invalidateSnapshot = true;
}
if (projectConfigFile != m_projectConfigFile) {
m_projectConfigFile = projectConfigFile;
if (projectConfigFile != state.projectConfigFile) {
state.projectConfigFile = projectConfigFile;
invalidateSnapshot = true;
}
if (precompiledHeaders != m_precompiledHeaders) {
m_precompiledHeaders = precompiledHeaders;
if (precompiledHeaders != state.precompiledHeaders) {
state.precompiledHeaders = precompiledHeaders;
invalidateSnapshot = true;
}
unsigned rev = 0;
if (Document::Ptr doc = document())
if (Document::Ptr doc = state.snapshot.document(filePath()))
rev = doc->revision();
else
invalidateSnapshot = true;
......@@ -115,26 +119,26 @@ void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
Snapshot globalSnapshot = modelManager->snapshot();
if (invalidateSnapshot) {
m_snapshot = Snapshot();
state.snapshot = Snapshot();
} else {
// Remove changed files from the snapshot
QSet<Utils::FileName> toRemove;
foreach (const Document::Ptr &doc, m_snapshot) {
foreach (const Document::Ptr &doc, state.snapshot) {
const Utils::FileName fileName = Utils::FileName::fromString(doc->fileName());
if (workingCopy.contains(fileName)) {
if (workingCopy.get(fileName).second != doc->editorRevision())
addFileAndDependencies(&toRemove, fileName);
addFileAndDependencies(&state.snapshot, &toRemove, fileName);
continue;
}
Document::Ptr otherDoc = globalSnapshot.document(fileName);
if (!otherDoc.isNull() && otherDoc->revision() != doc->revision())
addFileAndDependencies(&toRemove, fileName);
addFileAndDependencies(&state.snapshot, &toRemove, fileName);
}
if (!toRemove.isEmpty()) {
invalidateSnapshot = true;
foreach (const Utils::FileName &fileName, toRemove)
m_snapshot.remove(fileName);
state.snapshot.remove(fileName);
}
}
......@@ -142,19 +146,19 @@ void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
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(filePath());
state.snapshot.remove(configurationFileName);
if (!state.snapshot.contains(configurationFileName))
workingCopy.insert(configurationFileName, state.configFile);
state.snapshot.remove(filePath());
static const QString editorDefinesFileName
= CppModelManager::editorConfigurationFileName();
if (editorDefinesChanged_) {
m_snapshot.remove(editorDefinesFileName);
workingCopy.insert(editorDefinesFileName, editorDefines());
state.snapshot.remove(editorDefinesFileName);
workingCopy.insert(editorDefinesFileName, baseState.editorDefines);
}
CppSourceProcessor sourceProcessor(m_snapshot, [&](const Document::Ptr &doc) {
CppSourceProcessor sourceProcessor(state.snapshot, [&](const Document::Ptr &doc) {
const QString fileName = doc->fileName();
const bool isInEditor = fileName == filePath();
Document::Ptr otherDoc = modelManager->document(fileName);
......@@ -163,68 +167,64 @@ void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
newRev = qMax(rev + 1, newRev);
doc->setRevision(newRev);
modelManager->emitDocumentUpdated(doc);
if (m_releaseSourceAndAST)
if (releaseSourceAndAST_)
doc->releaseSourceAndAST();
});
Snapshot globalSnapshot = modelManager->snapshot();
globalSnapshot.remove(filePath());
sourceProcessor.setGlobalSnapshot(globalSnapshot);
sourceProcessor.setWorkingCopy(workingCopy);
sourceProcessor.setHeaderPaths(m_headerPaths);
sourceProcessor.setHeaderPaths(state.headerPaths);
sourceProcessor.setLanguageFeatures(features);
sourceProcessor.run(configurationFileName);
if (!m_projectConfigFile.isEmpty())
sourceProcessor.run(m_projectConfigFile);
if (usePrecompiledHeaders()) {
foreach (const QString &precompiledHeader, m_precompiledHeaders)
if (!state.projectConfigFile.isEmpty())
sourceProcessor.run(state.projectConfigFile);
if (baseConfig.usePrecompiledHeaders) {
foreach (const QString &precompiledHeader, state.precompiledHeaders)
sourceProcessor.run(precompiledHeader);
}
if (!editorDefines().isEmpty())
if (!baseState.editorDefines.isEmpty())
sourceProcessor.run(editorDefinesFileName);
sourceProcessor.run(filePath(), usePrecompiledHeaders() ? m_precompiledHeaders
: QStringList());
m_snapshot = sourceProcessor.snapshot();
Snapshot newSnapshot = m_snapshot.simplified(document());
for (Snapshot::const_iterator i = m_snapshot.begin(), ei = m_snapshot.end(); i != ei; ++i) {
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) {
if (Client::isInjectedFile(i.key().toString()))
newSnapshot.insert(i.value());
}
m_snapshot = newSnapshot;
m_snapshot.updateDependencyTable();
emit finished(document(), m_snapshot);
state.snapshot = newSnapshot;
state.snapshot.updateDependencyTable();
}
setState(baseState);
setExtraState(state);
if (invalidateSnapshot)
emit finished(state.snapshot.document(filePath()), state.snapshot);
}
void BuiltinEditorDocumentParser::releaseResources()
{
QMutexLocker locker(&m_mutex);
m_snapshot = Snapshot();
m_forceSnapshotInvalidation = true;
ExtraState s = extraState();
s.snapshot = Snapshot();
s.forceSnapshotInvalidation = true;
setExtraState(s);
}
Document::Ptr BuiltinEditorDocumentParser::document() const
{
QMutexLocker locker(&m_mutex);
return m_snapshot.document(filePath());
return extraState().snapshot.document(filePath());
}
Snapshot BuiltinEditorDocumentParser::snapshot() const
{
QMutexLocker locker(&m_mutex);
return m_snapshot;
return extraState().snapshot;
}
ProjectPart::HeaderPaths BuiltinEditorDocumentParser::headerPaths() const
{
QMutexLocker locker(&m_mutex);
return m_headerPaths;
}
void BuiltinEditorDocumentParser::setReleaseSourceAndAST(bool onoff)
{
QMutexLocker locker(&m_mutex);
m_releaseSourceAndAST = onoff;
return extraState().headerPaths;
}
BuiltinEditorDocumentParser *BuiltinEditorDocumentParser::get(const QString &filePath)
......@@ -234,12 +234,39 @@ BuiltinEditorDocumentParser *BuiltinEditorDocumentParser::get(const QString &fil
return 0;
}
void BuiltinEditorDocumentParser::addFileAndDependencies(QSet<Utils::FileName> *toRemove,
void BuiltinEditorDocumentParser::addFileAndDependencies(Snapshot *snapshot,
QSet<Utils::FileName> *toRemove,
const Utils::FileName &fileName) const
{
QTC_ASSERT(snapshot, return);
toRemove->insert(fileName);
if (fileName != Utils::FileName::fromString(filePath())) {
Utils::FileNameList deps = m_snapshot.filesDependingOn(fileName);
Utils::FileNameList deps = snapshot->filesDependingOn(fileName);
toRemove->unite(QSet<Utils::FileName>::fromList(deps));
}
}
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;
}
......@@ -37,7 +37,6 @@
#include <cplusplus/CppDocument.h>
#include <QMutex>
#include <QString>
namespace CppTools {
......@@ -49,14 +48,16 @@ class CPPTOOLS_EXPORT BuiltinEditorDocumentParser : public BaseEditorDocumentPar
public:
BuiltinEditorDocumentParser(const QString &filePath);
bool releaseSourceAndAST() const;
void setReleaseSourceAndAST(bool release);
void update(WorkingCopy workingCopy) override;
void releaseResources();
CPlusPlus::Document::Ptr document() const;
CPlusPlus::Snapshot snapshot() const;
ProjectPart::HeaderPaths headerPaths() const;
void setReleaseSourceAndAST(bool onoff);
void releaseResources();
signals:
void finished(CPlusPlus::Document::Ptr document, CPlusPlus::Snapshot snapshot);
......@@ -65,18 +66,26 @@ public:
static BuiltinEditorDocumentParser *get(const QString &filePath);
private:
void addFileAndDependencies(QSet<Utils::FileName> *toRemove, const Utils::FileName &fileName) const;
void addFileAndDependencies(CPlusPlus::Snapshot *snapshot,
QSet<Utils::FileName> *toRemove,
const Utils::FileName &fileName) const;
private:
QByteArray m_configFile;
struct ExtraState {
QByteArray configFile;
ProjectPart::HeaderPaths headerPaths;
QString projectConfigFile;
QStringList precompiledHeaders;
CPlusPlus::Snapshot snapshot;
bool forceSnapshotInvalidation = false;
};