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