From 3f4b73e182d058adbcd25ee98c23239cb1da1a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 15:49:32 +0100 Subject: [PATCH 1/2] Fix #15308 (metrics: remove duplicate function metrics (functions in headers used in several TUs)) --- cli/cppcheckexecutor.cpp | 52 +++++++++++++++++++++++++++++++++++++++- test/cli/metrics_test.py | 31 ++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index d590b6c48d3..705e3accbdd 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -59,6 +59,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -138,7 +141,43 @@ namespace { void reportMetric(const std::string &metric) override { - mFileMetrics.push_back(metric); + if (mSettings.emitDuplicates) { + mFileMetrics.push_back(metric); + return; + } + + // Metrics for code in headers are reported once per translation unit. + // HISCalling values are summed, other metrics are de-duplicated. + if (metric.find(" id=\"HISCalling\"") != std::string::npos) { + static const std::string valueAttr = " value=\""; + const std::string::size_type valueStart = metric.find(valueAttr); + if (valueStart != std::string::npos) { + const std::string::size_type numStart = valueStart + valueAttr.size(); + const std::string::size_type numEnd = metric.find('"', numStart); + if (numEnd != std::string::npos) { + try { + const long long value = std::stoll(metric.substr(numStart, numEnd - numStart)); + const std::string before = metric.substr(0, numStart); + const std::string after = metric.substr(numEnd); + const std::string key = before + after; + const auto it = mSummedMetrics.find(key); + if (it == mSummedMetrics.end()) { + mSummedMetrics.emplace(key, std::make_pair(mFileMetrics.size(), value)); + mFileMetrics.push_back(metric); + } else { + it->second.second += value; + mFileMetrics[it->second.first] = before + std::to_string(it->second.second) + after; + } + return; + } catch (const std::exception &) { + // not a number => fall through to de-duplication + } + } + } + } + + if (mShownMetrics.insert(metric).second) + mFileMetrics.push_back(metric); } void reportMetrics() @@ -248,6 +287,17 @@ namespace { */ std::vector mFileMetrics; + /** + * Used to filter out duplicate metrics + */ + std::unordered_set mShownMetrics; + + /** + * Metrics whose values are summed (HISCalling). + * Key is the metric without its value, mapped to index in mFileMetrics and summed value. + */ + std::unordered_map> mSummedMetrics; + /** * The file the cached active checkers are stored in */ diff --git a/test/cli/metrics_test.py b/test/cli/metrics_test.py index 6f5c7041dab..3ad93f2aa8e 100644 --- a/test/cli/metrics_test.py +++ b/test/cli/metrics_test.py @@ -76,3 +76,34 @@ def test_dummy_metrics_stdout(tmpdir): for expected in __expected_xml: assert stderr.find(expected) >= 0 + +def test_duplicate_metrics(tmpdir): + # Simulate a header function that is reported from several translation units + addon_path = os.path.join(tmpdir, 'addon.py') + with open(addon_path, 'w') as file: + file.write(''' +import sys +dumpfile = sys.argv[-1] +value = 5 if 'c.c' in dumpfile else 3 +print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISCalling","lineNumber":2,"value":1}}') +print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISStmt","lineNumber":2,"value":%d}}' % value) +print('{"metric":{"fileName":"util.h","id":"HISStmtFile","value":3}}') +''') + source_files = [] + for name in ('a.c', 'b.c', 'c.c'): + path = os.path.join(tmpdir, name) + with open(path, 'w') as _: + pass + source_files.append(path) + + args = [f'--addon={addon_path}', '--xml-version=3'] + source_files + ret, _, stderr = cppcheck(args) + assert ret == 0 + + metrics = [line.strip() for line in stderr.splitlines() if line.strip().startswith('', + '', + '', + '' + ] From 3ed82731d9cee1aa73fc19d44e9ff1e52f09909d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 3 Dec 2026 18:42:18 +0100 Subject: [PATCH 2/2] metrics_test: make test_duplicate_metrics work with injected build dir and clang Co-Authored-By: Claude Opus 5.5 --- test/cli/metrics_test.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/cli/metrics_test.py b/test/cli/metrics_test.py index 3ad93f2aa8e..8a47aac1b96 100644 --- a/test/cli/metrics_test.py +++ b/test/cli/metrics_test.py @@ -84,7 +84,8 @@ def test_duplicate_metrics(tmpdir): file.write(''' import sys dumpfile = sys.argv[-1] -value = 5 if 'c.c' in dumpfile else 3 +with open(dumpfile, 'rt') as f: + value = 5 if 'str="c"' in f.read() else 3 print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISCalling","lineNumber":2,"value":1}}') print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISStmt","lineNumber":2,"value":%d}}' % value) print('{"metric":{"fileName":"util.h","id":"HISStmtFile","value":3}}') @@ -92,8 +93,8 @@ def test_duplicate_metrics(tmpdir): source_files = [] for name in ('a.c', 'b.c', 'c.c'): path = os.path.join(tmpdir, name) - with open(path, 'w') as _: - pass + with open(path, 'w') as f: + f.write(f'int {name[0]};\n') source_files.append(path) args = [f'--addon={addon_path}', '--xml-version=3'] + source_files @@ -101,9 +102,9 @@ def test_duplicate_metrics(tmpdir): assert ret == 0 metrics = [line.strip() for line in stderr.splitlines() if line.strip().startswith('', '', '', '' - ] + ])