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..8a47aac1b96 100644 --- a/test/cli/metrics_test.py +++ b/test/cli/metrics_test.py @@ -76,3 +76,35 @@ 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] +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}}') +''') + source_files = [] + for name in ('a.c', 'b.c', 'c.c'): + path = os.path.join(tmpdir, name) + 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 + ret, _, stderr = cppcheck(args) + assert ret == 0 + + metrics = [line.strip() for line in stderr.splitlines() if line.strip().startswith('', + '', + '', + '' + ])