Vedant Kumar | f9151b9 | 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 | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file This file implements the text-based coverage renderer. |
| 11 | /// |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SourceCoverageViewText.h" |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Optional.h" |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 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 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | |
Vedant Kumar | 4f4e8d8 | 2016-06-25 03:27:29 +0000 | [diff] [blame] | 45 | static const unsigned LineCoverageColumnWidth = 7; |
| 46 | static const unsigned LineNumberColumnWidth = 5; |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 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::renderSourceName(raw_ostream &OS) { |
| 63 | getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName() |
| 64 | << ":\n"; |
| 65 | } |
| 66 | |
| 67 | void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS, |
| 68 | unsigned ViewDepth) { |
| 69 | for (unsigned I = 0; I < ViewDepth; ++I) |
| 70 | OS << " |"; |
| 71 | } |
| 72 | |
| 73 | void SourceCoverageViewText::renderViewDivider(raw_ostream &OS, |
| 74 | unsigned ViewDepth) { |
| 75 | assert(ViewDepth != 0 && "Cannot render divider at top level"); |
| 76 | renderLinePrefix(OS, ViewDepth - 1); |
| 77 | OS.indent(2); |
| 78 | unsigned Length = getDividerWidth(getOptions()); |
| 79 | for (unsigned I = 0; I < Length; ++I) |
| 80 | OS << '-'; |
| 81 | OS << '\n'; |
| 82 | } |
| 83 | |
| 84 | void SourceCoverageViewText::renderLine( |
| 85 | raw_ostream &OS, LineRef L, |
| 86 | const coverage::CoverageSegment *WrappedSegment, |
| 87 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { |
| 88 | StringRef Line = L.Line; |
| 89 | unsigned LineNumber = L.LineNo; |
| 90 | |
| 91 | Optional<raw_ostream::Colors> Highlight; |
| 92 | SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; |
| 93 | |
| 94 | // The first segment overlaps from a previous line, so we treat it specially. |
| 95 | if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0) |
| 96 | Highlight = raw_ostream::RED; |
| 97 | |
| 98 | // Output each segment of the line, possibly highlighted. |
| 99 | unsigned Col = 1; |
| 100 | for (const auto *S : Segments) { |
| 101 | unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1); |
| 102 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 103 | getOptions().Colors && Highlight, /*Bold=*/false, |
| 104 | /*BG=*/true) |
| 105 | << Line.substr(Col - 1, End - Col); |
| 106 | if (getOptions().Debug && Highlight) |
| 107 | HighlightedRanges.push_back(std::make_pair(Col, End)); |
| 108 | Col = End; |
| 109 | if (Col == ExpansionCol) |
| 110 | Highlight = raw_ostream::CYAN; |
| 111 | else if (S->HasCount && S->Count == 0) |
| 112 | Highlight = raw_ostream::RED; |
| 113 | else |
| 114 | Highlight = None; |
| 115 | } |
| 116 | |
| 117 | // Show the rest of the line. |
| 118 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 119 | getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true) |
| 120 | << Line.substr(Col - 1, Line.size() - Col + 1); |
| 121 | OS << '\n'; |
| 122 | |
| 123 | if (getOptions().Debug) { |
| 124 | for (const auto &Range : HighlightedRanges) |
| 125 | errs() << "Highlighted line " << LineNumber << ", " << Range.first |
| 126 | << " -> " << Range.second << '\n'; |
| 127 | if (Highlight) |
| 128 | errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n"; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void SourceCoverageViewText::renderLineCoverageColumn( |
| 133 | raw_ostream &OS, const LineCoverageStats &Line) { |
| 134 | if (!Line.isMapped()) { |
| 135 | OS.indent(LineCoverageColumnWidth) << '|'; |
| 136 | return; |
| 137 | } |
| 138 | std::string C = formatCount(Line.ExecutionCount); |
| 139 | OS.indent(LineCoverageColumnWidth - C.size()); |
| 140 | colored_ostream(OS, raw_ostream::MAGENTA, |
| 141 | Line.hasMultipleRegions() && getOptions().Colors) |
| 142 | << C; |
| 143 | OS << '|'; |
| 144 | } |
| 145 | |
| 146 | void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS, |
| 147 | unsigned LineNo) { |
| 148 | SmallString<32> Buffer; |
| 149 | raw_svector_ostream BufferOS(Buffer); |
| 150 | BufferOS << LineNo; |
| 151 | auto Str = BufferOS.str(); |
| 152 | // Trim and align to the right. |
| 153 | Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth)); |
| 154 | OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; |
| 155 | } |
| 156 | |
| 157 | void SourceCoverageViewText::renderRegionMarkers( |
| 158 | raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) { |
| 159 | renderLinePrefix(OS, ViewDepth); |
| 160 | OS.indent(getCombinedColumnWidth(getOptions())); |
| 161 | |
| 162 | unsigned PrevColumn = 1; |
| 163 | for (const auto *S : Segments) { |
| 164 | if (!S->IsRegionEntry) |
| 165 | continue; |
| 166 | // Skip to the new region. |
| 167 | if (S->Col > PrevColumn) |
| 168 | OS.indent(S->Col - PrevColumn); |
| 169 | PrevColumn = S->Col + 1; |
| 170 | std::string C = formatCount(S->Count); |
| 171 | PrevColumn += C.size(); |
| 172 | OS << '^' << C; |
| 173 | } |
| 174 | OS << '\n'; |
| 175 | |
| 176 | if (getOptions().Debug) |
| 177 | for (const auto *S : Segments) |
| 178 | errs() << "Marker at " << S->Line << ":" << S->Col << " = " |
| 179 | << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n"); |
| 180 | } |
| 181 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 182 | void SourceCoverageViewText::renderExpansionSite( |
| 183 | raw_ostream &OS, ExpansionView &ESV, LineRef L, |
Vedant Kumar | 8b12ecb | 2016-06-25 05:48:59 +0000 | [diff] [blame] | 184 | const coverage::CoverageSegment *WrappedSegment, |
| 185 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 186 | renderLinePrefix(OS, ViewDepth); |
| 187 | OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1)); |
| 188 | renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth); |
| 189 | } |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 190 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 191 | void SourceCoverageViewText::renderExpansionView(raw_ostream &OS, |
| 192 | ExpansionView &ESV, |
| 193 | unsigned ViewDepth) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 194 | // Render the child subview. |
| 195 | if (getOptions().Debug) |
| 196 | errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol() |
| 197 | << " -> " << ESV.getEndCol() << '\n'; |
| 198 | ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, |
| 199 | ViewDepth + 1); |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS, |
| 203 | InstantiationView &ISV, |
| 204 | unsigned ViewDepth) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 205 | renderLinePrefix(OS, ViewDepth); |
| 206 | OS << ' '; |
| 207 | ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth); |
| 208 | } |