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