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