forked from qt-creator/qt-creator
ClangCodeModel: Switch to LSP-based UI header approach
Generating ui headers in a well-known path and then including that one in the compilation database does not work in the presence of multiple ui files with the same name. As it turns out, we don't have to generate any files at all; instead, we pass the file contents directly to clangd, which then uses them when parsing includes of the respective header. User-visible behavior change apart from the abovementioned bug fix: Tooltips and "follow symbol" on the include directive now always use the actual location of the header provided by the build system. Fixes: QTCREATORBUG-27584 Change-Id: I6b13e12cb3a365199567b0bc824d12b373117697 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -27,7 +27,6 @@ add_qtc_plugin(ClangCodeModel
|
||||
clangmodelmanagersupport.cpp clangmodelmanagersupport.h
|
||||
clangpreprocessorassistproposalitem.cpp clangpreprocessorassistproposalitem.h
|
||||
clangtextmark.cpp clangtextmark.h
|
||||
clanguiheaderondiskmanager.cpp clanguiheaderondiskmanager.h
|
||||
clangutils.cpp clangutils.h
|
||||
tasktimers.cpp tasktimers.h
|
||||
EXPLICIT_MOC clangcodemodelplugin.h
|
||||
|
||||
@@ -54,8 +54,6 @@ QtcPlugin {
|
||||
"clangpreprocessorassistproposalitem.h",
|
||||
"clangtextmark.cpp",
|
||||
"clangtextmark.h",
|
||||
"clanguiheaderondiskmanager.cpp",
|
||||
"clanguiheaderondiskmanager.h",
|
||||
"clangutils.cpp",
|
||||
"clangutils.h",
|
||||
"tasktimers.cpp",
|
||||
|
||||
@@ -100,6 +100,30 @@ static const QList<TextEditor::TextDocument *> allCppDocuments()
|
||||
return Utils::qobject_container_cast<TextEditor::TextDocument *>(documents);
|
||||
}
|
||||
|
||||
static bool fileIsProjectBuildArtifact(const Client *client, const Utils::FilePath &filePath)
|
||||
{
|
||||
if (const auto p = client->project()) {
|
||||
if (const auto t = p->activeTarget()) {
|
||||
if (const auto bc = t->activeBuildConfiguration()) {
|
||||
if (filePath.isChildOf(bc->buildDirectory()))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static Client *clientForGeneratedFile(const Utils::FilePath &filePath)
|
||||
{
|
||||
for (Client * const client : LanguageClientManager::clients()) {
|
||||
if (qobject_cast<ClangdClient *>(client) && client->reachable()
|
||||
&& fileIsProjectBuildArtifact(client, filePath)) {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ClangModelManagerSupport::ClangModelManagerSupport()
|
||||
{
|
||||
QTC_CHECK(!m_instance);
|
||||
@@ -344,7 +368,7 @@ void ClangModelManagerSupport::updateLanguageClient(
|
||||
if (Client * const oldClient = clientForProject(project))
|
||||
LanguageClientManager::shutdownClient(oldClient);
|
||||
ClangdClient * const client = createClient(project, jsonDbDir);
|
||||
connect(client, &Client::initialized, this, [client, project, projectInfo, jsonDbDir] {
|
||||
connect(client, &Client::initialized, this, [this, client, project, projectInfo, jsonDbDir] {
|
||||
using namespace ProjectExplorer;
|
||||
if (!SessionManager::hasProject(project))
|
||||
return;
|
||||
@@ -384,6 +408,23 @@ void ClangModelManagerSupport::updateLanguageClient(
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = m_queuedShadowDocuments.begin(); it != m_queuedShadowDocuments.end();) {
|
||||
if (fileIsProjectBuildArtifact(client, it.key())) {
|
||||
if (it.value().isEmpty())
|
||||
client->removeShadowDocument(it.key());
|
||||
else
|
||||
client->setShadowDocument(it.key(), it.value());
|
||||
ClangdClient::handleUiHeaderChange(it.key().fileName());
|
||||
it = m_queuedShadowDocuments.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
connect(client, &Client::shadowDocumentSwitched, this,
|
||||
[](const Utils::FilePath &fp) {
|
||||
ClangdClient::handleUiHeaderChange(fp.fileName());
|
||||
});
|
||||
|
||||
if (client->state() == Client::Initialized)
|
||||
updateParserConfig();
|
||||
else
|
||||
@@ -593,18 +634,28 @@ void ClangModelManagerSupport::onAbstractEditorSupportContentsUpdated(const QStr
|
||||
|
||||
if (content.size() == 0)
|
||||
return; // Generation not yet finished.
|
||||
|
||||
m_uiHeaderOnDiskManager.write(filePath, content);
|
||||
ClangdClient::handleUiHeaderChange(Utils::FilePath::fromString(filePath).fileName());
|
||||
const auto fp = Utils::FilePath::fromString(filePath);
|
||||
const QString stringContent = QString::fromUtf8(content);
|
||||
if (Client * const client = clientForGeneratedFile(fp)) {
|
||||
client->setShadowDocument(fp, stringContent);
|
||||
ClangdClient::handleUiHeaderChange(fp.fileName());
|
||||
QTC_CHECK(m_queuedShadowDocuments.remove(fp) == 0);
|
||||
} else {
|
||||
m_queuedShadowDocuments.insert(fp, stringContent);
|
||||
}
|
||||
}
|
||||
|
||||
void ClangModelManagerSupport::onAbstractEditorSupportRemoved(const QString &filePath)
|
||||
{
|
||||
QTC_ASSERT(!filePath.isEmpty(), return);
|
||||
|
||||
if (!cppModelManager()->cppEditorDocument(filePath)) {
|
||||
m_uiHeaderOnDiskManager.remove(filePath);
|
||||
ClangdClient::handleUiHeaderChange(Utils::FilePath::fromString(filePath).fileName());
|
||||
const auto fp = Utils::FilePath::fromString(filePath);
|
||||
if (Client * const client = clientForGeneratedFile(fp)) {
|
||||
client->removeShadowDocument(fp);
|
||||
ClangdClient::handleUiHeaderChange(fp.fileName());
|
||||
QTC_CHECK(m_queuedShadowDocuments.remove(fp) == 0);
|
||||
} else {
|
||||
m_queuedShadowDocuments.insert(fp, {});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -738,16 +789,6 @@ ClangModelManagerSupport *ClangModelManagerSupport::instance()
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
QString ClangModelManagerSupport::dummyUiHeaderOnDiskPath(const QString &filePath) const
|
||||
{
|
||||
return m_uiHeaderOnDiskManager.mapPath(filePath);
|
||||
}
|
||||
|
||||
QString ClangModelManagerSupport::dummyUiHeaderOnDiskDirPath() const
|
||||
{
|
||||
return m_uiHeaderOnDiskManager.directoryPath();
|
||||
}
|
||||
|
||||
QString ClangModelManagerSupportProvider::id() const
|
||||
{
|
||||
return QLatin1String(Constants::CLANG_MODELMANAGERSUPPORT_ID);
|
||||
|
||||
@@ -25,14 +25,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "clanguiheaderondiskmanager.h"
|
||||
|
||||
#include <cppeditor/cppmodelmanagersupport.h>
|
||||
#include <cppeditor/projectinfo.h>
|
||||
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/futuresynchronizer.h>
|
||||
#include <utils/id.h>
|
||||
|
||||
#include <QHash>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
@@ -70,9 +70,6 @@ public:
|
||||
std::unique_ptr<CppEditor::AbstractOverviewModel> createOverviewModel() override;
|
||||
bool usesClangd(const TextEditor::TextDocument *document) const override;
|
||||
|
||||
QString dummyUiHeaderOnDiskDirPath() const;
|
||||
QString dummyUiHeaderOnDiskPath(const QString &filePath) const;
|
||||
|
||||
ClangdClient *clientForProject(const ProjectExplorer::Project *project) const;
|
||||
ClangdClient *clientForFile(const Utils::FilePath &file) const;
|
||||
|
||||
@@ -122,10 +119,9 @@ private:
|
||||
void watchForExternalChanges();
|
||||
void watchForInternalChanges();
|
||||
|
||||
UiHeaderOnDiskManager m_uiHeaderOnDiskManager;
|
||||
|
||||
Utils::FutureSynchronizer m_generatorSynchronizer;
|
||||
QList<QPointer<ClangdClient>> m_clientsToRestart;
|
||||
QHash<Utils::FilePath, QString> m_queuedShadowDocuments;
|
||||
};
|
||||
|
||||
class ClangModelManagerSupportProvider : public CppEditor::ModelManagerSupportProvider
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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/fileutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
UiHeaderOnDiskManager::UiHeaderOnDiskManager() : m_temporaryDir("clang-uiheader-XXXXXX")
|
||||
{
|
||||
QTC_CHECK(m_temporaryDir.isValid());
|
||||
}
|
||||
|
||||
QString UiHeaderOnDiskManager::write(const QString &filePath, const QByteArray &content)
|
||||
{
|
||||
const QString mappedPath = mapPath(filePath);
|
||||
QFile file(mappedPath);
|
||||
const bool fileCreated = file.open(QFile::WriteOnly);
|
||||
const qint64 bytesWritten = file.write(content);
|
||||
QTC_CHECK(fileCreated && bytesWritten != -1);
|
||||
|
||||
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().path();
|
||||
}
|
||||
|
||||
QString UiHeaderOnDiskManager::mapPath(const QString &filePath) const
|
||||
{
|
||||
return directoryPath() + '/' + QFileInfo(filePath).fileName();
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace ClangCodeModel
|
||||
@@ -1,50 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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 <utils/temporarydirectory.h>
|
||||
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
// TODO: Remove once libclang supports unsaved files that do not exist.
|
||||
class UiHeaderOnDiskManager
|
||||
{
|
||||
public:
|
||||
UiHeaderOnDiskManager();
|
||||
|
||||
QString write(const QString &filePath, const QByteArray &content);
|
||||
QString remove(const QString &filePath);
|
||||
|
||||
QString mapPath(const QString &filePath) const;
|
||||
QString directoryPath() const;
|
||||
|
||||
private:
|
||||
::Utils::TemporaryDirectory m_temporaryDir;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace ClangCodeModel
|
||||
@@ -25,8 +25,6 @@
|
||||
|
||||
#include "clangutils.h"
|
||||
|
||||
#include "clangmodelmanagersupport.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/idocument.h>
|
||||
#include <cppeditor/baseeditordocumentparser.h>
|
||||
@@ -355,12 +353,6 @@ CompilerOptionsBuilder clangOptionsBuilder(const ProjectPart &projectPart,
|
||||
useBuildSystemWarnings, clangIncludeDir);
|
||||
optionsBuilder.provideAdditionalMacros({ProjectExplorer::Macro("Q_CREATOR_RUN", "1")});
|
||||
optionsBuilder.build(ProjectFile::Unclassified, UsePrecompiledHeaders::No);
|
||||
const QString uiIncludePath
|
||||
= ClangModelManagerSupport::instance()->dummyUiHeaderOnDiskDirPath();
|
||||
if (!uiIncludePath.isEmpty()) {
|
||||
optionsBuilder.prepend(QDir::toNativeSeparators(uiIncludePath));
|
||||
optionsBuilder.prepend("-I");
|
||||
}
|
||||
optionsBuilder.add("-fmessage-length=0", /*gccOnlyOption=*/true);
|
||||
optionsBuilder.add("-fdiagnostics-show-note-include-stack", /*gccOnlyOption=*/true);
|
||||
optionsBuilder.add("-fretain-comments-from-system-headers", /*gccOnlyOption=*/true);
|
||||
|
||||
Reference in New Issue
Block a user