2014-09-25 11:11:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2014 Digia Plc
|
|
|
|
|
** All rights reserved.
|
|
|
|
|
** For any questions to Digia, please use contact form at http://qt.digia.com <http://qt.digia.com/>
|
|
|
|
|
**
|
|
|
|
|
** This file is part of the Qt Enterprise LicenseChecker Add-on.
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Enterprise licenses may use this file in
|
|
|
|
|
** accordance with the Qt Enterprise License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please use
|
|
|
|
|
** contact form at http://qt.digia.com <http://qt.digia.com/>
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangstaticanalyzerruncontrol.h"
|
|
|
|
|
|
|
|
|
|
#include "clangstaticanalyzerlogfilereader.h"
|
|
|
|
|
#include "clangstaticanalyzerrunner.h"
|
|
|
|
|
#include "clangstaticanalyzersettings.h"
|
|
|
|
|
#include "clangstaticanalyzerutils.h"
|
|
|
|
|
|
|
|
|
|
#include <analyzerbase/analyzermanager.h>
|
|
|
|
|
|
|
|
|
|
#include <clangcodemodel/clangutils.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/progressmanager/futureprogress.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
|
|
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2014-10-16 14:07:47 +02:00
|
|
|
#include <cpptools/cppprojects.h>
|
2014-09-25 11:11:58 +02:00
|
|
|
#include <cpptools/cppprojectfile.h>
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/project.h>
|
2014-10-24 10:21:32 +02:00
|
|
|
#include <projectexplorer/target.h>
|
2014-09-25 11:11:58 +02:00
|
|
|
|
2014-11-07 12:15:13 +01:00
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
|
2014-09-25 11:11:58 +02:00
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
|
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
2014-10-20 13:08:49 +02:00
|
|
|
static Q_LOGGING_CATEGORY(LOG, "qtc.clangstaticanalyzer.runcontrol")
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
namespace ClangStaticAnalyzer {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
ClangStaticAnalyzerRunControl::ClangStaticAnalyzerRunControl(
|
2014-11-05 13:28:44 +01:00
|
|
|
const Analyzer::AnalyzerStartParameters &startParams,
|
|
|
|
|
ProjectExplorer::RunConfiguration *runConfiguration,
|
|
|
|
|
const ProjectInfo &projectInfo)
|
2014-09-25 11:11:58 +02:00
|
|
|
: AnalyzerRunControl(startParams, runConfiguration)
|
2014-11-05 13:28:44 +01:00
|
|
|
, m_projectInfo(projectInfo)
|
2014-09-25 11:11:58 +02:00
|
|
|
, m_initialFilesToProcessSize(0)
|
2014-10-30 17:35:52 +01:00
|
|
|
, m_filesAnalyzed(0)
|
|
|
|
|
, m_filesNotAnalyzed(0)
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 11:57:09 +01:00
|
|
|
// Removes (1) filePath (2) -o <somePath>
|
|
|
|
|
static QStringList tweakedArguments(const QString &filePath, const QStringList &arguments)
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
2014-10-30 11:57:09 +01:00
|
|
|
QStringList newArguments;
|
|
|
|
|
|
|
|
|
|
bool skip = false;
|
|
|
|
|
foreach (const QString &argument, arguments) {
|
|
|
|
|
if (skip) {
|
|
|
|
|
skip = false;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (argument == QLatin1String("-o")) {
|
|
|
|
|
skip = true;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (argument == filePath) {
|
|
|
|
|
continue; // TODO: Let it in?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newArguments << argument;
|
|
|
|
|
}
|
|
|
|
|
QTC_CHECK(skip == false);
|
|
|
|
|
|
|
|
|
|
return newArguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList argumentsFromProjectPart(const CppTools::ProjectPart::Ptr &projectPart,
|
|
|
|
|
CppTools::ProjectFile::Kind fileKind)
|
|
|
|
|
{
|
|
|
|
|
QStringList result;
|
|
|
|
|
|
|
|
|
|
const bool objcExt = projectPart->languageExtensions & ProjectPart::ObjectiveCExtensions;
|
|
|
|
|
result += CppTools::CompilerOptionsBuilder::createLanguageOption(fileKind, objcExt);
|
|
|
|
|
result += CppTools::CompilerOptionsBuilder::createOptionsForLanguage(
|
|
|
|
|
projectPart->languageVersion,
|
|
|
|
|
projectPart->languageExtensions);
|
|
|
|
|
result += CppTools::CompilerOptionsBuilder::createDefineOptions(projectPart->toolchainDefines);
|
|
|
|
|
result += CppTools::CompilerOptionsBuilder::createDefineOptions(projectPart->projectDefines);
|
|
|
|
|
result += CppTools::CompilerOptionsBuilder::createHeaderPathOptions(projectPart->headerPaths);
|
|
|
|
|
result += QLatin1String("-fPIC"); // TODO: Remove?
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<ClangStaticAnalyzerRunControl::AnalyzeUnit> unitsToAnalyzeFromCompilerCallData(
|
|
|
|
|
const ProjectInfo::CompilerCallData &compilerCallData)
|
|
|
|
|
{
|
|
|
|
|
typedef ClangStaticAnalyzerRunControl::AnalyzeUnit AnalyzeUnit;
|
|
|
|
|
qCDebug(LOG) << "Taking arguments for analyzing from CompilerCallData.";
|
|
|
|
|
|
|
|
|
|
QList<ClangStaticAnalyzerRunControl::AnalyzeUnit> unitsToAnalyze;
|
|
|
|
|
|
|
|
|
|
QHashIterator<QString, QList<QStringList> > it(compilerCallData);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
const QString file = it.key();
|
|
|
|
|
const QList<QStringList> compilerCalls = it.value();
|
|
|
|
|
foreach (const QStringList &options, compilerCalls) {
|
|
|
|
|
const QStringList arguments = tweakedArguments(file, options);
|
|
|
|
|
unitsToAnalyze << AnalyzeUnit(file, arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unitsToAnalyze;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<ClangStaticAnalyzerRunControl::AnalyzeUnit> unitsToAnalyzeFromProjectParts(
|
|
|
|
|
const QList<ProjectPart::Ptr> projectParts)
|
|
|
|
|
{
|
|
|
|
|
typedef ClangStaticAnalyzerRunControl::AnalyzeUnit AnalyzeUnit;
|
|
|
|
|
qCDebug(LOG) << "Taking arguments for analyzing from ProjectParts.";
|
|
|
|
|
|
|
|
|
|
QList<ClangStaticAnalyzerRunControl::AnalyzeUnit> unitsToAnalyze;
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
foreach (const ProjectPart::Ptr &projectPart, projectParts) {
|
|
|
|
|
foreach (const ProjectFile &file, projectPart->files) {
|
|
|
|
|
if (file.path == CppModelManager::configurationFileName())
|
|
|
|
|
continue;
|
|
|
|
|
QTC_CHECK(file.kind != ProjectFile::Unclassified);
|
2014-10-30 11:57:09 +01:00
|
|
|
if (ProjectFile::isSource(file.kind)) {
|
|
|
|
|
const QStringList arguments = argumentsFromProjectPart(projectPart, file.kind);
|
|
|
|
|
unitsToAnalyze << AnalyzeUnit(file.path, arguments);
|
|
|
|
|
}
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 11:57:09 +01:00
|
|
|
return unitsToAnalyze;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-05 13:28:44 +01:00
|
|
|
static QList<ClangStaticAnalyzerRunControl::AnalyzeUnit> unitsToAnalyze(
|
|
|
|
|
const CppTools::ProjectInfo &projectInfo)
|
2014-10-30 11:57:09 +01:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(projectInfo.isValid(), return QList<ClangStaticAnalyzerRunControl::AnalyzeUnit>());
|
|
|
|
|
|
|
|
|
|
const ProjectInfo::CompilerCallData compilerCallData = projectInfo.compilerCallData();
|
|
|
|
|
if (!compilerCallData.isEmpty())
|
|
|
|
|
return unitsToAnalyzeFromCompilerCallData(compilerCallData);
|
|
|
|
|
return unitsToAnalyzeFromProjectParts(projectInfo.projectParts());
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangStaticAnalyzerRunControl::startEngine()
|
|
|
|
|
{
|
|
|
|
|
emit starting(this);
|
|
|
|
|
|
2014-11-05 13:28:44 +01:00
|
|
|
QTC_ASSERT(m_projectInfo.isValid(), emit finished(); return false);
|
|
|
|
|
const QString projectFile = m_projectInfo.project()->projectFilePath().toString();
|
2014-10-29 10:40:00 +01:00
|
|
|
appendMessage(tr("Running Clang Static Analyzer on %1").arg(projectFile) + QLatin1Char('\n'),
|
|
|
|
|
Utils::NormalMessageFormat);
|
|
|
|
|
|
2014-09-25 11:11:58 +02:00
|
|
|
// Check clang executable
|
|
|
|
|
bool isValidClangExecutable;
|
|
|
|
|
const QString executable = clangExecutableFromSettings(&isValidClangExecutable);
|
|
|
|
|
if (!isValidClangExecutable) {
|
2014-10-29 10:40:00 +01:00
|
|
|
emit appendMessage(tr("Clang Static Analyzer: Invalid executable \"%1\", stop.")
|
|
|
|
|
.arg(executable) + QLatin1Char('\n'),
|
2014-09-25 11:11:58 +02:00
|
|
|
Utils::ErrorMessageFormat);
|
|
|
|
|
emit finished();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_clangExecutable = executable;
|
|
|
|
|
|
|
|
|
|
// Create log dir
|
|
|
|
|
QTemporaryDir temporaryDir(QDir::tempPath() + QLatin1String("/qtc-clangstaticanalyzer-XXXXXX"));
|
|
|
|
|
temporaryDir.setAutoRemove(false);
|
|
|
|
|
if (!temporaryDir.isValid()) {
|
2014-10-29 10:40:00 +01:00
|
|
|
emit appendMessage(tr("Clang Static Analyzer: Failed to create temporary dir, stop.")
|
|
|
|
|
+ QLatin1Char('\n'), Utils::ErrorMessageFormat);
|
2014-09-25 11:11:58 +02:00
|
|
|
emit finished();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_clangLogFileDir = temporaryDir.path();
|
|
|
|
|
|
|
|
|
|
// Collect files
|
2014-11-07 12:15:13 +01:00
|
|
|
QList<AnalyzeUnit> unitsToProcess = unitsToAnalyze(m_projectInfo);
|
|
|
|
|
Utils::sort(unitsToProcess, [](const AnalyzeUnit &a1, const AnalyzeUnit &a2) -> bool {
|
|
|
|
|
return a1.file < a2.file;
|
|
|
|
|
});
|
|
|
|
|
|
2014-10-24 10:24:16 +02:00
|
|
|
qCDebug(LOG) << "Files to process:";
|
2014-11-07 12:15:13 +01:00
|
|
|
foreach (const AnalyzeUnit &fileConfig, unitsToProcess)
|
2014-10-30 11:57:09 +01:00
|
|
|
qCDebug(LOG) << fileConfig.file;
|
2014-11-07 12:15:13 +01:00
|
|
|
m_unitsToProcess = unitsToProcess;
|
2014-10-30 11:57:09 +01:00
|
|
|
m_initialFilesToProcessSize = m_unitsToProcess.count();
|
2014-10-30 17:35:52 +01:00
|
|
|
m_filesAnalyzed = 0;
|
|
|
|
|
m_filesNotAnalyzed = 0;
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
// Set up progress information
|
|
|
|
|
using namespace Core;
|
|
|
|
|
FutureProgress *futureProgress
|
|
|
|
|
= ProgressManager::addTask(m_progress.future(), tr("Analyzing"), "ClangStaticAnalyzer");
|
|
|
|
|
futureProgress->setKeepOnFinish(FutureProgress::HideOnFinish);
|
|
|
|
|
connect(futureProgress, &FutureProgress::canceled,
|
|
|
|
|
this, &ClangStaticAnalyzerRunControl::onProgressCanceled);
|
|
|
|
|
m_progress.setProgressRange(0, m_initialFilesToProcessSize);
|
|
|
|
|
m_progress.reportStarted();
|
|
|
|
|
|
|
|
|
|
// Start process(es)
|
2014-10-28 09:44:26 +01:00
|
|
|
m_runners.clear();
|
2014-09-25 11:11:58 +02:00
|
|
|
const int parallelRuns = ClangStaticAnalyzerSettings::instance()->simultaneousProcesses();
|
|
|
|
|
QTC_ASSERT(parallelRuns >= 1, emit finished(); return false);
|
2014-10-30 11:57:09 +01:00
|
|
|
while (m_runners.size() < parallelRuns && !m_unitsToProcess.isEmpty())
|
2014-09-25 11:11:58 +02:00
|
|
|
analyzeNextFile();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::stopEngine()
|
|
|
|
|
{
|
2014-10-28 09:44:26 +01:00
|
|
|
QSetIterator<ClangStaticAnalyzerRunner *> i(m_runners);
|
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
ClangStaticAnalyzerRunner *runner = i.next();
|
|
|
|
|
QObject::disconnect(runner, 0, this, 0);
|
|
|
|
|
delete runner;
|
|
|
|
|
}
|
|
|
|
|
m_runners.clear();
|
2014-10-30 11:57:09 +01:00
|
|
|
m_unitsToProcess.clear();
|
2014-10-30 17:35:52 +01:00
|
|
|
appendMessage(tr("Clang Static Analyzer stopped by user.") + QLatin1Char('\n'),
|
|
|
|
|
Utils::NormalMessageFormat);
|
|
|
|
|
m_progress.reportFinished();
|
|
|
|
|
emit finished();
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::analyzeNextFile()
|
|
|
|
|
{
|
|
|
|
|
if (m_progress.isFinished())
|
|
|
|
|
return; // The previous call already reported that we are finished.
|
|
|
|
|
|
2014-10-30 11:57:09 +01:00
|
|
|
if (m_unitsToProcess.isEmpty()) {
|
2014-10-28 09:44:26 +01:00
|
|
|
if (m_runners.size() == 0) {
|
2014-10-30 17:35:52 +01:00
|
|
|
appendMessage(tr("Clang Static Analyzer finished: "
|
|
|
|
|
"Processed %1 files successfully, %2 failed.")
|
|
|
|
|
.arg(m_filesAnalyzed)
|
|
|
|
|
.arg(m_filesNotAnalyzed)
|
|
|
|
|
+ QLatin1Char('\n'),
|
2014-10-29 10:40:00 +01:00
|
|
|
Utils::NormalMessageFormat);
|
2014-09-25 11:11:58 +02:00
|
|
|
m_progress.reportFinished();
|
|
|
|
|
emit finished();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 11:57:09 +01:00
|
|
|
const AnalyzeUnit unit = m_unitsToProcess.takeFirst();
|
|
|
|
|
qCDebug(LOG) << "analyzeNextFile:" << unit.file;
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
ClangStaticAnalyzerRunner *runner = createRunner();
|
2014-10-28 09:44:26 +01:00
|
|
|
m_runners.insert(runner);
|
2014-10-30 11:57:09 +01:00
|
|
|
QTC_ASSERT(runner->run(unit.file, unit.arguments), return);
|
|
|
|
|
|
|
|
|
|
appendMessage(tr("Analyzing \"%1\".").arg(unit.file) + QLatin1Char('\n'),
|
2014-10-30 17:35:52 +01:00
|
|
|
Utils::StdOutFormat);
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangStaticAnalyzerRunner *ClangStaticAnalyzerRunControl::createRunner()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_clangExecutable.isEmpty(), return 0);
|
|
|
|
|
QTC_ASSERT(!m_clangLogFileDir.isEmpty(), return 0);
|
|
|
|
|
|
|
|
|
|
ClangStaticAnalyzerRunner *runner
|
|
|
|
|
= new ClangStaticAnalyzerRunner(m_clangExecutable, m_clangLogFileDir, this);
|
|
|
|
|
connect(runner, &ClangStaticAnalyzerRunner::finishedWithSuccess,
|
|
|
|
|
this, &ClangStaticAnalyzerRunControl::onRunnerFinishedWithSuccess);
|
|
|
|
|
connect(runner, &ClangStaticAnalyzerRunner::finishedWithFailure,
|
|
|
|
|
this, &ClangStaticAnalyzerRunControl::onRunnerFinishedWithFailure);
|
|
|
|
|
return runner;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::onRunnerFinishedWithSuccess(const QString &logFilePath)
|
|
|
|
|
{
|
|
|
|
|
qCDebug(LOG) << "onRunnerFinishedWithSuccess:" << logFilePath;
|
|
|
|
|
|
|
|
|
|
QString errorMessage;
|
|
|
|
|
const QList<Diagnostic> diagnostics = LogFileReader::read(logFilePath, &errorMessage);
|
2014-10-30 17:35:52 +01:00
|
|
|
if (!errorMessage.isEmpty()) {
|
2014-09-25 11:11:58 +02:00
|
|
|
qCDebug(LOG) << "onRunnerFinishedWithSuccess: Error reading log file:" << errorMessage;
|
2014-10-30 17:35:52 +01:00
|
|
|
const QString filePath = qobject_cast<ClangStaticAnalyzerRunner *>(sender())->filePath();
|
|
|
|
|
appendMessage(tr("Failed to analyze \"%1\": %2").arg(filePath, errorMessage)
|
|
|
|
|
+ QLatin1Char('\n')
|
|
|
|
|
, Utils::StdErrFormat);
|
|
|
|
|
} else {
|
|
|
|
|
++m_filesAnalyzed;
|
|
|
|
|
if (!diagnostics.isEmpty())
|
|
|
|
|
emit newDiagnosticsAvailable(diagnostics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleFinished();
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::onRunnerFinishedWithFailure(const QString &errorMessage,
|
|
|
|
|
const QString &errorDetails)
|
|
|
|
|
{
|
|
|
|
|
qCDebug(LOG) << "onRunnerFinishedWithFailure:" << errorMessage << errorDetails;
|
2014-10-30 17:35:52 +01:00
|
|
|
|
|
|
|
|
++m_filesNotAnalyzed;
|
|
|
|
|
const QString filePath = qobject_cast<ClangStaticAnalyzerRunner *>(sender())->filePath();
|
|
|
|
|
appendMessage(tr("Failed to analyze \"%1\": %2").arg(filePath, errorMessage)
|
|
|
|
|
+ QLatin1Char('\n')
|
|
|
|
|
, Utils::StdErrFormat);
|
|
|
|
|
appendMessage(errorDetails, Utils::StdErrFormat);
|
|
|
|
|
|
2014-09-25 11:11:58 +02:00
|
|
|
handleFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::handleFinished()
|
|
|
|
|
{
|
2014-10-28 09:44:26 +01:00
|
|
|
m_runners.remove(qobject_cast<ClangStaticAnalyzerRunner *>(sender()));
|
2014-09-25 11:11:58 +02:00
|
|
|
updateProgressValue();
|
|
|
|
|
sender()->deleteLater();
|
|
|
|
|
analyzeNextFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::onProgressCanceled()
|
|
|
|
|
{
|
|
|
|
|
Analyzer::AnalyzerManager::stopTool();
|
|
|
|
|
m_progress.reportCanceled();
|
|
|
|
|
m_progress.reportFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangStaticAnalyzerRunControl::updateProgressValue()
|
|
|
|
|
{
|
2014-10-30 11:57:09 +01:00
|
|
|
m_progress.setProgressValue(m_initialFilesToProcessSize - m_unitsToProcess.size());
|
2014-10-23 15:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-25 11:11:58 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangStaticAnalyzer
|