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" |
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 | |
| 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)) {} |
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 | |
| 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)) {} |
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 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 100 | /// \brief A code coverage view of a specific source file. |
| 101 | /// It can have embedded coverage views. |
| 102 | class SourceCoverageView { |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 103 | private: |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 104 | /// A function or file name. |
| 105 | StringRef SourceName; |
| 106 | |
| 107 | /// A memory buffer backing the source on display. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 108 | const MemoryBuffer &File; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 109 | |
| 110 | /// Various options to guide the coverage renderer. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 111 | const CoverageViewOptions &Options; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 112 | |
| 113 | /// Complete coverage information about the source on display. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 114 | coverage::CoverageData CoverageInfo; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 115 | |
| 116 | /// 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] | 117 | std::vector<ExpansionView> ExpansionSubViews; |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 118 | |
| 119 | /// A container for all instantiations (e.g template functions) in the source |
| 120 | /// on display. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 121 | std::vector<InstantiationView> InstantiationSubViews; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 122 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 123 | /// \brief Render a source line with highlighting. |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 124 | void renderLine(raw_ostream &OS, StringRef Line, int64_t LineNumber, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 125 | const coverage::CoverageSegment *WrappedSegment, |
| 126 | ArrayRef<const coverage::CoverageSegment *> Segments, |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 127 | unsigned ExpansionCol); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 128 | |
Justin Bogner | 76e251c | 2014-09-16 06:21:57 +0000 | [diff] [blame] | 129 | void renderIndent(raw_ostream &OS, unsigned Level); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 130 | |
| 131 | void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS); |
| 132 | |
| 133 | /// \brief Render the line's execution count column. |
Vedant Kumar | 60dcb48 | 2016-06-24 00:34:48 +0000 | [diff] [blame] | 134 | void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageStats &Line); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 135 | |
| 136 | /// \brief Render the line number column. |
| 137 | void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo); |
| 138 | |
| 139 | /// \brief Render all the region's execution counts on a line. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 140 | void |
| 141 | renderRegionMarkers(raw_ostream &OS, |
| 142 | ArrayRef<const coverage::CoverageSegment *> Segments); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 143 | |
| 144 | static const unsigned LineCoverageColumnWidth = 7; |
| 145 | static const unsigned LineNumberColumnWidth = 5; |
| 146 | |
| 147 | public: |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 148 | SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 149 | const CoverageViewOptions &Options, |
| 150 | coverage::CoverageData &&CoverageInfo) |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 151 | : SourceName(SourceName), File(File), Options(Options), |
| 152 | CoverageInfo(std::move(CoverageInfo)) {} |
| 153 | |
| 154 | StringRef getSourceName() const { return SourceName; } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 155 | |
| 156 | const CoverageViewOptions &getOptions() const { return Options; } |
| 157 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 158 | /// \brief Add an expansion subview to this view. |
| 159 | void addExpansion(const coverage::CounterMappingRegion &Region, |
| 160 | std::unique_ptr<SourceCoverageView> View) { |
| 161 | ExpansionSubViews.emplace_back(Region, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 164 | /// \brief Add a function instantiation subview to this view. |
| 165 | void addInstantiation(StringRef FunctionName, unsigned Line, |
| 166 | std::unique_ptr<SourceCoverageView> View) { |
| 167 | InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /// \brief Print the code coverage information for a specific |
| 171 | /// portion of a source file to the output stream. |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 172 | void render(raw_ostream &OS, bool WholeFile, unsigned IndentLevel = 0); |
Vedant Kumar | 9d70d0b | 2016-06-24 00:34:51 +0000 | [diff] [blame^] | 173 | |
| 174 | /// \brief Print the source name corresponding to this view. |
| 175 | void renderSourceName(raw_ostream &OS); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // namespace llvm |
| 179 | |
| 180 | #endif // LLVM_COV_SOURCECOVERAGEVIEW_H |