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/Support/MemoryBuffer.h" 20 #include <vector> 21 22 namespace llvm { 23 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)) {} 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 } 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)) {} 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 } 69 70 friend bool operator<(const InstantiationView &LHS, 71 const InstantiationView &RHS) { 72 return LHS.Line < RHS.Line; 73 } 74 }; 75 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 100 /// \brief A code coverage view of a source file or function. 101 /// 102 /// A source coverage view and its nested sub-views form a file-oriented 103 /// representation of code coverage data. This view can be printed out by a 104 /// renderer which implements the Rendering Interface. 105 class SourceCoverageView { 106 /// A function or file name. 107 StringRef SourceName; 108 109 /// A memory buffer backing the source on display. 110 const MemoryBuffer &File; 111 112 /// Various options to guide the coverage renderer. 113 const CoverageViewOptions &Options; 114 115 /// Complete coverage information about the source on display. 116 coverage::CoverageData CoverageInfo; 117 118 /// A container for all expansions (e.g macros) in the source on display. 119 std::vector<ExpansionView> ExpansionSubViews; 120 121 /// A container for all instantiations (e.g template functions) in the source 122 /// on display. 123 std::vector<InstantiationView> InstantiationSubViews; 124 125 protected: 126 struct LineRef { 127 StringRef Line; 128 int64_t LineNo; 129 130 LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {} 131 }; 132 133 using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>; 134 135 /// @name Rendering Interface 136 /// @{ 137 138 /// \brief Render the source name for the view. 139 virtual void renderSourceName(raw_ostream &OS) = 0; 140 141 /// \brief Render the line prefix at the given \p ViewDepth. 142 virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0; 143 144 /// \brief Render a view divider at the given \p ViewDepth. 145 virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0; 146 147 /// \brief Render a source line with highlighting. 148 virtual void renderLine(raw_ostream &OS, LineRef L, 149 const coverage::CoverageSegment *WrappedSegment, 150 CoverageSegmentArray Segments, unsigned ExpansionCol, 151 unsigned ViewDepth) = 0; 152 153 /// \brief Render the line's execution count column. 154 virtual void renderLineCoverageColumn(raw_ostream &OS, 155 const LineCoverageStats &Line) = 0; 156 157 /// \brief Render the line number column. 158 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; 159 160 /// \brief Render all the region's execution counts on a line. 161 virtual void renderRegionMarkers(raw_ostream &OS, 162 CoverageSegmentArray Segments, 163 unsigned ViewDepth) = 0; 164 165 /// \brief Render the site of an expansion. 166 virtual void 167 renderExpansionSite(raw_ostream &OS, ExpansionView &ESV, LineRef L, 168 const coverage::CoverageSegment *WrappedSegment, 169 CoverageSegmentArray Segments, unsigned ExpansionCol, 170 unsigned ViewDepth) = 0; 171 172 /// \brief Render an expansion view and any nested views. 173 virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV, 174 unsigned ViewDepth) = 0; 175 176 /// \brief Render an instantiation view and any nested views. 177 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, 178 unsigned ViewDepth) = 0; 179 180 /// @} 181 182 /// \brief Format a count using engineering notation with 3 significant 183 /// digits. 184 static std::string formatCount(uint64_t N); 185 186 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, 187 const CoverageViewOptions &Options, 188 coverage::CoverageData &&CoverageInfo) 189 : SourceName(SourceName), File(File), Options(Options), 190 CoverageInfo(std::move(CoverageInfo)) {} 191 192 public: 193 static std::unique_ptr<SourceCoverageView> 194 create(StringRef SourceName, const MemoryBuffer &File, 195 const CoverageViewOptions &Options, 196 coverage::CoverageData &&CoverageInfo); 197 198 virtual ~SourceCoverageView() {} 199 200 StringRef getSourceName() const { return SourceName; } 201 202 const CoverageViewOptions &getOptions() const { return Options; } 203 204 /// \brief Add an expansion subview to this view. 205 void addExpansion(const coverage::CounterMappingRegion &Region, 206 std::unique_ptr<SourceCoverageView> View); 207 208 /// \brief Add a function instantiation subview to this view. 209 void addInstantiation(StringRef FunctionName, unsigned Line, 210 std::unique_ptr<SourceCoverageView> View); 211 212 /// \brief Print the code coverage information for a specific portion of a 213 /// source file to the output stream. 214 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, 215 unsigned ViewDepth = 0); 216 }; 217 218 } // namespace llvm 219 220 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H 221