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