blob: 4ce07b59f0f28c11d19340219c0ed032db3784bc [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
39// ------ RegionCoverage: dict => Object summarizing region coverage
40//
41//===----------------------------------------------------------------------===//
42
43#include "CoverageSummaryInfo.h"
44#include "CoverageViewOptions.h"
45#include "llvm/ProfileData/Coverage/CoverageMapping.h"
46#include <stack>
47
48/// \brief Major version of the JSON Coverage Export Format.
49#define LLVM_COVERAGE_EXPORT_JSON_MAJOR 1
50
51/// \brief Minor version of the JSON Coverage Export Format.
52#define LLVM_COVERAGE_EXPORT_JSON_MINOR 0
53
54/// \brief Patch version of the JSON Coverage Export Format.
55#define LLVM_COVERAGE_EXPORT_JSON_PATCH 0
56
57/// \brief The semantic version combined as a string.
58#define LLVM_COVERAGE_EXPORT_JSON_STR "1.0.0"
59
60/// \brief Unique type identifier for JSON coverage export.
61#define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
62
63using namespace llvm;
64using namespace coverage;
65
66class CoverageExporterJson {
67 /// \brief A Name of the object file coverage is for.
68 StringRef ObjectFilename;
69
70 /// \brief Output stream to print JSON to.
71 raw_ostream &OS;
72
73 /// \brief The full CoverageMapping object to export.
74 CoverageMapping Coverage;
75
76 /// \brief States that the JSON rendering machine can be in.
77 enum JsonState { None, NonEmptyElement, EmptyElement };
78
79 /// \brief Tracks state of the JSON output.
80 std::stack<JsonState> State;
81
82 /// \brief Get the object filename.
83 StringRef getObjectFilename() const { return ObjectFilename; }
84
85 /// \brief Emit a serialized scalar.
86 void emitSerialized(const int64_t Value) { OS << Value; }
87
88 /// \brief Emit a serialized string.
89 void emitSerialized(const std::string &Value) { OS << "\"" << Value << "\""; }
90
91 /// \brief Emit a comma if there is a previous element to delimit.
92 void emitComma() {
93 if (State.top() == JsonState::NonEmptyElement) {
94 OS << ",";
95 } else if (State.top() == JsonState::EmptyElement) {
96 State.pop();
97 assert((State.size() >= 1) && "Closed too many JSON elements");
98 State.push(JsonState::NonEmptyElement);
99 }
100 }
101
102 /// \brief Emit a starting dictionary/object character.
103 void emitDictStart() {
104 emitComma();
105 State.push(JsonState::EmptyElement);
106 OS << "{";
107 }
108
109 /// \brief Emit a dictionary/object key but no value.
110 void emitDictKey(const std::string &Key) {
111 emitComma();
112 OS << "\"" << Key << "\":";
113 State.pop();
114 assert((State.size() >= 1) && "Closed too many JSON elements");
115
116 // We do not want to emit a comma after this key.
117 State.push(JsonState::EmptyElement);
118 }
119
120 /// \brief Emit a dictionary/object key/value pair.
121 template <typename V>
122 void emitDictElement(const std::string &Key, const V &Value) {
123 emitComma();
124 emitSerialized(Key);
125 OS << ":";
126 emitSerialized(Value);
127 }
128
129 /// \brief Emit a closing dictionary/object character.
130 void emitDictEnd() {
131 State.pop();
132 assert((State.size() >= 1) && "Closed too many JSON elements");
133 OS << "}";
134 }
135
136 /// \brief Emit a starting array character.
137 void emitArrayStart() {
138 emitComma();
139 State.push(JsonState::EmptyElement);
140 OS << "[";
141 }
142
143 /// \brief Emit an array element.
144 template <typename V> void emitArrayElement(const V &Value) {
145 emitComma();
146 emitSerialized(Value);
147 }
148
149 /// \brief emit a closing array character.
150 void emitArrayEnd() {
151 State.pop();
152 assert((State.size() >= 1) && "Closed too many JSON elements");
153 OS << "]";
154 }
155
156 /// \brief Render the CoverageMapping object.
157 void renderRoot() {
158 // Start Root of JSON object.
159 emitDictStart();
160
161 emitDictElement("version", LLVM_COVERAGE_EXPORT_JSON_STR);
162 emitDictElement("type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR);
163 emitDictKey("data");
164
165 // Start List of Exports.
166 emitArrayStart();
167
168 // Start Export.
169 emitDictStart();
170 emitDictElement("object", getObjectFilename());
171
172 emitDictKey("files");
173 FileCoverageSummary Totals = FileCoverageSummary("Totals");
174 renderFiles(Coverage.getUniqueSourceFiles(), Totals);
175
176 emitDictKey("functions");
177 renderFunctions(Coverage.getCoveredFunctions());
178
179 emitDictKey("totals");
180 renderSummary(Totals);
181
182 // End Export.
183 emitDictEnd();
184
185 // End List of Exports.
186 emitArrayEnd();
187
188 // End Root of JSON Object.
189 emitDictEnd();
190
191 assert((State.top() == JsonState::None) &&
192 "All Elements In JSON were Closed");
193 }
194
195 /// \brief Render an array of all the given functions.
196 void
197 renderFunctions(const iterator_range<FunctionRecordIterator> &Functions) {
198 // Start List of Functions.
199 emitArrayStart();
200
201 for (const auto &Function : Functions) {
202 // Start Function.
203 emitDictStart();
204
205 emitDictElement("name", Function.Name);
206 emitDictElement("count", Function.ExecutionCount);
207 emitDictKey("regions");
208
209 renderRegions(Function.CountedRegions);
210
211 emitDictKey("filenames");
212
213 // Start Filenames for Function.
214 emitArrayStart();
215
216 for (const auto &FileName : Function.Filenames)
217 emitArrayElement(FileName);
218
219 // End Filenames for Function.
220 emitArrayEnd();
221
222 // End Function.
223 emitDictEnd();
224 }
225
226 // End List of Functions.
227 emitArrayEnd();
228 }
229
230 /// \brief Render an array of all the source files, also pass back a Summary.
231 void renderFiles(ArrayRef<StringRef> SourceFiles,
232 FileCoverageSummary &Summary) {
233 // Start List of Files.
234 emitArrayStart();
235 for (const auto &SourceFile : SourceFiles) {
236 // Render the file.
237 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
238 renderFile(FileCoverage);
239
240 for (const auto &F : Coverage.getCoveredFunctions(SourceFile))
241 Summary.addFunction(FunctionCoverageSummary::get(F));
242 }
243
244 // End List of Files.
245 emitArrayEnd();
246 }
247
248 /// \brief Render a single file.
249 void renderFile(const CoverageData &FileCoverage) {
250 // Start File.
251 emitDictStart();
252
253 emitDictElement("filename", FileCoverage.getFilename());
254 emitDictKey("segments");
255
256 // Start List of Segments.
257 emitArrayStart();
258
259 for (const auto &Segment : FileCoverage)
260 renderSegment(Segment);
261
262 // End List of Segments.
263 emitArrayEnd();
264
265 emitDictKey("expansions");
266
267 // Start List of Expansions.
268 emitArrayStart();
269
270 for (const auto &Expansion : FileCoverage.getExpansions())
271 renderExpansion(Expansion);
272
273 // End List of Expansions.
274 emitArrayEnd();
275
276 FileCoverageSummary Summary =
277 FileCoverageSummary(FileCoverage.getFilename());
278 for (const auto &F :
279 Coverage.getCoveredFunctions(FileCoverage.getFilename()))
280 Summary.addFunction(FunctionCoverageSummary::get(F));
281
282 emitDictKey("summary");
283 renderSummary(Summary);
284
285 // End File.
286 emitDictEnd();
287 }
288
289 /// \brief Render a CoverageSegment.
290 void renderSegment(const CoverageSegment &Segment) {
291 // Start Segment.
292 emitArrayStart();
293
294 emitArrayElement(Segment.Line);
295 emitArrayElement(Segment.Col);
296 emitArrayElement(Segment.Count);
297 emitArrayElement(Segment.HasCount);
298 emitArrayElement(Segment.IsRegionEntry);
299
300 // End Segment.
301 emitArrayEnd();
302 }
303
304 /// \brief Render an ExpansionRecord.
305 void renderExpansion(const ExpansionRecord &Expansion) {
306 // Start Expansion.
307 emitDictStart();
308
309 // Mark the beginning and end of this expansion in the source file.
310 emitDictKey("source_region");
311 renderRegion(Expansion.Region);
312
313 // Enumerate the coverage information for the expansion.
314 emitDictKey("target_regions");
315 renderRegions(Expansion.Function.CountedRegions);
316
317 emitDictKey("filenames");
318 // Start List of Filenames to map the fileIDs.
319 emitArrayStart();
320 for (const auto &Filename : Expansion.Function.Filenames)
321 emitArrayElement(Filename);
322 // End List of Filenames.
323 emitArrayEnd();
324
325 // End Expansion.
326 emitDictEnd();
327 }
328
329 /// \brief Render a list of CountedRegions.
330 void renderRegions(ArrayRef<CountedRegion> Regions) {
331 // Start List of Regions.
332 emitArrayStart();
333
334 for (const auto &Region : Regions)
335 renderRegion(Region);
336
337 // End List of Regions.
338 emitArrayEnd();
339 }
340
341 /// \brief Render a single CountedRegion.
342 void renderRegion(const CountedRegion &Region) {
343 // Start CountedRegion.
344 emitArrayStart();
345
346 emitArrayElement(Region.LineStart);
347 emitArrayElement(Region.ColumnStart);
348 emitArrayElement(Region.LineEnd);
349 emitArrayElement(Region.ColumnEnd);
350 emitArrayElement(Region.ExecutionCount);
351 emitArrayElement(Region.FileID);
352 emitArrayElement(Region.ExpandedFileID);
353 emitArrayElement(Region.Kind);
354
355 // End CountedRegion.
356 emitArrayEnd();
357 }
358
359 /// \brief Render a FileCoverageSummary.
360 void renderSummary(const FileCoverageSummary &Summary) {
361 // Start Summary for the file.
362 emitDictStart();
363
364 emitDictKey("lines");
365
366 // Start Line Coverage Summary.
367 emitDictStart();
368 emitDictElement("count", Summary.LineCoverage.NumLines);
369 emitDictElement("covered", Summary.LineCoverage.Covered);
370 emitDictElement("percent", Summary.LineCoverage.getPercentCovered());
371 emitDictElement("noncode", Summary.LineCoverage.NonCodeLines);
372 // End Line Coverage Summary.
373 emitDictEnd();
374
375 emitDictKey("functions");
376
377 // Start Function Coverage Summary.
378 emitDictStart();
379 emitDictElement("count", Summary.FunctionCoverage.NumFunctions);
380 emitDictElement("covered", Summary.FunctionCoverage.Executed);
381 emitDictElement("percent", Summary.FunctionCoverage.getPercentCovered());
382 // End Function Coverage Summary.
383 emitDictEnd();
384
385 emitDictKey("regions");
386
387 // Start Region Coverage Summary.
388 emitDictStart();
389 emitDictElement("count", Summary.RegionCoverage.NumRegions);
390 emitDictElement("covered", Summary.RegionCoverage.Covered);
391 emitDictElement("notcovered", Summary.RegionCoverage.NotCovered);
392 emitDictElement("percent", Summary.RegionCoverage.getPercentCovered());
393 // End Region Coverage Summary.
394 emitDictEnd();
395
396 // End Summary for the file.
397 emitDictEnd();
398 }
399
400public:
401 CoverageExporterJson(StringRef ObjectFilename,
402 const CoverageMapping &CoverageMapping, raw_ostream &OS)
403 : ObjectFilename(ObjectFilename), OS(OS), Coverage(CoverageMapping) {
404 State.push(JsonState::None);
405 }
406
407 /// \brief Print the CoverageMapping.
408 void print() { renderRoot(); }
409};
410
411/// \brief Export the given CoverageMapping to a JSON Format.
412void exportCoverageDataToJson(StringRef ObjectFilename,
413 const CoverageMapping &CoverageMapping,
414 raw_ostream &OS) {
415 auto Exporter = CoverageExporterJson(ObjectFilename, CoverageMapping, OS);
416
417 Exporter.print();
418}