Skip to content

Commit da5c442

Browse files
authored
Reland "[llvm-cov] Export decision coverage to output json (#144335)" (#145325)
Tests fixed 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
1 parent bb2bd5f commit da5c442

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/Support/Endian.h"
3232
#include "llvm/Support/Error.h"
3333
#include "llvm/Support/raw_ostream.h"
34+
#include <algorithm>
3435
#include <cassert>
3536
#include <cstdint>
3637
#include <iterator>
@@ -494,6 +495,17 @@ struct MCDCRecord {
494495
return TV[TestVectorIndex].first[PosToID[Condition]];
495496
}
496497

498+
/// Return the number of True and False decisions for all executed test
499+
/// vectors.
500+
std::pair<unsigned, unsigned> getDecisions() const {
501+
const unsigned TrueDecisions =
502+
std::count_if(TV.begin(), TV.end(), [](const auto &TestVec) {
503+
return TestVec.second == CondState::MCDC_True;
504+
});
505+
506+
return {TrueDecisions, TV.size() - TrueDecisions};
507+
}
508+
497509
/// Return the Result evaluation for an executed test vector.
498510
/// See MCDCRecordProcessor::RecordTestVector().
499511
CondState getTVResult(unsigned TestVectorIndex) {

llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ CHECK-SAME: "mcdc":{"count":0,"covered":0,"notcovered":0,"percent":0},
3333
CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}}
3434
CHECK-SAME: ],
3535
CHECK-SAME: "type":"llvm.coverage.json.export"
36-
CHECK-SAME: "version":"2.0.1"
36+
CHECK-SAME: "version":"3.0.0"

llvm/test/tools/llvm-cov/mcdc-export-json.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: llvm-profdata merge %S/Inputs/mcdc-general.proftext -o %t.profdata
22
// RUN: llvm-cov export --format=text %S/Inputs/mcdc-general.o -instr-profile %t.profdata | FileCheck %s
33

4-
// CHECK: 12,7,12,27,0,5,[true,true,true,true]
5-
// CHECK: 15,7,15,13,0,5,[true,true]
6-
// CHECK: 15,19,15,25,0,5,[true,false]
7-
// CHECK: 18,7,19,15,0,5,[true,true,false,true]
4+
// CHECK: 12,7,12,27,2,4,0,5,[true,true,true,true]
5+
// CHECK: 15,7,15,13,1,2,0,5,[true,true]
6+
// CHECK: 15,19,15,25,1,1,0,5,[true,false]
7+
// CHECK: 18,7,19,15,1,3,0,5,[true,true,false,true]
88
// CHECK: "mcdc":{"count":12,"covered":10,"notcovered":2,"percent":83.333333333333343}
99

1010
Instructions for regenerating the test:

llvm/tools/llvm-cov/CoverageExporterJson.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#include <utility>
6363

6464
/// The semantic version combined as a string.
65-
#define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.1"
65+
#define LLVM_COVERAGE_EXPORT_JSON_STR "3.0.0"
6666

6767
/// Unique type identifier for JSON coverage export.
6868
#define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
@@ -110,8 +110,10 @@ json::Array gatherConditions(const coverage::MCDCRecord &Record) {
110110

111111
json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) {
112112
const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion();
113+
const auto [TrueDecisions, FalseDecisions] = Record.getDecisions();
113114
return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd,
114-
CMR.ColumnEnd, CMR.ExpandedFileID, int64_t(CMR.Kind),
115+
CMR.ColumnEnd, TrueDecisions, FalseDecisions,
116+
CMR.ExpandedFileID, int64_t(CMR.Kind),
115117
gatherConditions(Record)});
116118
}
117119

0 commit comments

Comments
 (0)