From cb87084d4cdd0cab86585226ed4ee4ebc964c8bf Mon Sep 17 00:00:00 2001 From: uthmanna Date: Mon, 16 Jun 2025 13:18:55 +0200 Subject: [PATCH 1/2] [llvm-cov] Export decision coverage to output json --- .../llvm/ProfileData/Coverage/CoverageMapping.h | 3 +++ llvm/tools/llvm-cov/CoverageExporterJson.cpp | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h index d1230b0ba7c58..c9854e08a38e8 100644 --- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -494,6 +494,9 @@ struct MCDCRecord { return TV[TestVectorIndex].first[PosToID[Condition]]; } + /// Return the executed test vectors. + const TestVectors &getTV() const { return TV; } + /// Return the Result evaluation for an executed test vector. /// See MCDCRecordProcessor::RecordTestVector(). CondState getTVResult(unsigned TestVectorIndex) { diff --git a/llvm/tools/llvm-cov/CoverageExporterJson.cpp b/llvm/tools/llvm-cov/CoverageExporterJson.cpp index 4088c1b053aa8..cae7b86bc6b0c 100644 --- a/llvm/tools/llvm-cov/CoverageExporterJson.cpp +++ b/llvm/tools/llvm-cov/CoverageExporterJson.cpp @@ -62,7 +62,7 @@ #include /// 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" @@ -108,10 +108,22 @@ json::Array gatherConditions(const coverage::MCDCRecord &Record) { return Conditions; } +std::pair getDecisions(const coverage::MCDCRecord &Record) { + const coverage::MCDCRecord::TestVectors &TestVectors = Record.getTV(); + const unsigned TrueConditions = + std::count_if(TestVectors.begin(), TestVectors.end(), [](const auto &TV) { + return TV.second == coverage::MCDCRecord::CondState::MCDC_True; + }); + + return {TrueConditions, TestVectors.size() - TrueConditions}; +} + json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) { const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion(); + const auto [TrueConditions, FalseConditions] = getDecisions(Record); return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd, - CMR.ColumnEnd, CMR.ExpandedFileID, int64_t(CMR.Kind), + CMR.ColumnEnd, TrueConditions, FalseConditions, + CMR.ExpandedFileID, int64_t(CMR.Kind), gatherConditions(Record)}); } From 75cef15d9e6a81eae1cb52929c31e683cbbe297a Mon Sep 17 00:00:00 2001 From: uthmanna Date: Tue, 17 Jun 2025 09:44:01 +0200 Subject: [PATCH 2/2] Move getDecisions to MCDCRecord --- .../llvm/ProfileData/Coverage/CoverageMapping.h | 13 +++++++++++-- llvm/tools/llvm-cov/CoverageExporterJson.cpp | 14 ++------------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h index c9854e08a38e8..8e6180be25b51 100644 --- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -31,6 +31,7 @@ #include "llvm/Support/Endian.h" #include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" +#include #include #include #include @@ -494,8 +495,16 @@ struct MCDCRecord { return TV[TestVectorIndex].first[PosToID[Condition]]; } - /// Return the executed test vectors. - const TestVectors &getTV() const { return TV; } + /// Return the number of True and False decisions for all executed test + /// vectors. + std::pair 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(). diff --git a/llvm/tools/llvm-cov/CoverageExporterJson.cpp b/llvm/tools/llvm-cov/CoverageExporterJson.cpp index cae7b86bc6b0c..024693a24cc23 100644 --- a/llvm/tools/llvm-cov/CoverageExporterJson.cpp +++ b/llvm/tools/llvm-cov/CoverageExporterJson.cpp @@ -108,21 +108,11 @@ json::Array gatherConditions(const coverage::MCDCRecord &Record) { return Conditions; } -std::pair getDecisions(const coverage::MCDCRecord &Record) { - const coverage::MCDCRecord::TestVectors &TestVectors = Record.getTV(); - const unsigned TrueConditions = - std::count_if(TestVectors.begin(), TestVectors.end(), [](const auto &TV) { - return TV.second == coverage::MCDCRecord::CondState::MCDC_True; - }); - - return {TrueConditions, TestVectors.size() - TrueConditions}; -} - json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) { const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion(); - const auto [TrueConditions, FalseConditions] = getDecisions(Record); + const auto [TrueDecisions, FalseDecisions] = Record.getDecisions(); return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd, - CMR.ColumnEnd, TrueConditions, FalseConditions, + CMR.ColumnEnd, TrueDecisions, FalseDecisions, CMR.ExpandedFileID, int64_t(CMR.Kind), gatherConditions(Record)}); }