2013-12-10 14:37:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangcodemodelplugin.h"
|
2015-01-16 15:43:42 +01:00
|
|
|
|
2015-11-30 09:43:50 +01:00
|
|
|
#include "clangconstants.h"
|
2016-02-26 17:50:38 +01:00
|
|
|
#include "clangprojectsettingswidget.h"
|
2018-07-10 15:53:51 +02:00
|
|
|
#include "clangutils.h"
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#ifdef WITH_TESTS
|
2017-02-28 12:01:31 +01:00
|
|
|
# include "test/clangbatchfileprocessor.h"
|
2015-05-08 15:48:17 +02:00
|
|
|
# include "test/clangcodecompletion_test.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <projectexplorer/buildconfiguration.h>
|
2016-02-26 17:50:38 +01:00
|
|
|
#include <projectexplorer/projectpanelfactory.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <projectexplorer/target.h>
|
2018-04-12 14:49:07 +02:00
|
|
|
#include <projectexplorer/taskhub.h>
|
2016-02-26 17:50:38 +01:00
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
#include <texteditor/textmark.h>
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
static void addProjectPanelWidget()
|
2016-02-26 17:50:38 +01:00
|
|
|
{
|
|
|
|
|
auto panelFactory = new ProjectExplorer::ProjectPanelFactory();
|
|
|
|
|
panelFactory->setPriority(60);
|
|
|
|
|
panelFactory->setDisplayName(ClangProjectSettingsWidget::tr("Clang Code Model"));
|
2016-07-22 15:53:01 +02:00
|
|
|
panelFactory->setCreateWidgetFunction([](ProjectExplorer::Project *project) {
|
|
|
|
|
return new ClangProjectSettingsWidget(project);
|
|
|
|
|
});
|
2016-02-26 17:50:38 +01:00
|
|
|
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
void ClangCodeModelPlugin::generateCompilationDB() {
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
|
|
|
|
|
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
|
|
|
|
|
if (!project)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_generatorWatcher.setFuture(QtConcurrent::run(
|
|
|
|
|
&Utils::generateCompilationDB,
|
|
|
|
|
project->projectDirectory(),
|
|
|
|
|
CppModelManager::instance()->projectInfo(project)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isDBGenerationEnabled(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
if (!project)
|
|
|
|
|
return false;
|
|
|
|
|
ProjectInfo projectInfo = CppModelManager::instance()->projectInfo(project);
|
|
|
|
|
return projectInfo.isValid() && !projectInfo.projectParts().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangCodeModelPlugin::~ClangCodeModelPlugin()
|
|
|
|
|
{
|
|
|
|
|
m_generatorWatcher.waitForFinished();
|
|
|
|
|
}
|
2015-11-27 16:02:38 +01:00
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
bool ClangCodeModelPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
|
|
|
|
{
|
2016-05-10 15:10:15 +02:00
|
|
|
Q_UNUSED(arguments);
|
|
|
|
|
Q_UNUSED(errorMessage);
|
|
|
|
|
|
2018-04-12 14:49:07 +02:00
|
|
|
ProjectExplorer::TaskHub::addCategory(Constants::TASK_CATEGORY_DIAGNOSTICS,
|
|
|
|
|
tr("Clang Code Model"));
|
|
|
|
|
|
2016-05-10 15:10:15 +02:00
|
|
|
connect(ProjectExplorer::ProjectExplorerPlugin::instance(),
|
|
|
|
|
&ProjectExplorer::ProjectExplorerPlugin::finishedInitialization,
|
|
|
|
|
this,
|
|
|
|
|
&ClangCodeModelPlugin::maybeHandleBatchFileAndExit);
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2015-12-15 18:09:45 +01:00
|
|
|
CppTools::CppModelManager::instance()->activateClangCodeModel(&m_modelManagerSupportProvider);
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2016-02-26 17:50:38 +01:00
|
|
|
addProjectPanelWidget();
|
2015-09-21 16:09:02 +02:00
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
createCompilationDBButton();
|
|
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
void ClangCodeModelPlugin::createCompilationDBButton()
|
|
|
|
|
{
|
|
|
|
|
Core::ActionContainer *mbuild =
|
|
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
|
|
|
|
|
// generate compile_commands.json
|
|
|
|
|
m_generateCompilationDBAction = new ::Utils::ParameterAction(
|
2018-08-20 09:21:19 +02:00
|
|
|
tr("Generate Compilation Database"),
|
|
|
|
|
tr("Generate Compilation Database for \"%1\""),
|
2018-07-10 15:53:51 +02:00
|
|
|
::Utils::ParameterAction::AlwaysEnabled, this);
|
|
|
|
|
|
|
|
|
|
ProjectExplorer::Project *startupProject = ProjectExplorer::SessionManager::startupProject();
|
|
|
|
|
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(startupProject));
|
|
|
|
|
if (startupProject)
|
|
|
|
|
m_generateCompilationDBAction->setParameter(startupProject->displayName());
|
|
|
|
|
|
|
|
|
|
Core::Command *command = Core::ActionManager::registerAction(m_generateCompilationDBAction,
|
|
|
|
|
Constants::GENERATE_COMPILATION_DB);
|
|
|
|
|
command->setAttribute(Core::Command::CA_UpdateText);
|
|
|
|
|
command->setDescription(m_generateCompilationDBAction->text());
|
|
|
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
|
|
|
|
|
|
|
|
|
|
connect(&m_generatorWatcher, &QFutureWatcher<void>::finished, this, [this] () {
|
|
|
|
|
m_generateCompilationDBAction->setEnabled(
|
|
|
|
|
isDBGenerationEnabled(ProjectExplorer::SessionManager::startupProject()));
|
|
|
|
|
});
|
|
|
|
|
connect(m_generateCompilationDBAction, &QAction::triggered, this, [this] {
|
|
|
|
|
if (!m_generateCompilationDBAction->isEnabled())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_generateCompilationDBAction->setEnabled(false);
|
|
|
|
|
generateCompilationDB();
|
|
|
|
|
});
|
|
|
|
|
connect(CppTools::CppModelManager::instance(), &CppTools::CppModelManager::projectPartsUpdated,
|
|
|
|
|
this, [this](ProjectExplorer::Project *project) {
|
|
|
|
|
if (project != ProjectExplorer::SessionManager::startupProject())
|
|
|
|
|
return;
|
|
|
|
|
m_generateCompilationDBAction->setParameter(project->displayName());
|
|
|
|
|
if (!m_generatorWatcher.isRunning())
|
|
|
|
|
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(project));
|
|
|
|
|
});
|
|
|
|
|
connect(ProjectExplorer::SessionManager::instance(),
|
|
|
|
|
&ProjectExplorer::SessionManager::startupProjectChanged,
|
|
|
|
|
this,
|
|
|
|
|
[this](ProjectExplorer::Project *project) {
|
2018-08-14 08:07:01 +02:00
|
|
|
m_generateCompilationDBAction->setParameter(project ? project->displayName() : "");
|
2018-07-10 15:53:51 +02:00
|
|
|
if (!m_generatorWatcher.isRunning())
|
|
|
|
|
m_generateCompilationDBAction->setEnabled(isDBGenerationEnabled(project));
|
|
|
|
|
});
|
|
|
|
|
connect(ProjectExplorer::SessionManager::instance(),
|
|
|
|
|
&ProjectExplorer::SessionManager::projectDisplayNameChanged,
|
|
|
|
|
this,
|
|
|
|
|
[this](ProjectExplorer::Project *project) {
|
|
|
|
|
if (project != ProjectExplorer::SessionManager::startupProject())
|
|
|
|
|
return;
|
|
|
|
|
m_generateCompilationDBAction->setParameter(project->displayName());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
void ClangCodeModelPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 15:10:15 +02:00
|
|
|
// For e.g. creation of profile-guided optimization builds.
|
|
|
|
|
void ClangCodeModelPlugin::maybeHandleBatchFileAndExit() const
|
|
|
|
|
{
|
2017-02-27 16:08:04 +01:00
|
|
|
#ifdef WITH_TESTS
|
2016-05-10 15:10:15 +02:00
|
|
|
const QString batchFilePath = QString::fromLocal8Bit(qgetenv("QTC_CLANG_BATCH"));
|
|
|
|
|
if (!batchFilePath.isEmpty() && QTC_GUARD(QFileInfo::exists(batchFilePath))) {
|
|
|
|
|
const bool runSucceeded = runClangBatchFile(batchFilePath);
|
|
|
|
|
QCoreApplication::exit(!runSucceeded);
|
|
|
|
|
}
|
2017-02-27 16:08:04 +01:00
|
|
|
#endif
|
2016-05-10 15:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#ifdef WITH_TESTS
|
|
|
|
|
QList<QObject *> ClangCodeModelPlugin::createTestObjects() const
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
new Tests::ClangCodeCompletionTest,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Clang
|