Clang: Work around libclang not accepting not existing unsaved files

Feeding libclang with unsaved files (e.g. in-memory generated ui_*.h)
that do not exist on disk leads to regeneration of the preamble on every
parse/reparse/completion and thus renders the clang code model useless.

We could check the existence in the file system for every unsaved file
just before every parse/reparse/completion. Obviously this does not
scale (e.g. qtcreator.pro generates about 200 unsaves files) and would
also slow down the responsiveness of the completion, especially for the
dot-to-arrow correction case.

We could also set up a file system watcher. However, implementing the
"file got created" case is not trivial because QFileSystemWatcher does
not support it out of the box.

Instead, set up a custom include directory and create empty files in it
that represent the unsaved files and pass that include directory to
libclang as the last one. While this fixes the performance problems, it
also comes with at least two problems:

  * Because ui_*.h files are "relocated" to the same directory, two or
    more "foo.ui" in the same session will be problematic.

  * Because of the custom include directory, problems might arise for
    projects that include the ui_*.h as "some/relative/path/ui_foo.h"
    instead of "ui_foo.h". This should be the less common case.

Task-number: QTCREATORBUG-17245
Change-Id: I6e40e87c3ef095086eb22c972dd8c1a6459a8245
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Nikolai Kosjar
2016-11-21 13:30:26 +01:00
parent b4d6884c25
commit 2b9e9ebbfc
9 changed files with 173 additions and 18 deletions

View File

@@ -505,8 +505,11 @@ void IpcCommunicator::registerCurrentCodeModelUiHeaders()
using namespace CppTools;
const auto editorSupports = CppModelManager::instance()->abstractEditorSupports();
foreach (const AbstractEditorSupport *es, editorSupports)
updateUnsavedFile(es->fileName(), es->contents(), es->revision());
foreach (const AbstractEditorSupport *es, editorSupports) {
const QString mappedPath
= ModelManagerSupportClang::instance()->dummyUiHeaderOnDiskPath(es->fileName());
updateUnsavedFile(mappedPath, es->contents(), es->revision());
}
}
void IpcCommunicator::registerProjectsParts(const QList<CppTools::ProjectPart::Ptr> projectParts)

View File

@@ -33,6 +33,7 @@ SOURCES += \
clangprojectsettings.cpp \
clangprojectsettingswidget.cpp \
clangtextmark.cpp \
clanguiheaderondiskmanager.cpp \
clangutils.cpp
HEADERS += \
@@ -64,6 +65,7 @@ HEADERS += \
clangprojectsettings.h \
clangprojectsettingswidget.h \
clangtextmark.h \
clanguiheaderondiskmanager.h \
clangutils.h
FORMS += clangprojectsettingswidget.ui

View File

@@ -87,6 +87,8 @@ QtcPlugin {
"clangprojectsettingswidget.ui",
"clangtextmark.cpp",
"clangtextmark.h",
"clanguiheaderondiskmanager.cpp",
"clanguiheaderondiskmanager.h",
"clangutils.cpp",
"clangutils.h",
]

View File

@@ -50,7 +50,7 @@
using namespace ClangCodeModel;
using namespace ClangCodeModel::Internal;
static ModelManagerSupportClang *m_instance_forTestsOnly = 0;
static ModelManagerSupportClang *m_instance = 0;
static CppTools::CppModelManager *cppModelManager()
{
@@ -60,8 +60,8 @@ static CppTools::CppModelManager *cppModelManager()
ModelManagerSupportClang::ModelManagerSupportClang()
: m_completionAssistProvider(m_ipcCommunicator)
{
QTC_CHECK(!m_instance_forTestsOnly);
m_instance_forTestsOnly = this;
QTC_CHECK(!m_instance);
m_instance = this;
Core::EditorManager *editorManager = Core::EditorManager::instance();
connect(editorManager, &Core::EditorManager::editorOpened,
@@ -88,7 +88,7 @@ ModelManagerSupportClang::ModelManagerSupportClang()
ModelManagerSupportClang::~ModelManagerSupportClang()
{
m_instance_forTestsOnly = 0;
m_instance = 0;
}
CppTools::CppCompletionAssistProvider *ModelManagerSupportClang::completionAssistProvider()
@@ -244,15 +244,19 @@ void ModelManagerSupportClang::onAbstractEditorSupportContentsUpdated(const QStr
const QByteArray &content)
{
QTC_ASSERT(!filePath.isEmpty(), return);
m_ipcCommunicator.updateUnsavedFile(filePath, content, 0);
const QString mappedPath = m_uiHeaderOnDiskManager.createIfNeeded(filePath);
m_ipcCommunicator.updateUnsavedFile(mappedPath, content, 0);
}
void ModelManagerSupportClang::onAbstractEditorSupportRemoved(const QString &filePath)
{
QTC_ASSERT(!filePath.isEmpty(), return);
if (!cppModelManager()->cppEditorDocument(filePath)) {
const QString mappedPath = m_uiHeaderOnDiskManager.remove(filePath);
const QString projectPartId = Utils::projectPartIdForFile(filePath);
m_ipcCommunicator.unregisterUnsavedFilesForEditor({{filePath, projectPartId}});
m_ipcCommunicator.unregisterUnsavedFilesForEditor({{mappedPath, projectPartId}});
}
}
@@ -347,18 +351,26 @@ void ModelManagerSupportClang::unregisterTranslationUnitsWithProjectParts(
}
}
#ifdef QT_TESTLIB_LIB
ModelManagerSupportClang *ModelManagerSupportClang::instance_forTestsOnly()
ModelManagerSupportClang *ModelManagerSupportClang::instance()
{
return m_instance_forTestsOnly;
return m_instance;
}
#endif
IpcCommunicator &ModelManagerSupportClang::ipcCommunicator()
{
return m_ipcCommunicator;
}
QString ModelManagerSupportClang::dummyUiHeaderOnDiskPath(const QString &filePath) const
{
return m_uiHeaderOnDiskManager.mapPath(filePath);
}
QString ModelManagerSupportClang::dummyUiHeaderOnDiskDirPath() const
{
return m_uiHeaderOnDiskManager.directoryPath();
}
QString ModelManagerSupportProviderClang::id() const
{
return QLatin1String(Constants::CLANG_MODELMANAGERSUPPORT_ID);

View File

@@ -26,6 +26,7 @@
#pragma once
#include "clangcompletionassistprovider.h"
#include "clanguiheaderondiskmanager.h"
#include <cpptools/cppmodelmanagersupport.h>
@@ -58,10 +59,10 @@ public:
TextEditor::TextDocument *baseTextDocument) override;
IpcCommunicator &ipcCommunicator();
QString dummyUiHeaderOnDiskDirPath() const;
QString dummyUiHeaderOnDiskPath(const QString &filePath) const;
#ifdef QT_TESTLIB_LIB
static ModelManagerSupportClang *instance_forTestsOnly();
#endif
static ModelManagerSupportClang *instance();
private:
void onEditorOpened(Core::IEditor *editor);
@@ -96,6 +97,7 @@ private:
void connectToWidgetsMarkContextMenuRequested(QWidget *editorWidget);
private:
UiHeaderOnDiskManager m_uiHeaderOnDiskManager;
IpcCommunicator m_ipcCommunicator;
ClangCompletionAssistProvider m_completionAssistProvider;
};

View File

@@ -0,0 +1,75 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clanguiheaderondiskmanager.h"
#include <QFile>
#include <QFileInfo>
#include <utils/qtcassert.h>
namespace ClangCodeModel {
namespace Internal {
UiHeaderOnDiskManager::UiHeaderOnDiskManager()
: m_temporaryDir(QDir::tempPath() + QStringLiteral("/qtc-clang-uiheader-XXXXXX"))
{
QTC_CHECK(m_temporaryDir.isValid());
}
QString UiHeaderOnDiskManager::createIfNeeded(const QString &filePath)
{
const QString mappedPath = mapPath(filePath);
if (!QFileInfo::exists(mappedPath)) {
const bool fileCreated = QFile(mappedPath).open(QFile::WriteOnly); // touch file
QTC_CHECK(fileCreated);
}
return mappedPath;
}
QString UiHeaderOnDiskManager::remove(const QString &filePath)
{
const QString mappedPath = mapPath(filePath);
if (QFileInfo::exists(mappedPath)) {
const bool fileRemoved = QFile::remove(mappedPath);
QTC_CHECK(fileRemoved);
}
return mappedPath;
}
QString UiHeaderOnDiskManager::directoryPath() const
{
return m_temporaryDir.path();
}
QString UiHeaderOnDiskManager::mapPath(const QString &filePath) const
{
return directoryPath() + '/' + QFileInfo(filePath).fileName();
}
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -0,0 +1,50 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <QTemporaryDir>
namespace ClangCodeModel {
namespace Internal {
// TODO: Remove once libclang supports unsaved files that do not exist.
class UiHeaderOnDiskManager
{
public:
UiHeaderOnDiskManager();
QString createIfNeeded(const QString &filePath);
QString remove(const QString &filePath);
QString mapPath(const QString &filePath) const;
QString directoryPath() const;
private:
QTemporaryDir m_temporaryDir;
};
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -26,6 +26,7 @@
#include "clangutils.h"
#include "clangeditordocumentprocessor.h"
#include "clangmodelmanagersupport.h"
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
@@ -103,6 +104,7 @@ public:
optionsBuilder.addPredefinedMacrosAndHeaderPathsOptions();
optionsBuilder.addWrappedQtHeadersIncludePath();
optionsBuilder.addHeaderPathOptions();
optionsBuilder.addDummyUiHeaderOnDiskIncludePath();
optionsBuilder.addProjectConfigFileInclude();
optionsBuilder.addMsvcCompatibilityVersion();
@@ -172,6 +174,13 @@ private:
}
}
void addDummyUiHeaderOnDiskIncludePath()
{
const QString path = ModelManagerSupportClang::instance()->dummyUiHeaderOnDiskDirPath();
if (!path.isEmpty())
add(includeDirOption() + QDir::toNativeSeparators(path));
}
void addExtraOptions()
{
add(QLatin1String("-fmessage-length=0"));

View File

@@ -250,13 +250,13 @@ class ChangeIpcSender
public:
ChangeIpcSender(IpcSenderInterface *ipcSender)
{
auto &ipc = ModelManagerSupportClang::instance_forTestsOnly()->ipcCommunicator();
auto &ipc = ModelManagerSupportClang::instance()->ipcCommunicator();
m_previousSender = ipc.setIpcSender(ipcSender);
}
~ChangeIpcSender()
{
auto &ipc = ModelManagerSupportClang::instance_forTestsOnly()->ipcCommunicator();
auto &ipc = ModelManagerSupportClang::instance()->ipcCommunicator();
ipc.setIpcSender(m_previousSender);
}
@@ -1210,7 +1210,7 @@ void ClangCodeCompletionTest::testUpdateBackendAfterRestart()
spy.senderLog.clear();
// Kill backend process...
auto &ipcCommunicator = ModelManagerSupportClang::instance_forTestsOnly()->ipcCommunicator();
auto &ipcCommunicator = ModelManagerSupportClang::instance()->ipcCommunicator();
ipcCommunicator.killBackendProcess();
QSignalSpy waitForReinitializedBackend(&ipcCommunicator,
SIGNAL(backendReinitialized()));