Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / llvm-cov / CoverageExporterJson.cpp
blobe6361d48b8e1a47eb4fd6bce6c6b9584517e5ebe
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.
11 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
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 // -- Segments: array => List of Segments contained in the file
22 // -- Segment: dict => Describes a segment of the file with a counter
23 // -- Expansions: array => List of expansion records
24 // -- Expansion: dict => Object that descibes a single expansion
25 // -- CountedRegion: dict => The region to be expanded
26 // -- TargetRegions: array => List of Regions in the expansion
27 // -- CountedRegion: dict => Single Region in the expansion
28 // -- Summary: dict => Object summarizing the coverage for this file
29 // -- LineCoverage: dict => Object summarizing line coverage
30 // -- FunctionCoverage: dict => Object summarizing function coverage
31 // -- RegionCoverage: dict => Object summarizing region coverage
32 // -- Functions: array => List of objects describing coverage for functions
33 // -- Function: dict => Coverage info for a single function
34 // -- Filenames: array => List of filenames that the function relates to
35 // -- Summary: dict => Object summarizing the coverage for the entire binary
36 // -- LineCoverage: dict => Object summarizing line coverage
37 // -- FunctionCoverage: dict => Object summarizing function coverage
38 // -- InstantiationCoverage: dict => Object summarizing inst. coverage
39 // -- RegionCoverage: dict => Object summarizing region coverage
41 //===----------------------------------------------------------------------===//
43 #include "CoverageExporterJson.h"
44 #include "CoverageReport.h"
45 #include "llvm/Support/JSON.h"
47 /// The semantic version combined as a string.
48 #define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.0"
50 /// Unique type identifier for JSON coverage export.
51 #define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
53 using namespace llvm;
55 namespace {
57 json::Array renderSegment(const coverage::CoverageSegment &Segment) {
58 return json::Array({Segment.Line, Segment.Col, int64_t(Segment.Count),
59 Segment.HasCount, Segment.IsRegionEntry});
62 json::Array renderRegion(const coverage::CountedRegion &Region) {
63 return json::Array({Region.LineStart, Region.ColumnStart, Region.LineEnd,
64 Region.ColumnEnd, int64_t(Region.ExecutionCount),
65 Region.FileID, Region.ExpandedFileID,
66 int64_t(Region.Kind)});
69 json::Array renderRegions(ArrayRef<coverage::CountedRegion> Regions) {
70 json::Array RegionArray;
71 for (const auto &Region : Regions)
72 RegionArray.push_back(renderRegion(Region));
73 return RegionArray;
76 json::Object renderExpansion(const coverage::ExpansionRecord &Expansion) {
77 return json::Object(
78 {{"filenames", json::Array(Expansion.Function.Filenames)},
79 // Mark the beginning and end of this expansion in the source file.
80 {"source_region", renderRegion(Expansion.Region)},
81 // Enumerate the coverage information for the expansion.
82 {"target_regions", renderRegions(Expansion.Function.CountedRegions)}});
85 json::Object renderSummary(const FileCoverageSummary &Summary) {
86 return json::Object(
87 {{"lines",
88 json::Object({{"count", int64_t(Summary.LineCoverage.getNumLines())},
89 {"covered", int64_t(Summary.LineCoverage.getCovered())},
90 {"percent", Summary.LineCoverage.getPercentCovered()}})},
91 {"functions",
92 json::Object(
93 {{"count", int64_t(Summary.FunctionCoverage.getNumFunctions())},
94 {"covered", int64_t(Summary.FunctionCoverage.getExecuted())},
95 {"percent", Summary.FunctionCoverage.getPercentCovered()}})},
96 {"instantiations",
97 json::Object(
98 {{"count",
99 int64_t(Summary.InstantiationCoverage.getNumFunctions())},
100 {"covered", int64_t(Summary.InstantiationCoverage.getExecuted())},
101 {"percent", Summary.InstantiationCoverage.getPercentCovered()}})},
102 {"regions",
103 json::Object(
104 {{"count", int64_t(Summary.RegionCoverage.getNumRegions())},
105 {"covered", int64_t(Summary.RegionCoverage.getCovered())},
106 {"notcovered", int64_t(Summary.RegionCoverage.getNumRegions() -
107 Summary.RegionCoverage.getCovered())},
108 {"percent", Summary.RegionCoverage.getPercentCovered()}})}});
111 json::Array renderFileExpansions(const coverage::CoverageData &FileCoverage,
112 const FileCoverageSummary &FileReport) {
113 json::Array ExpansionArray;
114 for (const auto &Expansion : FileCoverage.getExpansions())
115 ExpansionArray.push_back(renderExpansion(Expansion));
116 return ExpansionArray;
119 json::Array renderFileSegments(const coverage::CoverageData &FileCoverage,
120 const FileCoverageSummary &FileReport) {
121 json::Array SegmentArray;
122 for (const auto &Segment : FileCoverage)
123 SegmentArray.push_back(renderSegment(Segment));
124 return SegmentArray;
127 json::Object renderFile(const coverage::CoverageMapping &Coverage,
128 const std::string &Filename,
129 const FileCoverageSummary &FileReport,
130 bool ExportSummaryOnly) {
131 json::Object File({{"filename", Filename}});
132 if (!ExportSummaryOnly) {
133 // Calculate and render detailed coverage information for given file.
134 auto FileCoverage = Coverage.getCoverageForFile(Filename);
135 File["segments"] = renderFileSegments(FileCoverage, FileReport);
136 File["expansions"] = renderFileExpansions(FileCoverage, FileReport);
138 File["summary"] = renderSummary(FileReport);
139 return File;
142 json::Array renderFiles(const coverage::CoverageMapping &Coverage,
143 ArrayRef<std::string> SourceFiles,
144 ArrayRef<FileCoverageSummary> FileReports,
145 bool ExportSummaryOnly) {
146 json::Array FileArray;
147 for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I)
148 FileArray.push_back(renderFile(Coverage, SourceFiles[I], FileReports[I],
149 ExportSummaryOnly));
150 return FileArray;
153 json::Array renderFunctions(
154 const iterator_range<coverage::FunctionRecordIterator> &Functions) {
155 json::Array FunctionArray;
156 for (const auto &F : Functions)
157 FunctionArray.push_back(
158 json::Object({{"name", F.Name},
159 {"count", int64_t(F.ExecutionCount)},
160 {"regions", renderRegions(F.CountedRegions)},
161 {"filenames", json::Array(F.Filenames)}}));
162 return FunctionArray;
165 } // end anonymous namespace
167 void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) {
168 std::vector<std::string> SourceFiles;
169 for (StringRef SF : Coverage.getUniqueSourceFiles()) {
170 if (!IgnoreFilters.matchesFilename(SF))
171 SourceFiles.emplace_back(SF);
173 renderRoot(SourceFiles);
176 void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) {
177 FileCoverageSummary Totals = FileCoverageSummary("Totals");
178 auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
179 SourceFiles, Options);
180 auto Export =
181 json::Object({{"files", renderFiles(Coverage, SourceFiles, FileReports,
182 Options.ExportSummaryOnly)},
183 {"totals", renderSummary(Totals)}});
184 // Skip functions-level information for summary-only export mode.
185 if (!Options.ExportSummaryOnly)
186 Export["functions"] = renderFunctions(Coverage.getCoveredFunctions());
188 auto ExportArray = json::Array({std::move(Export)});
190 OS << json::Object({{"version", LLVM_COVERAGE_EXPORT_JSON_STR},
191 {"type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR},
192 {"data", std::move(ExportArray)}});