Skip to content

Commit e57ca86

Browse files
committed
put metrics in xml output
1 parent 6f337e8 commit e57ca86

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,19 @@ int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& sup
483483
stdLogger.writeCheckersReport(supprs);
484484

485485
if (settings.outputFormat == Settings::OutputFormat::xml) {
486+
487+
if (settings.xml_version == 3) {
488+
std::cout << " <metrics>" << std::endl;
489+
for (const auto &metric : cppcheck.mFileMetrics) {
490+
std::cout << " <metric id=\"" << metric.id << "\" file=\"" << metric.file << "\"";
491+
for (const auto &pair : metric.params) {
492+
std::cout << " " << pair.first << "=\"" << pair.second << "\"";
493+
}
494+
std::cout << "/>" << std::endl;
495+
}
496+
std::cout << " </metrics>" << std::endl;
497+
}
498+
486499
stdLogger.reportErr(ErrorMessage::getXMLFooter(settings.xml_version));
487500
}
488501

lib/cppcheck.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,23 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
17641764
}
17651765
}
17661766

1767+
if (obj.count("metric") > 0) {
1768+
picojson::object metric_json = obj["metric"].get<picojson::object>();
1769+
const std::string id = metric_json["id"].get<std::string>();
1770+
const std::string file = metric_json["file"].get<std::string>();
1771+
FileMetric metric = { id, file };
1772+
1773+
picojson::object params = metric_json["params"].get<picojson::object>();
1774+
for (auto pair : params) {
1775+
const std::string key = pair.first;
1776+
const std::int64_t value = pair.second.get<std::int64_t>();
1777+
metric.params[key] = value;
1778+
}
1779+
1780+
mFileMetrics.push_back(metric);
1781+
continue;
1782+
}
1783+
17671784
errmsg.id = obj["addon"].get<std::string>() + "-" + obj["errorId"].get<std::string>();
17681785
if (misraC2023 && startsWith(errmsg.id, "misra-c2012-"))
17691786
errmsg.id = "misra-c2023-" + errmsg.id.substr(12);

lib/cppcheck.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <memory>
3232
#include <string>
3333
#include <vector>
34+
#include <map>
3435

3536
class TokenList;
3637
enum class SHOWTIME_MODES : std::uint8_t;
@@ -48,6 +49,12 @@ namespace simplecpp { class TokenList; }
4849
/// @addtogroup Core
4950
/// @{
5051

52+
struct FileMetric {
53+
std::string id;
54+
std::string file;
55+
std::map<std::string, std::int64_t> params;
56+
};
57+
5158
/**
5259
* @brief This is the base class which will use other classes to do
5360
* static code analysis for C and C++ code to find possible
@@ -139,6 +146,8 @@ class CPPCHECKLIB CppCheck {
139146
static void resetTimerResults();
140147
static void printTimerResults(SHOWTIME_MODES mode);
141148

149+
std::vector<FileMetric> mFileMetrics;
150+
142151
private:
143152
void purgedConfigurationMessage(const std::string &file, const std::string& configuration);
144153

0 commit comments

Comments
 (0)