[llvm-cov] Export decision coverage to output json (#144335)

This commit adds decision coverage counts derived from MC/DC test vector execution to the JSON output of llvm-cov, as
discussed here: [Missing Decision Coverage (DC) in output
json](https://discourse.llvm.org/t/missing-decision-coverage-dc-in-output-json/86783)
with @evodius96

---------

Co-authored-by: uthmanna <andre.uthmann@vector.com>
This commit is contained in:
uthmanna
2025-06-18 21:00:10 +02:00
committed by GitHub
parent 8c3fbaf0ee
commit ab6beeca9c
2 changed files with 16 additions and 2 deletions

View File

@@ -31,6 +31,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iterator>
@@ -494,6 +495,17 @@ public:
return TV[TestVectorIndex].first[PosToID[Condition]];
}
/// Return the number of True and False decisions for all executed test
/// vectors.
std::pair<unsigned, unsigned> getDecisions() const {
const unsigned TrueDecisions =
std::count_if(TV.begin(), TV.end(), [](const auto &TestVec) {
return TestVec.second == CondState::MCDC_True;
});
return {TrueDecisions, TV.size() - TrueDecisions};
}
/// Return the Result evaluation for an executed test vector.
/// See MCDCRecordProcessor::RecordTestVector().
CondState getTVResult(unsigned TestVectorIndex) {

View File

@@ -62,7 +62,7 @@
#include <utility>
/// The semantic version combined as a string.
#define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.1"
#define LLVM_COVERAGE_EXPORT_JSON_STR "3.0.0"
/// Unique type identifier for JSON coverage export.
#define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
@@ -110,8 +110,10 @@ json::Array gatherConditions(const coverage::MCDCRecord &Record) {
json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) {
const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion();
const auto [TrueDecisions, FalseDecisions] = Record.getDecisions();
return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd,
CMR.ColumnEnd, CMR.ExpandedFileID, int64_t(CMR.Kind),
CMR.ColumnEnd, TrueDecisions, FalseDecisions,
CMR.ExpandedFileID, int64_t(CMR.Kind),
gatherConditions(Record)});
}