Skip to content

Commit 6f95fcb

Browse files
committed
fix missing unmatchedSuppressions
1 parent ec95600 commit 6f95fcb

2 files changed

Lines changed: 73 additions & 15 deletions

File tree

‎cli/cppcheckexecutor.cpp‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -363,36 +363,50 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
363363
supprlist.addSuppression(std::move(s));
364364
}
365365

366-
const auto reportErrorsFn = [&](const std::string& sourcefile, std::size_t fsFileId, const std::vector<ErrorMessage>& errors) -> bool {
366+
const auto getCfgs = [&](const std::string &sourcefile) {
367+
std::vector<std::string> cfgs;
368+
for (const FileSettings &fs : fileSettings) {
369+
if (fs.file.spath() == Path::simplifyPath(sourcefile))
370+
cfgs.push_back(fs.cfg);
371+
}
372+
if (cfgs.empty())
373+
cfgs.emplace_back();
374+
return cfgs;
375+
};
376+
377+
const auto reportErrorsFn = [&](const std::string &sourcefile, const std::vector<std::string> &cfgs, std::size_t fsFileId, const std::vector<ErrorMessage> &errors) -> bool {
367378
if (errors.empty())
368379
return false;
369380

370381
// TODO: what if sourcefile is empty?
371382

372-
AnalyzerInformation analyzerInfo;
373-
// FIXME: this is a horrible hack
374-
// we need to "re-open" the file so we can add the unmatchedSuppression findings.
375-
// we cannot keep it open conditionally because the whole program analysis reads the XML.
376-
// re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks.
377-
analyzerInfo.reopen(settings.buildDir, sourcefile, /*cfgname*/ "", fsFileId);
383+
for (const std::string &cfg : cfgs) {
384+
AnalyzerInformation analyzerInfo;
385+
// FIXME: this is a horrible hack
386+
// we need to "re-open" the file so we can add the unmatchedSuppression findings.
387+
// we cannot keep it open conditionally because the whole program analysis reads the XML.
388+
// re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks.
389+
analyzerInfo.reopen(settings.buildDir, sourcefile, cfg, fsFileId);
378390

379-
for (const auto& errmsg : errors) {
380-
analyzerInfo.reportErr(errmsg);
381-
errorLogger.reportErr(errmsg);
391+
for (const auto &errmsg : errors)
392+
analyzerInfo.reportErr(errmsg);
382393
}
394+
395+
for (const auto &errmsg : errors)
396+
errorLogger.reportErr(errmsg);
383397
return true;
384398
};
385399

386400
bool err = false;
387401

388402
for (auto i = files.cbegin(); i != files.cend(); ++i) {
389403
const std::vector<ErrorMessage> errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(*i), settings.unmatchedSuppressionFilters);
390-
err |= reportErrorsFn(i->spath(), i->fsFileId(), errors);
404+
err |= reportErrorsFn(i->spath(), {""}, i->fsFileId(), errors);
391405
}
392406

393407
for (auto i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) {
394408
const std::vector<ErrorMessage> errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(i->file), settings.unmatchedSuppressionFilters);
395-
err |= reportErrorsFn(i->file.spath(), i->file.fsFileId(), errors);
409+
err |= reportErrorsFn(i->file.spath(), {i->cfg}, i->file.fsFileId(), errors);
396410
}
397411

398412
if (settings.inlineSuppressions) {
@@ -401,7 +415,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
401415
std::string sourcefile;
402416
if (!errmsg.callStack.empty())
403417
sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path?
404-
err |= reportErrorsFn(sourcefile, 0, {errmsg});
418+
err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg});
405419
}
406420
}
407421

@@ -410,7 +424,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
410424
std::string sourcefile;
411425
if (!errmsg.callStack.empty())
412426
sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path?
413-
err |= reportErrorsFn(sourcefile, 0, {errmsg});
427+
err |= reportErrorsFn(sourcefile, getCfgs(sourcefile), 0, {errmsg});
414428
}
415429
return err;
416430
}
@@ -750,4 +764,3 @@ int CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string> a
750764
#endif
751765
return res;
752766
}
753-

‎test/cli/other_test.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,51 @@ def test_inline_suppr_builddir_j_cached(tmp_path):
26212621
__test_inline_suppr(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2'])
26222622

26232623

2624+
def __test_inline_suppr_project(tmp_path, extra_args):
2625+
test_file = tmp_path / 'test.c'
2626+
with open(test_file, 'wt') as f:
2627+
f.write("""
2628+
void f() {
2629+
// cppcheck-suppress memleak
2630+
}
2631+
""")
2632+
2633+
configs = ''.join(
2634+
f'<ProjectConfiguration Include="{c}|x64"><Configuration>{c}</Configuration><Platform>x64</Platform></ProjectConfiguration>'
2635+
for c in ('Debug', 'Release'))
2636+
project_file = tmp_path / 'test.vcxproj'
2637+
with open(project_file, 'wt') as f:
2638+
f.write('<?xml version="1.0" encoding="utf-8"?>'
2639+
'<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">'
2640+
f'<ItemGroup Label="ProjectConfigurations">{configs}</ItemGroup>'
2641+
'<ItemGroup><ClCompile Include="test.c" /></ItemGroup>'
2642+
'</Project>')
2643+
2644+
args = [
2645+
'-q',
2646+
'--template=simple',
2647+
'--enable=information',
2648+
'--inline-suppr',
2649+
'--project={}'.format(project_file)
2650+
]
2651+
2652+
args += extra_args
2653+
2654+
exitcode, stdout, stderr, = cppcheck(args)
2655+
assert exitcode == 0, stdout
2656+
assert stdout == ''
2657+
assert stderr.splitlines() == [
2658+
'{}:4:1: information: Unmatched suppression: memleak [unmatchedSuppression]'.format(test_file)
2659+
]
2660+
2661+
2662+
def test_inline_suppr_builddir_project_cached(tmp_path):
2663+
build_dir = tmp_path / 'b1'
2664+
os.mkdir(build_dir)
2665+
__test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
2666+
__test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
2667+
2668+
26242669
def test_duplicate_suppression(tmp_path):
26252670
test_file = tmp_path / 'file.cpp'
26262671
with open(test_file, 'wt'):

0 commit comments

Comments
 (0)