ClangCodeModel: Restrict opening non-project files to headers

We cannot tell clangd to opt out of indexing for certain files, so we
must prevent "foreign" sources from ever getting opened, or we will have
strange effects in the case of non-unique symbols.
Note that there are more (upstream) problems in this area, but this
patch limits the damage at least.
Amends 8ad7ab2d2a.

Fixes: QTCREATORBUG-28452
Change-Id: I131be699a35da8eacea6415c630e9012cc905a47
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-11-24 16:08:12 +01:00
parent 1966591c7f
commit 0f202ea724
3 changed files with 24 additions and 11 deletions

View File

@@ -517,13 +517,15 @@ void ClangModelManagerSupport::updateLanguageClient(ProjectExplorer::Project *pr
&& currentClient->project() == docProject) {
continue;
}
if (!docProject || docProject == project) {
if (docProject != project
&& (docProject || !ProjectFile::isHeader(doc->filePath()))) {
continue;
}
if (currentClient)
currentClient->closeDocument(doc);
LanguageClientManager::openDocumentWithClient(doc, client);
hasDocuments = true;
}
}
for (auto it = m_queuedShadowDocuments.begin(); it != m_queuedShadowDocuments.end();) {
if (fileIsProjectBuildArtifact(client, it.key())) {
@@ -625,12 +627,14 @@ void ClangModelManagerSupport::claimNonProjectSources(ClangdClient *client)
}
if (!ClangdSettings::instance().sizeIsOkay(doc->filePath()))
continue;
if (!ProjectExplorer::SessionManager::projectForFile(doc->filePath())) {
if (ProjectExplorer::SessionManager::projectForFile(doc->filePath()))
continue;
if (client->project() && !ProjectFile::isHeader(doc->filePath()))
continue;
if (currentClient)
currentClient->closeDocument(doc);
LanguageClientManager::openDocumentWithClient(doc, client);
}
}
}
// If any open C/C++ source file is changed from outside Qt Creator, we restart the client
@@ -717,7 +721,7 @@ void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor)
return;
if (sessionModeEnabled())
project = nullptr;
else if (!project)
else if (!project && ProjectFile::isHeader(document->filePath()))
project = fallbackProject();
if (ClangdClient * const client = clientForProject(project))
LanguageClientManager::openDocumentWithClient(textDocument, client);

View File

@@ -6,6 +6,7 @@
#include "cppeditorconstants.h"
#include <coreplugin/icore.h>
#include <utils/filepath.h>
#include <utils/mimeutils.h>
#include <QDebug>
@@ -125,6 +126,11 @@ bool ProjectFile::isHeader(ProjectFile::Kind kind)
}
}
bool ProjectFile::isHeader(const Utils::FilePath &fp)
{
return isHeader(classify(fp.toString()));
}
bool ProjectFile::isSource(ProjectFile::Kind kind)
{
switch (kind) {

View File

@@ -7,6 +7,8 @@
#include <QString>
namespace Utils { class FilePath; }
namespace CppEditor {
class CPPEDITOR_EXPORT ProjectFile
@@ -39,6 +41,7 @@ public:
static bool isSource(Kind kind);
static bool isHeader(Kind kind);
static bool isHeader(const Utils::FilePath &fp);
static bool isC(Kind kind);
static bool isCxx(Kind kind);
static bool isAmbiguousHeader(const QString &filePath);