1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "clangtoolrunner.h"
#include <utils/environment.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/temporaryfile.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QLoggingCategory>
static Q_LOGGING_CATEGORY(LOG, "qtc.clangtools.runner", QtWarningMsg)
using namespace Utils;
namespace ClangTools {
namespace Internal {
static QString generalProcessError(const QString &name)
{
return ClangToolRunner::tr("An error occurred with the %1 process.").arg(name);
}
static QString finishedDueToCrash(const QString &name)
{
return ClangToolRunner::tr("%1 crashed.").arg(name);
}
static QString finishedWithBadExitCode(const QString &name, int exitCode)
{
return ClangToolRunner::tr("%1 finished with exit code: %2.").arg(name).arg(exitCode);
}
ClangToolRunner::ClangToolRunner(QObject *parent)
: QObject(parent)
{}
void ClangToolRunner::init(const FilePath &outputDirPath, const Environment &environment)
{
m_outputDirPath = outputDirPath;
QTC_CHECK(!m_outputDirPath.isEmpty());
m_process.setEnvironment(environment);
m_process.setUseCtrlCStub(true);
m_process.setWorkingDirectory(m_outputDirPath); // Current clang-cl puts log file into working dir.
connect(&m_process, &QtcProcess::done, this, &ClangToolRunner::onProcessDone);
}
QStringList ClangToolRunner::mainToolArguments() const
{
QStringList result;
result << "-export-fixes=" + m_outputFilePath;
if (!m_overlayFilePath.isEmpty() && supportsVFSOverlay())
result << "--vfsoverlay=" + m_overlayFilePath;
result << QDir::toNativeSeparators(m_fileToAnalyze);
return result;
}
bool ClangToolRunner::supportsVFSOverlay() const
{
static QMap<FilePath, bool> vfsCapabilities;
auto it = vfsCapabilities.find(m_executable);
if (it == vfsCapabilities.end()) {
QtcProcess p;
p.setCommand({m_executable, {"--help"}});
p.runBlocking();
it = vfsCapabilities.insert(m_executable, p.allOutput().contains("vfsoverlay"));
}
return it.value();
}
static QString createOutputFilePath(const FilePath &dirPath, const QString &fileToAnalyze)
{
const QString fileName = QFileInfo(fileToAnalyze).fileName();
const FilePath fileTemplate = dirPath.pathAppended("report-" + fileName + "-XXXXXX");
TemporaryFile temporaryFile("clangtools");
temporaryFile.setAutoRemove(false);
temporaryFile.setFileTemplate(fileTemplate.path());
if (temporaryFile.open()) {
temporaryFile.close();
return temporaryFile.fileName();
}
return QString();
}
bool ClangToolRunner::run(const QString &fileToAnalyze, const QStringList &compilerOptions)
{
QTC_ASSERT(!m_executable.isEmpty(), return false);
QTC_CHECK(!compilerOptions.contains(QLatin1String("-o")));
QTC_CHECK(!compilerOptions.contains(fileToAnalyze));
m_fileToAnalyze = fileToAnalyze;
m_outputFilePath = createOutputFilePath(m_outputDirPath, fileToAnalyze);
QTC_ASSERT(!m_outputFilePath.isEmpty(), return false);
m_commandLine = {m_executable, m_argsCreator(compilerOptions)};
qCDebug(LOG).noquote() << "Starting" << m_commandLine.toUserOutput();
m_process.setCommand(m_commandLine);
m_process.start();
return true;
}
void ClangToolRunner::onProcessDone()
{
if (m_process.result() == ProcessResult::StartFailed) {
emit finishedWithFailure(generalProcessError(m_name), commandlineAndOutput());
} else if (m_process.result() == ProcessResult::FinishedWithSuccess) {
qCDebug(LOG).noquote() << "Output:\n" << m_process.cleanedStdOut();
emit finishedWithSuccess(m_fileToAnalyze);
} else if (m_process.result() == ProcessResult::FinishedWithError) {
emit finishedWithFailure(finishedWithBadExitCode(m_name, m_process.exitCode()),
commandlineAndOutput());
} else { // == QProcess::CrashExit
emit finishedWithFailure(finishedDueToCrash(m_name), commandlineAndOutput());
}
}
QString ClangToolRunner::commandlineAndOutput() const
{
return tr("Command line: %1\n"
"Process Error: %2\n"
"Output:\n%3")
.arg(m_commandLine.toUserOutput())
.arg(m_process.error())
.arg(m_process.cleanedStdOut());
}
} // namespace Internal
} // namespace ClangTools
|