Compilation database: Add information to message pane

We must tell users where we generated the file, and also inform them
about errors.

Change-Id: I6383655e2f731f41b9121b2a6a31bba551d1c1de
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Christian Kandeler
2019-03-13 17:58:06 +01:00
parent ec9d35f237
commit 3a58c4652e
4 changed files with 40 additions and 10 deletions

View File

@@ -36,6 +36,7 @@
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cppmodelmanager.h>
@@ -72,7 +73,8 @@ void ClangCodeModelPlugin::generateCompilationDB() {
if (!project || !project->activeTarget())
return;
QFuture<void> task = QtConcurrent::run(&Utils::generateCompilationDB,
QFuture<Utils::GenerateCompilationDbResult> task
= QtConcurrent::run(&Utils::generateCompilationDB,
CppModelManager::instance()->projectInfo(project));
Core::ProgressManager::addTask(task, tr("Generating Compilation DB"), "generate compilation db");
m_generatorWatcher.setFuture(task);
@@ -135,7 +137,17 @@ void ClangCodeModelPlugin::createCompilationDBButton()
command->setDescription(m_generateCompilationDBAction->text());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(&m_generatorWatcher, &QFutureWatcher<void>::finished, this, [this] () {
connect(&m_generatorWatcher, &QFutureWatcher<Utils::GenerateCompilationDbResult>::finished,
this, [this] () {
const Utils::GenerateCompilationDbResult result = m_generatorWatcher.result();
QString message;
if (result.error.isEmpty()) {
message = tr("Clang compilation database generated at \"%1\".")
.arg(QDir::toNativeSeparators(result.filePath));
} else {
message = tr("Generating clang compilation database failed: %1").arg(result.error);
}
Core::MessageManager::write(message, Core::MessageManager::Flash);
m_generateCompilationDBAction->setEnabled(
isDBGenerationEnabled(ProjectExplorer::SessionManager::startupProject()));
});

View File

@@ -26,6 +26,7 @@
#pragma once
#include "clangmodelmanagersupport.h"
#include "clangutils.h"
#include <extensionsystem/iplugin.h>
@@ -54,8 +55,8 @@ private:
void createCompilationDBButton();
ClangModelManagerSupportProvider m_modelManagerSupportProvider;
Utils::ParameterAction *m_generateCompilationDBAction = nullptr;
QFutureWatcher<void> m_generatorWatcher;
::Utils::ParameterAction *m_generateCompilationDBAction = nullptr;
QFutureWatcher<Utils::GenerateCompilationDbResult> m_generatorWatcher;
#ifdef WITH_TESTS
QList<QObject *> createTestObjects() const override;
#endif

View File

@@ -386,18 +386,22 @@ static QJsonObject createFileObject(const ::Utils::FileName &buildDir,
return fileObject;
}
void generateCompilationDB(CppTools::ProjectInfo projectInfo)
GenerateCompilationDbResult generateCompilationDB(CppTools::ProjectInfo projectInfo)
{
const ::Utils::FileName buildDir = buildDirectory(*projectInfo.project());
QTC_ASSERT(!buildDir.isEmpty(), return;);
QTC_ASSERT(!buildDir.isEmpty(), return GenerateCompilationDbResult(QString(),
QCoreApplication::translate("ClangUtils", "Could not retrieve build directory.")));
QDir dir(buildDir.toString());
if (!dir.exists())
dir.mkpath(dir.path());
QFile compileCommandsFile(buildDir.toString() + "/compile_commands.json");
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
if (!fileOpened)
return;
if (!fileOpened) {
return GenerateCompilationDbResult(QString(),
QCoreApplication::translate("ClangUtils", "Could not create \"%1\": %2")
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString()));
}
compileCommandsFile.write("[");
for (ProjectPart::Ptr projectPart : projectInfo.projectParts()) {
@@ -412,6 +416,7 @@ void generateCompilationDB(CppTools::ProjectInfo projectInfo)
compileCommandsFile.write("\n]");
compileCommandsFile.close();
return GenerateCompilationDbResult(compileCommandsFile.fileName(), QString());
}
QString currentCppEditorDocumentFilePath()

View File

@@ -70,7 +70,19 @@ QString diagnosticCategoryPrefixRemoved(const QString &text);
::Utils::CodeModelIcon::Type iconTypeForToken(const ClangBackEnd::TokenInfoContainer &token);
void generateCompilationDB(CppTools::ProjectInfo projectInfo);
class GenerateCompilationDbResult
{
public:
GenerateCompilationDbResult() = default;
GenerateCompilationDbResult(const QString &filePath, const QString &error)
: filePath(filePath), error(error)
{}
QString filePath;
QString error;
};
GenerateCompilationDbResult generateCompilationDB(CppTools::ProjectInfo projectInfo);
class DiagnosticTextInfo
{