ClangUtils: Get rid of GenerateCompilationDbResult struct

Replace it with expected_str<FilePath>.
Check periodically for cancel request inside
generateCompilationDB() body.

Cancel the possibly running task on ClangCodeModelPlugin
destruction.

Handle the cancellation.

Change-Id: I8bcb956bc03627b4a17a3510a76810e66c82815d
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Jarek Kobus
2024-05-23 23:41:17 +02:00
parent d3b2bc32cc
commit 63a93760d9
4 changed files with 45 additions and 38 deletions

View File

@@ -146,23 +146,26 @@ static QJsonObject createFileObject(const FilePath &buildDir,
return fileObject;
}
GenerateCompilationDbResult generateCompilationDB(QList<ProjectInfo::ConstPtr> projectInfoList,
FilePath baseDir,
CompilationDbPurpose purpose,
ClangDiagnosticConfig warningsConfig,
QStringList projectOptions,
FilePath clangIncludeDir)
void generateCompilationDB(
QPromise<expected_str<FilePath>> &promise,
const QList<ProjectInfo::ConstPtr> &projectInfoList,
const FilePath &baseDir,
CompilationDbPurpose purpose,
const ClangDiagnosticConfig &warningsConfig,
const QStringList &projectOptions,
const FilePath &clangIncludeDir)
{
QTC_ASSERT(!baseDir.isEmpty(), return GenerateCompilationDbResult(QString(),
Tr::tr("Could not retrieve build directory.")));
QTC_ASSERT(!baseDir.isEmpty(),
promise.addResult(make_unexpected(Tr::tr("Could not retrieve build directory."))); return);
QTC_ASSERT(!projectInfoList.isEmpty(),
return GenerateCompilationDbResult(QString(), "Could not retrieve project info."));
promise.addResult(make_unexpected(Tr::tr("Could not retrieve project info."))); return);
QTC_CHECK(baseDir.ensureWritableDir());
QFile compileCommandsFile(baseDir.pathAppended("compile_commands.json").toFSPathString());
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
if (!fileOpened) {
return GenerateCompilationDbResult(QString(), Tr::tr("Could not create \"%1\": %2")
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString()));
promise.addResult(make_unexpected(Tr::tr("Could not create \"%1\": %2")
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString())));
return;
}
compileCommandsFile.write("[");
@@ -182,6 +185,8 @@ GenerateCompilationDbResult generateCompilationDB(QList<ProjectInfo::ConstPtr> p
jsonProjectOptions);
}
for (const ProjectFile &projFile : projectPart->files) {
if (promise.isCanceled())
return;
const QJsonObject json
= createFileObject(baseDir,
args,
@@ -200,7 +205,7 @@ GenerateCompilationDbResult generateCompilationDB(QList<ProjectInfo::ConstPtr> p
compileCommandsFile.write("]");
compileCommandsFile.close();
return GenerateCompilationDbResult(compileCommandsFile.fileName(), QString());
promise.addResult(FilePath::fromUserInput(compileCommandsFile.fileName()));
}
FilePath currentCppEditorDocumentFilePath()
@@ -262,7 +267,6 @@ QString DiagnosticTextInfo::clazyCheckName(const QString &option)
return option;
}
QJsonArray clangOptionsForFile(const ProjectFile &file, const ProjectPart &projectPart,
const QJsonArray &generalOptions, UsePrecompiledHeaders usePch,
bool clStyle)