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 "2.0.1" |
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 | return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd, |
114 | CMR.ColumnEnd, CMR.ExpandedFileID, int64_t(CMR.Kind), |
115 | gatherConditions(Record)}); |
116 | } |
117 | |
118 | json::Array renderRegions(ArrayRef<coverage::CountedRegion> Regions) { |
119 | json::Array RegionArray; |
120 | for (const auto &Region : Regions) |
121 | RegionArray.push_back(E: renderRegion(Region)); |
122 | return RegionArray; |
123 | } |
124 | |
125 | json::Array renderBranchRegions(ArrayRef<coverage::CountedRegion> Regions) { |
126 | json::Array RegionArray; |
127 | for (const auto &Region : Regions) |
128 | if (!Region.Folded) |
129 | RegionArray.push_back(E: renderBranch(Region)); |
130 | return RegionArray; |
131 | } |
132 | |
133 | json::Array renderMCDCRecords(ArrayRef<coverage::MCDCRecord> Records) { |
134 | json::Array RecordArray; |
135 | for (auto &Record : Records) |
136 | RecordArray.push_back(E: renderMCDCRecord(Record)); |
137 | return RecordArray; |
138 | } |
139 | |
140 | std::vector<llvm::coverage::CountedRegion> |
141 | collectNestedBranches(const coverage::CoverageMapping &Coverage, |
142 | ArrayRef<llvm::coverage::ExpansionRecord> Expansions) { |
143 | std::vector<llvm::coverage::CountedRegion> Branches; |
144 | for (const auto &Expansion : Expansions) { |
145 | auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); |
146 | |
147 | // Recursively collect branches from nested expansions. |
148 | auto NestedExpansions = ExpansionCoverage.getExpansions(); |
149 | auto NestedExBranches = collectNestedBranches(Coverage, Expansions: NestedExpansions); |
150 | append_range(C&: Branches, R&: NestedExBranches); |
151 | |
152 | // Add branches from this level of expansion. |
153 | auto ExBranches = ExpansionCoverage.getBranches(); |
154 | for (auto B : ExBranches) |
155 | if (B.FileID == Expansion.FileID) |
156 | Branches.push_back(x: B); |
157 | } |
158 | |
159 | return Branches; |
160 | } |
161 | |
162 | json::Object renderExpansion(const coverage::CoverageMapping &Coverage, |
163 | const coverage::ExpansionRecord &Expansion) { |
164 | std::vector<llvm::coverage::ExpansionRecord> Expansions = {Expansion}; |
165 | return json::Object( |
166 | {{.K: "filenames" , .V: json::Array(Expansion.Function.Filenames)}, |
167 | // Mark the beginning and end of this expansion in the source file. |
168 | {.K: "source_region" , .V: renderRegion(Region: Expansion.Region)}, |
169 | // Enumerate the coverage information for the expansion. |
170 | {.K: "target_regions" , .V: renderRegions(Regions: Expansion.Function.CountedRegions)}, |
171 | // Enumerate the branch coverage information for the expansion. |
172 | {.K: "branches" , |
173 | .V: renderBranchRegions(Regions: collectNestedBranches(Coverage, Expansions))}}); |
174 | } |
175 | |
176 | json::Object renderSummary(const FileCoverageSummary &Summary) { |
177 | return json::Object( |
178 | {{.K: "lines" , |
179 | .V: json::Object({{.K: "count" , .V: int64_t(Summary.LineCoverage.getNumLines())}, |
180 | {.K: "covered" , .V: int64_t(Summary.LineCoverage.getCovered())}, |
181 | {.K: "percent" , .V: Summary.LineCoverage.getPercentCovered()}})}, |
182 | {.K: "functions" , |
183 | .V: json::Object( |
184 | {{.K: "count" , .V: int64_t(Summary.FunctionCoverage.getNumFunctions())}, |
185 | {.K: "covered" , .V: int64_t(Summary.FunctionCoverage.getExecuted())}, |
186 | {.K: "percent" , .V: Summary.FunctionCoverage.getPercentCovered()}})}, |
187 | {.K: "instantiations" , |
188 | .V: json::Object( |
189 | {{.K: "count" , |
190 | .V: int64_t(Summary.InstantiationCoverage.getNumFunctions())}, |
191 | {.K: "covered" , .V: int64_t(Summary.InstantiationCoverage.getExecuted())}, |
192 | {.K: "percent" , .V: Summary.InstantiationCoverage.getPercentCovered()}})}, |
193 | {.K: "regions" , |
194 | .V: json::Object( |
195 | {{.K: "count" , .V: int64_t(Summary.RegionCoverage.getNumRegions())}, |
196 | {.K: "covered" , .V: int64_t(Summary.RegionCoverage.getCovered())}, |
197 | {.K: "notcovered" , .V: int64_t(Summary.RegionCoverage.getNumRegions() - |
198 | Summary.RegionCoverage.getCovered())}, |
199 | {.K: "percent" , .V: Summary.RegionCoverage.getPercentCovered()}})}, |
200 | {.K: "branches" , |
201 | .V: json::Object( |
202 | {{.K: "count" , .V: int64_t(Summary.BranchCoverage.getNumBranches())}, |
203 | {.K: "covered" , .V: int64_t(Summary.BranchCoverage.getCovered())}, |
204 | {.K: "notcovered" , .V: int64_t(Summary.BranchCoverage.getNumBranches() - |
205 | Summary.BranchCoverage.getCovered())}, |
206 | {.K: "percent" , .V: Summary.BranchCoverage.getPercentCovered()}})}, |
207 | {.K: "mcdc" , |
208 | .V: json::Object( |
209 | {{.K: "count" , .V: int64_t(Summary.MCDCCoverage.getNumPairs())}, |
210 | {.K: "covered" , .V: int64_t(Summary.MCDCCoverage.getCoveredPairs())}, |
211 | {.K: "notcovered" , .V: int64_t(Summary.MCDCCoverage.getNumPairs() - |
212 | Summary.MCDCCoverage.getCoveredPairs())}, |
213 | {.K: "percent" , .V: Summary.MCDCCoverage.getPercentCovered()}})}}); |
214 | } |
215 | |
216 | json::Array renderFileExpansions(const coverage::CoverageMapping &Coverage, |
217 | const coverage::CoverageData &FileCoverage, |
218 | const FileCoverageSummary &FileReport) { |
219 | json::Array ExpansionArray; |
220 | for (const auto &Expansion : FileCoverage.getExpansions()) |
221 | ExpansionArray.push_back(E: renderExpansion(Coverage, Expansion)); |
222 | return ExpansionArray; |
223 | } |
224 | |
225 | json::Array renderFileSegments(const coverage::CoverageData &FileCoverage, |
226 | const FileCoverageSummary &FileReport) { |
227 | json::Array SegmentArray; |
228 | for (const auto &Segment : FileCoverage) |
229 | SegmentArray.push_back(E: renderSegment(Segment)); |
230 | return SegmentArray; |
231 | } |
232 | |
233 | json::Array renderFileBranches(const coverage::CoverageData &FileCoverage, |
234 | const FileCoverageSummary &FileReport) { |
235 | json::Array BranchArray; |
236 | for (const auto &Branch : FileCoverage.getBranches()) |
237 | BranchArray.push_back(E: renderBranch(Region: Branch)); |
238 | return BranchArray; |
239 | } |
240 | |
241 | json::Array renderFileMCDC(const coverage::CoverageData &FileCoverage, |
242 | const FileCoverageSummary &FileReport) { |
243 | json::Array MCDCRecordArray; |
244 | for (const auto &Record : FileCoverage.getMCDCRecords()) |
245 | MCDCRecordArray.push_back(E: renderMCDCRecord(Record)); |
246 | return MCDCRecordArray; |
247 | } |
248 | |
249 | json::Object renderFile(const coverage::CoverageMapping &Coverage, |
250 | const std::string &Filename, |
251 | const FileCoverageSummary &FileReport, |
252 | const CoverageViewOptions &Options) { |
253 | json::Object File({{.K: "filename" , .V: Filename}}); |
254 | if (!Options.ExportSummaryOnly) { |
255 | // Calculate and render detailed coverage information for given file. |
256 | auto FileCoverage = Coverage.getCoverageForFile(Filename); |
257 | File["segments" ] = renderFileSegments(FileCoverage, FileReport); |
258 | File["branches" ] = renderFileBranches(FileCoverage, FileReport); |
259 | File["mcdc_records" ] = renderFileMCDC(FileCoverage, FileReport); |
260 | if (!Options.SkipExpansions) { |
261 | File["expansions" ] = |
262 | renderFileExpansions(Coverage, FileCoverage, FileReport); |
263 | } |
264 | } |
265 | File["summary" ] = renderSummary(Summary: FileReport); |
266 | return File; |
267 | } |
268 | |
269 | json::Array renderFiles(const coverage::CoverageMapping &Coverage, |
270 | ArrayRef<std::string> SourceFiles, |
271 | ArrayRef<FileCoverageSummary> FileReports, |
272 | const CoverageViewOptions &Options) { |
273 | ThreadPoolStrategy S = hardware_concurrency(ThreadCount: Options.NumThreads); |
274 | if (Options.NumThreads == 0) { |
275 | // If NumThreads is not specified, create one thread for each input, up to |
276 | // the number of hardware cores. |
277 | S = heavyweight_hardware_concurrency(ThreadCount: SourceFiles.size()); |
278 | S.Limit = true; |
279 | } |
280 | DefaultThreadPool Pool(S); |
281 | json::Array FileArray; |
282 | std::mutex FileArrayMutex; |
283 | |
284 | for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) { |
285 | auto &SourceFile = SourceFiles[I]; |
286 | auto &FileReport = FileReports[I]; |
287 | Pool.async(F: [&] { |
288 | auto File = renderFile(Coverage, Filename: SourceFile, FileReport, Options); |
289 | { |
290 | std::lock_guard<std::mutex> Lock(FileArrayMutex); |
291 | FileArray.push_back(E: std::move(File)); |
292 | } |
293 | }); |
294 | } |
295 | Pool.wait(); |
296 | return FileArray; |
297 | } |
298 | |
299 | json::Array renderFunctions( |
300 | const iterator_range<coverage::FunctionRecordIterator> &Functions) { |
301 | json::Array FunctionArray; |
302 | for (const auto &F : Functions) |
303 | FunctionArray.push_back( |
304 | E: json::Object({{.K: "name" , .V: F.Name}, |
305 | {.K: "count" , .V: clamp_uint64_to_int64(u: F.ExecutionCount)}, |
306 | {.K: "regions" , .V: renderRegions(Regions: F.CountedRegions)}, |
307 | {.K: "branches" , .V: renderBranchRegions(Regions: F.CountedBranchRegions)}, |
308 | {.K: "mcdc_records" , .V: renderMCDCRecords(Records: F.MCDCRecords)}, |
309 | {.K: "filenames" , .V: json::Array(F.Filenames)}})); |
310 | return FunctionArray; |
311 | } |
312 | |
313 | } // end anonymous namespace |
314 | |
315 | void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) { |
316 | std::vector<std::string> SourceFiles; |
317 | for (StringRef SF : Coverage.getUniqueSourceFiles()) { |
318 | if (!IgnoreFilters.matchesFilename(Filename: SF)) |
319 | SourceFiles.emplace_back(args&: SF); |
320 | } |
321 | renderRoot(SourceFiles); |
322 | } |
323 | |
324 | void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) { |
325 | FileCoverageSummary Totals = FileCoverageSummary("Totals" ); |
326 | auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals, |
327 | Files: SourceFiles, Options); |
328 | auto Files = renderFiles(Coverage, SourceFiles, FileReports, Options); |
329 | // Sort files in order of their names. |
330 | llvm::sort(C&: Files, Comp: [](const json::Value &A, const json::Value &B) { |
331 | const json::Object *ObjA = A.getAsObject(); |
332 | const json::Object *ObjB = B.getAsObject(); |
333 | assert(ObjA != nullptr && "Value A was not an Object" ); |
334 | assert(ObjB != nullptr && "Value B was not an Object" ); |
335 | const StringRef FilenameA = *ObjA->getString(K: "filename" ); |
336 | const StringRef FilenameB = *ObjB->getString(K: "filename" ); |
337 | return FilenameA.compare(RHS: FilenameB) < 0; |
338 | }); |
339 | auto Export = json::Object( |
340 | {{.K: "files" , .V: std::move(Files)}, {.K: "totals" , .V: renderSummary(Summary: Totals)}}); |
341 | // Skip functions-level information if necessary. |
342 | if (!Options.ExportSummaryOnly && !Options.SkipFunctions) |
343 | Export["functions" ] = renderFunctions(Functions: Coverage.getCoveredFunctions()); |
344 | |
345 | auto ExportArray = json::Array({std::move(Export)}); |
346 | |
347 | OS << json::Object({{.K: "version" , LLVM_COVERAGE_EXPORT_JSON_STR}, |
348 | {.K: "type" , LLVM_COVERAGE_EXPORT_JSON_TYPE_STR}, |
349 | {.K: "data" , .V: std::move(ExportArray)}}); |
350 | } |
351 | |