xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-cov/CoverageExporterJson.cpp (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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     append_range(Branches, NestedExBranches);
127 
128     // Add branches from this level of expansion.
129     auto ExBranches = ExpansionCoverage.getBranches();
130     for (auto B : ExBranches)
131       if (B.FileID == Expansion.FileID)
132         Branches.push_back(B);
133   }
134 
135   return Branches;
136 }
137 
138 json::Object renderExpansion(const coverage::CoverageMapping &Coverage,
139                              const coverage::ExpansionRecord &Expansion) {
140   std::vector<llvm::coverage::ExpansionRecord> Expansions = {Expansion};
141   return json::Object(
142       {{"filenames", json::Array(Expansion.Function.Filenames)},
143        // Mark the beginning and end of this expansion in the source file.
144        {"source_region", renderRegion(Expansion.Region)},
145        // Enumerate the coverage information for the expansion.
146        {"target_regions", renderRegions(Expansion.Function.CountedRegions)},
147        // Enumerate the branch coverage information for the expansion.
148        {"branches",
149         renderBranchRegions(collectNestedBranches(Coverage, Expansions))}});
150 }
151 
152 json::Object renderSummary(const FileCoverageSummary &Summary) {
153   return json::Object(
154       {{"lines",
155         json::Object({{"count", int64_t(Summary.LineCoverage.getNumLines())},
156                       {"covered", int64_t(Summary.LineCoverage.getCovered())},
157                       {"percent", Summary.LineCoverage.getPercentCovered()}})},
158        {"functions",
159         json::Object(
160             {{"count", int64_t(Summary.FunctionCoverage.getNumFunctions())},
161              {"covered", int64_t(Summary.FunctionCoverage.getExecuted())},
162              {"percent", Summary.FunctionCoverage.getPercentCovered()}})},
163        {"instantiations",
164         json::Object(
165             {{"count",
166               int64_t(Summary.InstantiationCoverage.getNumFunctions())},
167              {"covered", int64_t(Summary.InstantiationCoverage.getExecuted())},
168              {"percent", Summary.InstantiationCoverage.getPercentCovered()}})},
169        {"regions",
170         json::Object(
171             {{"count", int64_t(Summary.RegionCoverage.getNumRegions())},
172              {"covered", int64_t(Summary.RegionCoverage.getCovered())},
173              {"notcovered", int64_t(Summary.RegionCoverage.getNumRegions() -
174                                     Summary.RegionCoverage.getCovered())},
175              {"percent", Summary.RegionCoverage.getPercentCovered()}})},
176        {"branches",
177         json::Object(
178             {{"count", int64_t(Summary.BranchCoverage.getNumBranches())},
179              {"covered", int64_t(Summary.BranchCoverage.getCovered())},
180              {"notcovered", int64_t(Summary.BranchCoverage.getNumBranches() -
181                                     Summary.BranchCoverage.getCovered())},
182              {"percent", Summary.BranchCoverage.getPercentCovered()}})}});
183 }
184 
185 json::Array renderFileExpansions(const coverage::CoverageMapping &Coverage,
186                                  const coverage::CoverageData &FileCoverage,
187                                  const FileCoverageSummary &FileReport) {
188   json::Array ExpansionArray;
189   for (const auto &Expansion : FileCoverage.getExpansions())
190     ExpansionArray.push_back(renderExpansion(Coverage, Expansion));
191   return ExpansionArray;
192 }
193 
194 json::Array renderFileSegments(const coverage::CoverageData &FileCoverage,
195                                const FileCoverageSummary &FileReport) {
196   json::Array SegmentArray;
197   for (const auto &Segment : FileCoverage)
198     SegmentArray.push_back(renderSegment(Segment));
199   return SegmentArray;
200 }
201 
202 json::Array renderFileBranches(const coverage::CoverageData &FileCoverage,
203                                const FileCoverageSummary &FileReport) {
204   json::Array BranchArray;
205   for (const auto &Branch : FileCoverage.getBranches())
206     BranchArray.push_back(renderBranch(Branch));
207   return BranchArray;
208 }
209 
210 json::Object renderFile(const coverage::CoverageMapping &Coverage,
211                         const std::string &Filename,
212                         const FileCoverageSummary &FileReport,
213                         const CoverageViewOptions &Options) {
214   json::Object File({{"filename", Filename}});
215   if (!Options.ExportSummaryOnly) {
216     // Calculate and render detailed coverage information for given file.
217     auto FileCoverage = Coverage.getCoverageForFile(Filename);
218     File["segments"] = renderFileSegments(FileCoverage, FileReport);
219     File["branches"] = renderFileBranches(FileCoverage, FileReport);
220     if (!Options.SkipExpansions) {
221       File["expansions"] =
222           renderFileExpansions(Coverage, FileCoverage, FileReport);
223     }
224   }
225   File["summary"] = renderSummary(FileReport);
226   return File;
227 }
228 
229 json::Array renderFiles(const coverage::CoverageMapping &Coverage,
230                         ArrayRef<std::string> SourceFiles,
231                         ArrayRef<FileCoverageSummary> FileReports,
232                         const CoverageViewOptions &Options) {
233   ThreadPoolStrategy S = hardware_concurrency(Options.NumThreads);
234   if (Options.NumThreads == 0) {
235     // If NumThreads is not specified, create one thread for each input, up to
236     // the number of hardware cores.
237     S = heavyweight_hardware_concurrency(SourceFiles.size());
238     S.Limit = true;
239   }
240   ThreadPool Pool(S);
241   json::Array FileArray;
242   std::mutex FileArrayMutex;
243 
244   for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) {
245     auto &SourceFile = SourceFiles[I];
246     auto &FileReport = FileReports[I];
247     Pool.async([&] {
248       auto File = renderFile(Coverage, SourceFile, FileReport, Options);
249       {
250         std::lock_guard<std::mutex> Lock(FileArrayMutex);
251         FileArray.push_back(std::move(File));
252       }
253     });
254   }
255   Pool.wait();
256   return FileArray;
257 }
258 
259 json::Array renderFunctions(
260     const iterator_range<coverage::FunctionRecordIterator> &Functions) {
261   json::Array FunctionArray;
262   for (const auto &F : Functions)
263     FunctionArray.push_back(
264         json::Object({{"name", F.Name},
265                       {"count", clamp_uint64_to_int64(F.ExecutionCount)},
266                       {"regions", renderRegions(F.CountedRegions)},
267                       {"branches", renderBranchRegions(F.CountedBranchRegions)},
268                       {"filenames", json::Array(F.Filenames)}}));
269   return FunctionArray;
270 }
271 
272 } // end anonymous namespace
273 
274 void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) {
275   std::vector<std::string> SourceFiles;
276   for (StringRef SF : Coverage.getUniqueSourceFiles()) {
277     if (!IgnoreFilters.matchesFilename(SF))
278       SourceFiles.emplace_back(SF);
279   }
280   renderRoot(SourceFiles);
281 }
282 
283 void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) {
284   FileCoverageSummary Totals = FileCoverageSummary("Totals");
285   auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
286                                                         SourceFiles, Options);
287   auto Files = renderFiles(Coverage, SourceFiles, FileReports, Options);
288   // Sort files in order of their names.
289   llvm::sort(Files, [](const json::Value &A, const json::Value &B) {
290     const json::Object *ObjA = A.getAsObject();
291     const json::Object *ObjB = B.getAsObject();
292     assert(ObjA != nullptr && "Value A was not an Object");
293     assert(ObjB != nullptr && "Value B was not an Object");
294     const StringRef FilenameA = ObjA->getString("filename").getValue();
295     const StringRef FilenameB = ObjB->getString("filename").getValue();
296     return FilenameA.compare(FilenameB) < 0;
297   });
298   auto Export = json::Object(
299       {{"files", std::move(Files)}, {"totals", renderSummary(Totals)}});
300   // Skip functions-level information  if necessary.
301   if (!Options.ExportSummaryOnly && !Options.SkipFunctions)
302     Export["functions"] = renderFunctions(Coverage.getCoveredFunctions());
303 
304   auto ExportArray = json::Array({std::move(Export)});
305 
306   OS << json::Object({{"version", LLVM_COVERAGE_EXPORT_JSON_STR},
307                       {"type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR},
308                       {"data", std::move(ExportArray)}});
309 }
310