Skip to content

Commit 5e00e72

Browse files
authored
Fix #15308 (metrics: remove duplicate function metrics (functions in headers used in several TUs)) (#9113)
1 parent 07b525d commit 5e00e72

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎cli/cppcheckexecutor.cpp‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#include <memory>
6060
#include <set>
6161
#include <sstream>
62+
#include <stdexcept>
63+
#include <string>
64+
#include <unordered_map>
6265
#include <unordered_set>
6366
#include <utility>
6467
#include <vector>
@@ -138,7 +141,43 @@ namespace {
138141

139142
void reportMetric(const std::string &metric) override
140143
{
141-
mFileMetrics.push_back(metric);
144+
if (mSettings.emitDuplicates) {
145+
mFileMetrics.push_back(metric);
146+
return;
147+
}
148+
149+
// Metrics for code in headers are reported once per translation unit.
150+
// HISCalling values are summed, other metrics are de-duplicated.
151+
if (metric.find(" id=\"HISCalling\"") != std::string::npos) {
152+
static const std::string valueAttr = " value=\"";
153+
const std::string::size_type valueStart = metric.find(valueAttr);
154+
if (valueStart != std::string::npos) {
155+
const std::string::size_type numStart = valueStart + valueAttr.size();
156+
const std::string::size_type numEnd = metric.find('"', numStart);
157+
if (numEnd != std::string::npos) {
158+
try {
159+
const long long value = std::stoll(metric.substr(numStart, numEnd - numStart));
160+
const std::string before = metric.substr(0, numStart);
161+
const std::string after = metric.substr(numEnd);
162+
const std::string key = before + after;
163+
const auto it = mSummedMetrics.find(key);
164+
if (it == mSummedMetrics.end()) {
165+
mSummedMetrics.emplace(key, std::make_pair(mFileMetrics.size(), value));
166+
mFileMetrics.push_back(metric);
167+
} else {
168+
it->second.second += value;
169+
mFileMetrics[it->second.first] = before + std::to_string(it->second.second) + after;
170+
}
171+
return;
172+
} catch (const std::exception &) {
173+
// not a number => fall through to de-duplication
174+
}
175+
}
176+
}
177+
}
178+
179+
if (mShownMetrics.insert(metric).second)
180+
mFileMetrics.push_back(metric);
142181
}
143182

144183
void reportMetrics()
@@ -248,6 +287,17 @@ namespace {
248287
*/
249288
std::vector<std::string> mFileMetrics;
250289

290+
/**
291+
* Used to filter out duplicate metrics
292+
*/
293+
std::unordered_set<std::string> mShownMetrics;
294+
295+
/**
296+
* Metrics whose values are summed (HISCalling).
297+
* Key is the metric without its value, mapped to index in mFileMetrics and summed value.
298+
*/
299+
std::unordered_map<std::string, std::pair<std::size_t, long long>> mSummedMetrics;
300+
251301
/**
252302
* The file the cached active checkers are stored in
253303
*/

‎test/cli/metrics_test.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,35 @@ def test_dummy_metrics_stdout(tmpdir):
7676
for expected in __expected_xml:
7777
assert stderr.find(expected) >= 0
7878

79+
80+
def test_duplicate_metrics(tmpdir):
81+
# Simulate a header function that is reported from several translation units
82+
addon_path = os.path.join(tmpdir, 'addon.py')
83+
with open(addon_path, 'w') as file:
84+
file.write('''
85+
import sys
86+
dumpfile = sys.argv[-1]
87+
with open(dumpfile, 'rt') as f:
88+
value = 5 if 'str="c"' in f.read() else 3
89+
print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISCalling","lineNumber":2,"value":1}}')
90+
print('{"metric":{"fileName":"util.h","function":"clamp","id":"HISStmt","lineNumber":2,"value":%d}}' % value)
91+
print('{"metric":{"fileName":"util.h","id":"HISStmtFile","value":3}}')
92+
''')
93+
source_files = []
94+
for name in ('a.c', 'b.c', 'c.c'):
95+
path = os.path.join(tmpdir, name)
96+
with open(path, 'w') as f:
97+
f.write(f'int {name[0]};\n')
98+
source_files.append(path)
99+
100+
args = [f'--addon={addon_path}', '--xml-version=3'] + source_files
101+
ret, _, stderr = cppcheck(args)
102+
assert ret == 0
103+
104+
metrics = [line.strip() for line in stderr.splitlines() if line.strip().startswith('<metric ')]
105+
assert sorted(metrics) == sorted([
106+
'<metric fileName="util.h" function="clamp" id="HISCalling" lineNumber="2" value="3"/>',
107+
'<metric fileName="util.h" function="clamp" id="HISStmt" lineNumber="2" value="3"/>',
108+
'<metric fileName="util.h" id="HISStmtFile" value="3"/>',
109+
'<metric fileName="util.h" function="clamp" id="HISStmt" lineNumber="2" value="5"/>'
110+
])

0 commit comments

Comments
 (0)