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 61 friend bool operator<(const InstantiationView &LHS, 62 const InstantiationView &RHS) { 63 return LHS.Line < RHS.Line; 64 } 65 }; 66 67 /// \brief Coverage statistics for a single line. 68 struct LineCoverageStats { 69 uint64_t ExecutionCount; 70 bool HasMultipleRegions; 71 bool Mapped; 72 73 LineCoverageStats(ArrayRef<const coverage::CoverageSegment *> LineSegments, 74 const coverage::CoverageSegment *WrappedSegment); 75 76 bool isMapped() const { return Mapped; } 77 78 bool hasMultipleRegions() const { return HasMultipleRegions; } 79 }; 80 81 /// \brief A file manager that handles format-aware file creation. 82 class CoveragePrinter { 83 public: 84 struct StreamDestructor { 85 void operator()(raw_ostream *OS) const; 86 }; 87 88 using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>; 89 90 protected: 91 const CoverageViewOptions &Opts; 92 93 CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {} 94 95 /// \brief Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is 96 /// false, skip the ToplevelDir component. If \p Relative is false, skip the 97 /// OutputDir component. 98 std::string getOutputPath(StringRef Path, StringRef Extension, 99 bool InToplevel, bool Relative = true) const; 100 101 /// \brief If directory output is enabled, create a file in that directory 102 /// at the path given by getOutputPath(). Otherwise, return stdout. 103 Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension, 104 bool InToplevel) const; 105 106 /// \brief Return the sub-directory name for file coverage reports. 107 static StringRef getCoverageDir() { return "coverage"; } 108 109 public: 110 static std::unique_ptr<CoveragePrinter> 111 create(const CoverageViewOptions &Opts); 112 113 virtual ~CoveragePrinter() {} 114 115 /// @name File Creation Interface 116 /// @{ 117 118 /// \brief Create a file to print a coverage view into. 119 virtual Expected<OwnedStream> createViewFile(StringRef Path, 120 bool InToplevel) = 0; 121 122 /// \brief Close a file which has been used to print a coverage view. 123 virtual void closeViewFile(OwnedStream OS) = 0; 124 125 /// \brief Create an index which lists reports for the given source files. 126 virtual Error createIndexFile(ArrayRef<std::string> SourceFiles, 127 const coverage::CoverageMapping &Coverage) = 0; 128 129 /// @} 130 }; 131 132 /// \brief A code coverage view of a source file or function. 133 /// 134 /// A source coverage view and its nested sub-views form a file-oriented 135 /// representation of code coverage data. This view can be printed out by a 136 /// renderer which implements the Rendering Interface. 137 class SourceCoverageView { 138 /// A function or file name. 139 StringRef SourceName; 140 141 /// A memory buffer backing the source on display. 142 const MemoryBuffer &File; 143 144 /// Various options to guide the coverage renderer. 145 const CoverageViewOptions &Options; 146 147 /// Complete coverage information about the source on display. 148 coverage::CoverageData CoverageInfo; 149 150 /// A container for all expansions (e.g macros) in the source on display. 151 std::vector<ExpansionView> ExpansionSubViews; 152 153 /// A container for all instantiations (e.g template functions) in the source 154 /// on display. 155 std::vector<InstantiationView> InstantiationSubViews; 156 157 /// Get the first uncovered line number for the source file. 158 unsigned getFirstUncoveredLineNo(); 159 160 protected: 161 struct LineRef { 162 StringRef Line; 163 int64_t LineNo; 164 165 LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {} 166 }; 167 168 using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>; 169 170 /// @name Rendering Interface 171 /// @{ 172 173 /// \brief Render a header for the view. 174 virtual void renderViewHeader(raw_ostream &OS) = 0; 175 176 /// \brief Render a footer for the view. 177 virtual void renderViewFooter(raw_ostream &OS) = 0; 178 179 /// \brief Render the source name for the view. 180 virtual void renderSourceName(raw_ostream &OS, bool WholeFile) = 0; 181 182 /// \brief Render the line prefix at the given \p ViewDepth. 183 virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0; 184 185 /// \brief Render the line suffix at the given \p ViewDepth. 186 virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0; 187 188 /// \brief Render a view divider at the given \p ViewDepth. 189 virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0; 190 191 /// \brief Render a source line with highlighting. 192 virtual void renderLine(raw_ostream &OS, LineRef L, 193 const coverage::CoverageSegment *WrappedSegment, 194 CoverageSegmentArray Segments, unsigned ExpansionCol, 195 unsigned ViewDepth) = 0; 196 197 /// \brief Render the line's execution count column. 198 virtual void renderLineCoverageColumn(raw_ostream &OS, 199 const LineCoverageStats &Line) = 0; 200 201 /// \brief Render the line number column. 202 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; 203 204 /// \brief Render all the region's execution counts on a line. 205 virtual void renderRegionMarkers(raw_ostream &OS, 206 CoverageSegmentArray Segments, 207 unsigned ViewDepth) = 0; 208 209 /// \brief Render the site of an expansion. 210 virtual void 211 renderExpansionSite(raw_ostream &OS, LineRef L, 212 const coverage::CoverageSegment *WrappedSegment, 213 CoverageSegmentArray Segments, unsigned ExpansionCol, 214 unsigned ViewDepth) = 0; 215 216 /// \brief Render an expansion view and any nested views. 217 virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV, 218 unsigned ViewDepth) = 0; 219 220 /// \brief Render an instantiation view and any nested views. 221 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, 222 unsigned ViewDepth) = 0; 223 224 /// \brief Render \p Title, a project title if one is available, and the 225 /// created time. 226 virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0; 227 228 /// \brief Render the table header for a given source file. 229 virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo, 230 unsigned IndentLevel) = 0; 231 232 /// @} 233 234 /// \brief Format a count using engineering notation with 3 significant 235 /// digits. 236 static std::string formatCount(uint64_t N); 237 238 /// \brief Check if region marker output is expected for a line. 239 bool shouldRenderRegionMarkers(bool LineHasMultipleRegions) const; 240 241 /// \brief Check if there are any sub-views attached to this view. 242 bool hasSubViews() const; 243 244 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, 245 const CoverageViewOptions &Options, 246 coverage::CoverageData &&CoverageInfo) 247 : SourceName(SourceName), File(File), Options(Options), 248 CoverageInfo(std::move(CoverageInfo)) {} 249 250 public: 251 static std::unique_ptr<SourceCoverageView> 252 create(StringRef SourceName, const MemoryBuffer &File, 253 const CoverageViewOptions &Options, 254 coverage::CoverageData &&CoverageInfo); 255 256 virtual ~SourceCoverageView() {} 257 258 /// \brief Return the source name formatted for the host OS. 259 std::string getSourceName() const; 260 261 const CoverageViewOptions &getOptions() const { return Options; } 262 263 /// \brief Add an expansion subview to this view. 264 void addExpansion(const coverage::CounterMappingRegion &Region, 265 std::unique_ptr<SourceCoverageView> View); 266 267 /// \brief Add a function instantiation subview to this view. 268 void addInstantiation(StringRef FunctionName, unsigned Line, 269 std::unique_ptr<SourceCoverageView> View); 270 271 /// \brief Print the code coverage information for a specific portion of a 272 /// source file to the output stream. 273 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, 274 unsigned ViewDepth = 0); 275 }; 276 277 } // namespace llvm 278 279 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H 280