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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // 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 "SourceCoverageDataManager.h" |
| 19 | #include "llvm/ProfileData/CoverageMapping.h" |
| 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 | |
| 27 | /// \brief A view that represents a macro or include expansion |
| 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 | |
| 52 | /// \brief A view that represents a function instantiation |
| 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 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 77 | /// \brief A code coverage view of a specific source file. |
| 78 | /// It can have embedded coverage views. |
| 79 | class SourceCoverageView { |
| 80 | public: |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 81 | /// \brief Coverage information for a single line. |
| 82 | struct LineCoverageInfo { |
| 83 | uint64_t ExecutionCount; |
| 84 | unsigned RegionCount; |
| 85 | bool Mapped; |
| 86 | |
| 87 | LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {} |
| 88 | |
| 89 | bool isMapped() const { return Mapped; } |
| 90 | |
| 91 | bool hasMultipleRegions() const { return RegionCount > 1; } |
| 92 | |
| 93 | void addRegionStartCount(uint64_t Count) { |
| 94 | Mapped = true; |
| 95 | ExecutionCount = Count; |
| 96 | ++RegionCount; |
| 97 | } |
| 98 | |
| 99 | void addRegionCount(uint64_t Count) { |
| 100 | Mapped = true; |
| 101 | ExecutionCount = Count; |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | /// \brief A marker that points at the start |
| 106 | /// of a specific mapping region. |
| 107 | struct RegionMarker { |
| 108 | unsigned Line, Column; |
| 109 | uint64_t ExecutionCount; |
| 110 | |
| 111 | RegionMarker(unsigned Line, unsigned Column, uint64_t Value) |
| 112 | : Line(Line), Column(Column), ExecutionCount(Value) {} |
| 113 | }; |
| 114 | |
| 115 | /// \brief A single line source range used to |
| 116 | /// render highlighted text. |
| 117 | struct HighlightRange { |
| 118 | enum HighlightKind { |
| 119 | /// The code that wasn't executed. |
| 120 | NotCovered, |
| 121 | |
| 122 | /// The region of code that was expanded. |
| 123 | Expanded |
| 124 | }; |
| 125 | HighlightKind Kind; |
| 126 | unsigned Line; |
| 127 | unsigned ColumnStart; |
| 128 | unsigned ColumnEnd; |
| 129 | |
| 130 | HighlightRange(unsigned Line, unsigned ColumnStart, unsigned ColumnEnd, |
| 131 | HighlightKind Kind = NotCovered) |
| 132 | : Kind(Kind), Line(Line), ColumnStart(ColumnStart), |
| 133 | ColumnEnd(ColumnEnd) {} |
| 134 | |
| 135 | bool operator<(const HighlightRange &Other) const { |
| 136 | if (Line == Other.Line) |
| 137 | return ColumnStart < Other.ColumnStart; |
| 138 | return Line < Other.Line; |
| 139 | } |
| 140 | |
| 141 | bool columnStartOverlaps(const HighlightRange &Other) const { |
| 142 | return ColumnStart <= Other.ColumnStart && ColumnEnd > Other.ColumnStart; |
| 143 | } |
| 144 | bool columnEndOverlaps(const HighlightRange &Other) const { |
| 145 | return ColumnEnd >= Other.ColumnEnd && ColumnStart < Other.ColumnEnd; |
| 146 | } |
| 147 | bool contains(const HighlightRange &Other) const { |
| 148 | if (Line != Other.Line) |
| 149 | return false; |
| 150 | return ColumnStart <= Other.ColumnStart && ColumnEnd >= Other.ColumnEnd; |
| 151 | } |
| 152 | |
| 153 | bool overlaps(const HighlightRange &Other) const { |
| 154 | if (Line != Other.Line) |
| 155 | return false; |
| 156 | return columnStartOverlaps(Other) || columnEndOverlaps(Other); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | private: |
| 161 | const MemoryBuffer &File; |
| 162 | const CoverageViewOptions &Options; |
Justin Bogner | 7dad93b | 2014-09-15 03:41:04 +0000 | [diff] [blame] | 163 | unsigned LineOffset; |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 164 | std::vector<ExpansionView> ExpansionSubViews; |
| 165 | std::vector<InstantiationView> InstantiationSubViews; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 166 | std::vector<LineCoverageInfo> LineStats; |
| 167 | std::vector<HighlightRange> HighlightRanges; |
| 168 | std::vector<RegionMarker> Markers; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 169 | |
Justin Bogner | 0b3614f | 2014-09-15 22:12:28 +0000 | [diff] [blame] | 170 | /// \brief Initialize the visible source range for this view. |
| 171 | void setUpVisibleRange(SourceCoverageDataManager &Data); |
| 172 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 173 | /// \brief Create the line coverage information using the coverage data. |
| 174 | void createLineCoverageInfo(SourceCoverageDataManager &Data); |
| 175 | |
| 176 | /// \brief Create the line highlighting ranges using the coverage data. |
| 177 | void createHighlightRanges(SourceCoverageDataManager &Data); |
| 178 | |
| 179 | /// \brief Create the region markers using the coverage data. |
| 180 | void createRegionMarkers(SourceCoverageDataManager &Data); |
| 181 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 182 | /// \brief Render a source line with highlighting. |
| 183 | void renderLine(raw_ostream &OS, StringRef Line, |
| 184 | ArrayRef<HighlightRange> Ranges); |
| 185 | |
Justin Bogner | 76e251c | 2014-09-16 06:21:57 +0000 | [diff] [blame] | 186 | void renderIndent(raw_ostream &OS, unsigned Level); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 187 | |
| 188 | void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS); |
| 189 | |
| 190 | /// \brief Render the line's execution count column. |
| 191 | void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line); |
| 192 | |
| 193 | /// \brief Render the line number column. |
| 194 | void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo); |
| 195 | |
| 196 | /// \brief Render all the region's execution counts on a line. |
| 197 | void renderRegionMarkers(raw_ostream &OS, ArrayRef<RegionMarker> Regions); |
| 198 | |
| 199 | static const unsigned LineCoverageColumnWidth = 7; |
| 200 | static const unsigned LineNumberColumnWidth = 5; |
| 201 | |
| 202 | public: |
| 203 | SourceCoverageView(const MemoryBuffer &File, |
| 204 | const CoverageViewOptions &Options) |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 205 | : File(File), Options(Options), LineOffset(0) {} |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 206 | |
| 207 | const CoverageViewOptions &getOptions() const { return Options; } |
| 208 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 209 | /// \brief Add an expansion subview to this view. |
| 210 | void addExpansion(const coverage::CounterMappingRegion &Region, |
| 211 | std::unique_ptr<SourceCoverageView> View) { |
| 212 | ExpansionSubViews.emplace_back(Region, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 215 | /// \brief Add a function instantiation subview to this view. |
| 216 | void addInstantiation(StringRef FunctionName, unsigned Line, |
| 217 | std::unique_ptr<SourceCoverageView> View) { |
| 218 | InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /// \brief Print the code coverage information for a specific |
| 222 | /// portion of a source file to the output stream. |
Justin Bogner | 76e251c | 2014-09-16 06:21:57 +0000 | [diff] [blame] | 223 | void render(raw_ostream &OS, unsigned IndentLevel = 0); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 224 | |
| 225 | /// \brief Load the coverage information required for rendering |
| 226 | /// from the mapping regions in the data manager. |
| 227 | void load(SourceCoverageDataManager &Data); |
| 228 | }; |
| 229 | |
| 230 | } // namespace llvm |
| 231 | |
| 232 | #endif // LLVM_COV_SOURCECOVERAGEVIEW_H |