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 /// \file 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 "llvm/ProfileData/Coverage/CoverageMapping.h" 19 #include "llvm/ADT/Optional.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include <vector> 22 23 namespace llvm { 24 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)) {} 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 } 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)) {} 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 } 70 71 friend bool operator<(const InstantiationView &LHS, 72 const InstantiationView &RHS) { 73 return LHS.Line < RHS.Line; 74 } 75 }; 76 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 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. 106 class SourceCoverageView { 107 /// A function or file name. 108 StringRef SourceName; 109 110 /// A memory buffer backing the source on display. 111 const MemoryBuffer &File; 112 113 /// Various options to guide the coverage renderer. 114 const CoverageViewOptions &Options; 115 116 /// Complete coverage information about the source on display. 117 coverage::CoverageData CoverageInfo; 118 119 /// A container for all expansions (e.g macros) in the source on display. 120 std::vector<ExpansionView> ExpansionSubViews; 121 122 /// A container for all instantiations (e.g template functions) in the source 123 /// on display. 124 std::vector<InstantiationView> InstantiationSubViews; 125 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 148 /// \brief Render a source line with highlighting. 149 virtual void renderLine(raw_ostream &OS, LineRef L, 150 const coverage::CoverageSegment *WrappedSegment, 151 CoverageSegmentArray Segments, unsigned ExpansionCol, 152 unsigned ViewDepth) = 0; 153 154 /// \brief Render the line's execution count column. 155 virtual void renderLineCoverageColumn(raw_ostream &OS, 156 const LineCoverageStats &Line) = 0; 157 158 /// \brief Render the line number column. 159 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; 160 161 /// \brief Render all the region's execution counts on a line. 162 virtual void renderRegionMarkers(raw_ostream &OS, 163 CoverageSegmentArray Segments, 164 unsigned ViewDepth) = 0; 165 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; 174 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 185 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, 186 const CoverageViewOptions &Options, 187 coverage::CoverageData &&CoverageInfo) 188 : SourceName(SourceName), File(File), Options(Options), 189 CoverageInfo(std::move(CoverageInfo)) {} 190 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 199 StringRef getSourceName() const { return SourceName; } 200 201 const CoverageViewOptions &getOptions() const { return Options; } 202 203 /// \brief Add an expansion subview to this view. 204 void addExpansion(const coverage::CounterMappingRegion &Region, 205 std::unique_ptr<SourceCoverageView> View); 206 207 /// \brief Add a function instantiation subview to this view. 208 void addInstantiation(StringRef FunctionName, unsigned Line, 209 std::unique_ptr<SourceCoverageView> View); 210 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); 215 }; 216 217 } // namespace llvm 218 219 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H 220