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.
clamp_uint64_to_int64(uint64_t u)79 int64_t clamp_uint64_to_int64(uint64_t u) {
80   return std::min(u, static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
81 }
82 
renderSegment(const coverage::CoverageSegment & Segment)83 json::Array renderSegment(const coverage::CoverageSegment &Segment) {
84   return json::Array({Segment.Line, Segment.Col,
85                       clamp_uint64_to_int64(Segment.Count), Segment.HasCount,
86                       Segment.IsRegionEntry, Segment.IsGapRegion});
87 }
88 
renderRegion(const coverage::CountedRegion & Region)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(Region.ExecutionCount),
92                       Region.FileID, Region.ExpandedFileID,
93                       int64_t(Region.Kind)});
94 }
95 
renderBranch(const coverage::CountedRegion & Region)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(Region.ExecutionCount),
100        clamp_uint64_to_int64(Region.FalseExecutionCount), Region.FileID,
101        Region.ExpandedFileID, int64_t(Region.Kind)});
102 }
103 
gatherConditions(const coverage::MCDCRecord & Record)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(Record.isConditionIndependencePairCovered(c));
108   return Conditions;
109 }
110 
renderMCDCRecord(const coverage::MCDCRecord & Record)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 
renderRegions(ArrayRef<coverage::CountedRegion> Regions)118 json::Array renderRegions(ArrayRef<coverage::CountedRegion> Regions) {
119   json::Array RegionArray;
120   for (const auto &Region : Regions)
121     RegionArray.push_back(renderRegion(Region));
122   return RegionArray;
123 }
124 
renderBranchRegions(ArrayRef<coverage::CountedRegion> Regions)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(renderBranch(Region));
130   return RegionArray;
131 }
132 
renderMCDCRecords(ArrayRef<coverage::MCDCRecord> Records)133 json::Array renderMCDCRecords(ArrayRef<coverage::MCDCRecord> Records) {
134   json::Array RecordArray;
135   for (auto &Record : Records)
136     RecordArray.push_back(renderMCDCRecord(Record));
137   return RecordArray;
138 }
139 
140 std::vector<llvm::coverage::CountedRegion>
collectNestedBranches(const coverage::CoverageMapping & Coverage,ArrayRef<llvm::coverage::ExpansionRecord> Expansions)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, NestedExpansions);
150     append_range(Branches, 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(B);
157   }
158 
159   return Branches;
160 }
161 
renderExpansion(const coverage::CoverageMapping & Coverage,const coverage::ExpansionRecord & Expansion)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       {{"filenames", json::Array(Expansion.Function.Filenames)},
167        // Mark the beginning and end of this expansion in the source file.
168        {"source_region", renderRegion(Expansion.Region)},
169        // Enumerate the coverage information for the expansion.
170        {"target_regions", renderRegions(Expansion.Function.CountedRegions)},
171        // Enumerate the branch coverage information for the expansion.
172        {"branches",
173         renderBranchRegions(collectNestedBranches(Coverage, Expansions))}});
174 }
175 
renderSummary(const FileCoverageSummary & Summary)176 json::Object renderSummary(const FileCoverageSummary &Summary) {
177   return json::Object(
178       {{"lines",
179         json::Object({{"count", int64_t(Summary.LineCoverage.getNumLines())},
180                       {"covered", int64_t(Summary.LineCoverage.getCovered())},
181                       {"percent", Summary.LineCoverage.getPercentCovered()}})},
182        {"functions",
183         json::Object(
184             {{"count", int64_t(Summary.FunctionCoverage.getNumFunctions())},
185              {"covered", int64_t(Summary.FunctionCoverage.getExecuted())},
186              {"percent", Summary.FunctionCoverage.getPercentCovered()}})},
187        {"instantiations",
188         json::Object(
189             {{"count",
190               int64_t(Summary.InstantiationCoverage.getNumFunctions())},
191              {"covered", int64_t(Summary.InstantiationCoverage.getExecuted())},
192              {"percent", Summary.InstantiationCoverage.getPercentCovered()}})},
193        {"regions",
194         json::Object(
195             {{"count", int64_t(Summary.RegionCoverage.getNumRegions())},
196              {"covered", int64_t(Summary.RegionCoverage.getCovered())},
197              {"notcovered", int64_t(Summary.RegionCoverage.getNumRegions() -
198                                     Summary.RegionCoverage.getCovered())},
199              {"percent", Summary.RegionCoverage.getPercentCovered()}})},
200        {"branches",
201         json::Object(
202             {{"count", int64_t(Summary.BranchCoverage.getNumBranches())},
203              {"covered", int64_t(Summary.BranchCoverage.getCovered())},
204              {"notcovered", int64_t(Summary.BranchCoverage.getNumBranches() -
205                                     Summary.BranchCoverage.getCovered())},
206              {"percent", Summary.BranchCoverage.getPercentCovered()}})},
207        {"mcdc",
208         json::Object(
209             {{"count", int64_t(Summary.MCDCCoverage.getNumPairs())},
210              {"covered", int64_t(Summary.MCDCCoverage.getCoveredPairs())},
211              {"notcovered", int64_t(Summary.MCDCCoverage.getNumPairs() -
212                                     Summary.MCDCCoverage.getCoveredPairs())},
213              {"percent", Summary.MCDCCoverage.getPercentCovered()}})}});
214 }
215 
renderFileExpansions(const coverage::CoverageMapping & Coverage,const coverage::CoverageData & FileCoverage,const FileCoverageSummary & FileReport)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(renderExpansion(Coverage, Expansion));
222   return ExpansionArray;
223 }
224 
renderFileSegments(const coverage::CoverageData & FileCoverage,const FileCoverageSummary & FileReport)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(renderSegment(Segment));
230   return SegmentArray;
231 }
232 
renderFileBranches(const coverage::CoverageData & FileCoverage,const FileCoverageSummary & FileReport)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(renderBranch(Branch));
238   return BranchArray;
239 }
240 
renderFileMCDC(const coverage::CoverageData & FileCoverage,const FileCoverageSummary & FileReport)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(renderMCDCRecord(Record));
246   return MCDCRecordArray;
247 }
248 
renderFile(const coverage::CoverageMapping & Coverage,const std::string & Filename,const FileCoverageSummary & FileReport,const CoverageViewOptions & Options)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({{"filename", 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(FileReport);
266   return File;
267 }
268 
renderFiles(const coverage::CoverageMapping & Coverage,ArrayRef<std::string> SourceFiles,ArrayRef<FileCoverageSummary> FileReports,const CoverageViewOptions & Options)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(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(SourceFiles.size());
278     S.Limit = true;
279   }
280   ThreadPool 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([&] {
288       auto File = renderFile(Coverage, SourceFile, FileReport, Options);
289       {
290         std::lock_guard<std::mutex> Lock(FileArrayMutex);
291         FileArray.push_back(std::move(File));
292       }
293     });
294   }
295   Pool.wait();
296   return FileArray;
297 }
298 
renderFunctions(const iterator_range<coverage::FunctionRecordIterator> & Functions)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         json::Object({{"name", F.Name},
305                       {"count", clamp_uint64_to_int64(F.ExecutionCount)},
306                       {"regions", renderRegions(F.CountedRegions)},
307                       {"branches", renderBranchRegions(F.CountedBranchRegions)},
308                       {"mcdc_records", renderMCDCRecords(F.MCDCRecords)},
309                       {"filenames", json::Array(F.Filenames)}}));
310   return FunctionArray;
311 }
312 
313 } // end anonymous namespace
314 
renderRoot(const CoverageFilters & IgnoreFilters)315 void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) {
316   std::vector<std::string> SourceFiles;
317   for (StringRef SF : Coverage.getUniqueSourceFiles()) {
318     if (!IgnoreFilters.matchesFilename(SF))
319       SourceFiles.emplace_back(SF);
320   }
321   renderRoot(SourceFiles);
322 }
323 
renderRoot(ArrayRef<std::string> SourceFiles)324 void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) {
325   FileCoverageSummary Totals = FileCoverageSummary("Totals");
326   auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
327                                                         SourceFiles, Options);
328   auto Files = renderFiles(Coverage, SourceFiles, FileReports, Options);
329   // Sort files in order of their names.
330   llvm::sort(Files, [](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("filename");
336     const StringRef FilenameB = *ObjB->getString("filename");
337     return FilenameA.compare(FilenameB) < 0;
338   });
339   auto Export = json::Object(
340       {{"files", std::move(Files)}, {"totals", renderSummary(Totals)}});
341   // Skip functions-level information  if necessary.
342   if (!Options.ExportSummaryOnly && !Options.SkipFunctions)
343     Export["functions"] = renderFunctions(Coverage.getCoveredFunctions());
344 
345   auto ExportArray = json::Array({std::move(Export)});
346 
347   OS << json::Object({{"version", LLVM_COVERAGE_EXPORT_JSON_STR},
348                       {"type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR},
349                       {"data", std::move(ExportArray)}});
350 }
351