comparison tools/llvm-cov/SourceCoverageView.cpp @ 95:afa8332a0e37 LLVM3.8

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 60c9769439b8
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #include "SourceCoverageView.h" 14 #include "SourceCoverageView.h"
15 #include "llvm/ADT/Optional.h" 15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/LineIterator.h" 18 #include "llvm/Support/LineIterator.h"
18 19
19 using namespace llvm; 20 using namespace llvm;
20 21
21 void SourceCoverageView::renderLine( 22 void SourceCoverageView::renderLine(
75 OS.indent(2); 76 OS.indent(2);
76 for (unsigned I = 0; I < Length; ++I) 77 for (unsigned I = 0; I < Length; ++I)
77 OS << "-"; 78 OS << "-";
78 } 79 }
79 80
81 /// Format a count using engineering notation with 3 significant digits.
82 static std::string formatCount(uint64_t N) {
83 std::string Number = utostr(N);
84 int Len = Number.size();
85 if (Len <= 3)
86 return Number;
87 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
88 std::string Result(Number.data(), IntLen);
89 if (IntLen != 3) {
90 Result.push_back('.');
91 Result += Number.substr(IntLen, 3 - IntLen);
92 }
93 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
94 return Result;
95 }
96
80 void 97 void
81 SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS, 98 SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
82 const LineCoverageInfo &Line) { 99 const LineCoverageInfo &Line) {
83 if (!Line.isMapped()) { 100 if (!Line.isMapped()) {
84 OS.indent(LineCoverageColumnWidth) << '|'; 101 OS.indent(LineCoverageColumnWidth) << '|';
85 return; 102 return;
86 } 103 }
87 SmallString<32> Buffer; 104 std::string C = formatCount(Line.ExecutionCount);
88 raw_svector_ostream BufferOS(Buffer); 105 OS.indent(LineCoverageColumnWidth - C.size());
89 BufferOS << Line.ExecutionCount;
90 auto Str = BufferOS.str();
91 // Trim
92 Str = Str.substr(0, std::min(Str.size(), (size_t)LineCoverageColumnWidth));
93 // Align to the right
94 OS.indent(LineCoverageColumnWidth - Str.size());
95 colored_ostream(OS, raw_ostream::MAGENTA, 106 colored_ostream(OS, raw_ostream::MAGENTA,
96 Line.hasMultipleRegions() && Options.Colors) 107 Line.hasMultipleRegions() && Options.Colors)
97 << Str; 108 << C;
98 OS << '|'; 109 OS << '|';
99 } 110 }
100 111
101 void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS, 112 void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS,
102 unsigned LineNo) { 113 unsigned LineNo) {
109 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; 120 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
110 } 121 }
111 122
112 void SourceCoverageView::renderRegionMarkers( 123 void SourceCoverageView::renderRegionMarkers(
113 raw_ostream &OS, ArrayRef<const coverage::CoverageSegment *> Segments) { 124 raw_ostream &OS, ArrayRef<const coverage::CoverageSegment *> Segments) {
114 SmallString<32> Buffer;
115 raw_svector_ostream BufferOS(Buffer);
116
117 unsigned PrevColumn = 1; 125 unsigned PrevColumn = 1;
118 for (const auto *S : Segments) { 126 for (const auto *S : Segments) {
119 if (!S->IsRegionEntry) 127 if (!S->IsRegionEntry)
120 continue; 128 continue;
121 // Skip to the new region 129 // Skip to the new region
122 if (S->Col > PrevColumn) 130 if (S->Col > PrevColumn)
123 OS.indent(S->Col - PrevColumn); 131 OS.indent(S->Col - PrevColumn);
124 PrevColumn = S->Col + 1; 132 PrevColumn = S->Col + 1;
125 BufferOS << S->Count; 133 std::string C = formatCount(S->Count);
126 StringRef Str = BufferOS.str(); 134 PrevColumn += C.size();
127 // Trim the execution count 135 OS << '^' << C;
128 Str = Str.substr(0, std::min(Str.size(), (size_t)7));
129 PrevColumn += Str.size();
130 OS << '^' << Str;
131 Buffer.clear();
132 } 136 }
133 OS << "\n"; 137 OS << "\n";
134 138
135 if (Options.Debug) 139 if (Options.Debug)
136 for (const auto *S : Segments) 140 for (const auto *S : Segments)
137 errs() << "Marker at " << S->Line << ":" << S->Col << " = " << S->Count 141 errs() << "Marker at " << S->Line << ":" << S->Col << " = "
138 << (S->IsRegionEntry ? "\n" : " (pop)\n"); 142 << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
139 } 143 }
140 144
141 void SourceCoverageView::render(raw_ostream &OS, bool WholeFile, 145 void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
142 unsigned IndentLevel) { 146 unsigned IndentLevel) {
143 // The width of the leading columns 147 // The width of the leading columns