Skip to content

Commit 773663f

Browse files
authored
cleaned up CppCheck usage in GUI code (danmar#7025)
1 parent 1d490d9 commit 773663f

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

gui/checkthread.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <list>
3737
#include <set>
3838
#include <string>
39+
#include <utility>
3940
#include <vector>
4041

4142
#include <QByteArray>
@@ -61,7 +62,7 @@ static QString unquote(QString s) {
6162
}
6263

6364
// NOLINTNEXTLINE(performance-unnecessary-value-param) - used as callback so we need to preserve the signature
64-
int CheckThread::executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output) // cppcheck-suppress passedByValue
65+
int CheckThread::executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output) // cppcheck-suppress [passedByValue,passedByValueCallback]
6566
{
6667
output.clear();
6768

@@ -106,15 +107,13 @@ int CheckThread::executeCommand(std::string exe, std::vector<std::string> args,
106107

107108

108109
CheckThread::CheckThread(ThreadResult &result) :
109-
mResult(result),
110-
mCppcheck(result, true, executeCommand)
110+
mResult(result)
111111
{}
112112

113-
void CheckThread::check(const Settings &settings)
113+
void CheckThread::setSettings(const Settings &settings)
114114
{
115115
mFiles.clear();
116-
mCppcheck.settings() = settings;
117-
start();
116+
mSettings = settings; // this is a copy
118117
}
119118

120119
void CheckThread::analyseWholeProgram(const QStringList &files, const std::string& ctuInfo)
@@ -130,6 +129,9 @@ void CheckThread::run()
130129
{
131130
mState = Running;
132131

132+
CppCheck cppcheck(mResult, true, executeCommand);
133+
cppcheck.settings() = std::move(mSettings);
134+
133135
if (!mFiles.isEmpty() || mAnalyseWholeProgram) {
134136
mAnalyseWholeProgram = false;
135137
std::string ctuInfo;
@@ -139,7 +141,7 @@ void CheckThread::run()
139141
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
140142
return FileWithDetails{file.toStdString(), 0};
141143
});
142-
mCppcheck.analyseWholeProgram(mCppcheck.settings().buildDir, files2, {}, ctuInfo);
144+
cppcheck.analyseWholeProgram(cppcheck.settings().buildDir, files2, {}, ctuInfo);
143145
mFiles.clear();
144146
emit done();
145147
return;
@@ -148,8 +150,8 @@ void CheckThread::run()
148150
QString file = mResult.getNextFile();
149151
while (!file.isEmpty() && mState == Running) {
150152
qDebug() << "Checking file" << file;
151-
mCppcheck.check(FileWithDetails(file.toStdString()));
152-
runAddonsAndTools(nullptr, file);
153+
cppcheck.check(FileWithDetails(file.toStdString()));
154+
runAddonsAndTools(cppcheck.settings(), nullptr, file);
153155
emit fileChecked(file);
154156

155157
if (mState == Running)
@@ -161,8 +163,8 @@ void CheckThread::run()
161163
while (fileSettings && mState == Running) {
162164
file = QString::fromStdString(fileSettings->filename());
163165
qDebug() << "Checking file" << file;
164-
mCppcheck.check(*fileSettings);
165-
runAddonsAndTools(fileSettings, QString::fromStdString(fileSettings->filename()));
166+
cppcheck.check(*fileSettings);
167+
runAddonsAndTools(cppcheck.settings(), fileSettings, QString::fromStdString(fileSettings->filename()));
166168
emit fileChecked(file);
167169

168170
if (mState == Running)
@@ -177,7 +179,7 @@ void CheckThread::run()
177179
emit done();
178180
}
179181

180-
void CheckThread::runAddonsAndTools(const FileSettings *fileSettings, const QString &fileName)
182+
void CheckThread::runAddonsAndTools(const Settings& settings, const FileSettings *fileSettings, const QString &fileName)
181183
{
182184
for (const QString& addon : mAddonsAndTools) {
183185
if (addon == CLANG_ANALYZER || addon == CLANG_TIDY) {
@@ -229,15 +231,15 @@ void CheckThread::runAddonsAndTools(const FileSettings *fileSettings, const QStr
229231
args << ("-std=" + QString::fromStdString(fileSettings->standard));
230232
else {
231233
// TODO: pass C or C++ standard based on file type
232-
const std::string std = mCppcheck.settings().standards.getCPP();
234+
const std::string std = settings.standards.getCPP();
233235
if (!std.empty()) {
234236
args << ("-std=" + QString::fromStdString(std));
235237
}
236238
}
237239

238240
QString analyzerInfoFile;
239241

240-
const std::string &buildDir = mCppcheck.settings().buildDir;
242+
const std::string &buildDir = settings.buildDir;
241243
if (!buildDir.empty()) {
242244
analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(buildDir, fileSettings->filename(), fileSettings->cfg));
243245

gui/checkthread.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#ifndef CHECKTHREAD_H
2121
#define CHECKTHREAD_H
2222

23-
#include "cppcheck.h"
23+
#include "settings.h"
2424
#include "suppressions.h"
2525

2626
#include <atomic>
@@ -34,7 +34,6 @@
3434
#include <QStringList>
3535
#include <QThread>
3636

37-
class Settings;
3837
class ThreadResult;
3938
struct FileSettings;
4039

@@ -55,7 +54,7 @@ class CheckThread : public QThread {
5554
*
5655
* @param settings settings for cppcheck
5756
*/
58-
void check(const Settings &settings);
57+
void setSettings(const Settings &settings);
5958

6059
/**
6160
* @brief Run whole program analysis
@@ -130,13 +129,11 @@ class CheckThread : public QThread {
130129
std::atomic<State> mState{Ready};
131130

132131
ThreadResult &mResult;
133-
/**
134-
* @brief Cppcheck itself
135-
*/
136-
CppCheck mCppcheck;
132+
133+
Settings mSettings;
137134

138135
private:
139-
void runAddonsAndTools(const FileSettings *fileSettings, const QString &fileName);
136+
void runAddonsAndTools(const Settings& settings, const FileSettings *fileSettings, const QString &fileName);
140137

141138
void parseClangErrors(const QString &tool, const QString &file0, QString err);
142139

gui/threadhandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ void ThreadHandler::check(const Settings &settings)
109109
mThreads[i]->setAddonsAndTools(addonsAndTools);
110110
mThreads[i]->setSuppressions(mSuppressions);
111111
mThreads[i]->setClangIncludePaths(mClangIncludePaths);
112-
mThreads[i]->check(settings);
112+
mThreads[i]->setSettings(settings);
113+
mThreads[i]->start();
113114
}
114115

115116
// Date and time when checking starts..

gui/threadhandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "threadresult.h"
2525

2626
#include <set>
27+
#include <string>
2728

2829
#include <QDateTime>
2930
#include <QElapsedTimer>

0 commit comments

Comments
 (0)