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" |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 18 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 24 | class SourceCoverageView; |
| 25 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 26 | /// \brief A view that represents a macro or include expansion. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 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)) {} |
Justin Bogner | 99e9518 | 2014-09-17 06:32:48 +0000 | [diff] [blame] | 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 | } |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 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 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 51 | /// \brief A view that represents a function instantiation. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 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)) {} |
Justin Bogner | 99e9518 | 2014-09-17 06:32:48 +0000 | [diff] [blame] | 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 | } |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 69 | |
| 70 | friend bool operator<(const InstantiationView &LHS, |
| 71 | const InstantiationView &RHS) { |
| 72 | return LHS.Line < RHS.Line; |
| 73 | } |
| 74 | }; |
| 75 | |
Vedant Kumar | 60dcb48 | 2016-06-24 00:34:48 +0000 | [diff] [blame] | 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 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 100 | /// \brief A file manager that handles format-aware file creation. |
| 101 | class CoveragePrinter { |
| 102 | const CoverageViewOptions &Opts; |
| 103 | |
| 104 | public: |
| 105 | struct StreamDestructor { |
| 106 | void operator()(raw_ostream *OS) const; |
| 107 | }; |
| 108 | |
| 109 | using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>; |
| 110 | |
| 111 | protected: |
| 112 | CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {} |
| 113 | |
| 114 | /// \brief Return `OutputDir/ToplevelDir/Path.Extension`. |
| 115 | std::string getOutputPath(StringRef Path, StringRef Extension, |
| 116 | bool InToplevel); |
| 117 | |
| 118 | /// \brief If directory output is enabled, create a file in that directory |
| 119 | /// at the path given by getOutputPath(). Otherwise, return stdout. |
| 120 | Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension, |
| 121 | bool InToplevel); |
| 122 | |
| 123 | /// \brief Return the sub-directory name for file coverage reports. |
| 124 | static StringRef getCoverageDir() { return "coverage"; } |
| 125 | |
| 126 | public: |
| 127 | static std::unique_ptr<CoveragePrinter> |
| 128 | create(const CoverageViewOptions &Opts); |
| 129 | |
| 130 | virtual ~CoveragePrinter() {} |
| 131 | |
| 132 | /// @name File Creation Interface |
| 133 | /// @{ |
| 134 | |
| 135 | /// \brief Create a file to print a coverage view into. |
| 136 | virtual Expected<OwnedStream> createViewFile(StringRef Path, |
| 137 | bool InToplevel) = 0; |
| 138 | |
| 139 | /// \brief Close a file which has been used to print a coverage view. |
| 140 | virtual void closeViewFile(OwnedStream OS) = 0; |
| 141 | |
| 142 | /// \brief Create an index which lists reports for the given source files. |
| 143 | virtual Error createIndexFile(ArrayRef<StringRef> SourceFiles) = 0; |
| 144 | |
| 145 | /// @} |
| 146 | }; |
| 147 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 148 | /// \brief A code coverage view of a source file or function. |
| 149 | /// |
| 150 | /// A source coverage view and its nested sub-views form a file-oriented |
| 151 | /// 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] | 152 | /// renderer which implements the Rendering Interface. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 153 | class SourceCoverageView { |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 154 | /// A function or file name. |
| 155 | StringRef SourceName; |
| 156 | |
| 157 | /// A memory buffer backing the source on display. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 158 | const MemoryBuffer &File; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 159 | |
| 160 | /// Various options to guide the coverage renderer. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 161 | const CoverageViewOptions &Options; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 162 | |
| 163 | /// Complete coverage information about the source on display. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 164 | coverage::CoverageData CoverageInfo; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 165 | |
| 166 | /// 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] | 167 | std::vector<ExpansionView> ExpansionSubViews; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 168 | |
| 169 | /// A container for all instantiations (e.g template functions) in the source |
| 170 | /// on display. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 171 | std::vector<InstantiationView> InstantiationSubViews; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 172 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 173 | protected: |
| 174 | struct LineRef { |
| 175 | StringRef Line; |
| 176 | int64_t LineNo; |
| 177 | |
| 178 | LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {} |
| 179 | }; |
| 180 | |
| 181 | using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>; |
| 182 | |
| 183 | /// @name Rendering Interface |
| 184 | /// @{ |
| 185 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 186 | /// \brief Render a header for the view. |
| 187 | virtual void renderViewHeader(raw_ostream &OS) = 0; |
| 188 | |
| 189 | /// \brief Render a footer for the view. |
| 190 | virtual void renderViewFooter(raw_ostream &OS) = 0; |
| 191 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 192 | /// \brief Render the source name for the view. |
| 193 | virtual void renderSourceName(raw_ostream &OS) = 0; |
| 194 | |
| 195 | /// \brief Render the line prefix at the given \p ViewDepth. |
| 196 | virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0; |
| 197 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 198 | /// \brief Render the line suffix at the given \p ViewDepth. |
| 199 | virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0; |
| 200 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 201 | /// \brief Render a view divider at the given \p ViewDepth. |
| 202 | virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0; |
| 203 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 204 | /// \brief Render a source line with highlighting. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 205 | virtual void renderLine(raw_ostream &OS, LineRef L, |
| 206 | const coverage::CoverageSegment *WrappedSegment, |
| 207 | CoverageSegmentArray Segments, unsigned ExpansionCol, |
| 208 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 209 | |
| 210 | /// \brief Render the line's execution count column. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 211 | virtual void renderLineCoverageColumn(raw_ostream &OS, |
| 212 | const LineCoverageStats &Line) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 213 | |
| 214 | /// \brief Render the line number column. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 215 | virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 216 | |
| 217 | /// \brief Render all the region's execution counts on a line. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 218 | virtual void renderRegionMarkers(raw_ostream &OS, |
| 219 | CoverageSegmentArray Segments, |
| 220 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 221 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 222 | /// \brief Render the site of an expansion. |
| 223 | virtual void |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 224 | renderExpansionSite(raw_ostream &OS, LineRef L, |
Vedant Kumar | 8b12ecb | 2016-06-25 05:48:59 +0000 | [diff] [blame] | 225 | const coverage::CoverageSegment *WrappedSegment, |
| 226 | CoverageSegmentArray Segments, unsigned ExpansionCol, |
| 227 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 228 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 229 | /// \brief Render an expansion view and any nested views. |
| 230 | virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV, |
| 231 | unsigned ViewDepth) = 0; |
| 232 | |
| 233 | /// \brief Render an instantiation view and any nested views. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 234 | virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, |
| 235 | unsigned ViewDepth) = 0; |
| 236 | |
| 237 | /// @} |
| 238 | |
| 239 | /// \brief Format a count using engineering notation with 3 significant |
| 240 | /// digits. |
| 241 | static std::string formatCount(uint64_t N); |
| 242 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 243 | /// \brief Check if region marker output is expected for a line. |
| 244 | bool shouldRenderRegionMarkers(bool LineHasMultipleRegions) const; |
| 245 | |
| 246 | /// \brief Check if there are any sub-views attached to this view. |
| 247 | bool hasSubViews() const; |
| 248 | |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 249 | SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 250 | const CoverageViewOptions &Options, |
| 251 | coverage::CoverageData &&CoverageInfo) |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 252 | : SourceName(SourceName), File(File), Options(Options), |
| 253 | CoverageInfo(std::move(CoverageInfo)) {} |
| 254 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 255 | public: |
| 256 | static std::unique_ptr<SourceCoverageView> |
| 257 | create(StringRef SourceName, const MemoryBuffer &File, |
| 258 | const CoverageViewOptions &Options, |
| 259 | coverage::CoverageData &&CoverageInfo); |
| 260 | |
| 261 | virtual ~SourceCoverageView() {} |
| 262 | |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 263 | StringRef getSourceName() const { return SourceName; } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 264 | |
| 265 | const CoverageViewOptions &getOptions() const { return Options; } |
| 266 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 267 | /// \brief Add an expansion subview to this view. |
| 268 | void addExpansion(const coverage::CounterMappingRegion &Region, |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 269 | std::unique_ptr<SourceCoverageView> View); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 270 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 271 | /// \brief Add a function instantiation subview to this view. |
| 272 | void addInstantiation(StringRef FunctionName, unsigned Line, |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 273 | std::unique_ptr<SourceCoverageView> View); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 274 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 275 | /// \brief Print the code coverage information for a specific portion of a |
| 276 | /// source file to the output stream. |
| 277 | void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, |
| 278 | unsigned ViewDepth = 0); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | } // namespace llvm |
| 282 | |
| 283 | #endif // LLVM_COV_SOURCECOVERAGEVIEW_H |