Skip to content

Commit 51f6d91

Browse files
authored
Fix #13905 (propagate metrics to xml output) (#7547)
1 parent f6bd94f commit 51f6d91

27 files changed

+2410
-2242
lines changed

cli/cmdlineparser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ namespace {
128128
reportOut(msg.toXML());
129129
}
130130

131+
void reportMetric(const std::string &metric) override
132+
{
133+
/* Not used here */
134+
(void) metric;
135+
}
136+
131137
void reportProgress(const std::string & /*filename*/, const char /*stage*/[], const std::size_t /*value*/) override
132138
{}
133139
};

cli/cppcheckexecutor.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,23 @@ namespace {
281281
*/
282282
void reportErr(const std::string &errmsg);
283283

284+
void reportMetric(const std::string &metric) override
285+
{
286+
mFileMetrics.push_back(metric);
287+
}
288+
289+
void reportMetrics()
290+
{
291+
if (!mFileMetrics.empty()) {
292+
auto &out = mErrorOutput ? *mErrorOutput : std::cerr;
293+
out << " <metrics>" << std::endl;
294+
for (const auto &metric : mFileMetrics) {
295+
out << " " << metric << std::endl;
296+
}
297+
out << " </metrics>" << std::endl;
298+
}
299+
}
300+
284301
/**
285302
* @brief Write the checkers report
286303
*/
@@ -353,6 +370,11 @@ namespace {
353370
* Coding standard guideline mapping
354371
*/
355372
std::map<std::string, std::string> mGuidelineMapping;
373+
374+
/**
375+
* File metrics
376+
*/
377+
std::vector<std::string> mFileMetrics;
356378
};
357379
}
358380

@@ -487,6 +509,8 @@ int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& sup
487509
stdLogger.writeCheckersReport(supprs);
488510

489511
if (settings.outputFormat == Settings::OutputFormat::xml) {
512+
if (settings.xml_version == 3)
513+
stdLogger.reportMetrics();
490514
stdLogger.reportErr(ErrorMessage::getXMLFooter(settings.xml_version));
491515
}
492516

cli/processexecutor.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ProcessExecutor::ProcessExecutor(const std::list<FileWithDetails> &files, const
7878
namespace {
7979
class PipeWriter : public ErrorLogger {
8080
public:
81-
enum PipeSignal : std::uint8_t {REPORT_OUT='1',REPORT_ERROR='2',REPORT_SUPPR_INLINE='3',REPORT_SUPPR='4',CHILD_END='5'};
81+
enum PipeSignal : std::uint8_t {REPORT_OUT='1',REPORT_ERROR='2',REPORT_SUPPR_INLINE='3',REPORT_SUPPR='4',CHILD_END='5',REPORT_METRIC='6'};
8282

8383
explicit PipeWriter(int pipe) : mWpipe(pipe) {}
8484

@@ -100,6 +100,10 @@ namespace {
100100
}
101101
}
102102

103+
void reportMetric(const std::string &metric) override {
104+
writeToPipe(REPORT_METRIC, metric);
105+
}
106+
103107
void writeEnd(const std::string& str) const {
104108
writeToPipe(CHILD_END, str);
105109
}
@@ -179,7 +183,8 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
179183
type != PipeWriter::REPORT_ERROR &&
180184
type != PipeWriter::REPORT_SUPPR_INLINE &&
181185
type != PipeWriter::REPORT_SUPPR &&
182-
type != PipeWriter::CHILD_END) {
186+
type != PipeWriter::CHILD_END &&
187+
type != PipeWriter::REPORT_METRIC) {
183188
std::cerr << "#### ThreadExecutor::handleRead(" << filename << ") invalid type " << int(type) << std::endl;
184189
std::exit(EXIT_FAILURE);
185190
}
@@ -256,6 +261,8 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
256261
} else if (type == PipeWriter::CHILD_END) {
257262
result += std::stoi(buf);
258263
res = false;
264+
} else if (type == PipeWriter::REPORT_METRIC) {
265+
mErrorLogger.reportMetric(buf);
259266
}
260267

261268
return res;

cli/threadexecutor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class SyncLogForwarder : public ErrorLogger
6666
mErrorLogger.reportErr(msg);
6767
}
6868

69+
void reportMetric(const std::string &metric) override {
70+
std::lock_guard<std::mutex> lg(mReportSync);
71+
mErrorLogger.reportMetric(metric);
72+
}
73+
6974
void reportStatus(std::size_t fileindex, std::size_t filecount, std::size_t sizedone, std::size_t sizetotal) {
7075
std::lock_guard<std::mutex> lg(mReportSync);
7176
mThreadExecutor.reportStatus(fileindex, filecount, sizedone, sizetotal);

democlient/democlient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class CppcheckExecutor : public ErrorLogger {
8181
if (logfile != nullptr)
8282
std::fprintf(logfile, "%s\n", s.c_str());
8383
}
84+
void reportMetric(const std::string & /*metric*/) override {}
8485

8586
void reportProgress(const std::string& /*filename*/,
8687
const char /*stage*/[],

0 commit comments

Comments
 (0)