1 //===- SourceCoverageViewText.cpp - A text-based code coverage view -------===// 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 file implements the text-based coverage renderer. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "SourceCoverageViewText.h" 15 #include "llvm/ADT/Optional.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringExtras.h" 18 19 using namespace llvm; 20 21 Expected<CoveragePrinter::OwnedStream> 22 CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) { 23 return createOutputStream(Path, "txt", InToplevel); 24 } 25 26 void CoveragePrinterText::closeViewFile(OwnedStream OS) { 27 OS->operator<<('\n'); 28 } 29 30 Error CoveragePrinterText::createIndexFile(ArrayRef<StringRef> SourceFiles) { 31 auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true); 32 if (Error E = OSOrErr.takeError()) 33 return E; 34 auto OS = std::move(OSOrErr.get()); 35 raw_ostream &OSRef = *OS.get(); 36 37 for (StringRef SF : SourceFiles) 38 OSRef << getOutputPath(SF, "txt", /*InToplevel=*/false) << '\n'; 39 40 return Error::success(); 41 } 42 43 namespace { 44 45 static const unsigned LineCoverageColumnWidth = 7; 46 static const unsigned LineNumberColumnWidth = 5; 47 48 /// \brief Get the width of the leading columns. 49 unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) { 50 return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) + 51 (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0); 52 } 53 54 /// \brief The width of the line that is used to divide between the view and 55 /// the subviews. 56 unsigned getDividerWidth(const CoverageViewOptions &Opts) { 57 return getCombinedColumnWidth(Opts) + 4; 58 } 59 60 } // anonymous namespace 61 62 void SourceCoverageViewText::renderViewHeader(raw_ostream &) {} 63 64 void SourceCoverageViewText::renderViewFooter(raw_ostream &) {} 65 66 void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile, 67 unsigned FirstUncoveredLineNo) { 68 std::string ViewInfo = WholeFile ? getVerboseSourceName() : getSourceName(); 69 getOptions().colored_ostream(OS, raw_ostream::CYAN) << ViewInfo << ":\n"; 70 } 71 72 void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS, 73 unsigned ViewDepth) { 74 for (unsigned I = 0; I < ViewDepth; ++I) 75 OS << " |"; 76 } 77 78 void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {} 79 80 void SourceCoverageViewText::renderViewDivider(raw_ostream &OS, 81 unsigned ViewDepth) { 82 assert(ViewDepth != 0 && "Cannot render divider at top level"); 83 renderLinePrefix(OS, ViewDepth - 1); 84 OS.indent(2); 85 unsigned Length = getDividerWidth(getOptions()); 86 for (unsigned I = 0; I < Length; ++I) 87 OS << '-'; 88 OS << '\n'; 89 } 90 91 void SourceCoverageViewText::renderLine( 92 raw_ostream &OS, LineRef L, 93 const coverage::CoverageSegment *WrappedSegment, 94 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { 95 StringRef Line = L.Line; 96 unsigned LineNumber = L.LineNo; 97 98 Optional<raw_ostream::Colors> Highlight; 99 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; 100 101 // The first segment overlaps from a previous line, so we treat it specially. 102 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0) 103 Highlight = raw_ostream::RED; 104 105 // Output each segment of the line, possibly highlighted. 106 unsigned Col = 1; 107 for (const auto *S : Segments) { 108 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1); 109 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, 110 getOptions().Colors && Highlight, /*Bold=*/false, 111 /*BG=*/true) 112 << Line.substr(Col - 1, End - Col); 113 if (getOptions().Debug && Highlight) 114 HighlightedRanges.push_back(std::make_pair(Col, End)); 115 Col = End; 116 if (Col == ExpansionCol) 117 Highlight = raw_ostream::CYAN; 118 else if (S->HasCount && S->Count == 0) 119 Highlight = raw_ostream::RED; 120 else 121 Highlight = None; 122 } 123 124 // Show the rest of the line. 125 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, 126 getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true) 127 << Line.substr(Col - 1, Line.size() - Col + 1); 128 OS << '\n'; 129 130 if (getOptions().Debug) { 131 for (const auto &Range : HighlightedRanges) 132 errs() << "Highlighted line " << LineNumber << ", " << Range.first 133 << " -> " << Range.second << '\n'; 134 if (Highlight) 135 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n"; 136 } 137 } 138 139 void SourceCoverageViewText::renderLineCoverageColumn( 140 raw_ostream &OS, const LineCoverageStats &Line) { 141 if (!Line.isMapped()) { 142 OS.indent(LineCoverageColumnWidth) << '|'; 143 return; 144 } 145 std::string C = formatCount(Line.ExecutionCount); 146 OS.indent(LineCoverageColumnWidth - C.size()); 147 colored_ostream(OS, raw_ostream::MAGENTA, 148 Line.hasMultipleRegions() && getOptions().Colors) 149 << C; 150 OS << '|'; 151 } 152 153 void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS, 154 unsigned LineNo) { 155 SmallString<32> Buffer; 156 raw_svector_ostream BufferOS(Buffer); 157 BufferOS << LineNo; 158 auto Str = BufferOS.str(); 159 // Trim and align to the right. 160 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth)); 161 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; 162 } 163 164 void SourceCoverageViewText::renderRegionMarkers( 165 raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) { 166 renderLinePrefix(OS, ViewDepth); 167 OS.indent(getCombinedColumnWidth(getOptions())); 168 169 unsigned PrevColumn = 1; 170 for (const auto *S : Segments) { 171 if (!S->IsRegionEntry) 172 continue; 173 // Skip to the new region. 174 if (S->Col > PrevColumn) 175 OS.indent(S->Col - PrevColumn); 176 PrevColumn = S->Col + 1; 177 std::string C = formatCount(S->Count); 178 PrevColumn += C.size(); 179 OS << '^' << C; 180 } 181 OS << '\n'; 182 183 if (getOptions().Debug) 184 for (const auto *S : Segments) 185 errs() << "Marker at " << S->Line << ":" << S->Col << " = " 186 << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n"); 187 } 188 189 void SourceCoverageViewText::renderExpansionSite( 190 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment, 191 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { 192 renderLinePrefix(OS, ViewDepth); 193 OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1)); 194 renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth); 195 } 196 197 void SourceCoverageViewText::renderExpansionView(raw_ostream &OS, 198 ExpansionView &ESV, 199 unsigned ViewDepth) { 200 // Render the child subview. 201 if (getOptions().Debug) 202 errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol() 203 << " -> " << ESV.getEndCol() << '\n'; 204 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, 205 ViewDepth + 1); 206 } 207 208 void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS, 209 InstantiationView &ISV, 210 unsigned ViewDepth) { 211 renderLinePrefix(OS, ViewDepth); 212 OS << ' '; 213 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth); 214 } 215 216 void SourceCoverageViewText::renderCellInTitle(raw_ostream &OS, 217 StringRef CellText) { 218 if (getOptions().hasProjectTitle()) 219 getOptions().colored_ostream(OS, raw_ostream::CYAN) 220 << getOptions().ProjectTitle << "\n"; 221 222 getOptions().colored_ostream(OS, raw_ostream::CYAN) << CellText << "\n"; 223 224 if (getOptions().hasCreatedTime()) 225 getOptions().colored_ostream(OS, raw_ostream::CYAN) 226 << getOptions().CreatedTimeStr << "\n"; 227 } 228 229 void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned) {} 230