1 //===- CoverageExporterJson.h - Code coverage JSON exporter ---------------===//
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 class implements a code coverage exporter for JSON format.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_COV_COVERAGEEXPORTERJSON_H
15 #define LLVM_COV_COVERAGEEXPORTERJSON_H
16 
17 #include "CoverageExporter.h"
18 #include <stack>
19 
20 namespace llvm {
21 
22 class CoverageExporterJson : public CoverageExporter {
23   /// States that the JSON rendering machine can be in.
24   enum JsonState { None, NonEmptyElement, EmptyElement };
25 
26   /// Tracks state of the JSON output.
27   std::stack<JsonState> State;
28 
29   /// Emit a serialized scalar.
30   void emitSerialized(const int64_t Value);
31 
32   /// Emit a serialized string.
33   void emitSerialized(const std::string &Value);
34 
35   /// Emit a comma if there is a previous element to delimit.
36   void emitComma();
37 
38   /// Emit a starting dictionary/object character.
39   void emitDictStart();
40 
41   /// Emit a dictionary/object key but no value.
42   void emitDictKey(const std::string &Key);
43 
44   /// Emit a dictionary/object key/value pair.
45   template <typename V>
emitDictElement(const std::string & Key,const V & Value)46   void emitDictElement(const std::string &Key, const V &Value) {
47     emitComma();
48     emitSerialized(Key);
49     OS << ":";
50     emitSerialized(Value);
51   }
52 
53   /// Emit a closing dictionary/object character.
54   void emitDictEnd();
55 
56   /// Emit a starting array character.
57   void emitArrayStart();
58 
59   /// Emit an array element.
emitArrayElement(const V & Value)60   template <typename V> void emitArrayElement(const V &Value) {
61     emitComma();
62     emitSerialized(Value);
63   }
64 
65   /// emit a closing array character.
66   void emitArrayEnd();
67 
68   /// Render an array of all the given functions.
69   void renderFunctions(
70       const iterator_range<coverage::FunctionRecordIterator> &Functions);
71 
72   /// Render an array of all the source files, also pass back a Summary.
73   void renderFiles(ArrayRef<std::string> SourceFiles,
74                    ArrayRef<FileCoverageSummary> FileReports);
75 
76   /// Render a single file.
77   void renderFile(const std::string &Filename,
78                   const FileCoverageSummary &FileReport);
79 
80   /// Render summary for a single file.
81   void renderFileCoverage(const coverage::CoverageData &FileCoverage,
82                           const FileCoverageSummary &FileReport);
83 
84   /// Render a CoverageSegment.
85   void renderSegment(const coverage::CoverageSegment &Segment);
86 
87   /// Render an ExpansionRecord.
88   void renderExpansion(const coverage::ExpansionRecord &Expansion);
89 
90   /// Render a list of CountedRegions.
91   void renderRegions(ArrayRef<coverage::CountedRegion> Regions);
92 
93   /// Render a single CountedRegion.
94   void renderRegion(const coverage::CountedRegion &Region);
95 
96   /// Render a FileCoverageSummary.
97   void renderSummary(const FileCoverageSummary &Summary);
98 
99 public:
100   CoverageExporterJson(const coverage::CoverageMapping &CoverageMapping,
101                        const CoverageViewOptions &Options, raw_ostream &OS);
102 
103   /// Render the CoverageMapping object.
104   void renderRoot(const CoverageFilters &IgnoreFilenameFilters) override;
105 
106   /// Render the CoverageMapping object for specified source files.
107   void renderRoot(const std::vector<std::string> &SourceFiles) override;
108 };
109 
110 } // end namespace llvm
111 
112 #endif // LLVM_COV_COVERAGEEXPORTERJSON_H
113