Skip to content

Commit 49da3e3

Browse files
authored
Fixed #12281 (IDE plugin integration is broken by checkers report) (#5779)
1 parent 3b1c701 commit 49da3e3

5 files changed

Lines changed: 56 additions & 28 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CppCheckExecutor::StdLogger : public ErrorLogger
116116
/**
117117
* @brief Write the checkers report
118118
*/
119-
void writeCheckersReport() const;
119+
void writeCheckersReport();
120120

121121
bool hasCriticalErrors() const {
122122
return !mCriticalErrors.empty();
@@ -286,12 +286,13 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
286286
cppcheck.tooManyConfigsError(emptyString,0U);
287287
}
288288

289+
if (settings.safety || settings.severity.isEnabled(Severity::information) || !settings.checkersReportFilename.empty())
290+
mStdLogger->writeCheckersReport();
291+
289292
if (settings.xml) {
290293
mStdLogger->reportErr(ErrorMessage::getXMLFooter());
291294
}
292295

293-
mStdLogger->writeCheckersReport();
294-
295296
if (settings.safety && mStdLogger->hasCriticalErrors())
296297
return EXIT_FAILURE;
297298

@@ -300,28 +301,39 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
300301
return EXIT_SUCCESS;
301302
}
302303

303-
void CppCheckExecutor::StdLogger::writeCheckersReport() const
304+
void CppCheckExecutor::StdLogger::writeCheckersReport()
304305
{
305306
CheckersReport checkersReport(mSettings, mActiveCheckers);
306307

307-
if (!mSettings.quiet) {
308+
bool suppressed = false;
309+
for (const Suppressions::Suppression& s : mSettings.nomsg.getSuppressions()) {
310+
if (s.errorId == "checkersReport")
311+
suppressed = true;
312+
}
313+
314+
if (!suppressed) {
315+
ErrorMessage msg;
316+
msg.severity = Severity::information;
317+
msg.id = "checkersReport";
318+
308319
const int activeCheckers = checkersReport.getActiveCheckersCount();
309320
const int totalCheckers = checkersReport.getAllCheckersCount();
310321

311-
const std::string extra = mSettings.verbose ? " (use --checkers-report=<filename> to see details)" : "";
322+
std::string what;
312323
if (mCriticalErrors.empty())
313-
std::cout << "Active checkers: " << activeCheckers << "/" << totalCheckers << extra << std::endl;
324+
what = std::to_string(activeCheckers) + "/" + std::to_string(totalCheckers);
314325
else
315-
std::cout << "Active checkers: There was critical errors" << extra << std::endl;
316-
}
317-
318-
if (mSettings.checkersReportFilename.empty())
319-
return;
326+
what = "There was critical errors";
327+
msg.setmsg("Active checkers: " + what + " (use --checkers-report=<filename> to see details)");
320328

321-
std::ofstream fout(mSettings.checkersReportFilename);
322-
if (fout.is_open())
323-
fout << checkersReport.getReport(mCriticalErrors);
329+
reportErr(msg);
330+
}
324331

332+
if (!mSettings.checkersReportFilename.empty()) {
333+
std::ofstream fout(mSettings.checkersReportFilename);
334+
if (fout.is_open())
335+
fout << checkersReport.getReport(mCriticalErrors);
336+
}
325337
}
326338

327339
#ifdef _WIN32

lib/suppressions.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
#include "xml.h"
3737

38+
static const char ID_UNUSEDFUNCTION[] = "unusedFunction";
39+
static const char ID_CHECKERSREPORT[] = "checkersReport";
40+
3841
Suppressions::ErrorMessage Suppressions::ErrorMessage::fromErrorMessage(const ::ErrorMessage &msg, const std::set<std::string> &macroNames)
3942
{
4043
Suppressions::ErrorMessage ret;
@@ -468,7 +471,9 @@ std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions
468471
continue;
469472
if (s.hash > 0)
470473
continue;
471-
if (!unusedFunctionChecking && s.errorId == "unusedFunction")
474+
if (s.errorId == ID_CHECKERSREPORT)
475+
continue;
476+
if (!unusedFunctionChecking && s.errorId == ID_UNUSEDFUNCTION)
472477
continue;
473478
if (tmpFile.empty() || !s.isLocal() || s.fileName != tmpFile)
474479
continue;
@@ -485,7 +490,9 @@ std::list<Suppressions::Suppression> Suppressions::getUnmatchedGlobalSuppression
485490
continue;
486491
if (s.hash > 0)
487492
continue;
488-
if (!unusedFunctionChecking && s.errorId == "unusedFunction")
493+
if (!unusedFunctionChecking && s.errorId == ID_UNUSEDFUNCTION)
494+
continue;
495+
if (s.errorId == ID_CHECKERSREPORT)
489496
continue;
490497
if (s.isLocal())
491498
continue;

releasenotes.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GUI:
1212
-
1313

1414
Changed interface:
15-
-
15+
- Final report of active checkers is reported as a normal information message instead.
1616

1717
Deprecations:
1818
- "--showtime=top5" has been deprecated and will be removed in Cppcheck 2.14. Please use --showtime=top5_file or --showtime=top5_summary instead.
@@ -44,6 +44,3 @@ Other:
4444
- Markup files will now be processed after the regular source files when using multiple threads/processes (some issues remain - see Trac #12167 for details).
4545
- Added file name to ValueFlow "--debug" output.
4646
- Fixed build when using "clang-cl" in CMake.
47-
48-
Safety critical fixes:
49-
- Added "--safety" option. It makes Cppcheck more strict about critical errors.

test/cli/test-suppress-syntaxError.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def test_j2_suppress():
1414
assert len(stderr) == 0
1515

1616
def test_safety_suppress_syntax_error_implicitly(tmpdir):
17-
ret, stdout, stderr = cppcheck(['--safety', '--suppress=*', 'proj-suppress-syntaxError'], remove_active_checkers=False)
17+
ret, stdout, stderr = cppcheck(['--safety', '--suppress=*', 'proj-suppress-syntaxError'], remove_checkers_report=False)
1818
assert ret == 1
1919
assert '[syntaxError]' in stderr
20-
assert 'Active checkers: There was critical errors' in stdout
20+
assert 'Active checkers: There was critical errors' in stderr
2121

2222
def test_safety_suppress_syntax_error_explicitly():
23-
ret, stdout, stderr = cppcheck(['--safety', '--suppress=syntaxError', 'proj-suppress-syntaxError'], remove_active_checkers=False)
23+
ret, stdout, stderr = cppcheck(['--safety', '--suppress=syntaxError', 'proj-suppress-syntaxError'], remove_checkers_report=False)
2424
assert ret == 1
2525
assert '[syntaxError]' not in stderr
26-
assert 'Active checkers: There was critical errors' in stdout
26+
assert 'Active checkers: There was critical errors' in stderr
2727

test/cli/testutils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __lookup_cppcheck_exe():
7171

7272

7373
# Run Cppcheck with args
74-
def cppcheck(args, env=None, remove_active_checkers=True):
74+
def cppcheck(args, env=None, remove_checkers_report=True):
7575
exe = __lookup_cppcheck_exe()
7676
assert exe is not None, 'no cppcheck binary found'
7777

@@ -80,8 +80,20 @@ def cppcheck(args, env=None, remove_active_checkers=True):
8080
comm = p.communicate()
8181
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
8282
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
83-
if remove_active_checkers and stdout.find('\nActive checkers:') > 0:
84-
stdout = stdout[:1 + stdout.find('\nActive checkers:')]
83+
if remove_checkers_report:
84+
if stderr.find('[checkersReport]\n') > 0:
85+
start_id = stderr.find('[checkersReport]\n')
86+
start_line = stderr.rfind('\n', 0, start_id)
87+
if start_line <= 0:
88+
stderr = ''
89+
else:
90+
stderr = stderr[:start_line + 1]
91+
elif stderr.find(': (information) Active checkers: ') >= 0:
92+
pos = stderr.find(': (information) Active checkers: ')
93+
if pos == 0:
94+
stderr = ''
95+
elif stderr[pos - 1] == '\n':
96+
stderr = stderr[:pos]
8597
return p.returncode, stdout, stderr
8698

8799

0 commit comments

Comments
 (0)