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" |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Optional.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 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 25 | class SourceCoverageView; |
| 26 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 27 | /// \brief A view that represents a macro or include expansion. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 28 | struct ExpansionView { |
| 29 | coverage::CounterMappingRegion Region; |
| 30 | std::unique_ptr<SourceCoverageView> View; |
| 31 | |
| 32 | ExpansionView(const coverage::CounterMappingRegion &Region, |
| 33 | std::unique_ptr<SourceCoverageView> View) |
| 34 | : Region(Region), View(std::move(View)) {} |
Justin Bogner | 99e9518 | 2014-09-17 06:32:48 +0000 | [diff] [blame] | 35 | ExpansionView(ExpansionView &&RHS) |
| 36 | : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {} |
| 37 | ExpansionView &operator=(ExpansionView &&RHS) { |
| 38 | Region = std::move(RHS.Region); |
| 39 | View = std::move(RHS.View); |
| 40 | return *this; |
| 41 | } |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 42 | |
| 43 | unsigned getLine() const { return Region.LineStart; } |
| 44 | unsigned getStartCol() const { return Region.ColumnStart; } |
| 45 | unsigned getEndCol() const { return Region.ColumnEnd; } |
| 46 | |
| 47 | friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) { |
| 48 | return LHS.Region.startLoc() < RHS.Region.startLoc(); |
| 49 | } |
| 50 | }; |
| 51 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 52 | /// \brief A view that represents a function instantiation. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 53 | struct InstantiationView { |
| 54 | StringRef FunctionName; |
| 55 | unsigned Line; |
| 56 | std::unique_ptr<SourceCoverageView> View; |
| 57 | |
| 58 | InstantiationView(StringRef FunctionName, unsigned Line, |
| 59 | std::unique_ptr<SourceCoverageView> View) |
| 60 | : FunctionName(FunctionName), Line(Line), View(std::move(View)) {} |
Justin Bogner | 99e9518 | 2014-09-17 06:32:48 +0000 | [diff] [blame] | 61 | InstantiationView(InstantiationView &&RHS) |
| 62 | : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)), |
| 63 | View(std::move(RHS.View)) {} |
| 64 | InstantiationView &operator=(InstantiationView &&RHS) { |
| 65 | FunctionName = std::move(RHS.FunctionName); |
| 66 | Line = std::move(RHS.Line); |
| 67 | View = std::move(RHS.View); |
| 68 | return *this; |
| 69 | } |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 70 | |
| 71 | friend bool operator<(const InstantiationView &LHS, |
| 72 | const InstantiationView &RHS) { |
| 73 | return LHS.Line < RHS.Line; |
| 74 | } |
| 75 | }; |
| 76 | |
Vedant Kumar | 60dcb48 | 2016-06-24 00:34:48 +0000 | [diff] [blame] | 77 | /// \brief Coverage statistics for a single line. |
| 78 | struct LineCoverageStats { |
| 79 | uint64_t ExecutionCount; |
| 80 | unsigned RegionCount; |
| 81 | bool Mapped; |
| 82 | |
| 83 | LineCoverageStats() : ExecutionCount(0), RegionCount(0), Mapped(false) {} |
| 84 | |
| 85 | bool isMapped() const { return Mapped; } |
| 86 | |
| 87 | bool hasMultipleRegions() const { return RegionCount > 1; } |
| 88 | |
| 89 | void addRegionStartCount(uint64_t Count) { |
| 90 | // The max of all region starts is the most interesting value. |
| 91 | addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count); |
| 92 | ++RegionCount; |
| 93 | } |
| 94 | |
| 95 | void addRegionCount(uint64_t Count) { |
| 96 | Mapped = true; |
| 97 | ExecutionCount = Count; |
| 98 | } |
| 99 | }; |
| 100 | |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 101 | /// \brief A code coverage view of a source file or function. |
| 102 | /// |
| 103 | /// A source coverage view and its nested sub-views form a file-oriented |
| 104 | /// representation of code coverage data. This view can be printed out by a |
| 105 | /// renderer which implements the Rendering Interface. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 106 | class SourceCoverageView { |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 107 | /// A function or file name. |
| 108 | StringRef SourceName; |
| 109 | |
| 110 | /// A memory buffer backing the source on display. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 111 | const MemoryBuffer &File; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 112 | |
| 113 | /// Various options to guide the coverage renderer. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 114 | const CoverageViewOptions &Options; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 115 | |
| 116 | /// Complete coverage information about the source on display. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 117 | coverage::CoverageData CoverageInfo; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 118 | |
| 119 | /// 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] | 120 | std::vector<ExpansionView> ExpansionSubViews; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 121 | |
| 122 | /// A container for all instantiations (e.g template functions) in the source |
| 123 | /// on display. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 124 | std::vector<InstantiationView> InstantiationSubViews; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 125 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 126 | protected: |
| 127 | struct LineRef { |
| 128 | StringRef Line; |
| 129 | int64_t LineNo; |
| 130 | |
| 131 | LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {} |
| 132 | }; |
| 133 | |
| 134 | using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>; |
| 135 | |
| 136 | /// @name Rendering Interface |
| 137 | /// @{ |
| 138 | |
| 139 | /// \brief Render the source name for the view. |
| 140 | virtual void renderSourceName(raw_ostream &OS) = 0; |
| 141 | |
| 142 | /// \brief Render the line prefix at the given \p ViewDepth. |
| 143 | virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0; |
| 144 | |
| 145 | /// \brief Render a view divider at the given \p ViewDepth. |
| 146 | virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0; |
| 147 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 148 | /// \brief Render a source line with highlighting. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 149 | virtual void renderLine(raw_ostream &OS, LineRef L, |
| 150 | const coverage::CoverageSegment *WrappedSegment, |
| 151 | CoverageSegmentArray Segments, unsigned ExpansionCol, |
| 152 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 153 | |
| 154 | /// \brief Render the line's execution count column. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 155 | virtual void renderLineCoverageColumn(raw_ostream &OS, |
| 156 | const LineCoverageStats &Line) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 157 | |
| 158 | /// \brief Render the line number column. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 159 | virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 160 | |
| 161 | /// \brief Render all the region's execution counts on a line. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 162 | virtual void renderRegionMarkers(raw_ostream &OS, |
| 163 | CoverageSegmentArray Segments, |
| 164 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 165 | |
Vedant Kumar | 8b12ecb | 2016-06-25 05:48:59 +0000 | [diff] [blame^] | 166 | /// \brief Render an expansion view. If the expansion site must be re-rendered |
| 167 | /// for clarity, it is passed in via \p FirstLine. |
| 168 | virtual unsigned |
| 169 | renderExpansionView(raw_ostream &OS, ExpansionView &ESV, |
| 170 | Optional<LineRef> FirstLine, |
| 171 | const coverage::CoverageSegment *WrappedSegment, |
| 172 | CoverageSegmentArray Segments, unsigned ExpansionCol, |
| 173 | unsigned ViewDepth) = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 174 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 175 | /// \brief Render an instantiation view. |
| 176 | virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, |
| 177 | unsigned ViewDepth) = 0; |
| 178 | |
| 179 | /// @} |
| 180 | |
| 181 | /// \brief Format a count using engineering notation with 3 significant |
| 182 | /// digits. |
| 183 | static std::string formatCount(uint64_t N); |
| 184 | |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 185 | SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 186 | const CoverageViewOptions &Options, |
| 187 | coverage::CoverageData &&CoverageInfo) |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 188 | : SourceName(SourceName), File(File), Options(Options), |
| 189 | CoverageInfo(std::move(CoverageInfo)) {} |
| 190 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 191 | public: |
| 192 | static std::unique_ptr<SourceCoverageView> |
| 193 | create(StringRef SourceName, const MemoryBuffer &File, |
| 194 | const CoverageViewOptions &Options, |
| 195 | coverage::CoverageData &&CoverageInfo); |
| 196 | |
| 197 | virtual ~SourceCoverageView() {} |
| 198 | |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame] | 199 | StringRef getSourceName() const { return SourceName; } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 200 | |
| 201 | const CoverageViewOptions &getOptions() const { return Options; } |
| 202 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 203 | /// \brief Add an expansion subview to this view. |
| 204 | void addExpansion(const coverage::CounterMappingRegion &Region, |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 205 | std::unique_ptr<SourceCoverageView> View); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 206 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 207 | /// \brief Add a function instantiation subview to this view. |
| 208 | void addInstantiation(StringRef FunctionName, unsigned Line, |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 209 | std::unique_ptr<SourceCoverageView> View); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 210 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 211 | /// \brief Print the code coverage information for a specific portion of a |
| 212 | /// source file to the output stream. |
| 213 | void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, |
| 214 | unsigned ViewDepth = 0); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // namespace llvm |
| 218 | |
| 219 | #endif // LLVM_COV_SOURCECOVERAGEVIEW_H |