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