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