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 coverage::CoverageSegment *WrappedSegment, 183 CoverageSegmentArray Segments, unsigned ExpansionCol, 184 unsigned ViewDepth) = 0; 185 186 /// \brief Render the line's execution count column. 187 virtual void renderLineCoverageColumn(raw_ostream &OS, 188 const LineCoverageStats &Line) = 0; 189 190 /// \brief Render the line number column. 191 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0; 192 193 /// \brief Render all the region's execution counts on a line. 194 virtual void renderRegionMarkers(raw_ostream &OS, 195 CoverageSegmentArray Segments, 196 unsigned ViewDepth) = 0; 197 198 /// \brief Render the site of an expansion. 199 virtual void 200 renderExpansionSite(raw_ostream &OS, LineRef L, 201 const coverage::CoverageSegment *WrappedSegment, 202 CoverageSegmentArray Segments, unsigned ExpansionCol, 203 unsigned ViewDepth) = 0; 204 205 /// \brief Render an expansion view and any nested views. 206 virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV, 207 unsigned ViewDepth) = 0; 208 209 /// \brief Render an instantiation view and any nested views. 210 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, 211 unsigned ViewDepth) = 0; 212 213 /// \brief Render \p Title, a project title if one is available, and the 214 /// created time. 215 virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0; 216 217 /// \brief Render the table header for a given source file. 218 virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo, 219 unsigned IndentLevel) = 0; 220 221 /// @} 222 223 /// \brief Format a count using engineering notation with 3 significant 224 /// digits. 225 static std::string formatCount(uint64_t N); 226 227 /// \brief Check if region marker output is expected for a line. 228 bool shouldRenderRegionMarkers(CoverageSegmentArray Segments) const; 229 230 /// \brief Check if there are any sub-views attached to this view. 231 bool hasSubViews() const; 232 233 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File, 234 const CoverageViewOptions &Options, 235 coverage::CoverageData &&CoverageInfo) 236 : SourceName(SourceName), File(File), Options(Options), 237 CoverageInfo(std::move(CoverageInfo)) {} 238 239 public: 240 static std::unique_ptr<SourceCoverageView> 241 create(StringRef SourceName, const MemoryBuffer &File, 242 const CoverageViewOptions &Options, 243 coverage::CoverageData &&CoverageInfo); 244 245 virtual ~SourceCoverageView() {} 246 247 /// \brief Return the source name formatted for the host OS. 248 std::string getSourceName() const; 249 250 const CoverageViewOptions &getOptions() const { return Options; } 251 252 /// \brief Add an expansion subview to this view. 253 void addExpansion(const coverage::CounterMappingRegion &Region, 254 std::unique_ptr<SourceCoverageView> View); 255 256 /// \brief Add a function instantiation subview to this view. 257 void addInstantiation(StringRef FunctionName, unsigned Line, 258 std::unique_ptr<SourceCoverageView> View); 259 260 /// \brief Print the code coverage information for a specific portion of a 261 /// source file to the output stream. 262 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, 263 bool ShowTitle, unsigned ViewDepth = 0); 264 }; 265 266 } // namespace llvm 267 268 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H 269