Skip to content

Commit 42d3df2

Browse files
authored
moved settings ownership out of CppCheck (danmar#4964)
1 parent 8de9992 commit 42d3df2

File tree

12 files changed

+128
-112
lines changed

12 files changed

+128
-112
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& sup
440440
if (!settings.checkersReportFilename.empty())
441441
std::remove(settings.checkersReportFilename.c_str());
442442

443-
CppCheck cppcheck(supprs, stdLogger, true, executeCommand);
444-
cppcheck.settings() = settings; // this is a copy
443+
CppCheck cppcheck(settings, supprs, stdLogger, true, executeCommand);
445444

446445
unsigned int returnValue = 0;
447446
if (settings.useSingleJob()) {

cli/processexecutor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,12 @@ unsigned int ProcessExecutor::check()
284284
close(pipes[0]);
285285

286286
PipeWriter pipewriter(pipes[1]);
287-
CppCheck fileChecker(mSuppressions, pipewriter, false, mExecuteCommand);
288-
fileChecker.settings() = mSettings;
287+
CppCheck fileChecker(mSettings, mSuppressions, pipewriter, false, mExecuteCommand);
289288
unsigned int resultOfCheck = 0;
290289

291290
if (iFileSettings != mFileSettings.end()) {
292291
resultOfCheck = fileChecker.check(*iFileSettings);
293-
if (fileChecker.settings().clangTidy)
292+
if (mSettings.clangTidy)
294293
fileChecker.analyseClangTidy(*iFileSettings);
295294
} else {
296295
// Read file from a file

cli/threadexecutor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,13 @@ class ThreadData
112112
}
113113

114114
unsigned int check(ErrorLogger &errorLogger, const FileWithDetails *file, const FileSettings *fs) const {
115-
CppCheck fileChecker(mSuppressions, errorLogger, false, mExecuteCommand);
116-
fileChecker.settings() = mSettings; // this is a copy
115+
CppCheck fileChecker(mSettings, mSuppressions, errorLogger, false, mExecuteCommand);
117116

118117
unsigned int result;
119118
if (fs) {
120119
// file settings..
121120
result = fileChecker.check(*fs);
122-
if (fileChecker.settings().clangTidy)
121+
if (mSettings.clangTidy)
123122
fileChecker.analyseClangTidy(*fs);
124123
} else {
125124
// Read file from a file

democlient/democlient.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ class CppcheckExecutor : public ErrorLogger {
6161
CppCheck cppcheck;
6262

6363
public:
64-
CppcheckExecutor()
65-
: ErrorLogger()
66-
, stoptime(std::time(nullptr)+2U)
67-
, cppcheck(supprs, *this, false, nullptr) {
68-
cppcheck.settings().addEnabled("all");
69-
cppcheck.settings().certainty.enable(Certainty::inconclusive);
70-
}
64+
CppcheckExecutor(const Settings& settings)
65+
: stoptime(std::time(nullptr)+2U)
66+
, cppcheck(settings, supprs, *this, false, nullptr)
67+
{}
7168

7269
void run(const char code[]) {
7370
cppcheck.check(FileWithDetails("test.cpp"), code);
@@ -129,7 +126,10 @@ int main()
129126

130127
std::cout << "<html><body>Cppcheck " CPPCHECK_VERSION_STRING "<pre>";
131128

132-
CppcheckExecutor cppcheckExecutor;
129+
Settings s;
130+
s.addEnabled("all");
131+
s.certainty.enable(Certainty::inconclusive);
132+
CppcheckExecutor cppcheckExecutor(s);
133133
cppcheckExecutor.run(code);
134134

135135
std::fclose(logfile);

gui/checkthread.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <list>
3838
#include <set>
3939
#include <string>
40-
#include <utility>
4140
#include <vector>
4241

4342
#include <QByteArray>
@@ -131,8 +130,7 @@ void CheckThread::run()
131130
{
132131
mState = Running;
133132

134-
CppCheck cppcheck(*mSuppressions, mResult, true, executeCommand);
135-
cppcheck.settings() = std::move(mSettings);
133+
CppCheck cppcheck(mSettings, *mSuppressions, mResult, true, executeCommand);
136134

137135
if (!mFiles.isEmpty() || mAnalyseWholeProgram) {
138136
mAnalyseWholeProgram = false;
@@ -141,9 +139,9 @@ void CheckThread::run()
141139
qDebug() << "Whole program analysis";
142140
std::list<FileWithDetails> files2;
143141
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
144-
return FileWithDetails{file.toStdString(), Path::identify(file.toStdString(), cppcheck.settings().cppHeaderProbe), 0};
142+
return FileWithDetails{file.toStdString(), Path::identify(file.toStdString(), mSettings.cppHeaderProbe), 0};
145143
});
146-
cppcheck.analyseWholeProgram(cppcheck.settings().buildDir, files2, {}, ctuInfo);
144+
cppcheck.analyseWholeProgram(mSettings.buildDir, files2, {}, ctuInfo);
147145
mFiles.clear();
148146
emit done();
149147
return;
@@ -153,7 +151,7 @@ void CheckThread::run()
153151
while (!file.isEmpty() && mState == Running) {
154152
qDebug() << "Checking file" << file;
155153
cppcheck.check(FileWithDetails(file.toStdString()));
156-
runAddonsAndTools(cppcheck.settings(), nullptr, file);
154+
runAddonsAndTools(mSettings, nullptr, file);
157155
emit fileChecked(file);
158156

159157
if (mState == Running)
@@ -166,7 +164,7 @@ void CheckThread::run()
166164
file = QString::fromStdString(fileSettings->filename());
167165
qDebug() << "Checking file" << file;
168166
cppcheck.check(*fileSettings);
169-
runAddonsAndTools(cppcheck.settings(), fileSettings, QString::fromStdString(fileSettings->filename()));
167+
runAddonsAndTools(mSettings, fileSettings, QString::fromStdString(fileSettings->filename()));
170168
emit fileChecked(file);
171169

172170
if (mState == Running)

gui/mainwindow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
695695
mUI->mResults, SLOT(debugError(ErrorItem)));
696696

697697
// Create CppCheck instance
698-
CppCheck cppcheck(supprs, result, true, nullptr);
699-
cppcheck.settings() = checkSettings;
698+
CppCheck cppcheck(checkSettings, supprs, result, true, nullptr);
700699

701700
// Check
702701
checkLockDownUI();

lib/cppcheck.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "path.h"
3333
#include "platform.h"
3434
#include "preprocessor.h"
35+
#include "settings.h"
3536
#include "standards.h"
3637
#include "suppressions.h"
3738
#include "timer.h"
@@ -528,11 +529,13 @@ static std::string getDefinesFlags(const std::string &semicolonSeparatedString)
528529
return flags;
529530
}
530531

531-
CppCheck::CppCheck(Suppressions& supprs,
532+
CppCheck::CppCheck(const Settings& settings,
533+
Suppressions& supprs,
532534
ErrorLogger &errorLogger,
533535
bool useGlobalSuppressions,
534536
ExecuteCmdFn executeCommand)
535-
: mSuppressions(supprs)
537+
: mSettings(settings)
538+
, mSuppressions(supprs)
536539
, mLogger(new CppCheckLogger(errorLogger, mSettings, mSuppressions, useGlobalSuppressions))
537540
, mErrorLogger(*mLogger)
538541
, mErrorLoggerDirect(errorLogger)
@@ -783,31 +786,33 @@ unsigned int CppCheck::check(const FileSettings &fs)
783786
if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck)
784787
mUnusedFunctionsCheck.reset(new CheckUnusedFunctions());
785788

786-
// need to pass the externally provided ErrorLogger instead of our internal wrapper
787-
CppCheck temp(mSuppressions, mErrorLoggerDirect, mUseGlobalSuppressions, mExecuteCommand);
788-
temp.mSettings = mSettings;
789-
if (!temp.mSettings.userDefines.empty())
790-
temp.mSettings.userDefines += ';';
789+
Settings tempSettings = mSettings; // this is a copy
790+
if (!tempSettings.userDefines.empty())
791+
tempSettings.userDefines += ';';
791792
if (mSettings.clang)
792-
temp.mSettings.userDefines += fs.defines;
793+
tempSettings.userDefines += fs.defines;
793794
else
794-
temp.mSettings.userDefines += fs.cppcheckDefines();
795-
temp.mSettings.includePaths = fs.includePaths;
796-
temp.mSettings.userUndefs.insert(fs.undefs.cbegin(), fs.undefs.cend());
795+
tempSettings.userDefines += fs.cppcheckDefines();
796+
tempSettings.includePaths = fs.includePaths;
797+
tempSettings.userUndefs.insert(fs.undefs.cbegin(), fs.undefs.cend());
797798
if (fs.standard.find("++") != std::string::npos)
798-
temp.mSettings.standards.setCPP(fs.standard);
799+
tempSettings.standards.setCPP(fs.standard);
799800
else if (!fs.standard.empty())
800-
temp.mSettings.standards.setC(fs.standard);
801+
tempSettings.standards.setC(fs.standard);
801802
if (fs.platformType != Platform::Type::Unspecified)
802-
temp.mSettings.platform.set(fs.platformType);
803+
tempSettings.platform.set(fs.platformType);
803804
if (mSettings.clang) {
804-
temp.mSettings.includePaths.insert(temp.mSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());
805+
tempSettings.includePaths.insert(tempSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());
806+
// need to pass the externally provided ErrorLogger instead of our internal wrapper
807+
CppCheck temp(tempSettings, mSuppressions, mErrorLoggerDirect, mUseGlobalSuppressions, mExecuteCommand);
805808
// TODO: propagate back mFileInfo
806809
const unsigned int returnValue = temp.check(fs.file);
807810
if (mUnusedFunctionsCheck)
808811
mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
809812
return returnValue;
810813
}
814+
// need to pass the externally provided ErrorLogger instead of our internal wrapper
815+
CppCheck temp(tempSettings, mSuppressions, mErrorLoggerDirect, mUseGlobalSuppressions, mExecuteCommand);
811816
const unsigned int returnValue = temp.checkFile(fs.file, fs.cfg);
812817
if (mUnusedFunctionsCheck)
813818
mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
@@ -1791,11 +1796,6 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
17911796
executeAddons(ctuInfoFiles, "");
17921797
}
17931798

1794-
Settings &CppCheck::settings()
1795-
{
1796-
return mSettings;
1797-
}
1798-
17991799
void CppCheck::tooManyConfigsError(const std::string &file, const int numberOfConfigurations)
18001800
{
18011801
if (!mSettings.severity.isEnabled(Severity::information) && !mTooManyConfigs)
@@ -1861,16 +1861,18 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st
18611861

18621862
void CppCheck::getErrorMessages(ErrorLogger &errorlogger)
18631863
{
1864-
Settings s;
1865-
s.addEnabled("all");
1864+
Settings settings;
18661865
Suppressions supprs;
18671866

1868-
CppCheck cppcheck(supprs, errorlogger, true, nullptr);
1867+
CppCheck cppcheck(settings, supprs, errorlogger, true, nullptr);
18691868
cppcheck.purgedConfigurationMessage("","");
18701869
cppcheck.mTooManyConfigs = true;
18711870
cppcheck.tooManyConfigsError("",0U);
18721871
// TODO: add functions to get remaining error messages
18731872

1873+
Settings s;
1874+
s.addEnabled("all");
1875+
18741876
// call all "getErrorMessages" in all registered Check classes
18751877
for (auto it = Check::instances().cbegin(); it != Check::instances().cend(); ++it)
18761878
(*it)->getErrorMessages(&errorlogger, &s);

lib/cppcheck.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "check.h"
2525
#include "config.h"
26-
#include "settings.h"
2726
#include "standards.h"
2827

2928
#include <cstdint>
@@ -42,6 +41,7 @@ class Tokenizer;
4241
class FileWithDetails;
4342
class AnalyzerInformation;
4443
class ErrorLogger;
44+
class Settings;
4545
struct Suppressions;
4646

4747
namespace simplecpp { class TokenList; }
@@ -62,7 +62,8 @@ class CPPCHECKLIB CppCheck {
6262
/**
6363
* @brief Constructor.
6464
*/
65-
CppCheck(Suppressions& supprs,
65+
CppCheck(const Settings& settings,
66+
Suppressions& supprs,
6667
ErrorLogger &errorLogger,
6768
bool useGlobalSuppressions,
6869
ExecuteCmdFn executeCommand);
@@ -102,12 +103,6 @@ class CPPCHECKLIB CppCheck {
102103
*/
103104
unsigned int check(const FileWithDetails &file, const std::string &content);
104105

105-
/**
106-
* @brief Get reference to current settings.
107-
* @return a reference to current settings
108-
*/
109-
Settings &settings();
110-
111106
/**
112107
* @brief Returns current version number as a string.
113108
* @return version, e.g. "1.38"
@@ -210,7 +205,7 @@ class CPPCHECKLIB CppCheck {
210205

211206
unsigned int checkClang(const FileWithDetails &file);
212207

213-
Settings mSettings;
208+
const Settings& mSettings;
214209
Suppressions& mSuppressions;
215210

216211
class CppCheckLogger;

oss-fuzz/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ static const FileWithDetails s_file("test.cpp");
5050

5151
static void doCheck(const std::string& code)
5252
{
53+
// TODO: create the settings only once
54+
Settings s;
55+
s.addEnabled("all");
56+
s.certainty.setEnabled(Certainty::inconclusive, true);
5357
Suppressions supprs;
54-
CppCheck cppcheck(supprs, s_errorLogger, false, nullptr);
55-
cppcheck.settings().addEnabled("all");
56-
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
58+
CppCheck cppcheck(s, supprs, s_errorLogger, false, nullptr);
5759
cppcheck.check(s_file, code);
5860
}
5961

0 commit comments

Comments
 (0)