2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2018-01-17 15:08:30 +01:00
|
|
|
|
|
|
|
|
#include "clangtoolrunner.h"
|
|
|
|
|
|
2023-01-11 23:48:53 +01:00
|
|
|
#include "clangtoolstr.h"
|
2023-01-10 17:51:34 +01:00
|
|
|
#include "clangtoolsutils.h"
|
|
|
|
|
|
2023-01-11 15:32:46 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2023-01-10 17:51:34 +01:00
|
|
|
#include <cppeditor/clangdiagnosticconfigsmodel.h>
|
2023-01-11 15:32:46 +01:00
|
|
|
#include <cppeditor/cpptoolsreuse.h>
|
2023-01-10 17:51:34 +01:00
|
|
|
|
2018-01-17 15:08:30 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2021-05-05 18:21:22 +02:00
|
|
|
#include <utils/qtcprocess.h>
|
2018-01-17 15:08:30 +01:00
|
|
|
#include <utils/temporaryfile.h>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
2018-10-12 09:33:30 +03:00
|
|
|
static Q_LOGGING_CATEGORY(LOG, "qtc.clangtools.runner", QtWarningMsg)
|
2018-01-17 15:08:30 +01:00
|
|
|
|
2023-01-10 17:51:34 +01:00
|
|
|
using namespace CppEditor;
|
2021-04-30 17:10:00 +02:00
|
|
|
using namespace Utils;
|
2023-01-11 23:48:53 +01:00
|
|
|
using namespace Tasking;
|
2021-04-30 17:10:00 +02:00
|
|
|
|
2018-01-17 15:08:30 +01:00
|
|
|
namespace ClangTools {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2023-01-11 15:32:46 +01:00
|
|
|
AnalyzeUnit::AnalyzeUnit(const FileInfo &fileInfo,
|
|
|
|
|
const FilePath &clangIncludeDir,
|
|
|
|
|
const QString &clangVersion)
|
|
|
|
|
{
|
|
|
|
|
const FilePath actualClangIncludeDir = Core::ICore::clangIncludeDirectory(
|
|
|
|
|
clangVersion, clangIncludeDir);
|
|
|
|
|
CompilerOptionsBuilder optionsBuilder(*fileInfo.projectPart,
|
|
|
|
|
UseSystemHeader::No,
|
|
|
|
|
UseTweakedHeaderPaths::Tools,
|
|
|
|
|
UseLanguageDefines::No,
|
|
|
|
|
UseBuildSystemWarnings::No,
|
|
|
|
|
actualClangIncludeDir);
|
|
|
|
|
file = fileInfo.file.toString();
|
|
|
|
|
arguments = extraClangToolsPrependOptions();
|
|
|
|
|
arguments.append(optionsBuilder.build(fileInfo.kind, CppEditor::getPchUsage()));
|
|
|
|
|
arguments.append(extraClangToolsAppendOptions());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 17:51:34 +01:00
|
|
|
static bool isClMode(const QStringList &options)
|
|
|
|
|
{
|
|
|
|
|
return options.contains("--driver-mode=cl");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList checksArguments(ClangToolType tool,
|
|
|
|
|
const ClangDiagnosticConfig &diagnosticConfig)
|
|
|
|
|
{
|
|
|
|
|
if (tool == ClangToolType::Tidy) {
|
|
|
|
|
const ClangDiagnosticConfig::TidyMode tidyMode = diagnosticConfig.clangTidyMode();
|
|
|
|
|
// The argument "-config={}" stops stating/evaluating the .clang-tidy file.
|
|
|
|
|
if (tidyMode == ClangDiagnosticConfig::TidyMode::UseDefaultChecks)
|
|
|
|
|
return {"-config={}", "-checks=-clang-diagnostic-*"};
|
|
|
|
|
if (tidyMode == ClangDiagnosticConfig::TidyMode::UseCustomChecks)
|
|
|
|
|
return {"-config=" + diagnosticConfig.clangTidyChecksAsJson()};
|
|
|
|
|
return {"--warnings-as-errors=-*", "-checks=-clang-diagnostic-*"};
|
|
|
|
|
}
|
|
|
|
|
const QString clazyChecks = diagnosticConfig.checks(ClangToolType::Clazy);
|
|
|
|
|
if (!clazyChecks.isEmpty())
|
|
|
|
|
return {"-checks=" + diagnosticConfig.checks(ClangToolType::Clazy)};
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList clangArguments(const ClangDiagnosticConfig &diagnosticConfig,
|
|
|
|
|
const QStringList &baseOptions)
|
|
|
|
|
{
|
|
|
|
|
QStringList arguments;
|
|
|
|
|
arguments << ClangDiagnosticConfigsModel::globalDiagnosticOptions()
|
|
|
|
|
<< (isClMode(baseOptions) ? clangArgsForCl(diagnosticConfig.clangOptions())
|
|
|
|
|
: diagnosticConfig.clangOptions())
|
|
|
|
|
<< baseOptions;
|
|
|
|
|
|
|
|
|
|
if (LOG().isDebugEnabled())
|
|
|
|
|
arguments << QLatin1String("-v");
|
|
|
|
|
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 07:12:33 +02:00
|
|
|
static QString createOutputFilePath(const FilePath &dirPath, const QString &fileToAnalyze)
|
2019-08-29 14:25:49 +02:00
|
|
|
{
|
|
|
|
|
const QString fileName = QFileInfo(fileToAnalyze).fileName();
|
2021-07-02 07:12:33 +02:00
|
|
|
const FilePath fileTemplate = dirPath.pathAppended("report-" + fileName + "-XXXXXX");
|
2019-08-29 14:25:49 +02:00
|
|
|
|
2021-07-02 07:12:33 +02:00
|
|
|
TemporaryFile temporaryFile("clangtools");
|
2019-08-29 14:25:49 +02:00
|
|
|
temporaryFile.setAutoRemove(false);
|
2021-07-02 07:12:33 +02:00
|
|
|
temporaryFile.setFileTemplate(fileTemplate.path());
|
2019-08-29 14:25:49 +02:00
|
|
|
if (temporaryFile.open()) {
|
|
|
|
|
temporaryFile.close();
|
|
|
|
|
return temporaryFile.fileName();
|
|
|
|
|
}
|
2023-01-10 17:51:34 +01:00
|
|
|
return {};
|
2019-08-29 14:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-11 23:48:53 +01:00
|
|
|
TaskItem clangToolTask(const AnalyzeInputData &input,
|
|
|
|
|
const AnalyzeSetupHandler &setupHandler,
|
|
|
|
|
const AnalyzeOutputHandler &outputHandler)
|
2018-01-17 15:08:30 +01:00
|
|
|
{
|
2023-01-11 23:48:53 +01:00
|
|
|
struct ClangToolStorage {
|
|
|
|
|
QString name;
|
|
|
|
|
Utils::FilePath executable;
|
|
|
|
|
QString outputFilePath;
|
|
|
|
|
};
|
|
|
|
|
const TreeStorage<ClangToolStorage> storage;
|
|
|
|
|
|
|
|
|
|
const auto mainToolArguments = [=](const ClangToolStorage *data)
|
|
|
|
|
{
|
|
|
|
|
QStringList result;
|
|
|
|
|
result << "-export-fixes=" + data->outputFilePath;
|
|
|
|
|
if (!input.overlayFilePath.isEmpty() && isVFSOverlaySupported(data->executable))
|
|
|
|
|
result << "--vfsoverlay=" + input.overlayFilePath;
|
|
|
|
|
result << QDir::toNativeSeparators(input.unit.file);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto onGroupSetup = [=] {
|
|
|
|
|
if (setupHandler && !setupHandler())
|
2023-01-20 10:44:21 +01:00
|
|
|
return TaskAction::StopWithError;
|
2023-01-11 23:48:53 +01:00
|
|
|
|
|
|
|
|
ClangToolStorage *data = storage.activeStorage();
|
|
|
|
|
data->name = clangToolName(input.tool);
|
|
|
|
|
data->executable = toolExecutable(input.tool);
|
|
|
|
|
if (!data->executable.isExecutableFile()) {
|
|
|
|
|
qWarning() << "Can't start:" << data->executable << "as" << data->name;
|
2023-01-20 10:44:21 +01:00
|
|
|
return TaskAction::StopWithError;
|
2023-01-11 23:48:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTC_CHECK(!input.unit.arguments.contains(QLatin1String("-o")));
|
|
|
|
|
QTC_CHECK(!input.unit.arguments.contains(input.unit.file));
|
2023-01-20 10:44:21 +01:00
|
|
|
QTC_ASSERT(FilePath::fromString(input.unit.file).exists(), return TaskAction::StopWithError);
|
2023-01-11 23:48:53 +01:00
|
|
|
data->outputFilePath = createOutputFilePath(input.outputDirPath, input.unit.file);
|
2023-01-20 10:44:21 +01:00
|
|
|
QTC_ASSERT(!data->outputFilePath.isEmpty(), return TaskAction::StopWithError);
|
2023-01-11 23:48:53 +01:00
|
|
|
|
2023-01-20 10:44:21 +01:00
|
|
|
return TaskAction::Continue;
|
2023-01-11 23:48:53 +01:00
|
|
|
};
|
|
|
|
|
const auto onProcessSetup = [=](QtcProcess &process) {
|
|
|
|
|
process.setEnvironment(input.environment);
|
|
|
|
|
process.setUseCtrlCStub(true);
|
|
|
|
|
process.setWorkingDirectory(input.outputDirPath); // Current clang-cl puts log file into working dir.
|
|
|
|
|
|
|
|
|
|
const ClangToolStorage *data = storage.activeStorage();
|
|
|
|
|
|
|
|
|
|
const QStringList args = checksArguments(input.tool, input.config)
|
|
|
|
|
+ mainToolArguments(data)
|
|
|
|
|
+ QStringList{"--"}
|
|
|
|
|
+ clangArguments(input.config, input.unit.arguments);
|
|
|
|
|
const CommandLine commandLine = {data->executable, args};
|
|
|
|
|
|
|
|
|
|
qCDebug(LOG).noquote() << "Starting" << commandLine.toUserOutput();
|
|
|
|
|
process.setCommand(commandLine);
|
|
|
|
|
};
|
|
|
|
|
const auto onProcessDone = [=](const QtcProcess &process) {
|
|
|
|
|
qCDebug(LOG).noquote() << "Output:\n" << process.cleanedStdOut();
|
|
|
|
|
if (!outputHandler)
|
|
|
|
|
return;
|
|
|
|
|
const ClangToolStorage *data = storage.activeStorage();
|
2023-01-12 02:12:33 +01:00
|
|
|
outputHandler({true, input.unit.file, data->outputFilePath, input.tool});
|
2023-01-11 23:48:53 +01:00
|
|
|
};
|
|
|
|
|
const auto onProcessError = [=](const QtcProcess &process) {
|
|
|
|
|
if (!outputHandler)
|
|
|
|
|
return;
|
|
|
|
|
const QString details = Tr::tr("Command line: %1\nProcess Error: %2\nOutput:\n%3")
|
|
|
|
|
.arg(process.commandLine().toUserOutput())
|
|
|
|
|
.arg(process.error())
|
|
|
|
|
.arg(process.cleanedStdOut());
|
|
|
|
|
const ClangToolStorage *data = storage.activeStorage();
|
|
|
|
|
QString message;
|
|
|
|
|
if (process.result() == ProcessResult::StartFailed)
|
|
|
|
|
message = Tr::tr("An error occurred with the %1 process.").arg(data->name);
|
|
|
|
|
else if (process.result() == ProcessResult::FinishedWithError)
|
|
|
|
|
message = Tr::tr("%1 finished with exit code: %2.").arg(data->name).arg(process.exitCode());
|
|
|
|
|
else
|
|
|
|
|
message = Tr::tr("%1 crashed.").arg(data->name);
|
2023-01-12 02:12:33 +01:00
|
|
|
outputHandler({false, input.unit.file, data->outputFilePath, input.tool, message, details});
|
2023-01-11 23:48:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Group group {
|
|
|
|
|
Storage(storage),
|
|
|
|
|
DynamicSetup(onGroupSetup),
|
|
|
|
|
Group {
|
|
|
|
|
optional,
|
|
|
|
|
Process(onProcessSetup, onProcessDone, onProcessError)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return group;
|
2018-01-17 15:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangTools
|