64
64
#include < windows.h>
65
65
#endif
66
66
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
73
69
{
74
- printRaw ( " cppcheck: " + message);
75
- }
70
+ public:
71
+ CmdLineLoggerStd () = default ;
76
72
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
+ }
81
77
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
+ }
87
82
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
93
90
{
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
+ }
96
98
}
97
- }
98
99
99
- ~StdLogger () override {
100
- delete mErrorOutput ;
101
- }
100
+ ~StdLogger () override {
101
+ delete mErrorOutput ;
102
+ }
102
103
103
- StdLogger (const StdLogger&) = delete ;
104
- StdLogger& operator =(const SingleExecutor &) = delete ;
104
+ StdLogger (const StdLogger&) = delete ;
105
+ StdLogger& operator =(const SingleExecutor &) = delete ;
105
106
106
- void resetLatestProgressOutputTime () {
107
- mLatestProgressOutputTime = std::time (nullptr );
108
- }
107
+ void resetLatestProgressOutputTime () {
108
+ mLatestProgressOutputTime = std::time (nullptr );
109
+ }
109
110
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);
115
116
116
- /* *
117
- * @brief Write the checkers report
118
- */
119
- void writeCheckersReport ();
117
+ /* *
118
+ * @brief Write the checkers report
119
+ */
120
+ void writeCheckersReport ();
120
121
121
- bool hasCriticalErrors () const {
122
- return !mCriticalErrors .empty ();
123
- }
122
+ bool hasCriticalErrors () const {
123
+ return !mCriticalErrors .empty ();
124
+ }
124
125
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
+ }
170
172
171
173
// TODO: do not directly write to stdout
172
174
@@ -193,28 +195,21 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
193
195
194
196
settings.setMisraRuleTexts (executeCommand);
195
197
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);
202
199
203
- delete mStdLogger ;
204
- mStdLogger = nullptr ;
205
200
return ret;
206
201
}
207
202
208
- int CppCheckExecutor::check_wrapper (CppCheck& cppcheck )
203
+ int CppCheckExecutor::check_wrapper (const Settings& settings )
209
204
{
210
205
#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 );
213
208
#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 );
216
211
#endif
217
- return check_internal (cppcheck );
212
+ return check_internal (settings );
218
213
}
219
214
220
215
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
246
241
/*
247
242
* That is a method which gets called from check_wrapper
248
243
* */
249
- int CppCheckExecutor::check_internal (CppCheck& cppcheck ) const
244
+ int CppCheckExecutor::check_internal (const Settings& settings ) const
250
245
{
251
- const auto & settings = cppcheck.settings ();
252
- auto & suppressions = cppcheck.settings ().nomsg ;
246
+ StdLogger stdLogger (settings);
253
247
254
248
if (settings.reportProgress >= 0 )
255
- mStdLogger -> resetLatestProgressOutputTime ();
249
+ stdLogger. resetLatestProgressOutputTime ();
256
250
257
251
if (settings.xml ) {
258
- mStdLogger -> reportErr (ErrorMessage::getXMLHeader (settings.cppcheckCfgProductName ));
252
+ stdLogger. reportErr (ErrorMessage::getXMLHeader (settings.cppcheckCfgProductName ));
259
253
}
260
254
261
255
if (!settings.buildDir .empty ()) {
@@ -268,24 +262,28 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
268
262
if (!settings.checkersReportFilename .empty ())
269
263
std::remove (settings.checkersReportFilename .c_str ());
270
264
265
+ CppCheck cppcheck (stdLogger, true , executeCommand);
266
+ cppcheck.settings () = settings; // this is a copy
267
+ auto & suppressions = cppcheck.settings ().nomsg ;
268
+
271
269
unsigned int returnValue;
272
270
if (settings.useSingleJob ()) {
273
271
// Single process
274
- SingleExecutor executor (cppcheck, mFiles , mFileSettings , settings, suppressions, * mStdLogger );
272
+ SingleExecutor executor (cppcheck, mFiles , mFileSettings , settings, suppressions, stdLogger );
275
273
returnValue = executor.check ();
276
274
} else {
277
275
#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);
279
277
#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);
281
279
#endif
282
280
returnValue = executor.check ();
283
281
}
284
282
285
283
cppcheck.analyseWholeProgram (settings.buildDir , mFiles , mFileSettings );
286
284
287
285
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 );
289
287
if (err && returnValue == 0 )
290
288
returnValue = settings.exitCode ;
291
289
}
@@ -295,21 +293,21 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
295
293
}
296
294
297
295
if (settings.safety || settings.severity .isEnabled (Severity::information) || !settings.checkersReportFilename .empty ())
298
- mStdLogger -> writeCheckersReport ();
296
+ stdLogger. writeCheckersReport ();
299
297
300
298
if (settings.xml ) {
301
- mStdLogger -> reportErr (ErrorMessage::getXMLFooter ());
299
+ stdLogger. reportErr (ErrorMessage::getXMLFooter ());
302
300
}
303
301
304
- if (settings.safety && mStdLogger -> hasCriticalErrors ())
302
+ if (settings.safety && stdLogger. hasCriticalErrors ())
305
303
return EXIT_FAILURE;
306
304
307
305
if (returnValue)
308
306
return settings.exitCode ;
309
307
return EXIT_SUCCESS;
310
308
}
311
309
312
- void CppCheckExecutor:: StdLogger::writeCheckersReport ()
310
+ void StdLogger::writeCheckersReport ()
313
311
{
314
312
CheckersReport checkersReport (mSettings , mActiveCheckers );
315
313
@@ -368,7 +366,7 @@ static inline std::string ansiToOEM(const std::string &msg, bool doConvert)
368
366
#define ansiToOEM (msg, doConvert ) (msg)
369
367
#endif
370
368
371
- void CppCheckExecutor:: StdLogger::reportErr (const std::string &errmsg)
369
+ void StdLogger::reportErr (const std::string &errmsg)
372
370
{
373
371
if (mErrorOutput )
374
372
*mErrorOutput << errmsg << std::endl;
@@ -377,7 +375,7 @@ void CppCheckExecutor::StdLogger::reportErr(const std::string &errmsg)
377
375
}
378
376
}
379
377
380
- void CppCheckExecutor:: StdLogger::reportOut (const std::string &outmsg, Color c)
378
+ void StdLogger::reportOut (const std::string &outmsg, Color c)
381
379
{
382
380
if (c == Color::Reset)
383
381
std::cout << ansiToOEM (outmsg, true ) << std::endl;
@@ -386,7 +384,7 @@ void CppCheckExecutor::StdLogger::reportOut(const std::string &outmsg, Color c)
386
384
}
387
385
388
386
// 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)
390
388
{
391
389
(void )filename;
392
390
@@ -410,7 +408,7 @@ void CppCheckExecutor::StdLogger::reportProgress(const std::string &filename, co
410
408
}
411
409
}
412
410
413
- void CppCheckExecutor:: StdLogger::reportErr (const ErrorMessage &msg)
411
+ void StdLogger::reportErr (const ErrorMessage &msg)
414
412
{
415
413
if (msg.severity == Severity::internal && (msg.id == " logChecker" || endsWith (msg.id , " -logChecker" ))) {
416
414
const std::string& checker = msg.shortMessage ();
0 commit comments