1 //===- 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
39 // ------ InstantiationCoverage: dict => Object summarizing inst. coverage
40 // ------ RegionCoverage: dict => Object summarizing region coverage
41 //
42 //===----------------------------------------------------------------------===//
43
44 #include "CoverageExporterJson.h"
45 #include "CoverageReport.h"
46
47 /// The semantic version combined as a string.
48 #define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.0"
49
50 /// Unique type identifier for JSON coverage export.
51 #define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
52
53 using namespace llvm;
54
CoverageExporterJson(const coverage::CoverageMapping & CoverageMapping,const CoverageViewOptions & Options,raw_ostream & OS)55 CoverageExporterJson::CoverageExporterJson(
56 const coverage::CoverageMapping &CoverageMapping,
57 const CoverageViewOptions &Options, raw_ostream &OS)
58 : CoverageExporter(CoverageMapping, Options, OS) {
59 State.push(JsonState::None);
60 }
61
emitSerialized(const int64_t Value)62 void CoverageExporterJson::emitSerialized(const int64_t Value) { OS << Value; }
63
emitSerialized(const std::string & Value)64 void CoverageExporterJson::emitSerialized(const std::string &Value) {
65 OS << "\"";
66 for (char C : Value) {
67 if (C != '\\')
68 OS << C;
69 else
70 OS << "\\\\";
71 }
72 OS << "\"";
73 }
74
emitComma()75 void CoverageExporterJson::emitComma() {
76 if (State.top() == JsonState::NonEmptyElement) {
77 OS << ",";
78 } else if (State.top() == JsonState::EmptyElement) {
79 State.pop();
80 assert((State.size() >= 1) && "Closed too many JSON elements");
81 State.push(JsonState::NonEmptyElement);
82 }
83 }
84
emitDictStart()85 void CoverageExporterJson::emitDictStart() {
86 emitComma();
87 State.push(JsonState::EmptyElement);
88 OS << "{";
89 }
90
emitDictKey(const std::string & Key)91 void 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");
97
98 // We do not want to emit a comma after this key.
99 State.push(JsonState::EmptyElement);
100 }
101
emitDictEnd()102 void CoverageExporterJson::emitDictEnd() {
103 State.pop();
104 assert((State.size() >= 1) && "Closed too many JSON elements");
105 OS << "}";
106 }
107
emitArrayStart()108 void CoverageExporterJson::emitArrayStart() {
109 emitComma();
110 State.push(JsonState::EmptyElement);
111 OS << "[";
112 }
113
emitArrayEnd()114 void CoverageExporterJson::emitArrayEnd() {
115 State.pop();
116 assert((State.size() >= 1) && "Closed too many JSON elements");
117 OS << "]";
118 }
119
renderRoot(const CoverageFilters & IgnoreFilenameFilters)120 void CoverageExporterJson::renderRoot(
121 const CoverageFilters &IgnoreFilenameFilters) {
122 std::vector<std::string> SourceFiles;
123 for (StringRef SF : Coverage.getUniqueSourceFiles()) {
124 if (!IgnoreFilenameFilters.matchesFilename(SF))
125 SourceFiles.emplace_back(SF);
126 }
127 renderRoot(SourceFiles);
128 }
129
renderRoot(const std::vector<std::string> & SourceFiles)130 void CoverageExporterJson::renderRoot(
131 const std::vector<std::string> &SourceFiles) {
132 // Start Root of JSON object.
133 emitDictStart();
134
135 emitDictElement("version", LLVM_COVERAGE_EXPORT_JSON_STR);
136 emitDictElement("type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR);
137 emitDictKey("data");
138
139 // Start List of Exports.
140 emitArrayStart();
141
142 // Start Export.
143 emitDictStart();
144
145 emitDictKey("files");
146
147 FileCoverageSummary Totals = FileCoverageSummary("Totals");
148 auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
149 SourceFiles, Options);
150 renderFiles(SourceFiles, FileReports);
151
152 // Skip functions-level information for summary-only export mode.
153 if (!Options.ExportSummaryOnly) {
154 emitDictKey("functions");
155 renderFunctions(Coverage.getCoveredFunctions());
156 }
157
158 emitDictKey("totals");
159 renderSummary(Totals);
160
161 // End Export.
162 emitDictEnd();
163
164 // End List of Exports.
165 emitArrayEnd();
166
167 // End Root of JSON Object.
168 emitDictEnd();
169
170 assert((State.top() == JsonState::None) &&
171 "All Elements In JSON were Closed");
172 }
173
renderFunctions(const iterator_range<coverage::FunctionRecordIterator> & Functions)174 void CoverageExporterJson::renderFunctions(
175 const iterator_range<coverage::FunctionRecordIterator> &Functions) {
176 // Start List of Functions.
177 emitArrayStart();
178
179 for (const auto &Function : Functions) {
180 // Start Function.
181 emitDictStart();
182
183 emitDictElement("name", Function.Name);
184 emitDictElement("count", Function.ExecutionCount);
185 emitDictKey("regions");
186
187 renderRegions(Function.CountedRegions);
188
189 emitDictKey("filenames");
190
191 // Start Filenames for Function.
192 emitArrayStart();
193
194 for (const auto &FileName : Function.Filenames)
195 emitArrayElement(FileName);
196
197 // End Filenames for Function.
198 emitArrayEnd();
199
200 // End Function.
201 emitDictEnd();
202 }
203
204 // End List of Functions.
205 emitArrayEnd();
206 }
207
renderFiles(ArrayRef<std::string> SourceFiles,ArrayRef<FileCoverageSummary> FileReports)208 void CoverageExporterJson::renderFiles(
209 ArrayRef<std::string> SourceFiles,
210 ArrayRef<FileCoverageSummary> FileReports) {
211 // Start List of Files.
212 emitArrayStart();
213
214 for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) {
215 renderFile(SourceFiles[I], FileReports[I]);
216 }
217
218 // End List of Files.
219 emitArrayEnd();
220 }
221
renderFile(const std::string & Filename,const FileCoverageSummary & FileReport)222 void CoverageExporterJson::renderFile(const std::string &Filename,
223 const FileCoverageSummary &FileReport) {
224 // Start File.
225 emitDictStart();
226
227 emitDictElement("filename", Filename);
228
229 if (!Options.ExportSummaryOnly) {
230 // Calculate and render detailed coverage information for given file.
231 auto FileCoverage = Coverage.getCoverageForFile(Filename);
232 renderFileCoverage(FileCoverage, FileReport);
233 }
234
235 emitDictKey("summary");
236 renderSummary(FileReport);
237
238 // End File.
239 emitDictEnd();
240 }
241
242
renderFileCoverage(const coverage::CoverageData & FileCoverage,const FileCoverageSummary & FileReport)243 void CoverageExporterJson::renderFileCoverage(
244 const coverage::CoverageData &FileCoverage,
245 const FileCoverageSummary &FileReport) {
246 emitDictKey("segments");
247
248 // Start List of Segments.
249 emitArrayStart();
250
251 for (const auto &Segment : FileCoverage)
252 renderSegment(Segment);
253
254 // End List of Segments.
255 emitArrayEnd();
256
257 emitDictKey("expansions");
258
259 // Start List of Expansions.
260 emitArrayStart();
261
262 for (const auto &Expansion : FileCoverage.getExpansions())
263 renderExpansion(Expansion);
264
265 // End List of Expansions.
266 emitArrayEnd();
267 }
268
renderSegment(const coverage::CoverageSegment & Segment)269 void CoverageExporterJson::renderSegment(
270 const coverage::CoverageSegment &Segment) {
271 // Start Segment.
272 emitArrayStart();
273
274 emitArrayElement(Segment.Line);
275 emitArrayElement(Segment.Col);
276 emitArrayElement(Segment.Count);
277 emitArrayElement(Segment.HasCount);
278 emitArrayElement(Segment.IsRegionEntry);
279
280 // End Segment.
281 emitArrayEnd();
282 }
283
renderExpansion(const coverage::ExpansionRecord & Expansion)284 void CoverageExporterJson::renderExpansion(
285 const coverage::ExpansionRecord &Expansion) {
286 // Start Expansion.
287 emitDictStart();
288
289 // Mark the beginning and end of this expansion in the source file.
290 emitDictKey("source_region");
291 renderRegion(Expansion.Region);
292
293 // Enumerate the coverage information for the expansion.
294 emitDictKey("target_regions");
295 renderRegions(Expansion.Function.CountedRegions);
296
297 emitDictKey("filenames");
298 // Start List of Filenames to map the fileIDs.
299 emitArrayStart();
300 for (const auto &Filename : Expansion.Function.Filenames)
301 emitArrayElement(Filename);
302 // End List of Filenames.
303 emitArrayEnd();
304
305 // End Expansion.
306 emitDictEnd();
307 }
308
renderRegions(ArrayRef<coverage::CountedRegion> Regions)309 void CoverageExporterJson::renderRegions(
310 ArrayRef<coverage::CountedRegion> Regions) {
311 // Start List of Regions.
312 emitArrayStart();
313
314 for (const auto &Region : Regions)
315 renderRegion(Region);
316
317 // End List of Regions.
318 emitArrayEnd();
319 }
320
renderRegion(const coverage::CountedRegion & Region)321 void CoverageExporterJson::renderRegion(const coverage::CountedRegion &Region) {
322 // Start CountedRegion.
323 emitArrayStart();
324
325 emitArrayElement(Region.LineStart);
326 emitArrayElement(Region.ColumnStart);
327 emitArrayElement(Region.LineEnd);
328 emitArrayElement(Region.ColumnEnd);
329 emitArrayElement(Region.ExecutionCount);
330 emitArrayElement(Region.FileID);
331 emitArrayElement(Region.ExpandedFileID);
332 emitArrayElement(Region.Kind);
333
334 // End CountedRegion.
335 emitArrayEnd();
336 }
337
renderSummary(const FileCoverageSummary & Summary)338 void CoverageExporterJson::renderSummary(const FileCoverageSummary &Summary) {
339 // Start Summary for the file.
340 emitDictStart();
341
342 emitDictKey("lines");
343
344 // Start Line Coverage Summary.
345 emitDictStart();
346 emitDictElement("count", Summary.LineCoverage.getNumLines());
347 emitDictElement("covered", Summary.LineCoverage.getCovered());
348 emitDictElement("percent", Summary.LineCoverage.getPercentCovered());
349 // End Line Coverage Summary.
350 emitDictEnd();
351
352 emitDictKey("functions");
353
354 // Start Function Coverage Summary.
355 emitDictStart();
356 emitDictElement("count", Summary.FunctionCoverage.getNumFunctions());
357 emitDictElement("covered", Summary.FunctionCoverage.getExecuted());
358 emitDictElement("percent", Summary.FunctionCoverage.getPercentCovered());
359 // End Function Coverage Summary.
360 emitDictEnd();
361
362 emitDictKey("instantiations");
363
364 // Start Instantiation Coverage Summary.
365 emitDictStart();
366 emitDictElement("count", Summary.InstantiationCoverage.getNumFunctions());
367 emitDictElement("covered", Summary.InstantiationCoverage.getExecuted());
368 emitDictElement("percent", Summary.InstantiationCoverage.getPercentCovered());
369 // End Function Coverage Summary.
370 emitDictEnd();
371
372 emitDictKey("regions");
373
374 // Start Region Coverage Summary.
375 emitDictStart();
376 emitDictElement("count", Summary.RegionCoverage.getNumRegions());
377 emitDictElement("covered", Summary.RegionCoverage.getCovered());
378 emitDictElement("notcovered", Summary.RegionCoverage.getNumRegions() -
379 Summary.RegionCoverage.getCovered());
380 emitDictElement("percent", Summary.RegionCoverage.getPercentCovered());
381 // End Region Coverage Summary.
382 emitDictEnd();
383
384 // End Summary for the file.
385 emitDictEnd();
386 }
387