1 //===- CoverageExporterJson.cpp - Code coverage export --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements export of code coverage data to JSON.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
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
42 //===----------------------------------------------------------------------===//
44 #include "CoverageReport.h"
45 #include "CoverageSummaryInfo.h"
46 #include "CoverageViewOptions.h"
47 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
50 /// \brief The semantic version combined as a string.
51 #define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.0"
53 /// \brief Unique type identifier for JSON coverage export.
54 #define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
57 using namespace coverage;
59 class CoverageExporterJson {
60 /// \brief Output stream to print JSON to.
63 /// \brief The full CoverageMapping object to export.
64 const CoverageMapping &Coverage;
66 /// \brief States that the JSON rendering machine can be in.
67 enum JsonState { None, NonEmptyElement, EmptyElement };
69 /// \brief Tracks state of the JSON output.
70 std::stack<JsonState> State;
72 /// \brief Emit a serialized scalar.
73 void emitSerialized(const int64_t Value) { OS << Value; }
75 /// \brief Emit a serialized string.
76 void emitSerialized(const std::string &Value) {
78 for (char C : Value) {
87 /// \brief Emit a comma if there is a previous element to delimit.
89 if (State.top() == JsonState::NonEmptyElement) {
91 } else if (State.top() == JsonState::EmptyElement) {
93 assert((State.size() >= 1) && "Closed too many JSON elements");
94 State.push(JsonState::NonEmptyElement);
98 /// \brief Emit a starting dictionary/object character.
99 void emitDictStart() {
101 State.push(JsonState::EmptyElement);
105 /// \brief Emit a dictionary/object key but no value.
106 void emitDictKey(const std::string &Key) {
111 assert((State.size() >= 1) && "Closed too many JSON elements");
113 // We do not want to emit a comma after this key.
114 State.push(JsonState::EmptyElement);
117 /// \brief Emit a dictionary/object key/value pair.
118 template <typename V>
119 void emitDictElement(const std::string &Key, const V &Value) {
123 emitSerialized(Value);
126 /// \brief Emit a closing dictionary/object character.
129 assert((State.size() >= 1) && "Closed too many JSON elements");
133 /// \brief Emit a starting array character.
134 void emitArrayStart() {
136 State.push(JsonState::EmptyElement);
140 /// \brief Emit an array element.
141 template <typename V> void emitArrayElement(const V &Value) {
143 emitSerialized(Value);
146 /// \brief emit a closing array character.
147 void emitArrayEnd() {
149 assert((State.size() >= 1) && "Closed too many JSON elements");
153 /// \brief Render the CoverageMapping object.
155 // Start Root of JSON object.
158 emitDictElement("version", LLVM_COVERAGE_EXPORT_JSON_STR);
159 emitDictElement("type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR);
162 // Start List of Exports.
168 emitDictKey("files");
170 FileCoverageSummary Totals = FileCoverageSummary("Totals");
171 std::vector<std::string> SourceFiles;
172 for (StringRef SF : Coverage.getUniqueSourceFiles())
173 SourceFiles.emplace_back(SF);
175 CoverageReport::prepareFileReports(Coverage, Totals, SourceFiles);
176 renderFiles(SourceFiles, FileReports);
178 emitDictKey("functions");
179 renderFunctions(Coverage.getCoveredFunctions());
181 emitDictKey("totals");
182 renderSummary(Totals);
187 // End List of Exports.
190 // End Root of JSON Object.
193 assert((State.top() == JsonState::None) &&
194 "All Elements In JSON were Closed");
197 /// \brief Render an array of all the given functions.
199 renderFunctions(const iterator_range<FunctionRecordIterator> &Functions) {
200 // Start List of Functions.
203 for (const auto &Function : Functions) {
207 emitDictElement("name", Function.Name);
208 emitDictElement("count", Function.ExecutionCount);
209 emitDictKey("regions");
211 renderRegions(Function.CountedRegions);
213 emitDictKey("filenames");
215 // Start Filenames for Function.
218 for (const auto &FileName : Function.Filenames)
219 emitArrayElement(FileName);
221 // End Filenames for Function.
228 // End List of Functions.
232 /// \brief Render an array of all the source files, also pass back a Summary.
233 void renderFiles(ArrayRef<std::string> SourceFiles,
234 ArrayRef<FileCoverageSummary> FileReports) {
235 // Start List of Files.
238 for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) {
240 auto FileCoverage = Coverage.getCoverageForFile(SourceFiles[I]);
241 renderFile(FileCoverage, FileReports[I]);
244 // End List of Files.
248 /// \brief Render a single file.
249 void renderFile(const CoverageData &FileCoverage,
250 const FileCoverageSummary &FileReport) {
254 emitDictElement("filename", FileCoverage.getFilename());
255 emitDictKey("segments");
257 // Start List of Segments.
260 for (const auto &Segment : FileCoverage)
261 renderSegment(Segment);
263 // End List of Segments.
266 emitDictKey("expansions");
268 // Start List of Expansions.
271 for (const auto &Expansion : FileCoverage.getExpansions())
272 renderExpansion(Expansion);
274 // End List of Expansions.
277 emitDictKey("summary");
278 renderSummary(FileReport);
284 /// \brief Render a CoverageSegment.
285 void renderSegment(const CoverageSegment &Segment) {
289 emitArrayElement(Segment.Line);
290 emitArrayElement(Segment.Col);
291 emitArrayElement(Segment.Count);
292 emitArrayElement(Segment.HasCount);
293 emitArrayElement(Segment.IsRegionEntry);
299 /// \brief Render an ExpansionRecord.
300 void renderExpansion(const ExpansionRecord &Expansion) {
304 // Mark the beginning and end of this expansion in the source file.
305 emitDictKey("source_region");
306 renderRegion(Expansion.Region);
308 // Enumerate the coverage information for the expansion.
309 emitDictKey("target_regions");
310 renderRegions(Expansion.Function.CountedRegions);
312 emitDictKey("filenames");
313 // Start List of Filenames to map the fileIDs.
315 for (const auto &Filename : Expansion.Function.Filenames)
316 emitArrayElement(Filename);
317 // End List of Filenames.
324 /// \brief Render a list of CountedRegions.
325 void renderRegions(ArrayRef<CountedRegion> Regions) {
326 // Start List of Regions.
329 for (const auto &Region : Regions)
330 renderRegion(Region);
332 // End List of Regions.
336 /// \brief Render a single CountedRegion.
337 void renderRegion(const CountedRegion &Region) {
338 // Start CountedRegion.
341 emitArrayElement(Region.LineStart);
342 emitArrayElement(Region.ColumnStart);
343 emitArrayElement(Region.LineEnd);
344 emitArrayElement(Region.ColumnEnd);
345 emitArrayElement(Region.ExecutionCount);
346 emitArrayElement(Region.FileID);
347 emitArrayElement(Region.ExpandedFileID);
348 emitArrayElement(Region.Kind);
350 // End CountedRegion.
354 /// \brief Render a FileCoverageSummary.
355 void renderSummary(const FileCoverageSummary &Summary) {
356 // Start Summary for the file.
359 emitDictKey("lines");
361 // Start Line Coverage Summary.
363 emitDictElement("count", Summary.LineCoverage.NumLines);
364 emitDictElement("covered", Summary.LineCoverage.Covered);
365 emitDictElement("percent", Summary.LineCoverage.getPercentCovered());
366 // End Line Coverage Summary.
369 emitDictKey("functions");
371 // Start Function Coverage Summary.
373 emitDictElement("count", Summary.FunctionCoverage.NumFunctions);
374 emitDictElement("covered", Summary.FunctionCoverage.Executed);
375 emitDictElement("percent", Summary.FunctionCoverage.getPercentCovered());
376 // End Function Coverage Summary.
379 emitDictKey("instantiations");
381 // Start Instantiation Coverage Summary.
383 emitDictElement("count", Summary.InstantiationCoverage.NumFunctions);
384 emitDictElement("covered", Summary.InstantiationCoverage.Executed);
385 emitDictElement("percent",
386 Summary.InstantiationCoverage.getPercentCovered());
387 // End Function Coverage Summary.
390 emitDictKey("regions");
392 // Start Region Coverage Summary.
394 emitDictElement("count", Summary.RegionCoverage.NumRegions);
395 emitDictElement("covered", Summary.RegionCoverage.Covered);
396 emitDictElement("notcovered", Summary.RegionCoverage.NotCovered);
397 emitDictElement("percent", Summary.RegionCoverage.getPercentCovered());
398 // End Region Coverage Summary.
401 // End Summary for the file.
406 CoverageExporterJson(const CoverageMapping &CoverageMapping, raw_ostream &OS)
407 : OS(OS), Coverage(CoverageMapping) {
408 State.push(JsonState::None);
411 /// \brief Print the CoverageMapping.
412 void print() { renderRoot(); }
415 /// \brief Export the given CoverageMapping to a JSON Format.
416 void exportCoverageDataToJson(const CoverageMapping &CoverageMapping,
418 auto Exporter = CoverageExporterJson(CoverageMapping, OS);