Skip to content

Commit d429d7d

Browse files
authored
some CppCheck and related usage cleanups (#5979)
1 parent 2bfd469 commit d429d7d

9 files changed

+141
-153
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 119 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -64,109 +64,111 @@
6464
#include <windows.h>
6565
#endif
6666

67-
class CmdLineLoggerStd : public CmdLineLogger
68-
{
69-
public:
70-
CmdLineLoggerStd() = default;
71-
72-
void printMessage(const std::string &message) override
67+
namespace {
68+
class CmdLineLoggerStd : public CmdLineLogger
7369
{
74-
printRaw("cppcheck: " + message);
75-
}
70+
public:
71+
CmdLineLoggerStd() = default;
7672

77-
void printError(const std::string &message) override
78-
{
79-
printMessage("error: " + message);
80-
}
73+
void printMessage(const std::string &message) override
74+
{
75+
printRaw("cppcheck: " + message);
76+
}
8177

82-
void printRaw(const std::string &message) override
83-
{
84-
std::cout << message << std::endl;
85-
}
86-
};
78+
void printError(const std::string &message) override
79+
{
80+
printMessage("error: " + message);
81+
}
8782

88-
class CppCheckExecutor::StdLogger : public ErrorLogger
89-
{
90-
public:
91-
explicit StdLogger(const Settings& settings)
92-
: mSettings(settings)
83+
void printRaw(const std::string &message) override
84+
{
85+
std::cout << message << std::endl;
86+
}
87+
};
88+
89+
class StdLogger : public ErrorLogger
9390
{
94-
if (!mSettings.outputFile.empty()) {
95-
mErrorOutput = new std::ofstream(settings.outputFile);
91+
public:
92+
explicit StdLogger(const Settings& settings)
93+
: mSettings(settings)
94+
{
95+
if (!mSettings.outputFile.empty()) {
96+
mErrorOutput = new std::ofstream(settings.outputFile);
97+
}
9698
}
97-
}
9899

99-
~StdLogger() override {
100-
delete mErrorOutput;
101-
}
100+
~StdLogger() override {
101+
delete mErrorOutput;
102+
}
102103

103-
StdLogger(const StdLogger&) = delete;
104-
StdLogger& operator=(const SingleExecutor &) = delete;
104+
StdLogger(const StdLogger&) = delete;
105+
StdLogger& operator=(const SingleExecutor &) = delete;
105106

106-
void resetLatestProgressOutputTime() {
107-
mLatestProgressOutputTime = std::time(nullptr);
108-
}
107+
void resetLatestProgressOutputTime() {
108+
mLatestProgressOutputTime = std::time(nullptr);
109+
}
109110

110-
/**
111-
* Helper function to print out errors. Appends a line change.
112-
* @param errmsg String printed to error stream
113-
*/
114-
void reportErr(const std::string &errmsg);
111+
/**
112+
* Helper function to print out errors. Appends a line change.
113+
* @param errmsg String printed to error stream
114+
*/
115+
void reportErr(const std::string &errmsg);
115116

116-
/**
117-
* @brief Write the checkers report
118-
*/
119-
void writeCheckersReport();
117+
/**
118+
* @brief Write the checkers report
119+
*/
120+
void writeCheckersReport();
120121

121-
bool hasCriticalErrors() const {
122-
return !mCriticalErrors.empty();
123-
}
122+
bool hasCriticalErrors() const {
123+
return !mCriticalErrors.empty();
124+
}
124125

125-
private:
126-
/**
127-
* Information about progress is directed here. This should be
128-
* called by the CppCheck class only.
129-
*
130-
* @param outmsg Progress message e.g. "Checking main.cpp..."
131-
*/
132-
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
133-
134-
/** xml output of errors */
135-
void reportErr(const ErrorMessage &msg) override;
136-
137-
void reportProgress(const std::string &filename, const char stage[], const std::size_t value) override;
138-
139-
/**
140-
* Pointer to current settings; set while check() is running for reportError().
141-
*/
142-
const Settings& mSettings;
143-
144-
/**
145-
* Used to filter out duplicate error messages.
146-
*/
147-
// TODO: store hashes instead of the full messages
148-
std::unordered_set<std::string> mShownErrors;
149-
150-
/**
151-
* Report progress time
152-
*/
153-
std::time_t mLatestProgressOutputTime{};
154-
155-
/**
156-
* Error output
157-
*/
158-
std::ofstream* mErrorOutput{};
159-
160-
/**
161-
* Checkers that has been executed
162-
*/
163-
std::set<std::string> mActiveCheckers;
164-
165-
/**
166-
* True if there are critical errors
167-
*/
168-
std::string mCriticalErrors;
169-
};
126+
private:
127+
/**
128+
* Information about progress is directed here. This should be
129+
* called by the CppCheck class only.
130+
*
131+
* @param outmsg Progress message e.g. "Checking main.cpp..."
132+
*/
133+
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
134+
135+
/** xml output of errors */
136+
void reportErr(const ErrorMessage &msg) override;
137+
138+
void reportProgress(const std::string &filename, const char stage[], const std::size_t value) override;
139+
140+
/**
141+
* Pointer to current settings; set while check() is running for reportError().
142+
*/
143+
const Settings& mSettings;
144+
145+
/**
146+
* Used to filter out duplicate error messages.
147+
*/
148+
// TODO: store hashes instead of the full messages
149+
std::unordered_set<std::string> mShownErrors;
150+
151+
/**
152+
* Report progress time
153+
*/
154+
std::time_t mLatestProgressOutputTime{};
155+
156+
/**
157+
* Error output
158+
*/
159+
std::ofstream* mErrorOutput{};
160+
161+
/**
162+
* Checkers that has been executed
163+
*/
164+
std::set<std::string> mActiveCheckers;
165+
166+
/**
167+
* True if there are critical errors
168+
*/
169+
std::string mCriticalErrors;
170+
};
171+
}
170172

171173
// TODO: do not directly write to stdout
172174

@@ -193,28 +195,21 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
193195

194196
settings.setMisraRuleTexts(executeCommand);
195197

196-
mStdLogger = new StdLogger(settings);
197-
198-
CppCheck cppCheck(*mStdLogger, true, executeCommand);
199-
cppCheck.settings() = settings; // this is a copy
200-
201-
const int ret = check_wrapper(cppCheck);
198+
const int ret = check_wrapper(settings);
202199

203-
delete mStdLogger;
204-
mStdLogger = nullptr;
205200
return ret;
206201
}
207202

208-
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
203+
int CppCheckExecutor::check_wrapper(const Settings& settings)
209204
{
210205
#ifdef USE_WINDOWS_SEH
211-
if (cppcheck.settings().exceptionHandling)
212-
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, cppcheck);
206+
if (settings.exceptionHandling)
207+
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, settings);
213208
#elif defined(USE_UNIX_SIGNAL_HANDLING)
214-
if (cppcheck.settings().exceptionHandling)
215-
return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, cppcheck);
209+
if (settings.exceptionHandling)
210+
return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, settings);
216211
#endif
217-
return check_internal(cppcheck);
212+
return check_internal(settings);
218213
}
219214

220215
bool CppCheckExecutor::reportSuppressions(const Settings &settings, const Suppressions& suppressions, bool unusedFunctionCheckEnabled, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, ErrorLogger& errorLogger) {
@@ -246,16 +241,15 @@ bool CppCheckExecutor::reportSuppressions(const Settings &settings, const Suppre
246241
/*
247242
* That is a method which gets called from check_wrapper
248243
* */
249-
int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
244+
int CppCheckExecutor::check_internal(const Settings& settings) const
250245
{
251-
const auto& settings = cppcheck.settings();
252-
auto& suppressions = cppcheck.settings().nomsg;
246+
StdLogger stdLogger(settings);
253247

254248
if (settings.reportProgress >= 0)
255-
mStdLogger->resetLatestProgressOutputTime();
249+
stdLogger.resetLatestProgressOutputTime();
256250

257251
if (settings.xml) {
258-
mStdLogger->reportErr(ErrorMessage::getXMLHeader(settings.cppcheckCfgProductName));
252+
stdLogger.reportErr(ErrorMessage::getXMLHeader(settings.cppcheckCfgProductName));
259253
}
260254

261255
if (!settings.buildDir.empty()) {
@@ -268,24 +262,28 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
268262
if (!settings.checkersReportFilename.empty())
269263
std::remove(settings.checkersReportFilename.c_str());
270264

265+
CppCheck cppcheck(stdLogger, true, executeCommand);
266+
cppcheck.settings() = settings; // this is a copy
267+
auto& suppressions = cppcheck.settings().nomsg;
268+
271269
unsigned int returnValue;
272270
if (settings.useSingleJob()) {
273271
// Single process
274-
SingleExecutor executor(cppcheck, mFiles, mFileSettings, settings, suppressions, *mStdLogger);
272+
SingleExecutor executor(cppcheck, mFiles, mFileSettings, settings, suppressions, stdLogger);
275273
returnValue = executor.check();
276274
} else {
277275
#if defined(THREADING_MODEL_THREAD)
278-
ThreadExecutor executor(mFiles, mFileSettings, settings, suppressions, *mStdLogger, CppCheckExecutor::executeCommand);
276+
ThreadExecutor executor(mFiles, mFileSettings, settings, suppressions, stdLogger, CppCheckExecutor::executeCommand);
279277
#elif defined(THREADING_MODEL_FORK)
280-
ProcessExecutor executor(mFiles, mFileSettings, settings, suppressions, *mStdLogger, CppCheckExecutor::executeCommand);
278+
ProcessExecutor executor(mFiles, mFileSettings, settings, suppressions, stdLogger, CppCheckExecutor::executeCommand);
281279
#endif
282280
returnValue = executor.check();
283281
}
284282

285283
cppcheck.analyseWholeProgram(settings.buildDir, mFiles, mFileSettings);
286284

287285
if (settings.severity.isEnabled(Severity::information) || settings.checkConfiguration) {
288-
const bool err = reportSuppressions(settings, settings.nomsg, cppcheck.isUnusedFunctionCheckEnabled(), mFiles, mFileSettings, *mStdLogger);
286+
const bool err = reportSuppressions(settings, suppressions, settings.isUnusedFunctionCheckEnabled(), mFiles, mFileSettings, stdLogger);
289287
if (err && returnValue == 0)
290288
returnValue = settings.exitCode;
291289
}
@@ -295,21 +293,21 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
295293
}
296294

297295
if (settings.safety || settings.severity.isEnabled(Severity::information) || !settings.checkersReportFilename.empty())
298-
mStdLogger->writeCheckersReport();
296+
stdLogger.writeCheckersReport();
299297

300298
if (settings.xml) {
301-
mStdLogger->reportErr(ErrorMessage::getXMLFooter());
299+
stdLogger.reportErr(ErrorMessage::getXMLFooter());
302300
}
303301

304-
if (settings.safety && mStdLogger->hasCriticalErrors())
302+
if (settings.safety && stdLogger.hasCriticalErrors())
305303
return EXIT_FAILURE;
306304

307305
if (returnValue)
308306
return settings.exitCode;
309307
return EXIT_SUCCESS;
310308
}
311309

312-
void CppCheckExecutor::StdLogger::writeCheckersReport()
310+
void StdLogger::writeCheckersReport()
313311
{
314312
CheckersReport checkersReport(mSettings, mActiveCheckers);
315313

@@ -368,7 +366,7 @@ static inline std::string ansiToOEM(const std::string &msg, bool doConvert)
368366
#define ansiToOEM(msg, doConvert) (msg)
369367
#endif
370368

371-
void CppCheckExecutor::StdLogger::reportErr(const std::string &errmsg)
369+
void StdLogger::reportErr(const std::string &errmsg)
372370
{
373371
if (mErrorOutput)
374372
*mErrorOutput << errmsg << std::endl;
@@ -377,7 +375,7 @@ void CppCheckExecutor::StdLogger::reportErr(const std::string &errmsg)
377375
}
378376
}
379377

380-
void CppCheckExecutor::StdLogger::reportOut(const std::string &outmsg, Color c)
378+
void StdLogger::reportOut(const std::string &outmsg, Color c)
381379
{
382380
if (c == Color::Reset)
383381
std::cout << ansiToOEM(outmsg, true) << std::endl;
@@ -386,7 +384,7 @@ void CppCheckExecutor::StdLogger::reportOut(const std::string &outmsg, Color c)
386384
}
387385

388386
// TODO: remove filename parameter?
389-
void CppCheckExecutor::StdLogger::reportProgress(const std::string &filename, const char stage[], const std::size_t value)
387+
void StdLogger::reportProgress(const std::string &filename, const char stage[], const std::size_t value)
390388
{
391389
(void)filename;
392390

@@ -410,7 +408,7 @@ void CppCheckExecutor::StdLogger::reportProgress(const std::string &filename, co
410408
}
411409
}
412410

413-
void CppCheckExecutor::StdLogger::reportErr(const ErrorMessage &msg)
411+
void StdLogger::reportErr(const ErrorMessage &msg)
414412
{
415413
if (msg.severity == Severity::internal && (msg.id == "logChecker" || endsWith(msg.id, "-logChecker"))) {
416414
const std::string& checker = msg.shortMessage();

cli/cppcheckexecutor.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <utility>
2929
#include <vector>
3030

31-
class CppCheck;
3231
class Settings;
3332
class ErrorLogger;
3433
class Suppressions;
@@ -88,21 +87,21 @@ class CppCheckExecutor {
8887
* Wrapper around check_internal
8988
* - installs optional platform dependent signal handling
9089
*
91-
* @param cppcheck cppcheck instance
90+
* @param settings the settings
9291
**/
93-
int check_wrapper(CppCheck& cppcheck);
92+
int check_wrapper(const Settings& settings);
9493

9594
/**
9695
* Starts the checking.
9796
*
98-
* @param cppcheck cppcheck instance
97+
* @param settings the settings
9998
* @return EXIT_FAILURE if arguments are invalid or no input files
10099
* were found.
101100
* If errors are found and --error-exitcode is used,
102101
* given value is returned instead of default 0.
103102
* If no errors are found, 0 is returned.
104103
*/
105-
int check_internal(CppCheck& cppcheck) const;
104+
int check_internal(const Settings& settings) const;
106105

107106
/**
108107
* Filename associated with size of file
@@ -117,9 +116,6 @@ class CppCheckExecutor {
117116
*/
118117
static FILE* mExceptionOutput;
119118
#endif
120-
121-
class StdLogger;
122-
StdLogger* mStdLogger{};
123119
};
124120

125121
#endif // CPPCHECKEXECUTOR_H

0 commit comments

Comments
 (0)