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