CppTools: Turn some classes into pure value types

ProjectInfo, ProjectPart and ProjectUpdateInfo used to carry pointers
to Project and/or Toolchain, even though they were used in contexts
where these pointers were either unsafe to access or not guaranteed to
be valid anymore, which made their use difficult and error-prone.
We turn these classes into pure value types by copying in all relevant
information before the first async operation takes place.

Fixes: QTCREATORBUG-25678
Change-Id: I1914b0dbda6c7dfba6c95e5e92f2d69977755590
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Christian Kandeler
2021-05-07 16:10:07 +02:00
parent 3143ba79e3
commit 33108795d6
61 changed files with 1086 additions and 958 deletions

View File

@@ -264,14 +264,14 @@ private:
}
};
SelectableFilesDialog::SelectableFilesDialog(const ProjectInfo &projectInfo,
SelectableFilesDialog::SelectableFilesDialog(Project *project,
const FileInfoProviders &fileInfoProviders,
int initialProviderIndex)
: QDialog(nullptr)
, m_ui(new Ui::SelectableFilesDialog)
, m_filesModel(new SelectableFilesModel)
, m_fileInfoProviders(fileInfoProviders)
, m_project(projectInfo.project())
, m_project(project)
, m_analyzeButton(new QPushButton(tr("Analyze"), this))
{
m_ui->setupUi(this);

View File

@@ -47,7 +47,7 @@ class SelectableFilesDialog : public QDialog
Q_OBJECT
public:
explicit SelectableFilesDialog(const CppTools::ProjectInfo &projectInfo,
explicit SelectableFilesDialog(ProjectExplorer::Project *project,
const FileInfoProviders &fileInfoProviders,
int initialProviderIndex);
~SelectableFilesDialog() override;

View File

@@ -736,17 +736,17 @@ FileInfos ClangTool::collectFileInfos(Project *project, FileSelection fileSelect
return {};
}
auto projectInfo = CppTools::CppModelManager::instance()->projectInfo(project);
QTC_ASSERT(projectInfo.isValid(), return FileInfos());
const auto projectInfo = CppTools::CppModelManager::instance()->projectInfo(project);
QTC_ASSERT(projectInfo, return FileInfos());
const FileInfos allFileInfos = sortedFileInfos(projectInfo.projectParts());
const FileInfos allFileInfos = sortedFileInfos(projectInfo->projectParts());
if (selectionType && *selectionType == FileSelectionType::AllFiles)
return allFileInfos;
if (selectionType && *selectionType == FileSelectionType::AskUser) {
static int initialProviderIndex = 0;
SelectableFilesDialog dialog(projectInfo,
SelectableFilesDialog dialog(project,
fileInfoProviders(project, allFileInfos),
initialProviderIndex);
if (dialog.exec() == QDialog::Rejected)
@@ -875,7 +875,7 @@ static bool canAnalyzeProject(Project *project)
const bool projectSupportsLanguage = project->projectLanguages().contains(c)
|| project->projectLanguages().contains(cxx);
return projectSupportsLanguage
&& CppModelManager::instance()->projectInfo(project).isValid()
&& CppModelManager::instance()->projectInfo(project)
&& ToolChainKitAspect::cxxToolChain(target->kit());
}
return false;

View File

@@ -135,7 +135,7 @@ AnalyzeUnit::AnalyzeUnit(const FileInfo &fileInfo,
AnalyzeUnits ClangToolRunWorker::unitsToAnalyze(const FilePath &clangIncludeDir,
const QString &clangVersion)
{
QTC_ASSERT(m_projectInfo.isValid(), return AnalyzeUnits());
QTC_ASSERT(m_projectInfo, return AnalyzeUnits());
AnalyzeUnits units;
for (const FileInfo &fileInfo : m_fileInfos)
@@ -217,10 +217,14 @@ void ClangToolRunWorker::start()
const QString &toolName = tool()->name();
Project *project = runControl()->project();
m_projectInfo = CppTools::CppModelManager::instance()->projectInfo(project);
if (!m_projectInfo) {
reportFailure(tr("No code model data available for project."));
return;
}
m_projectFiles = Utils::toSet(project->files(Project::AllFiles));
// Project changed in the mean time?
if (m_projectInfo.configurationOrFilesChanged(m_projectInfoBeforeBuild)) {
if (m_projectInfo->configurationOrFilesChanged(*m_projectInfoBeforeBuild)) {
// If it's more than a release/debug build configuration change, e.g.
// a version control checkout, files might be not valid C++ anymore
// or even gone, so better stop here.
@@ -239,7 +243,7 @@ void ClangToolRunWorker::start()
return;
}
const Utils::FilePath projectFile = m_projectInfo.project()->projectFilePath();
const Utils::FilePath projectFile = m_projectInfo->projectFilePath();
appendMessage(tr("Running %1 on %2 with configuration \"%3\".")
.arg(toolName)
.arg(projectFile.toUserOutput())

View File

@@ -114,8 +114,8 @@ private:
Utils::Environment m_environment;
Utils::TemporaryDirectory m_temporaryDir;
CppTools::ProjectInfo m_projectInfoBeforeBuild;
CppTools::ProjectInfo m_projectInfo;
CppTools::ProjectInfo::Ptr m_projectInfoBeforeBuild;
CppTools::ProjectInfo::Ptr m_projectInfo;
QString m_targetTriple;
Utils::Id m_toolChainType;

View File

@@ -115,9 +115,7 @@ void ClangToolsUnitTests::testProject()
// Open project
Tests::ProjectOpenerAndCloser projectManager;
const ProjectInfo projectInfo = projectManager.open(projectFilePath, true, m_kit);
const bool isProjectOpen = projectInfo.isValid();
QVERIFY(isProjectOpen);
QVERIFY(projectManager.open(projectFilePath, true, m_kit));
// Run tool
ClangTool *tool = ClangTool::instance();

View File

@@ -137,12 +137,13 @@ static VirtualFileSystemOverlay &vfso()
static FileInfo getFileInfo(const Utils::FilePath &file, ProjectExplorer::Project *project)
{
CppTools::ProjectInfo projectInfo = CppTools::CppModelManager::instance()->projectInfo(project);
if (!projectInfo.isValid())
const CppTools::ProjectInfo::Ptr projectInfo
= CppTools::CppModelManager::instance()->projectInfo(project);
if (!projectInfo)
return {};
FileInfo candidate;
for (const CppTools::ProjectPart::Ptr &projectPart : projectInfo.projectParts()) {
for (const CppTools::ProjectPart::Ptr &projectPart : projectInfo->projectParts()) {
QTC_ASSERT(projectPart, continue);
for (const CppTools::ProjectFile &projectFile : qAsConst(projectPart->files)) {