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 | |
| 21 | namespace { |
| 22 | |
Vedant Kumar | 4f4e8d8 | 2016-06-25 03:27:29 +0000 | [diff] [blame] | 23 | static const unsigned LineCoverageColumnWidth = 7; |
| 24 | static const unsigned LineNumberColumnWidth = 5; |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 25 | |
| 26 | /// \brief Get the width of the leading columns. |
| 27 | unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) { |
| 28 | return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) + |
| 29 | (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0); |
| 30 | } |
| 31 | |
| 32 | /// \brief The width of the line that is used to divide between the view and |
| 33 | /// the subviews. |
| 34 | unsigned getDividerWidth(const CoverageViewOptions &Opts) { |
| 35 | return getCombinedColumnWidth(Opts) + 4; |
| 36 | } |
| 37 | |
| 38 | } // anonymous namespace |
| 39 | |
| 40 | void SourceCoverageViewText::renderSourceName(raw_ostream &OS) { |
| 41 | getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName() |
| 42 | << ":\n"; |
| 43 | } |
| 44 | |
| 45 | void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS, |
| 46 | unsigned ViewDepth) { |
| 47 | for (unsigned I = 0; I < ViewDepth; ++I) |
| 48 | OS << " |"; |
| 49 | } |
| 50 | |
| 51 | void SourceCoverageViewText::renderViewDivider(raw_ostream &OS, |
| 52 | unsigned ViewDepth) { |
| 53 | assert(ViewDepth != 0 && "Cannot render divider at top level"); |
| 54 | renderLinePrefix(OS, ViewDepth - 1); |
| 55 | OS.indent(2); |
| 56 | unsigned Length = getDividerWidth(getOptions()); |
| 57 | for (unsigned I = 0; I < Length; ++I) |
| 58 | OS << '-'; |
| 59 | OS << '\n'; |
| 60 | } |
| 61 | |
| 62 | void SourceCoverageViewText::renderLine( |
| 63 | raw_ostream &OS, LineRef L, |
| 64 | const coverage::CoverageSegment *WrappedSegment, |
| 65 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { |
| 66 | StringRef Line = L.Line; |
| 67 | unsigned LineNumber = L.LineNo; |
| 68 | |
| 69 | Optional<raw_ostream::Colors> Highlight; |
| 70 | SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; |
| 71 | |
| 72 | // The first segment overlaps from a previous line, so we treat it specially. |
| 73 | if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0) |
| 74 | Highlight = raw_ostream::RED; |
| 75 | |
| 76 | // Output each segment of the line, possibly highlighted. |
| 77 | unsigned Col = 1; |
| 78 | for (const auto *S : Segments) { |
| 79 | unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1); |
| 80 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 81 | getOptions().Colors && Highlight, /*Bold=*/false, |
| 82 | /*BG=*/true) |
| 83 | << Line.substr(Col - 1, End - Col); |
| 84 | if (getOptions().Debug && Highlight) |
| 85 | HighlightedRanges.push_back(std::make_pair(Col, End)); |
| 86 | Col = End; |
| 87 | if (Col == ExpansionCol) |
| 88 | Highlight = raw_ostream::CYAN; |
| 89 | else if (S->HasCount && S->Count == 0) |
| 90 | Highlight = raw_ostream::RED; |
| 91 | else |
| 92 | Highlight = None; |
| 93 | } |
| 94 | |
| 95 | // Show the rest of the line. |
| 96 | colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR, |
| 97 | getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true) |
| 98 | << Line.substr(Col - 1, Line.size() - Col + 1); |
| 99 | OS << '\n'; |
| 100 | |
| 101 | if (getOptions().Debug) { |
| 102 | for (const auto &Range : HighlightedRanges) |
| 103 | errs() << "Highlighted line " << LineNumber << ", " << Range.first |
| 104 | << " -> " << Range.second << '\n'; |
| 105 | if (Highlight) |
| 106 | errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n"; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void SourceCoverageViewText::renderLineCoverageColumn( |
| 111 | raw_ostream &OS, const LineCoverageStats &Line) { |
| 112 | if (!Line.isMapped()) { |
| 113 | OS.indent(LineCoverageColumnWidth) << '|'; |
| 114 | return; |
| 115 | } |
| 116 | std::string C = formatCount(Line.ExecutionCount); |
| 117 | OS.indent(LineCoverageColumnWidth - C.size()); |
| 118 | colored_ostream(OS, raw_ostream::MAGENTA, |
| 119 | Line.hasMultipleRegions() && getOptions().Colors) |
| 120 | << C; |
| 121 | OS << '|'; |
| 122 | } |
| 123 | |
| 124 | void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS, |
| 125 | unsigned LineNo) { |
| 126 | SmallString<32> Buffer; |
| 127 | raw_svector_ostream BufferOS(Buffer); |
| 128 | BufferOS << LineNo; |
| 129 | auto Str = BufferOS.str(); |
| 130 | // Trim and align to the right. |
| 131 | Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth)); |
| 132 | OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; |
| 133 | } |
| 134 | |
| 135 | void SourceCoverageViewText::renderRegionMarkers( |
| 136 | raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) { |
| 137 | renderLinePrefix(OS, ViewDepth); |
| 138 | OS.indent(getCombinedColumnWidth(getOptions())); |
| 139 | |
| 140 | unsigned PrevColumn = 1; |
| 141 | for (const auto *S : Segments) { |
| 142 | if (!S->IsRegionEntry) |
| 143 | continue; |
| 144 | // Skip to the new region. |
| 145 | if (S->Col > PrevColumn) |
| 146 | OS.indent(S->Col - PrevColumn); |
| 147 | PrevColumn = S->Col + 1; |
| 148 | std::string C = formatCount(S->Count); |
| 149 | PrevColumn += C.size(); |
| 150 | OS << '^' << C; |
| 151 | } |
| 152 | OS << '\n'; |
| 153 | |
| 154 | if (getOptions().Debug) |
| 155 | for (const auto *S : Segments) |
| 156 | errs() << "Marker at " << S->Line << ":" << S->Col << " = " |
| 157 | << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n"); |
| 158 | } |
| 159 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame^] | 160 | void SourceCoverageViewText::renderExpansionSite( |
| 161 | raw_ostream &OS, ExpansionView &ESV, LineRef L, |
Vedant Kumar | 8b12ecb | 2016-06-25 05:48:59 +0000 | [diff] [blame] | 162 | const coverage::CoverageSegment *WrappedSegment, |
| 163 | CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) { |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame^] | 164 | renderLinePrefix(OS, ViewDepth); |
| 165 | OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1)); |
| 166 | renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth); |
| 167 | } |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 168 | |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame^] | 169 | void SourceCoverageViewText::renderExpansionView(raw_ostream &OS, |
| 170 | ExpansionView &ESV, |
| 171 | unsigned ViewDepth) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 172 | // Render the child subview. |
| 173 | if (getOptions().Debug) |
| 174 | errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol() |
| 175 | << " -> " << ESV.getEndCol() << '\n'; |
| 176 | ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false, |
| 177 | ViewDepth + 1); |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS, |
| 181 | InstantiationView &ISV, |
| 182 | unsigned ViewDepth) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 183 | renderLinePrefix(OS, ViewDepth); |
| 184 | OS << ' '; |
| 185 | ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth); |
| 186 | } |