1 //===- SourceCoverageView.cpp - Code coverage view for source code --------===// 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 // This class implements rendering for code coverage of source code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SourceCoverageView.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/Support/LineIterator.h" 17 18 using namespace llvm; 19 20 void SourceCoverageView::renderLine(raw_ostream &OS, StringRef Line, 21 ArrayRef<HighlightRange> Ranges) { 22 if (Ranges.empty()) { 23 OS << Line << "\n"; 24 return; 25 } 26 if (Line.empty()) 27 Line = " "; 28 29 unsigned PrevColumnStart = 0; 30 unsigned Start = 1; 31 for (const auto &Range : Ranges) { 32 if (PrevColumnStart == Range.ColumnStart) 33 continue; 34 35 // Show the unhighlighted part 36 unsigned ColumnStart = PrevColumnStart = Range.ColumnStart; 37 OS << Line.substr(Start - 1, ColumnStart - Start); 38 39 // Show the highlighted part 40 auto Color = Range.Kind == HighlightRange::NotCovered ? raw_ostream::RED 41 : raw_ostream::CYAN; 42 OS.changeColor(Color, false, true); 43 unsigned ColumnEnd = std::min(Range.ColumnEnd, (unsigned)Line.size() + 1); 44 OS << Line.substr(ColumnStart - 1, ColumnEnd - ColumnStart); 45 Start = ColumnEnd; 46 OS.resetColor(); 47 } 48 49 // Show the rest of the line 50 OS << Line.substr(Start - 1, Line.size() - Start + 1); 51 OS << "\n"; 52 53 if (Options.Debug) { 54 for (const auto &Range : Ranges) { 55 errs() << "Highlighted line " << Range.Line << ", " << Range.ColumnStart 56 << " -> "; 57 if (Range.ColumnEnd == std::numeric_limits<unsigned>::max()) { 58 errs() << "?\n"; 59 } else { 60 errs() << Range.ColumnEnd << "\n"; 61 } 62 } 63 } 64 } 65 66 void SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) { 67 for (unsigned I = 0; I < Level; ++I) 68 OS << " |"; 69 } 70 71 void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length, 72 raw_ostream &OS) { 73 assert(Level != 0 && "Cannot render divider at top level"); 74 renderIndent(OS, Level - 1); 75 OS.indent(2); 76 for (unsigned I = 0; I < Length; ++I) 77 OS << "-"; 78 } 79 80 void 81 SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS, 82 const LineCoverageInfo &Line) { 83 if (!Line.isMapped()) { 84 OS.indent(LineCoverageColumnWidth) << '|'; 85 return; 86 } 87 SmallString<32> Buffer; 88 raw_svector_ostream BufferOS(Buffer); 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, 96 Line.hasMultipleRegions() && Options.Colors) 97 << Str; 98 OS << '|'; 99 } 100 101 void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS, 102 unsigned LineNo) { 103 SmallString<32> Buffer; 104 raw_svector_ostream BufferOS(Buffer); 105 BufferOS << LineNo; 106 auto Str = BufferOS.str(); 107 // Trim and align to the right 108 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth)); 109 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|'; 110 } 111 112 void SourceCoverageView::renderRegionMarkers(raw_ostream &OS, 113 ArrayRef<RegionMarker> Regions) { 114 SmallString<32> Buffer; 115 raw_svector_ostream BufferOS(Buffer); 116 117 unsigned PrevColumn = 1; 118 for (const auto &Region : Regions) { 119 // Skip to the new region 120 if (Region.Column > PrevColumn) 121 OS.indent(Region.Column - PrevColumn); 122 PrevColumn = Region.Column + 1; 123 BufferOS << Region.ExecutionCount; 124 StringRef Str = BufferOS.str(); 125 // Trim the execution count 126 Str = Str.substr(0, std::min(Str.size(), (size_t)7)); 127 PrevColumn += Str.size(); 128 OS << '^' << Str; 129 Buffer.clear(); 130 } 131 OS << "\n"; 132 133 if (Options.Debug) { 134 for (const auto &Region : Regions) { 135 errs() << "Marker at " << Region.Line << ":" << Region.Column << " = " 136 << Region.ExecutionCount << "\n"; 137 } 138 } 139 } 140 141 /// \brief Insert a new highlighting range into the line's highlighting ranges 142 /// Return line's new highlighting ranges in result. 143 static void insertExpansionHighlightRange( 144 ArrayRef<SourceCoverageView::HighlightRange> Ranges, 145 unsigned Line, unsigned StartCol, unsigned EndCol, 146 SmallVectorImpl<SourceCoverageView::HighlightRange> &Result) { 147 auto RangeToInsert = SourceCoverageView::HighlightRange( 148 Line, StartCol, EndCol, SourceCoverageView::HighlightRange::Expanded); 149 Result.clear(); 150 size_t I = 0; 151 auto E = Ranges.size(); 152 for (; I < E; ++I) { 153 if (RangeToInsert.ColumnStart < Ranges[I].ColumnEnd) { 154 const auto &Range = Ranges[I]; 155 bool NextRangeContainsInserted = false; 156 // If the next range starts before the inserted range, move the end of the 157 // next range to the start of the inserted range. 158 if (Range.ColumnStart < RangeToInsert.ColumnStart) { 159 if (RangeToInsert.ColumnStart != Range.ColumnStart) 160 Result.push_back(SourceCoverageView::HighlightRange( 161 Range.Line, Range.ColumnStart, RangeToInsert.ColumnStart, 162 Range.Kind)); 163 // If the next range also ends after the inserted range, keep this range 164 // and create a new range that starts at the inserted range and ends 165 // at the next range later. 166 if (Range.ColumnEnd > RangeToInsert.ColumnEnd) 167 NextRangeContainsInserted = true; 168 } 169 if (!NextRangeContainsInserted) { 170 ++I; 171 // Ignore ranges that are contained in inserted range 172 while (I < E && RangeToInsert.contains(Ranges[I])) 173 ++I; 174 } 175 break; 176 } 177 Result.push_back(Ranges[I]); 178 } 179 Result.push_back(RangeToInsert); 180 // If the next range starts before the inserted range end, move the start 181 // of the next range to the end of the inserted range. 182 if (I < E && Ranges[I].ColumnStart < RangeToInsert.ColumnEnd) { 183 const auto &Range = Ranges[I]; 184 if (RangeToInsert.ColumnEnd != Range.ColumnEnd) 185 Result.push_back(SourceCoverageView::HighlightRange( 186 Range.Line, RangeToInsert.ColumnEnd, Range.ColumnEnd, Range.Kind)); 187 ++I; 188 } 189 // Add the remaining ranges that are located after the inserted range 190 for (; I < E; ++I) 191 Result.push_back(Ranges[I]); 192 } 193 194 template <typename T> 195 ArrayRef<T> gatherLineItems(size_t &CurrentIdx, const std::vector<T> &Items, 196 unsigned LineNo) { 197 auto PrevIdx = CurrentIdx; 198 auto E = Items.size(); 199 while (CurrentIdx < E && Items[CurrentIdx].Line == LineNo) 200 ++CurrentIdx; 201 return ArrayRef<T>(Items.data() + PrevIdx, CurrentIdx - PrevIdx); 202 } 203 204 void SourceCoverageView::render(raw_ostream &OS, unsigned IndentLevel) { 205 SmallVector<HighlightRange, 8> AdjustedLineHighlightRanges; 206 size_t CurrentHighlightRange = 0; 207 size_t CurrentRegionMarker = 0; 208 209 line_iterator Lines(File, /*SkipBlanks=*/false); 210 // Advance the line iterator to the first line. 211 while (Lines.line_number() < LineOffset) 212 ++Lines; 213 214 // The width of the leading columns 215 unsigned CombinedColumnWidth = 216 (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) + 217 (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0); 218 // The width of the line that is used to divide between the view and the 219 // subviews. 220 unsigned DividerWidth = CombinedColumnWidth + 4; 221 222 // We need the expansions and instantiations sorted so we can go through them 223 // while we iterate lines. 224 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end()); 225 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end()); 226 auto NextESV = ExpansionSubViews.begin(); 227 auto EndESV = ExpansionSubViews.end(); 228 auto NextISV = InstantiationSubViews.begin(); 229 auto EndISV = InstantiationSubViews.end(); 230 231 for (size_t I = 0, E = LineStats.size(); I < E && !Lines.is_at_eof(); 232 ++I, ++Lines) { 233 unsigned LineNo = Lines.line_number(); 234 235 renderIndent(OS, IndentLevel); 236 if (Options.ShowLineStats) 237 renderLineCoverageColumn(OS, LineStats[I]); 238 if (Options.ShowLineNumbers) 239 renderLineNumberColumn(OS, LineNo); 240 241 // Gather highlighting ranges. 242 auto LineHighlightRanges = 243 gatherLineItems(CurrentHighlightRange, HighlightRanges, LineNo); 244 auto LineRanges = LineHighlightRanges; 245 // Highlight the expansion range if there is an expansion subview on this 246 // line. 247 if (NextESV != EndESV && NextESV->getLine() == LineNo && Options.Colors) { 248 insertExpansionHighlightRange( 249 LineHighlightRanges, NextESV->getLine(), NextESV->getStartCol(), 250 NextESV->getEndCol(), AdjustedLineHighlightRanges); 251 LineRanges = AdjustedLineHighlightRanges; 252 } 253 254 // Display the source code for the current line. 255 StringRef Line = *Lines; 256 renderLine(OS, Line, LineRanges); 257 258 // Show the region markers. 259 bool ShowMarkers = !Options.ShowLineStatsOrRegionMarkers || 260 LineStats[I].hasMultipleRegions(); 261 auto LineMarkers = gatherLineItems(CurrentRegionMarker, Markers, LineNo); 262 if (ShowMarkers && !LineMarkers.empty()) { 263 renderIndent(OS, IndentLevel); 264 OS.indent(CombinedColumnWidth); 265 renderRegionMarkers(OS, LineMarkers); 266 } 267 268 // Show the expansions and instantiations for this line. 269 unsigned NestedIndent = IndentLevel + 1; 270 bool RenderedSubView = false; 271 for (; NextESV != EndESV && NextESV->getLine() == LineNo; ++NextESV) { 272 renderViewDivider(NestedIndent, DividerWidth, OS); 273 OS << "\n"; 274 if (RenderedSubView) { 275 // Re-render the current line and highlight the expansion range for 276 // this subview. 277 insertExpansionHighlightRange( 278 LineHighlightRanges, NextESV->getLine(), NextESV->getStartCol(), 279 NextESV->getEndCol(), AdjustedLineHighlightRanges); 280 renderIndent(OS, IndentLevel); 281 OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1)); 282 renderLine(OS, Line, AdjustedLineHighlightRanges); 283 renderViewDivider(NestedIndent, DividerWidth, OS); 284 OS << "\n"; 285 } 286 // Render the child subview 287 NextESV->View->render(OS, NestedIndent); 288 RenderedSubView = true; 289 } 290 for (; NextISV != EndISV && NextISV->Line == LineNo; ++NextISV) { 291 renderViewDivider(NestedIndent, DividerWidth, OS); 292 OS << "\n"; 293 renderIndent(OS, NestedIndent); 294 OS << ' '; 295 Options.colored_ostream(OS, raw_ostream::CYAN) << NextISV->FunctionName 296 << ":"; 297 OS << "\n"; 298 NextISV->View->render(OS, NestedIndent); 299 RenderedSubView = true; 300 } 301 if (RenderedSubView) { 302 renderViewDivider(NestedIndent, DividerWidth, OS); 303 OS << "\n"; 304 } 305 } 306 } 307 308 void SourceCoverageView::setUpVisibleRange(SourceCoverageDataManager &Data) { 309 auto CountedRegions = Data.getSourceRegions(); 310 if (!CountedRegions.size()) 311 return; 312 313 unsigned Start = CountedRegions.front().LineStart, End = 0; 314 for (const auto &CR : CountedRegions) { 315 Start = std::min(Start, CR.LineStart); 316 End = std::max(End, CR.LineEnd); 317 } 318 LineOffset = Start; 319 LineStats.resize(End - Start + 1); 320 } 321 322 void 323 SourceCoverageView::createLineCoverageInfo(SourceCoverageDataManager &Data) { 324 auto CountedRegions = Data.getSourceRegions(); 325 for (const auto &CR : CountedRegions) { 326 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion) { 327 // Reset the line stats for skipped regions. 328 for (unsigned Line = CR.LineStart; Line <= CR.LineEnd; 329 ++Line) 330 LineStats[Line - LineOffset] = LineCoverageInfo(); 331 continue; 332 } 333 LineStats[CR.LineStart - LineOffset].addRegionStartCount(CR.ExecutionCount); 334 for (unsigned Line = CR.LineStart + 1; Line <= CR.LineEnd; ++Line) 335 LineStats[Line - LineOffset].addRegionCount(CR.ExecutionCount); 336 } 337 } 338 339 void 340 SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) { 341 auto CountedRegions = Data.getSourceRegions(); 342 std::vector<bool> AlreadyHighlighted; 343 AlreadyHighlighted.resize(CountedRegions.size(), false); 344 345 for (size_t I = 0, S = CountedRegions.size(); I < S; ++I) { 346 const auto &CR = CountedRegions[I]; 347 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion || 348 CR.ExecutionCount != 0) 349 continue; 350 if (AlreadyHighlighted[I]) 351 continue; 352 for (size_t J = 0; J < S; ++J) { 353 if (CR.contains(CountedRegions[J])) { 354 AlreadyHighlighted[J] = true; 355 } 356 } 357 if (CR.LineStart == CR.LineEnd) { 358 HighlightRanges.push_back(HighlightRange( 359 CR.LineStart, CR.ColumnStart, CR.ColumnEnd)); 360 continue; 361 } 362 HighlightRanges.push_back( 363 HighlightRange(CR.LineStart, CR.ColumnStart, 364 std::numeric_limits<unsigned>::max())); 365 HighlightRanges.push_back( 366 HighlightRange(CR.LineEnd, 1, CR.ColumnEnd)); 367 for (unsigned Line = CR.LineStart + 1; Line < CR.LineEnd; 368 ++Line) { 369 HighlightRanges.push_back( 370 HighlightRange(Line, 1, std::numeric_limits<unsigned>::max())); 371 } 372 } 373 374 std::sort(HighlightRanges.begin(), HighlightRanges.end()); 375 } 376 377 void SourceCoverageView::createRegionMarkers(SourceCoverageDataManager &Data) { 378 for (const auto &CR : Data.getSourceRegions()) 379 if (CR.Kind != coverage::CounterMappingRegion::SkippedRegion) 380 Markers.push_back( 381 RegionMarker(CR.LineStart, CR.ColumnStart, CR.ExecutionCount)); 382 } 383 384 void SourceCoverageView::load(SourceCoverageDataManager &Data) { 385 setUpVisibleRange(Data); 386 if (Options.ShowLineStats) 387 createLineCoverageInfo(Data); 388 if (Options.Colors) 389 createHighlightRanges(Data); 390 if (Options.ShowRegionMarkers) 391 createRegionMarkers(Data); 392 } 393