|
59 | 59 | #include <memory> |
60 | 60 | #include <set> |
61 | 61 | #include <sstream> |
| 62 | +#include <stdexcept> |
| 63 | +#include <string> |
| 64 | +#include <unordered_map> |
62 | 65 | #include <unordered_set> |
63 | 66 | #include <utility> |
64 | 67 | #include <vector> |
@@ -138,7 +141,43 @@ namespace { |
138 | 141 |
|
139 | 142 | void reportMetric(const std::string &metric) override |
140 | 143 | { |
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); |
142 | 181 | } |
143 | 182 |
|
144 | 183 | void reportMetrics() |
@@ -248,6 +287,17 @@ namespace { |
248 | 287 | */ |
249 | 288 | std::vector<std::string> mFileMetrics; |
250 | 289 |
|
| 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 | + |
251 | 301 | /** |
252 | 302 | * The file the cached active checkers are stored in |
253 | 303 | */ |
|
0 commit comments