blob: 3621f9ab97f186b14703b4a0f8b7b433213ac692 [file] [log] [blame]
Vedant Kumar7101d732016-07-26 22:50:58 +00001//===- CoverageExporterJson.cpp - Code coverage export --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements export of code coverage data to JSON.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//
16// The json code coverage export follows the following format
17// Root: dict => Root Element containing metadata
18// -- Data: array => Homogeneous array of one or more export objects
19// ---- Export: dict => Json representation of one CoverageMapping
20// ------ Files: array => List of objects describing coverage for files
21// -------- File: dict => Coverage for a single file
22// ---------- Segments: array => List of Segments contained in the file
23// ------------ Segment: dict => Describes a segment of the file with a counter
24// ---------- Expansions: array => List of expansion records
25// ------------ Expansion: dict => Object that descibes a single expansion
26// -------------- CountedRegion: dict => The region to be expanded
27// -------------- TargetRegions: array => List of Regions in the expansion
28// ---------------- CountedRegion: dict => Single Region in the expansion
29// ---------- Summary: dict => Object summarizing the coverage for this file
30// ------------ LineCoverage: dict => Object summarizing line coverage
31// ------------ FunctionCoverage: dict => Object summarizing function coverage
32// ------------ RegionCoverage: dict => Object summarizing region coverage
33// ------ Functions: array => List of objects describing coverage for functions
34// -------- Function: dict => Coverage info for a single function
35// ---------- Filenames: array => List of filenames that the function relates to
36// ---- Summary: dict => Object summarizing the coverage for the entire binary
37// ------ LineCoverage: dict => Object summarizing line coverage
38// ------ FunctionCoverage: dict => Object summarizing function coverage
Vedant Kumar3c46abb2016-09-19 00:38:29 +000039// ------ InstantiationCoverage: dict => Object summarizing inst. coverage
Vedant Kumar7101d732016-07-26 22:50:58 +000040// ------ RegionCoverage: dict => Object summarizing region coverage
41//
42//===----------------------------------------------------------------------===//
43
Max Moroz1ef3a772018-01-04 19:33:29 +000044#include "CoverageExporterJson.h"
Vedant Kumar3c46abb2016-09-19 00:38:29 +000045#include "CoverageReport.h"
Vedant Kumar7101d732016-07-26 22:50:58 +000046
Vedant Kumar7101d732016-07-26 22:50:58 +000047/// \brief The semantic version combined as a string.
Vedant Kumar5c61c702016-10-25 00:08:33 +000048#define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.0"
Vedant Kumar7101d732016-07-26 22:50:58 +000049
50/// \brief Unique type identifier for JSON coverage export.
51#define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
52
53using namespace llvm;
Vedant Kumar7101d732016-07-26 22:50:58 +000054
Max Moroz1ef3a772018-01-04 19:33:29 +000055CoverageExporterJson::CoverageExporterJson(
56 const coverage::CoverageMapping &CoverageMapping,
57 const CoverageViewOptions &Options, raw_ostream &OS)
58 : CoverageExporter(CoverageMapping, Options, OS) {
59 State.push(JsonState::None);
60}
Vedant Kumar72c3a112017-09-08 18:44:49 +000061
Max Moroz1ef3a772018-01-04 19:33:29 +000062void CoverageExporterJson::emitSerialized(const int64_t Value) { OS << Value; }
Vedant Kumar7101d732016-07-26 22:50:58 +000063
Max Moroz1ef3a772018-01-04 19:33:29 +000064void CoverageExporterJson::emitSerialized(const std::string &Value) {
65 OS << "\"";
66 for (char C : Value) {
67 if (C != '\\')
68 OS << C;
69 else
70 OS << "\\\\";
Vedant Kumar90be9db2016-07-27 04:08:32 +000071 }
Max Moroz1ef3a772018-01-04 19:33:29 +000072 OS << "\"";
73}
Vedant Kumar7101d732016-07-26 22:50:58 +000074
Max Moroz1ef3a772018-01-04 19:33:29 +000075void CoverageExporterJson::emitComma() {
76 if (State.top() == JsonState::NonEmptyElement) {
77 OS << ",";
78 } else if (State.top() == JsonState::EmptyElement) {
Vedant Kumar7101d732016-07-26 22:50:58 +000079 State.pop();
80 assert((State.size() >= 1) && "Closed too many JSON elements");
Max Moroz1ef3a772018-01-04 19:33:29 +000081 State.push(JsonState::NonEmptyElement);
Vedant Kumar7101d732016-07-26 22:50:58 +000082 }
Max Moroz1ef3a772018-01-04 19:33:29 +000083}
Vedant Kumar7101d732016-07-26 22:50:58 +000084
Max Moroz1ef3a772018-01-04 19:33:29 +000085void CoverageExporterJson::emitDictStart() {
86 emitComma();
87 State.push(JsonState::EmptyElement);
88 OS << "{";
89}
Vedant Kumar7101d732016-07-26 22:50:58 +000090
Max Moroz1ef3a772018-01-04 19:33:29 +000091void CoverageExporterJson::emitDictKey(const std::string &Key) {
92 emitComma();
93 emitSerialized(Key);
94 OS << ":";
95 State.pop();
96 assert((State.size() >= 1) && "Closed too many JSON elements");
Vedant Kumar7101d732016-07-26 22:50:58 +000097
Max Moroz1ef3a772018-01-04 19:33:29 +000098 // We do not want to emit a comma after this key.
99 State.push(JsonState::EmptyElement);
100}
Vedant Kumar7101d732016-07-26 22:50:58 +0000101
Max Moroz1ef3a772018-01-04 19:33:29 +0000102void CoverageExporterJson::emitDictEnd() {
103 State.pop();
104 assert((State.size() >= 1) && "Closed too many JSON elements");
105 OS << "}";
106}
Vedant Kumar7101d732016-07-26 22:50:58 +0000107
Max Moroz1ef3a772018-01-04 19:33:29 +0000108void CoverageExporterJson::emitArrayStart() {
109 emitComma();
110 State.push(JsonState::EmptyElement);
111 OS << "[";
112}
Vedant Kumar7101d732016-07-26 22:50:58 +0000113
Max Moroz1ef3a772018-01-04 19:33:29 +0000114void CoverageExporterJson::emitArrayEnd() {
115 State.pop();
116 assert((State.size() >= 1) && "Closed too many JSON elements");
117 OS << "]";
118}
Vedant Kumar7101d732016-07-26 22:50:58 +0000119
Max Moroz1ef3a772018-01-04 19:33:29 +0000120void CoverageExporterJson::renderRoot() {
121 std::vector<std::string> SourceFiles;
122 for (StringRef SF : Coverage.getUniqueSourceFiles())
123 SourceFiles.emplace_back(SF);
124 renderRoot(SourceFiles);
125}
Vedant Kumar7101d732016-07-26 22:50:58 +0000126
Max Moroz1ef3a772018-01-04 19:33:29 +0000127void CoverageExporterJson::renderRoot(
128 const std::vector<std::string> &SourceFiles) {
129 // Start Root of JSON object.
130 emitDictStart();
Vedant Kumar7101d732016-07-26 22:50:58 +0000131
Max Moroz1ef3a772018-01-04 19:33:29 +0000132 emitDictElement("version", LLVM_COVERAGE_EXPORT_JSON_STR);
133 emitDictElement("type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR);
134 emitDictKey("data");
Vedant Kumar7101d732016-07-26 22:50:58 +0000135
Max Moroz1ef3a772018-01-04 19:33:29 +0000136 // Start List of Exports.
137 emitArrayStart();
Vedant Kumar3c46abb2016-09-19 00:38:29 +0000138
Max Moroz1ef3a772018-01-04 19:33:29 +0000139 // Start Export.
140 emitDictStart();
Vedant Kumar7101d732016-07-26 22:50:58 +0000141
Max Moroz1ef3a772018-01-04 19:33:29 +0000142 emitDictKey("files");
Vedant Kumar7101d732016-07-26 22:50:58 +0000143
Max Moroz1ef3a772018-01-04 19:33:29 +0000144 FileCoverageSummary Totals = FileCoverageSummary("Totals");
145 auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
146 SourceFiles, Options);
147 renderFiles(SourceFiles, FileReports);
Vedant Kumar7101d732016-07-26 22:50:58 +0000148
Max Moroz1ef3a772018-01-04 19:33:29 +0000149 // Skip functions-level information for summary-only export mode.
150 if (!Options.ExportSummaryOnly) {
Vedant Kumar7101d732016-07-26 22:50:58 +0000151 emitDictKey("functions");
Max Moroz1ef3a772018-01-04 19:33:29 +0000152 renderFunctions(Coverage.getCoveredFunctions());
153 }
Vedant Kumar7101d732016-07-26 22:50:58 +0000154
Max Moroz1ef3a772018-01-04 19:33:29 +0000155 emitDictKey("totals");
156 renderSummary(Totals);
157
158 // End Export.
159 emitDictEnd();
160
161 // End List of Exports.
162 emitArrayEnd();
163
164 // End Root of JSON Object.
165 emitDictEnd();
166
167 assert((State.top() == JsonState::None) &&
168 "All Elements In JSON were Closed");
169}
170
171void CoverageExporterJson::renderFunctions(
172 const iterator_range<coverage::FunctionRecordIterator> &Functions) {
173 // Start List of Functions.
174 emitArrayStart();
175
176 for (const auto &Function : Functions) {
177 // Start Function.
Vedant Kumar7101d732016-07-26 22:50:58 +0000178 emitDictStart();
Vedant Kumar7101d732016-07-26 22:50:58 +0000179
Max Moroz1ef3a772018-01-04 19:33:29 +0000180 emitDictElement("name", Function.Name);
181 emitDictElement("count", Function.ExecutionCount);
Vedant Kumar7101d732016-07-26 22:50:58 +0000182 emitDictKey("regions");
183
Max Moroz1ef3a772018-01-04 19:33:29 +0000184 renderRegions(Function.CountedRegions);
Vedant Kumar7101d732016-07-26 22:50:58 +0000185
Max Moroz1ef3a772018-01-04 19:33:29 +0000186 emitDictKey("filenames");
187
188 // Start Filenames for Function.
189 emitArrayStart();
190
191 for (const auto &FileName : Function.Filenames)
192 emitArrayElement(FileName);
193
194 // End Filenames for Function.
195 emitArrayEnd();
196
197 // End Function.
Vedant Kumar7101d732016-07-26 22:50:58 +0000198 emitDictEnd();
199 }
200
Max Moroz1ef3a772018-01-04 19:33:29 +0000201 // End List of Functions.
202 emitArrayEnd();
203}
204
205void CoverageExporterJson::renderFiles(
206 ArrayRef<std::string> SourceFiles,
207 ArrayRef<FileCoverageSummary> FileReports) {
208 // Start List of Files.
209 emitArrayStart();
210
211 for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) {
Max Moroz6242cac2018-01-12 20:31:32 +0000212 renderFile(SourceFiles[I], FileReports[I]);
Vedant Kumar7101d732016-07-26 22:50:58 +0000213 }
214
Max Moroz1ef3a772018-01-04 19:33:29 +0000215 // End List of Files.
216 emitArrayEnd();
217}
Vedant Kumar7101d732016-07-26 22:50:58 +0000218
Max Moroz6242cac2018-01-12 20:31:32 +0000219void CoverageExporterJson::renderFile(const std::string &Filename,
220 const FileCoverageSummary &FileReport) {
221 // Start File.
Max Moroz1ef3a772018-01-04 19:33:29 +0000222 emitDictStart();
Vedant Kumar7101d732016-07-26 22:50:58 +0000223
Max Moroz6242cac2018-01-12 20:31:32 +0000224 emitDictElement("filename", Filename);
225
Max Moroz1ef3a772018-01-04 19:33:29 +0000226 if (!Options.ExportSummaryOnly) {
Max Moroz6242cac2018-01-12 20:31:32 +0000227 // Calculate and render detailed coverage information for given file.
228 auto FileCoverage = Coverage.getCoverageForFile(Filename);
229 renderFileCoverage(FileCoverage, FileReport);
Max Moroz1ef3a772018-01-04 19:33:29 +0000230 }
231
232 emitDictKey("summary");
233 renderSummary(FileReport);
234
235 // End File.
236 emitDictEnd();
237}
238
Max Moroz6242cac2018-01-12 20:31:32 +0000239
240void CoverageExporterJson::renderFileCoverage(
241 const coverage::CoverageData &FileCoverage,
242 const FileCoverageSummary &FileReport) {
243 emitDictKey("segments");
244
245 // Start List of Segments.
246 emitArrayStart();
247
248 for (const auto &Segment : FileCoverage)
249 renderSegment(Segment);
250
251 // End List of Segments.
252 emitArrayEnd();
253
254 emitDictKey("expansions");
255
256 // Start List of Expansions.
257 emitArrayStart();
258
259 for (const auto &Expansion : FileCoverage.getExpansions())
260 renderExpansion(Expansion);
261
262 // End List of Expansions.
263 emitArrayEnd();
264}
265
Max Moroz1ef3a772018-01-04 19:33:29 +0000266void CoverageExporterJson::renderSegment(
267 const coverage::CoverageSegment &Segment) {
268 // Start Segment.
269 emitArrayStart();
270
271 emitArrayElement(Segment.Line);
272 emitArrayElement(Segment.Col);
273 emitArrayElement(Segment.Count);
274 emitArrayElement(Segment.HasCount);
275 emitArrayElement(Segment.IsRegionEntry);
276
277 // End Segment.
278 emitArrayEnd();
279}
280
281void CoverageExporterJson::renderExpansion(
282 const coverage::ExpansionRecord &Expansion) {
283 // Start Expansion.
284 emitDictStart();
285
286 // Mark the beginning and end of this expansion in the source file.
287 emitDictKey("source_region");
288 renderRegion(Expansion.Region);
289
290 // Enumerate the coverage information for the expansion.
291 emitDictKey("target_regions");
292 renderRegions(Expansion.Function.CountedRegions);
293
294 emitDictKey("filenames");
295 // Start List of Filenames to map the fileIDs.
296 emitArrayStart();
297 for (const auto &Filename : Expansion.Function.Filenames)
298 emitArrayElement(Filename);
299 // End List of Filenames.
300 emitArrayEnd();
301
302 // End Expansion.
303 emitDictEnd();
304}
305
306void CoverageExporterJson::renderRegions(
307 ArrayRef<coverage::CountedRegion> Regions) {
308 // Start List of Regions.
309 emitArrayStart();
310
311 for (const auto &Region : Regions)
312 renderRegion(Region);
313
314 // End List of Regions.
315 emitArrayEnd();
316}
317
318void CoverageExporterJson::renderRegion(const coverage::CountedRegion &Region) {
319 // Start CountedRegion.
320 emitArrayStart();
321
322 emitArrayElement(Region.LineStart);
323 emitArrayElement(Region.ColumnStart);
324 emitArrayElement(Region.LineEnd);
325 emitArrayElement(Region.ColumnEnd);
326 emitArrayElement(Region.ExecutionCount);
327 emitArrayElement(Region.FileID);
328 emitArrayElement(Region.ExpandedFileID);
329 emitArrayElement(Region.Kind);
330
331 // End CountedRegion.
332 emitArrayEnd();
333}
334
335void CoverageExporterJson::renderSummary(const FileCoverageSummary &Summary) {
336 // Start Summary for the file.
337 emitDictStart();
338
339 emitDictKey("lines");
340
341 // Start Line Coverage Summary.
342 emitDictStart();
343 emitDictElement("count", Summary.LineCoverage.getNumLines());
344 emitDictElement("covered", Summary.LineCoverage.getCovered());
345 emitDictElement("percent", Summary.LineCoverage.getPercentCovered());
346 // End Line Coverage Summary.
347 emitDictEnd();
348
349 emitDictKey("functions");
350
351 // Start Function Coverage Summary.
352 emitDictStart();
353 emitDictElement("count", Summary.FunctionCoverage.getNumFunctions());
354 emitDictElement("covered", Summary.FunctionCoverage.getExecuted());
355 emitDictElement("percent", Summary.FunctionCoverage.getPercentCovered());
356 // End Function Coverage Summary.
357 emitDictEnd();
358
359 emitDictKey("instantiations");
360
361 // Start Instantiation Coverage Summary.
362 emitDictStart();
363 emitDictElement("count", Summary.InstantiationCoverage.getNumFunctions());
364 emitDictElement("covered", Summary.InstantiationCoverage.getExecuted());
365 emitDictElement("percent", Summary.InstantiationCoverage.getPercentCovered());
366 // End Function Coverage Summary.
367 emitDictEnd();
368
369 emitDictKey("regions");
370
371 // Start Region Coverage Summary.
372 emitDictStart();
373 emitDictElement("count", Summary.RegionCoverage.getNumRegions());
374 emitDictElement("covered", Summary.RegionCoverage.getCovered());
375 emitDictElement("notcovered", Summary.RegionCoverage.getNumRegions() -
376 Summary.RegionCoverage.getCovered());
377 emitDictElement("percent", Summary.RegionCoverage.getPercentCovered());
378 // End Region Coverage Summary.
379 emitDictEnd();
380
381 // End Summary for the file.
382 emitDictEnd();
Vedant Kumar7101d732016-07-26 22:50:58 +0000383}