| 1 | //===- CoverageExporterJson.cpp - Code coverage export --------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements export of code coverage data to JSON. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // |
| 15 | // The json code coverage export follows the following format |
| 16 | // Root: dict => Root Element containing metadata |
| 17 | // -- Data: array => Homogeneous array of one or more export objects |
| 18 | // -- Export: dict => Json representation of one CoverageMapping |
| 19 | // -- Files: array => List of objects describing coverage for files |
| 20 | // -- File: dict => Coverage for a single file |
| 21 | // -- Branches: array => List of Branches in the file |
| 22 | // -- Branch: dict => Describes a branch of the file with counters |
| 23 | // -- MCDC Records: array => List of MCDC records in the file |
| 24 | // -- MCDC Values: array => List of T/F covered condition values |
| 25 | // -- Segments: array => List of Segments contained in the file |
| 26 | // -- Segment: dict => Describes a segment of the file with a counter |
| 27 | // -- Expansions: array => List of expansion records |
| 28 | // -- Expansion: dict => Object that descibes a single expansion |
| 29 | // -- CountedRegion: dict => The region to be expanded |
| 30 | // -- TargetRegions: array => List of Regions in the expansion |
| 31 | // -- CountedRegion: dict => Single Region in the expansion |
| 32 | // -- Branches: array => List of Branches in the expansion |
| 33 | // -- Branch: dict => Describes a branch in expansion and counters |
| 34 | // -- Summary: dict => Object summarizing the coverage for this file |
| 35 | // -- LineCoverage: dict => Object summarizing line coverage |
| 36 | // -- FunctionCoverage: dict => Object summarizing function coverage |
| 37 | // -- RegionCoverage: dict => Object summarizing region coverage |
| 38 | // -- BranchCoverage: dict => Object summarizing branch coverage |
| 39 | // -- MCDCCoverage: dict => Object summarizing MC/DC coverage |
| 40 | // -- Functions: array => List of objects describing coverage for functions |
| 41 | // -- Function: dict => Coverage info for a single function |
| 42 | // -- Filenames: array => List of filenames that the function relates to |
| 43 | // -- Summary: dict => Object summarizing the coverage for the entire binary |
| 44 | // -- LineCoverage: dict => Object summarizing line coverage |
| 45 | // -- FunctionCoverage: dict => Object summarizing function coverage |
| 46 | // -- InstantiationCoverage: dict => Object summarizing inst. coverage |
| 47 | // -- RegionCoverage: dict => Object summarizing region coverage |
| 48 | // -- BranchCoverage: dict => Object summarizing branch coverage |
| 49 | // -- MCDCCoverage: dict => Object summarizing MC/DC coverage |
| 50 | // |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | #include "CoverageExporterJson.h" |
| 54 | #include "CoverageReport.h" |
| 55 | #include "llvm/ADT/StringRef.h" |
| 56 | #include "llvm/Support/JSON.h" |
| 57 | #include "llvm/Support/ThreadPool.h" |
| 58 | #include "llvm/Support/Threading.h" |
| 59 | #include <algorithm> |
| 60 | #include <limits> |
| 61 | #include <mutex> |
| 62 | #include <utility> |
| 63 | |
| 64 | /// The semantic version combined as a string. |
| 65 | #define LLVM_COVERAGE_EXPORT_JSON_STR "3.0.0" |
| 66 | |
| 67 | /// Unique type identifier for JSON coverage export. |
| 68 | #define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export" |
| 69 | |
| 70 | using namespace llvm; |
| 71 | |
| 72 | namespace { |
| 73 | |
| 74 | // The JSON library accepts int64_t, but profiling counts are stored as uint64_t. |
| 75 | // Therefore we need to explicitly convert from unsigned to signed, since a naive |
| 76 | // cast is implementation-defined behavior when the unsigned value cannot be |
| 77 | // represented as a signed value. We choose to clamp the values to preserve the |
| 78 | // invariant that counts are always >= 0. |
| 79 | int64_t clamp_uint64_to_int64(uint64_t u) { |
| 80 | return std::min(a: u, b: static_cast<uint64_t>(std::numeric_limits<int64_t>::max())); |
| 81 | } |
| 82 | |
| 83 | json::Array renderSegment(const coverage::CoverageSegment &Segment) { |
| 84 | return json::Array({Segment.Line, Segment.Col, |
| 85 | clamp_uint64_to_int64(u: Segment.Count), Segment.HasCount, |
| 86 | Segment.IsRegionEntry, Segment.IsGapRegion}); |
| 87 | } |
| 88 | |
| 89 | json::Array renderRegion(const coverage::CountedRegion &Region) { |
| 90 | return json::Array({Region.LineStart, Region.ColumnStart, Region.LineEnd, |
| 91 | Region.ColumnEnd, clamp_uint64_to_int64(u: Region.ExecutionCount), |
| 92 | Region.FileID, Region.ExpandedFileID, |
| 93 | int64_t(Region.Kind)}); |
| 94 | } |
| 95 | |
| 96 | json::Array renderBranch(const coverage::CountedRegion &Region) { |
| 97 | return json::Array( |
| 98 | {Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd, |
| 99 | clamp_uint64_to_int64(u: Region.ExecutionCount), |
| 100 | clamp_uint64_to_int64(u: Region.FalseExecutionCount), Region.FileID, |
| 101 | Region.ExpandedFileID, int64_t(Region.Kind)}); |
| 102 | } |
| 103 | |
| 104 | json::Array gatherConditions(const coverage::MCDCRecord &Record) { |
| 105 | json::Array Conditions; |
| 106 | for (unsigned c = 0; c < Record.getNumConditions(); c++) |
| 107 | Conditions.push_back(E: Record.isConditionIndependencePairCovered(Condition: c)); |
| 108 | return Conditions; |
| 109 | } |
| 110 | |
| 111 | json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) { |
| 112 | const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion(); |
| 113 | const auto [TrueDecisions, FalseDecisions] = Record.getDecisions(); |
| 114 | return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd, |
| 115 | CMR.ColumnEnd, TrueDecisions, FalseDecisions, |
| 116 | CMR.ExpandedFileID, int64_t(CMR.Kind), |
| 117 | gatherConditions(Record)}); |
| 118 | } |
| 119 | |
| 120 | json::Array renderRegions(ArrayRef<coverage::CountedRegion> Regions) { |
| 121 | json::Array RegionArray; |
| 122 | for (const auto &Region : Regions) |
| 123 | RegionArray.push_back(E: renderRegion(Region)); |
| 124 | return RegionArray; |
| 125 | } |
| 126 | |
| 127 | json::Array renderBranchRegions(ArrayRef<coverage::CountedRegion> Regions) { |
| 128 | json::Array RegionArray; |
| 129 | for (const auto &Region : Regions) |
| 130 | if (!Region.TrueFolded || !Region.FalseFolded) |
| 131 | RegionArray.push_back(E: renderBranch(Region)); |
| 132 | return RegionArray; |
| 133 | } |
| 134 | |
| 135 | json::Array renderMCDCRecords(ArrayRef<coverage::MCDCRecord> Records) { |
| 136 | json::Array RecordArray; |
| 137 | for (auto &Record : Records) |
| 138 | RecordArray.push_back(E: renderMCDCRecord(Record)); |
| 139 | return RecordArray; |
| 140 | } |
| 141 | |
| 142 | std::vector<llvm::coverage::CountedRegion> |
| 143 | collectNestedBranches(const coverage::CoverageMapping &Coverage, |
| 144 | ArrayRef<llvm::coverage::ExpansionRecord> Expansions) { |
| 145 | std::vector<llvm::coverage::CountedRegion> Branches; |
| 146 | for (const auto &Expansion : Expansions) { |
| 147 | auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); |
| 148 | |
| 149 | // Recursively collect branches from nested expansions. |
| 150 | auto NestedExpansions = ExpansionCoverage.getExpansions(); |
| 151 | auto NestedExBranches = collectNestedBranches(Coverage, Expansions: NestedExpansions); |
| 152 | append_range(C&: Branches, R&: NestedExBranches); |
| 153 | |
| 154 | // Add branches from this level of expansion. |
| 155 | auto ExBranches = ExpansionCoverage.getBranches(); |
| 156 | for (auto B : ExBranches) |
| 157 | if (B.FileID == Expansion.FileID) |
| 158 | Branches.push_back(x: B); |
| 159 | } |
| 160 | |
| 161 | return Branches; |
| 162 | } |
| 163 | |
| 164 | json::Object renderExpansion(const coverage::CoverageMapping &Coverage, |
| 165 | const coverage::ExpansionRecord &Expansion) { |
| 166 | std::vector<llvm::coverage::ExpansionRecord> Expansions = {Expansion}; |
| 167 | return json::Object( |
| 168 | {{.K: "filenames" , .V: json::Array(Expansion.Function.Filenames)}, |
| 169 | // Mark the beginning and end of this expansion in the source file. |
| 170 | {.K: "source_region" , .V: renderRegion(Region: Expansion.Region)}, |
| 171 | // Enumerate the coverage information for the expansion. |
| 172 | {.K: "target_regions" , .V: renderRegions(Regions: Expansion.Function.CountedRegions)}, |
| 173 | // Enumerate the branch coverage information for the expansion. |
| 174 | {.K: "branches" , |
| 175 | .V: renderBranchRegions(Regions: collectNestedBranches(Coverage, Expansions))}}); |
| 176 | } |
| 177 | |
| 178 | json::Object renderSummary(const FileCoverageSummary &Summary) { |
| 179 | return json::Object( |
| 180 | {{.K: "lines" , |
| 181 | .V: json::Object({{.K: "count" , .V: int64_t(Summary.LineCoverage.getNumLines())}, |
| 182 | {.K: "covered" , .V: int64_t(Summary.LineCoverage.getCovered())}, |
| 183 | {.K: "percent" , .V: Summary.LineCoverage.getPercentCovered()}})}, |
| 184 | {.K: "functions" , |
| 185 | .V: json::Object( |
| 186 | {{.K: "count" , .V: int64_t(Summary.FunctionCoverage.getNumFunctions())}, |
| 187 | {.K: "covered" , .V: int64_t(Summary.FunctionCoverage.getExecuted())}, |
| 188 | {.K: "percent" , .V: Summary.FunctionCoverage.getPercentCovered()}})}, |
| 189 | {.K: "instantiations" , |
| 190 | .V: json::Object( |
| 191 | {{.K: "count" , |
| 192 | .V: int64_t(Summary.InstantiationCoverage.getNumFunctions())}, |
| 193 | {.K: "covered" , .V: int64_t(Summary.InstantiationCoverage.getExecuted())}, |
| 194 | {.K: "percent" , .V: Summary.InstantiationCoverage.getPercentCovered()}})}, |
| 195 | {.K: "regions" , |
| 196 | .V: json::Object( |
| 197 | {{.K: "count" , .V: int64_t(Summary.RegionCoverage.getNumRegions())}, |
| 198 | {.K: "covered" , .V: int64_t(Summary.RegionCoverage.getCovered())}, |
| 199 | {.K: "notcovered" , .V: int64_t(Summary.RegionCoverage.getNumRegions() - |
| 200 | Summary.RegionCoverage.getCovered())}, |
| 201 | {.K: "percent" , .V: Summary.RegionCoverage.getPercentCovered()}})}, |
| 202 | {.K: "branches" , |
| 203 | .V: json::Object( |
| 204 | {{.K: "count" , .V: int64_t(Summary.BranchCoverage.getNumBranches())}, |
| 205 | {.K: "covered" , .V: int64_t(Summary.BranchCoverage.getCovered())}, |
| 206 | {.K: "notcovered" , .V: int64_t(Summary.BranchCoverage.getNumBranches() - |
| 207 | Summary.BranchCoverage.getCovered())}, |
| 208 | {.K: "percent" , .V: Summary.BranchCoverage.getPercentCovered()}})}, |
| 209 | {.K: "mcdc" , |
| 210 | .V: json::Object( |
| 211 | {{.K: "count" , .V: int64_t(Summary.MCDCCoverage.getNumPairs())}, |
| 212 | {.K: "covered" , .V: int64_t(Summary.MCDCCoverage.getCoveredPairs())}, |
| 213 | {.K: "notcovered" , .V: int64_t(Summary.MCDCCoverage.getNumPairs() - |
| 214 | Summary.MCDCCoverage.getCoveredPairs())}, |
| 215 | {.K: "percent" , .V: Summary.MCDCCoverage.getPercentCovered()}})}}); |
| 216 | } |
| 217 | |
| 218 | json::Array renderFileExpansions(const coverage::CoverageMapping &Coverage, |
| 219 | const coverage::CoverageData &FileCoverage, |
| 220 | const FileCoverageSummary &FileReport) { |
| 221 | json::Array ExpansionArray; |
| 222 | for (const auto &Expansion : FileCoverage.getExpansions()) |
| 223 | ExpansionArray.push_back(E: renderExpansion(Coverage, Expansion)); |
| 224 | return ExpansionArray; |
| 225 | } |
| 226 | |
| 227 | json::Array renderFileSegments(const coverage::CoverageData &FileCoverage, |
| 228 | const FileCoverageSummary &FileReport) { |
| 229 | json::Array SegmentArray; |
| 230 | for (const auto &Segment : FileCoverage) |
| 231 | SegmentArray.push_back(E: renderSegment(Segment)); |
| 232 | return SegmentArray; |
| 233 | } |
| 234 | |
| 235 | json::Array renderFileBranches(const coverage::CoverageData &FileCoverage, |
| 236 | const FileCoverageSummary &FileReport) { |
| 237 | json::Array BranchArray; |
| 238 | for (const auto &Branch : FileCoverage.getBranches()) |
| 239 | BranchArray.push_back(E: renderBranch(Region: Branch)); |
| 240 | return BranchArray; |
| 241 | } |
| 242 | |
| 243 | json::Array renderFileMCDC(const coverage::CoverageData &FileCoverage, |
| 244 | const FileCoverageSummary &FileReport) { |
| 245 | json::Array MCDCRecordArray; |
| 246 | for (const auto &Record : FileCoverage.getMCDCRecords()) |
| 247 | MCDCRecordArray.push_back(E: renderMCDCRecord(Record)); |
| 248 | return MCDCRecordArray; |
| 249 | } |
| 250 | |
| 251 | json::Object renderFile(const coverage::CoverageMapping &Coverage, |
| 252 | const std::string &Filename, |
| 253 | const FileCoverageSummary &FileReport, |
| 254 | const CoverageViewOptions &Options) { |
| 255 | json::Object File({{.K: "filename" , .V: Filename}}); |
| 256 | if (!Options.ExportSummaryOnly) { |
| 257 | // Calculate and render detailed coverage information for given file. |
| 258 | auto FileCoverage = Coverage.getCoverageForFile(Filename); |
| 259 | File["segments" ] = renderFileSegments(FileCoverage, FileReport); |
| 260 | File["branches" ] = renderFileBranches(FileCoverage, FileReport); |
| 261 | File["mcdc_records" ] = renderFileMCDC(FileCoverage, FileReport); |
| 262 | if (!Options.SkipExpansions) { |
| 263 | File["expansions" ] = |
| 264 | renderFileExpansions(Coverage, FileCoverage, FileReport); |
| 265 | } |
| 266 | } |
| 267 | File["summary" ] = renderSummary(Summary: FileReport); |
| 268 | return File; |
| 269 | } |
| 270 | |
| 271 | json::Array renderFiles(const coverage::CoverageMapping &Coverage, |
| 272 | ArrayRef<std::string> SourceFiles, |
| 273 | ArrayRef<FileCoverageSummary> FileReports, |
| 274 | const CoverageViewOptions &Options) { |
| 275 | ThreadPoolStrategy S = hardware_concurrency(ThreadCount: Options.NumThreads); |
| 276 | if (Options.NumThreads == 0) { |
| 277 | // If NumThreads is not specified, create one thread for each input, up to |
| 278 | // the number of hardware cores. |
| 279 | S = heavyweight_hardware_concurrency(ThreadCount: SourceFiles.size()); |
| 280 | S.Limit = true; |
| 281 | } |
| 282 | DefaultThreadPool Pool(S); |
| 283 | json::Array FileArray; |
| 284 | std::mutex FileArrayMutex; |
| 285 | |
| 286 | for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) { |
| 287 | auto &SourceFile = SourceFiles[I]; |
| 288 | auto &FileReport = FileReports[I]; |
| 289 | Pool.async(F: [&] { |
| 290 | auto File = renderFile(Coverage, Filename: SourceFile, FileReport, Options); |
| 291 | { |
| 292 | std::lock_guard<std::mutex> Lock(FileArrayMutex); |
| 293 | FileArray.push_back(E: std::move(File)); |
| 294 | } |
| 295 | }); |
| 296 | } |
| 297 | Pool.wait(); |
| 298 | return FileArray; |
| 299 | } |
| 300 | |
| 301 | json::Array renderFunctions( |
| 302 | const iterator_range<coverage::FunctionRecordIterator> &Functions) { |
| 303 | json::Array FunctionArray; |
| 304 | for (const auto &F : Functions) |
| 305 | FunctionArray.push_back( |
| 306 | E: json::Object({{.K: "name" , .V: F.Name}, |
| 307 | {.K: "count" , .V: clamp_uint64_to_int64(u: F.ExecutionCount)}, |
| 308 | {.K: "regions" , .V: renderRegions(Regions: F.CountedRegions)}, |
| 309 | {.K: "branches" , .V: renderBranchRegions(Regions: F.CountedBranchRegions)}, |
| 310 | {.K: "mcdc_records" , .V: renderMCDCRecords(Records: F.MCDCRecords)}, |
| 311 | {.K: "filenames" , .V: json::Array(F.Filenames)}})); |
| 312 | return FunctionArray; |
| 313 | } |
| 314 | |
| 315 | } // end anonymous namespace |
| 316 | |
| 317 | void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) { |
| 318 | std::vector<std::string> SourceFiles; |
| 319 | for (StringRef SF : Coverage.getUniqueSourceFiles()) { |
| 320 | if (!IgnoreFilters.matchesFilename(Filename: SF)) |
| 321 | SourceFiles.emplace_back(args&: SF); |
| 322 | } |
| 323 | renderRoot(SourceFiles); |
| 324 | } |
| 325 | |
| 326 | void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) { |
| 327 | FileCoverageSummary Totals = FileCoverageSummary("Totals" ); |
| 328 | auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals, |
| 329 | Files: SourceFiles, Options); |
| 330 | auto Files = renderFiles(Coverage, SourceFiles, FileReports, Options); |
| 331 | // Sort files in order of their names. |
| 332 | llvm::sort(C&: Files, Comp: [](const json::Value &A, const json::Value &B) { |
| 333 | const json::Object *ObjA = A.getAsObject(); |
| 334 | const json::Object *ObjB = B.getAsObject(); |
| 335 | assert(ObjA != nullptr && "Value A was not an Object" ); |
| 336 | assert(ObjB != nullptr && "Value B was not an Object" ); |
| 337 | const StringRef FilenameA = *ObjA->getString(K: "filename" ); |
| 338 | const StringRef FilenameB = *ObjB->getString(K: "filename" ); |
| 339 | return FilenameA.compare(RHS: FilenameB) < 0; |
| 340 | }); |
| 341 | auto Export = json::Object( |
| 342 | {{.K: "files" , .V: std::move(Files)}, {.K: "totals" , .V: renderSummary(Summary: Totals)}}); |
| 343 | // Skip functions-level information if necessary. |
| 344 | if (!Options.ExportSummaryOnly && !Options.SkipFunctions) |
| 345 | Export["functions" ] = renderFunctions(Functions: Coverage.getCoveredFunctions()); |
| 346 | |
| 347 | auto ExportArray = json::Array({std::move(Export)}); |
| 348 | |
| 349 | OS << json::Object({{.K: "version" , LLVM_COVERAGE_EXPORT_JSON_STR}, |
| 350 | {.K: "type" , LLVM_COVERAGE_EXPORT_JSON_TYPE_STR}, |
| 351 | {.K: "data" , .V: std::move(ExportArray)}}); |
| 352 | } |
| 353 | |