Skip to content

Commit e1f82c1

Browse files
authored
Fix #15206: Dumpfile shared between project configurations of the same file with --cppcheck-build-dir (#9017)
1 parent 4ccfcc6 commit e1f82c1

4 files changed

Lines changed: 177 additions & 23 deletions

File tree

‎cli/cppcheckexecutor.cpp‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -363,36 +363,51 @@ 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+
using AnalyzerInfoId = std::pair<std::string, std::size_t>;
367+
const auto getAnalyzerInfoIds = [&](const std::string &sourcefile) {
368+
std::vector<AnalyzerInfoId> ids;
369+
for (const FileSettings &fs : fileSettings) {
370+
if (fs.file.spath() == Path::simplifyPath(sourcefile))
371+
ids.emplace_back(fs.cfg, fs.file.fsFileId());
372+
}
373+
if (ids.empty())
374+
ids.emplace_back("", 0);
375+
return ids;
376+
};
377+
378+
const auto reportErrorsFn = [&](const std::string &sourcefile, const std::vector<AnalyzerInfoId> &ids, const std::vector<ErrorMessage> &errors) -> bool {
367379
if (errors.empty())
368380
return false;
369381

370382
// TODO: what if sourcefile is empty?
371383

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);
384+
for (const AnalyzerInfoId &id : ids) {
385+
AnalyzerInformation analyzerInfo;
386+
// FIXME: this is a horrible hack
387+
// we need to "re-open" the file so we can add the unmatchedSuppression findings.
388+
// we cannot keep it open conditionally because the whole program analysis reads the XML.
389+
// re-ordering the code is also not an option because the unmatched suppression reporting needs to be run after all other checks.
390+
analyzerInfo.reopen(settings.buildDir, sourcefile, id.first, id.second);
378391

379-
for (const auto& errmsg : errors) {
380-
analyzerInfo.reportErr(errmsg);
381-
errorLogger.reportErr(errmsg);
392+
for (const auto &errmsg : errors)
393+
analyzerInfo.reportErr(errmsg);
382394
}
395+
396+
for (const auto &errmsg : errors)
397+
errorLogger.reportErr(errmsg);
383398
return true;
384399
};
385400

386401
bool err = false;
387402

388403
for (auto i = files.cbegin(); i != files.cend(); ++i) {
389404
const std::vector<ErrorMessage> errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(*i), settings.unmatchedSuppressionFilters);
390-
err |= reportErrorsFn(i->spath(), i->fsFileId(), errors);
405+
err |= reportErrorsFn(i->spath(), {{"", i->fsFileId()}}, errors);
391406
}
392407

393408
for (auto i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) {
394409
const std::vector<ErrorMessage> errors = getUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(i->file), settings.unmatchedSuppressionFilters);
395-
err |= reportErrorsFn(i->file.spath(), i->file.fsFileId(), errors);
410+
err |= reportErrorsFn(i->file.spath(), {{i->cfg, i->file.fsFileId()}}, errors);
396411
}
397412

398413
if (settings.inlineSuppressions) {
@@ -401,7 +416,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
401416
std::string sourcefile;
402417
if (!errmsg.callStack.empty())
403418
sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path?
404-
err |= reportErrorsFn(sourcefile, 0, {errmsg});
419+
err |= reportErrorsFn(sourcefile, getAnalyzerInfoIds(sourcefile), {errmsg});
405420
}
406421
}
407422

@@ -410,7 +425,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
410425
std::string sourcefile;
411426
if (!errmsg.callStack.empty())
412427
sourcefile = errmsg.callStack.cbegin()->getfile(false); // TODO: simplify path?
413-
err |= reportErrorsFn(sourcefile, 0, {errmsg});
428+
err |= reportErrorsFn(sourcefile, getAnalyzerInfoIds(sourcefile), {errmsg});
414429
}
415430
return err;
416431
}
@@ -750,4 +765,3 @@ int CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string> a
750765
#endif
751766
return res;
752767
}
753-

‎lib/cppcheck.cpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static std::vector<std::string> split(const std::string &str, const std::string
357357
return ret;
358358
}
359359

360-
static std::string getDumpFileName(const Settings& settings, const FileWithDetails& file)
360+
static std::string getDumpFileName(const Settings& settings, const FileWithDetails& file, const std::string& cfg)
361361
{
362362
std::string extension = ".dump";
363363
if (file.fsFileId() > 0)
@@ -366,7 +366,7 @@ static std::string getDumpFileName(const Settings& settings, const FileWithDetai
366366
extension = "." + std::to_string(settings.pid) + extension;
367367

368368
if (!settings.dump && !settings.buildDir.empty())
369-
return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, file.spath(), "", file.fsFileId()) + extension;
369+
return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension;
370370
return file.spath() + extension;
371371
}
372372

@@ -377,12 +377,13 @@ static std::string getCtuInfoFileName(const std::string &dumpFile)
377377

378378
static void createDumpFile(const Settings& settings,
379379
const FileWithDetails& file,
380+
const std::string& cfg,
380381
std::ofstream& fdump,
381382
std::string& dumpFile)
382383
{
383384
if (!settings.dump && settings.addons.empty())
384385
return;
385-
dumpFile = getDumpFileName(settings, file);
386+
dumpFile = getDumpFileName(settings, file, cfg);
386387

387388
fdump.open(dumpFile);
388389
if (!fdump.is_open())
@@ -783,7 +784,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
783784
// create dumpfile
784785
std::ofstream fdump;
785786
std::string dumpFile;
786-
createDumpFile(mSettings, file, fdump, dumpFile);
787+
createDumpFile(mSettings, file, "", fdump, dumpFile);
787788
if (fdump.is_open()) {
788789
fdump << getLibraryDumpData();
789790
// TODO: use tinyxml2 to create XML
@@ -1156,7 +1157,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11561157
// write dump file xml prolog
11571158
std::ofstream fdump;
11581159
std::string dumpFile;
1159-
createDumpFile(mSettings, file, fdump, dumpFile);
1160+
createDumpFile(mSettings, file, cfgname, fdump, dumpFile);
11601161
if (fdump.is_open()) {
11611162
fdump << getLibraryDumpData();
11621163
if (!mSettings.dump)
@@ -1725,12 +1726,12 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
17251726

17261727
std::vector<std::string> ctuInfoFiles;
17271728
for (const auto &f: files) {
1728-
const std::string &dumpFileName = getDumpFileName(mSettings, f);
1729+
const std::string &dumpFileName = getDumpFileName(mSettings, f, "");
17291730
ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName));
17301731
}
17311732

17321733
for (const auto &fs: fileSettings) {
1733-
const std::string &dumpFileName = getDumpFileName(mSettings, fs.file);
1734+
const std::string &dumpFileName = getDumpFileName(mSettings, fs.file, fs.cfg);
17341735
ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName));
17351736
}
17361737

@@ -1842,7 +1843,7 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
18421843
std::string line;
18431844

18441845
if (!mSettings.buildDir.empty()) {
1845-
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), "", fileSettings.file.fsFileId());
1846+
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
18461847
std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
18471848
fcmd << istr.str();
18481849
}

‎test/cli/dumpfile_test.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,32 @@ def test_suppression_this_and_next_line(tmpdir):
165165
dump = f.read()
166166
assert 'lineNumber="2" thisAndNextLine="true" inline="true"' in dump
167167
assert 'lineNumber="4" inline="true"' in dump
168+
169+
def test_build_dir_project_configurations(tmp_path):
170+
test_file = tmp_path / 'main.c'
171+
with open(test_file, 'wt') as f:
172+
f.write('int main(void) { return 0; }\n')
173+
174+
configs = ''.join(
175+
f'<ProjectConfiguration Include="{c}|{p}"><Configuration>{c}</Configuration><Platform>{p}</Platform></ProjectConfiguration>'
176+
for c in ('Debug', 'Release') for p in ('Win32', 'x64'))
177+
project_file = tmp_path / 'test.vcxproj'
178+
with open(project_file, 'wt') as f:
179+
f.write('<?xml version="1.0" encoding="utf-8"?>'
180+
'<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">'
181+
f'<ItemGroup Label="ProjectConfigurations">{configs}</ItemGroup>'
182+
'<ItemGroup><ClCompile Include="main.c" /></ItemGroup>'
183+
'</Project>')
184+
185+
build_dir = tmp_path / 'b'
186+
os.mkdir(build_dir)
187+
188+
args = ['-q', '--project=test.vcxproj', '--cppcheck-build-dir=b', '--addon=misra']
189+
exitcode, stdout, stderr = cppcheck(args, cwd=str(tmp_path))
190+
assert exitcode == 0, stdout
191+
assert stderr == ''
192+
193+
files = os.listdir(build_dir)
194+
assert 'main.c.analyzerinfo.ctu-info' not in files
195+
for i in range(1, 5):
196+
assert f'main.a{i}.ctu-info' in files

‎test/cli/other_test.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,36 @@ def test_showtime_summary_clang_tidy_compdb(tmp_path):
11651165
__test_showtime_summary(tmp_path, use_clang_tidy=True, use_compdb=True)
11661166

11671167

1168+
@pytest.mark.skipif(not __has_clang_tidy, reason='clang-tidy is not available')
1169+
def test_clang_tidy_builddir_project_configurations(tmp_path):
1170+
test_file = tmp_path / 'test.c'
1171+
with open(test_file, 'wt') as f:
1172+
f.write('int main(void) { return 0; }\n')
1173+
1174+
configs = ''.join(
1175+
f'<ProjectConfiguration Include="{c}|x64"><Configuration>{c}</Configuration><Platform>x64</Platform></ProjectConfiguration>'
1176+
for c in ('Debug', 'Release'))
1177+
project_file = tmp_path / 'test.vcxproj'
1178+
with open(project_file, 'wt') as f:
1179+
f.write('<?xml version="1.0" encoding="utf-8"?>'
1180+
'<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">'
1181+
f'<ItemGroup Label="ProjectConfigurations">{configs}</ItemGroup>'
1182+
'<ItemGroup><ClCompile Include="test.c" /></ItemGroup>'
1183+
'</Project>')
1184+
1185+
build_dir = tmp_path / 'b'
1186+
os.mkdir(build_dir)
1187+
1188+
args = ['-q', '--clang-tidy', '--project=test.vcxproj', '--cppcheck-build-dir=b']
1189+
exitcode, stdout, _ = cppcheck(args, cwd=str(tmp_path))
1190+
assert exitcode == 0, stdout
1191+
1192+
files = os.listdir(build_dir)
1193+
assert 'test.c.analyzerinfo.clang-tidy-cmd' not in files
1194+
for i in range(1, 3):
1195+
assert f'test.a{i}.clang-tidy-cmd' in files
1196+
1197+
11681198
def __test_showtime_file_total(tmp_path, use_compdb=False):
11691199
__test_showtime(tmp_path, 'file-total', 0, 'Check time: ', use_compdb=use_compdb)
11701200

@@ -2621,6 +2651,86 @@ def test_inline_suppr_builddir_j_cached(tmp_path):
26212651
__test_inline_suppr(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j2'])
26222652

26232653

2654+
def __test_inline_suppr_project(tmp_path, extra_args):
2655+
test_file = tmp_path / 'test.c'
2656+
with open(test_file, 'wt') as f:
2657+
f.write("""
2658+
void f() {
2659+
// cppcheck-suppress memleak
2660+
}
2661+
""")
2662+
2663+
configs = ''.join(
2664+
f'<ProjectConfiguration Include="{c}|x64"><Configuration>{c}</Configuration><Platform>x64</Platform></ProjectConfiguration>'
2665+
for c in ('Debug', 'Release'))
2666+
project_file = tmp_path / 'test.vcxproj'
2667+
with open(project_file, 'wt') as f:
2668+
f.write('<?xml version="1.0" encoding="utf-8"?>'
2669+
'<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">'
2670+
f'<ItemGroup Label="ProjectConfigurations">{configs}</ItemGroup>'
2671+
'<ItemGroup><ClCompile Include="test.c" /></ItemGroup>'
2672+
'</Project>')
2673+
2674+
args = [
2675+
'-q',
2676+
'--template=simple',
2677+
'--enable=information',
2678+
'--inline-suppr',
2679+
'--project={}'.format(project_file)
2680+
]
2681+
2682+
args += extra_args
2683+
2684+
exitcode, stdout, stderr, = cppcheck(args)
2685+
assert exitcode == 0, stdout
2686+
assert stdout == ''
2687+
assert stderr.splitlines() == [
2688+
'{}:4:1: information: Unmatched suppression: memleak [unmatchedSuppression]'.format(test_file)
2689+
]
2690+
2691+
2692+
def test_inline_suppr_builddir_project_cached(tmp_path):
2693+
build_dir = tmp_path / 'b1'
2694+
os.mkdir(build_dir)
2695+
__test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
2696+
__test_inline_suppr_project(tmp_path, ['--cppcheck-build-dir={}'.format(build_dir), '-j1'])
2697+
2698+
2699+
def test_inline_suppr_builddir_compdb_duplicate(tmp_path):
2700+
test_file = tmp_path / 'test.c'
2701+
with open(test_file, 'wt') as f:
2702+
f.write("""
2703+
void f() {
2704+
// cppcheck-suppress memleak
2705+
}
2706+
""")
2707+
2708+
# The same file listed twice gets one analyzer information file per entry
2709+
compdb_file = tmp_path / 'compile_commands.json'
2710+
create_compile_commands(compdb_file, [test_file, test_file])
2711+
2712+
build_dir = tmp_path / 'b1'
2713+
os.mkdir(build_dir)
2714+
2715+
args = [
2716+
'-q',
2717+
'--template=simple',
2718+
'--enable=information',
2719+
'--inline-suppr',
2720+
'--cppcheck-build-dir={}'.format(build_dir),
2721+
'--project={}'.format(compdb_file)
2722+
]
2723+
2724+
exitcode, stdout, stderr, = cppcheck(args)
2725+
assert exitcode == 0, stdout
2726+
assert stdout == ''
2727+
assert '{}:4:1: information: Unmatched suppression: memleak [unmatchedSuppression]'.format(test_file) in stderr.splitlines()
2728+
2729+
for afile in ('test.a1', 'test.a2'):
2730+
with open(build_dir / afile, 'rt') as f:
2731+
assert f.read().count('id="unmatchedSuppression"') == 1, afile
2732+
2733+
26242734
def test_duplicate_suppression(tmp_path):
26252735
test_file = tmp_path / 'file.cpp'
26262736
with open(test_file, 'wt'):

0 commit comments

Comments
 (0)