1 //===- SourceCoverageView.h - Code coverage view for source code ----------===//
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 /// \file This class implements rendering for code coverage of source code.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15 #define LLVM_COV_SOURCECOVERAGEVIEW_H
16 
17 #include "CoverageViewOptions.h"
18 #include "CoverageSummaryInfo.h"
19 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <vector>
22 
23 namespace llvm {
24 
25 using namespace coverage;
26 
27 class CoverageFiltersMatchAll;
28 class SourceCoverageView;
29 
30 /// A view that represents a macro or include expansion.
31 struct ExpansionView {
32   CounterMappingRegion Region;
33   std::unique_ptr<SourceCoverageView> View;
34 
ExpansionViewExpansionView35   ExpansionView(const CounterMappingRegion &Region,
36                 std::unique_ptr<SourceCoverageView> View)
37       : Region(Region), View(std::move(View)) {}
ExpansionViewExpansionView38   ExpansionView(ExpansionView &&RHS)
39       : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
40   ExpansionView &operator=(ExpansionView &&RHS) {
41     Region = std::move(RHS.Region);
42     View = std::move(RHS.View);
43     return *this;
44   }
45 
getLineExpansionView46   unsigned getLine() const { return Region.LineStart; }
getStartColExpansionView47   unsigned getStartCol() const { return Region.ColumnStart; }
getEndColExpansionView48   unsigned getEndCol() const { return Region.ColumnEnd; }
49 
50   friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
51     return LHS.Region.startLoc() < RHS.Region.startLoc();
52   }
53 };
54 
55 /// A view that represents a function instantiation.
56 struct InstantiationView {
57   StringRef FunctionName;
58   unsigned Line;
59   std::unique_ptr<SourceCoverageView> View;
60 
InstantiationViewInstantiationView61   InstantiationView(StringRef FunctionName, unsigned Line,
62                     std::unique_ptr<SourceCoverageView> View)
63       : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
64 
65   friend bool operator<(const InstantiationView &LHS,
66                         const InstantiationView &RHS) {
67     return LHS.Line < RHS.Line;
68   }
69 };
70 
71 /// A file manager that handles format-aware file creation.
72 class CoveragePrinter {
73 public:
74   struct StreamDestructor {
75     void operator()(raw_ostream *OS) const;
76   };
77 
78   using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>;
79 
80 protected:
81   const CoverageViewOptions &Opts;
82 
CoveragePrinter(const CoverageViewOptions & Opts)83   CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {}
84 
85   /// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
86   /// false, skip the ToplevelDir component. If \p Relative is false, skip the
87   /// OutputDir component.
88   std::string getOutputPath(StringRef Path, StringRef Extension,
89                             bool InToplevel, bool Relative = true) const;
90 
91   /// If directory output is enabled, create a file in that directory
92   /// at the path given by getOutputPath(). Otherwise, return stdout.
93   Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension,
94                                            bool InToplevel) const;
95 
96   /// Return the sub-directory name for file coverage reports.
getCoverageDir()97   static StringRef getCoverageDir() { return "coverage"; }
98 
99 public:
100   static std::unique_ptr<CoveragePrinter>
101   create(const CoverageViewOptions &Opts);
102 
~CoveragePrinter()103   virtual ~CoveragePrinter() {}
104 
105   /// @name File Creation Interface
106   /// @{
107 
108   /// Create a file to print a coverage view into.
109   virtual Expected<OwnedStream> createViewFile(StringRef Path,
110                                                bool InToplevel) = 0;
111 
112   /// Close a file which has been used to print a coverage view.
113   virtual void closeViewFile(OwnedStream OS) = 0;
114 
115   /// Create an index which lists reports for the given source files.
116   virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
117                                 const CoverageMapping &Coverage,
118                                 const CoverageFiltersMatchAll &Filters) = 0;
119 
120   /// @}
121 };
122 
123 /// A code coverage view of a source file or function.
124 ///
125 /// A source coverage view and its nested sub-views form a file-oriented
126 /// representation of code coverage data. This view can be printed out by a
127 /// renderer which implements the Rendering Interface.
128 class SourceCoverageView {
129   /// A function or file name.
130   StringRef SourceName;
131 
132   /// A memory buffer backing the source on display.
133   const MemoryBuffer &File;
134 
135   /// Various options to guide the coverage renderer.
136   const CoverageViewOptions &Options;
137 
138   /// Complete coverage information about the source on display.
139   CoverageData CoverageInfo;
140 
141   /// A container for all expansions (e.g macros) in the source on display.
142   std::vector<ExpansionView> ExpansionSubViews;
143 
144   /// A container for all instantiations (e.g template functions) in the source
145   /// on display.
146   std::vector<InstantiationView> InstantiationSubViews;
147 
148   /// Get the first uncovered line number for the source file.
149   unsigned getFirstUncoveredLineNo();
150 
151 protected:
152   struct LineRef {
153     StringRef Line;
154     int64_t LineNo;
155 
LineRefLineRef156     LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
157   };
158 
159   using CoverageSegmentArray = ArrayRef<const CoverageSegment *>;
160 
161   /// @name Rendering Interface
162   /// @{
163 
164   /// Render a header for the view.
165   virtual void renderViewHeader(raw_ostream &OS) = 0;
166 
167   /// Render a footer for the view.
168   virtual void renderViewFooter(raw_ostream &OS) = 0;
169 
170   /// Render the source name for the view.
171   virtual void renderSourceName(raw_ostream &OS, bool WholeFile) = 0;
172 
173   /// Render the line prefix at the given \p ViewDepth.
174   virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
175 
176   /// Render the line suffix at the given \p ViewDepth.
177   virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
178 
179   /// Render a view divider at the given \p ViewDepth.
180   virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
181 
182   /// Render a source line with highlighting.
183   virtual void renderLine(raw_ostream &OS, LineRef L,
184                           const LineCoverageStats &LCS, unsigned ExpansionCol,
185                           unsigned ViewDepth) = 0;
186 
187   /// Render the line's execution count column.
188   virtual void renderLineCoverageColumn(raw_ostream &OS,
189                                         const LineCoverageStats &Line) = 0;
190 
191   /// Render the line number column.
192   virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
193 
194   /// Render all the region's execution counts on a line.
195   virtual void renderRegionMarkers(raw_ostream &OS,
196                                    const LineCoverageStats &Line,
197                                    unsigned ViewDepth) = 0;
198 
199   /// Render the site of an expansion.
200   virtual void renderExpansionSite(raw_ostream &OS, LineRef L,
201                                    const LineCoverageStats &LCS,
202                                    unsigned ExpansionCol,
203                                    unsigned ViewDepth) = 0;
204 
205   /// Render an expansion view and any nested views.
206   virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
207                                    unsigned ViewDepth) = 0;
208 
209   /// Render an instantiation view and any nested views.
210   virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
211                                        unsigned ViewDepth) = 0;
212 
213   /// Render \p Title, a project title if one is available, and the
214   /// created time.
215   virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0;
216 
217   /// Render the table header for a given source file.
218   virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo,
219                                  unsigned IndentLevel) = 0;
220 
221   /// @}
222 
223   /// Format a count using engineering notation with 3 significant
224   /// digits.
225   static std::string formatCount(uint64_t N);
226 
227   /// Check if region marker output is expected for a line.
228   bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
229 
230   /// Check if there are any sub-views attached to this view.
231   bool hasSubViews() const;
232 
SourceCoverageView(StringRef SourceName,const MemoryBuffer & File,const CoverageViewOptions & Options,CoverageData && CoverageInfo)233   SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
234                      const CoverageViewOptions &Options,
235                      CoverageData &&CoverageInfo)
236       : SourceName(SourceName), File(File), Options(Options),
237         CoverageInfo(std::move(CoverageInfo)) {}
238 
239 public:
240   static std::unique_ptr<SourceCoverageView>
241   create(StringRef SourceName, const MemoryBuffer &File,
242          const CoverageViewOptions &Options, CoverageData &&CoverageInfo);
243 
~SourceCoverageView()244   virtual ~SourceCoverageView() {}
245 
246   /// Return the source name formatted for the host OS.
247   std::string getSourceName() const;
248 
getOptions()249   const CoverageViewOptions &getOptions() const { return Options; }
250 
251   /// Add an expansion subview to this view.
252   void addExpansion(const CounterMappingRegion &Region,
253                     std::unique_ptr<SourceCoverageView> View);
254 
255   /// Add a function instantiation subview to this view.
256   void addInstantiation(StringRef FunctionName, unsigned Line,
257                         std::unique_ptr<SourceCoverageView> View);
258 
259   /// Print the code coverage information for a specific portion of a
260   /// source file to the output stream.
261   void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
262              bool ShowTitle, unsigned ViewDepth = 0);
263 };
264 
265 } // namespace llvm
266 
267 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H
268