Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Vedant Kumar | ee5a5e9 | 2016-06-25 05:48:54 +0000 | [diff] [blame^] | 9 | /// |
| 10 | /// \file This class implements rendering for code coverage of source code. |
| 11 | /// |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SourceCoverageView.h" |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 15 | #include "SourceCoverageViewText.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Justin Bogner | d0ceebf | 2015-05-13 22:41:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LineIterator.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 22 | std::string SourceCoverageView::formatCount(uint64_t N) { |
Justin Bogner | d0ceebf | 2015-05-13 22:41:48 +0000 | [diff] [blame] | 23 | std::string Number = utostr(N); |
| 24 | int Len = Number.size(); |
| 25 | if (Len <= 3) |
| 26 | return Number; |
| 27 | int IntLen = Len % 3 == 0 ? 3 : Len % 3; |
| 28 | std::string Result(Number.data(), IntLen); |
| 29 | if (IntLen != 3) { |
| 30 | Result.push_back('.'); |
| 31 | Result += Number.substr(IntLen, 3 - IntLen); |
| 32 | } |
| 33 | Result.push_back(" kMGTPEZY"[(Len - 1) / 3]); |
| 34 | return Result; |
| 35 | } |
| 36 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 37 | void SourceCoverageView::addExpansion( |
| 38 | const coverage::CounterMappingRegion &Region, |
| 39 | std::unique_ptr<SourceCoverageView> View) { |
| 40 | ExpansionSubViews.emplace_back(Region, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 43 | void SourceCoverageView::addInstantiation( |
| 44 | StringRef FunctionName, unsigned Line, |
| 45 | std::unique_ptr<SourceCoverageView> View) { |
| 46 | InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 49 | std::unique_ptr<SourceCoverageView> |
| 50 | SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File, |
| 51 | const CoverageViewOptions &Options, |
| 52 | coverage::CoverageData &&CoverageInfo) { |
| 53 | return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options, |
| 54 | std::move(CoverageInfo)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 57 | void SourceCoverageView::print(raw_ostream &OS, bool WholeFile, |
| 58 | bool ShowSourceName, unsigned ViewDepth) { |
| 59 | if (ShowSourceName) |
| 60 | renderSourceName(OS); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 61 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 62 | // We need the expansions and instantiations sorted so we can go through them |
| 63 | // while we iterate lines. |
| 64 | std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end()); |
| 65 | std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end()); |
| 66 | auto NextESV = ExpansionSubViews.begin(); |
| 67 | auto EndESV = ExpansionSubViews.end(); |
| 68 | auto NextISV = InstantiationSubViews.begin(); |
| 69 | auto EndISV = InstantiationSubViews.end(); |
| 70 | |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 71 | // Get the coverage information for the file. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 72 | auto NextSegment = CoverageInfo.begin(); |
| 73 | auto EndSegment = CoverageInfo.end(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 74 | |
Justin Bogner | 13ba23b | 2014-09-19 08:13:16 +0000 | [diff] [blame] | 75 | unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 76 | const coverage::CoverageSegment *WrappedSegment = nullptr; |
| 77 | SmallVector<const coverage::CoverageSegment *, 8> LineSegments; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 78 | for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) { |
| 79 | // If we aren't rendering the whole file, we need to filter out the prologue |
| 80 | // and epilogue. |
| 81 | if (!WholeFile) { |
| 82 | if (NextSegment == EndSegment) |
| 83 | break; |
Justin Bogner | 13ba23b | 2014-09-19 08:13:16 +0000 | [diff] [blame] | 84 | else if (LI.line_number() < FirstLine) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 85 | continue; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 88 | // Collect the coverage information relevant to this line. |
| 89 | if (LineSegments.size()) |
| 90 | WrappedSegment = LineSegments.back(); |
| 91 | LineSegments.clear(); |
| 92 | while (NextSegment != EndSegment && NextSegment->Line == LI.line_number()) |
| 93 | LineSegments.push_back(&*NextSegment++); |
| 94 | |
| 95 | // Calculate a count to be for the line as a whole. |
Vedant Kumar | 60dcb48 | 2016-06-24 00:34:48 +0000 | [diff] [blame] | 96 | LineCoverageStats LineCount; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 97 | if (WrappedSegment && WrappedSegment->HasCount) |
| 98 | LineCount.addRegionCount(WrappedSegment->Count); |
| 99 | for (const auto *S : LineSegments) |
| 100 | if (S->HasCount && S->IsRegionEntry) |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 101 | LineCount.addRegionStartCount(S->Count); |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 102 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 103 | renderLinePrefix(OS, ViewDepth); |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 104 | if (getOptions().ShowLineStats) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 105 | renderLineCoverageColumn(OS, LineCount); |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 106 | if (getOptions().ShowLineNumbers) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 107 | renderLineNumberColumn(OS, LI.line_number()); |
| 108 | |
| 109 | // If there are expansion subviews, we want to highlight the first one. |
| 110 | unsigned ExpansionColumn = 0; |
| 111 | if (NextESV != EndESV && NextESV->getLine() == LI.line_number() && |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 112 | getOptions().Colors) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 113 | ExpansionColumn = NextESV->getStartCol(); |
| 114 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 115 | // Display the source code for the current line. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 116 | renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments, |
| 117 | ExpansionColumn, ViewDepth); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 118 | |
| 119 | // Show the region markers. |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 120 | if (getOptions().ShowRegionMarkers && |
| 121 | (!getOptions().ShowLineStatsOrRegionMarkers || |
| 122 | LineCount.hasMultipleRegions()) && |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 123 | !LineSegments.empty()) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 124 | renderRegionMarkers(OS, LineSegments, ViewDepth); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 127 | // Show the expansions and instantiations for this line. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 128 | bool RenderedSubView = false; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 129 | for (; NextESV != EndESV && NextESV->getLine() == LI.line_number(); |
| 130 | ++NextESV) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 131 | renderViewDivider(OS, ViewDepth + 1); |
| 132 | ExpansionColumn = renderExpansionView( |
| 133 | OS, *NextESV, |
| 134 | RenderedSubView ? Optional<LineRef>({*LI, LI.line_number()}) |
| 135 | : Optional<LineRef>(), |
| 136 | ExpansionColumn, WrappedSegment, LineSegments, ViewDepth); |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 137 | RenderedSubView = true; |
| 138 | } |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 139 | for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 140 | renderInstantiationView(OS, *NextISV, ViewDepth + 1); |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 141 | RenderedSubView = true; |
| 142 | } |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 143 | if (RenderedSubView) |
| 144 | renderViewDivider(OS, ViewDepth + 1); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 145 | } |
| 146 | } |