Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Vedant Kumar | a6154f9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file This file implements the text-based coverage renderer. |
| 11 | /// |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Vedant Kumar | 84dc751 | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 14 | #include "CoverageReport.h" |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 15 | #include "SourceCoverageViewText.h" |
Vedant Kumar | e435661 | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
Vedant Kumar | 028d73c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 22 | Expected<CoveragePrinter::OwnedStream> |
| 23 | CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) { |
| 24 | return createOutputStream(Path, "txt", InToplevel); |
| 25 | } |
| 26 | |
| 27 | void CoveragePrinterText::closeViewFile(OwnedStream OS) { |
| 28 | OS->operator<<('\n'); |
| 29 | } |
| 30 | |
Vedant Kumar | 84dc751 | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 31 | Error CoveragePrinterText::createIndexFile( |
Vedant Kumar | 34ea0de | 2017-10-18 23:58:27 +0000 | [diff] [blame] | 32 | ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage, |
Sean Eveson | b863c40 | 2017-10-03 11:05:28 +0000 | [diff] [blame] | 33 | const CoverageFiltersMatchAll &Filters) { |
Vedant Kumar | 028d73c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 34 | auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true); |
| 35 | if (Error E = OSOrErr.takeError()) |
| 36 | return E; |
| 37 | auto OS = std::move(OSOrErr.get()); |
| 38 | raw_ostream &OSRef = *OS.get(); |
| 39 | |
Vedant Kumar | 84dc751 | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 40 | CoverageReport Report(Opts, Coverage); |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 41 | Report.renderFileReports(OSRef, SourceFiles, Filters); |
Vedant Kumar | 028d73c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 42 | |
Ying Yi | 503f462 | 2016-09-13 11:28:31 +0000 | [diff] [blame] | 43 | Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n" |
| 44 | << Opts.getLLVMVersionString(); |
| 45 | |
Vedant Kumar | 028d73c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 46 | return Error::success(); |
| 47 | } |
| 48 | |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
Vedant Kumar | 49b4592 | 2016-06-25 03:27:29 +0000 | [diff] [blame] | 51 | static const unsigned LineCoverageColumnWidth = 7; |
| 52 | static const unsigned LineNumberColumnWidth = 5; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 53 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 54 | /// Get the width of the leading columns. |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 55 | unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) { |
| 56 | return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) + |
| 57 | (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0); |
| 58 | } |
| 59 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 60 | /// The width of the line that is used to divide between the view and |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 61 | /// the subviews. |
| 62 | unsigned getDividerWidth(const CoverageViewOptions &Opts) { |
| 63 | return getCombinedColumnWidth(Opts) + 4; |
| 64 | } |
| 65 | |
| 66 | } // anonymous namespace |
| 67 | |
Vedant Kumar | 0e9a272 | 2016-07-06 22:02:55 +0000 | [diff] [blame] | 68 | void SourceCoverageViewText::renderViewHeader(raw_ostream &) {} |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 69 | |
Vedant Kumar | e7f7e18 | 2016-09-13 23:00:13 +0000 | [diff] [blame] | 70 | void SourceCoverageViewText::renderViewFooter(raw_ostream &) {} |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 71 | |
Vedant Kumar | a8edd76 | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 72 | void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) { |
Vedant Kumar | 5b5c5e7 | 2016-10-25 00:08:33 +0000 | [diff] [blame] | 73 | getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName() |
| 74 | << ":\n"; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS, |
| 78 | unsigned ViewDepth) { |
| 79 | for (unsigned I = 0; I < ViewDepth; ++I) |
| 80 | OS << " |"; |
| 81 | } |
| 82 | |
Vedant Kumar | 0e9a272 | 2016-07-06 22:02:55 +0000 | [diff] [blame] | 83 | void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {} |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 84 | |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 85 | void SourceCoverageViewText::renderViewDivider(raw_ostream &OS, |
| 86 | unsigned ViewDepth) { |
| 87 | assert(ViewDepth != 0 && "Cannot render divider at top level"); |
| 88 | renderLinePrefix(OS, ViewDepth - 1); |
| 89 | OS.indent(2); |
| 90 | unsigned Length = getDividerWidth(getOptions()); |
| 91 | for (unsigned I = 0; I < Length; ++I) |
| 92 | OS << '-'; |
| 93 | OS << '\n'; |
| 94 | } |
| 95 | |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 96 | void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L, |
| 97 | const LineCoverageStats &LCS, |
| 98 | unsigned ExpansionCol, |
| 99 | unsigned ViewDepth) { |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 100 | StringRef Line = L.Line; |
| 101 | unsigned LineNumber = L.LineNo; |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 102 | auto *WrappedSegment = LCS.getWrappedSegment(); |
| 103 | CoverageSegmentArray Segments = LCS.getLineSegments(); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 104 | |
| 105 | Optional<raw_ostream::Colors> Highlight; |
| 106 | SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; |
| 107 | |
| 108 | // The first segment overlaps from a previous line, so we treat it specially. |
Vedant Kumar | f361756 | 2017-11-09 02:33:43 +0000 | [diff] [blame] | 109 | if (WrappedSegment && !WrappedSegment->IsGapRegion && |
| 110 | WrappedSegment->HasCount && WrappedSegment->Count == 0) |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 111 | Highlight = raw_ostream::RED; |
| 112 | |
| 113 | // Output each segment of the line, possibly highlighted. |
| 114 | unsigned Col = 1; |
| 115 | for (const auto *S : Segments) { |
| 116 | unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1); |
| 117 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 118 | getOptions().Colors && Highlight, /*Bold=*/false, |
| 119 | /*BG=*/true) |
| 120 | << Line.substr(Col - 1, End - Col); |
| 121 | if (getOptions().Debug && Highlight) |
| 122 | HighlightedRanges.push_back(std::make_pair(Col, End)); |
| 123 | Col = End; |
Vedant Kumar | f361756 | 2017-11-09 02:33:43 +0000 | [diff] [blame] | 124 | if ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) && |
| 125 | S->HasCount && S->Count == 0) |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 126 | Highlight = raw_ostream::RED; |
Vedant Kumar | f361756 | 2017-11-09 02:33:43 +0000 | [diff] [blame] | 127 | else if (Col == ExpansionCol) |
| 128 | Highlight = raw_ostream::CYAN; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 129 | else |
| 130 | Highlight = None; |
| 131 | } |
| 132 | |
| 133 | // Show the rest of the line. |
| 134 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 135 | getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true) |
| 136 | << Line.substr(Col - 1, Line.size() - Col + 1); |
| 137 | OS << '\n'; |
| 138 | |
| 139 | if (getOptions().Debug) { |
| 140 | for (const auto &Range : HighlightedRanges) |
| 141 | errs() << "Highlighted line " << LineNumber << ", " << Range.first |
| 142 | << " -> " << Range.second << '\n'; |
| 143 | if (Highlight) |
| 144 | errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n"; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void SourceCoverageViewText::renderLineCoverageColumn( |
| 149 | raw_ostream &OS, const LineCoverageStats &Line) { |
| 150 | if (!Line.isMapped()) { |
| 151 | OS.indent(LineCoverageColumnWidth) << '|'; |
| 152 | return; |
| 153 | } |
Vedant Kumar | 5371945 | 2017-10-14 02:27:29 +0000 | [diff] [blame] | 154 | std::string C = formatCount(Line.getExecutionCount()); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 155 | OS.indent(LineCoverageColumnWidth - C.size()); |
| 156 | colored_ostream(OS, raw_ostream::MAGENTA, |
| 157 | Line.hasMultipleRegions() && getOptions().Colors) |
| 158 | << C; |
| 159 | OS << '|'; |
| 160 | } |
| 161 | |
| 162 | void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS, |
| 163 | unsigned LineNo) { |
| 164 | SmallString<32> Buffer; |
| 165 | raw_svector_ostream BufferOS(Buffer); |
| 166 | BufferOS << LineNo; |
| 167 | auto Str = BufferOS.str(); |
| 168 | // Trim and align to the right. |
| 169 | Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth)); |
| 170 | OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; |
| 171 | } |
| 172 | |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 173 | void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS, |
| 174 | const LineCoverageStats &Line, |
| 175 | unsigned ViewDepth) { |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 176 | renderLinePrefix(OS, ViewDepth); |
| 177 | OS.indent(getCombinedColumnWidth(getOptions())); |
| 178 | |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 179 | CoverageSegmentArray Segments = Line.getLineSegments(); |
| 180 | |
Vedant Kumar | 2cefdfa | 2017-09-08 18:44:46 +0000 | [diff] [blame] | 181 | // Just consider the segments which start *and* end on this line. |
| 182 | if (Segments.size() > 1) |
| 183 | Segments = Segments.drop_back(); |
| 184 | |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 185 | unsigned PrevColumn = 1; |
| 186 | for (const auto *S : Segments) { |
| 187 | if (!S->IsRegionEntry) |
| 188 | continue; |
Vedant Kumar | 4a5c81f | 2017-10-18 18:52:29 +0000 | [diff] [blame] | 189 | if (S->Count == Line.getExecutionCount()) |
| 190 | continue; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 191 | // Skip to the new region. |
| 192 | if (S->Col > PrevColumn) |
| 193 | OS.indent(S->Col - PrevColumn); |
| 194 | PrevColumn = S->Col + 1; |
| 195 | std::string C = formatCount(S->Count); |
| 196 | PrevColumn += C.size(); |
| 197 | OS << '^' << C; |
Vedant Kumar | 2cefdfa | 2017-09-08 18:44:46 +0000 | [diff] [blame] | 198 | |
| 199 | if (getOptions().Debug) |
| 200 | errs() << "Marker at " << S->Line << ":" << S->Col << " = " |
| 201 | << formatCount(S->Count) << "\n"; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 202 | } |
| 203 | OS << '\n'; |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 206 | void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L, |
| 207 | const LineCoverageStats &LCS, |
| 208 | unsigned ExpansionCol, |
| 209 | unsigned ViewDepth) { |
Vedant Kumar | e435661 | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 210 | renderLinePrefix(OS, ViewDepth); |
| 211 | OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1)); |
Vedant Kumar | d35bb38 | 2017-10-18 18:52:28 +0000 | [diff] [blame] | 212 | renderLine(OS, L, LCS, ExpansionCol, ViewDepth); |
Vedant Kumar | e435661 | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 213 | } |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 214 | |
Vedant Kumar | e435661 | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 215 | void SourceCoverageViewText::renderExpansionView(raw_ostream &OS, |
| 216 | ExpansionView &ESV, |
| 217 | unsigned ViewDepth) { |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 218 | // Render the child subview. |
| 219 | if (getOptions().Debug) |
| 220 | errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol() |
| 221 | << " -> " << ESV.getEndCol() << '\n'; |
| 222 | ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 223 | /*ShowTitle=*/false, ViewDepth + 1); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS, |
| 227 | InstantiationView &ISV, |
| 228 | unsigned ViewDepth) { |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 229 | renderLinePrefix(OS, ViewDepth); |
| 230 | OS << ' '; |
Vedant Kumar | 61f5694 | 2016-09-15 06:44:51 +0000 | [diff] [blame] | 231 | if (!ISV.View) |
| 232 | getOptions().colored_ostream(OS, raw_ostream::RED) |
| 233 | << "Unexecuted instantiation: " << ISV.FunctionName << "\n"; |
| 234 | else |
| 235 | ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 236 | /*ShowTitle=*/false, ViewDepth); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 237 | } |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 238 | |
Vedant Kumar | 7551d48 | 2016-09-15 04:45:59 +0000 | [diff] [blame] | 239 | void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) { |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 240 | if (getOptions().hasProjectTitle()) |
| 241 | getOptions().colored_ostream(OS, raw_ostream::CYAN) |
| 242 | << getOptions().ProjectTitle << "\n"; |
| 243 | |
Vedant Kumar | 7551d48 | 2016-09-15 04:45:59 +0000 | [diff] [blame] | 244 | getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n"; |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 245 | |
| 246 | if (getOptions().hasCreatedTime()) |
| 247 | getOptions().colored_ostream(OS, raw_ostream::CYAN) |
| 248 | << getOptions().CreatedTimeStr << "\n"; |
| 249 | } |
| 250 | |
Vedant Kumar | a8edd76 | 2016-09-10 19:37:26 +0000 | [diff] [blame] | 251 | void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned, |
| 252 | unsigned) {} |