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" |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LineIterator.h" |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 24 | void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const { |
| 25 | if (OS == &outs()) |
| 26 | return; |
| 27 | delete OS; |
| 28 | } |
| 29 | |
| 30 | std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension, |
| 31 | bool InToplevel) { |
| 32 | assert(Extension.size() && "The file extension may not be empty"); |
| 33 | |
| 34 | SmallString<256> FullPath(Opts.ShowOutputDirectory); |
| 35 | if (!InToplevel) |
| 36 | sys::path::append(FullPath, getCoverageDir()); |
| 37 | |
| 38 | auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path)); |
| 39 | sys::path::append(FullPath, PathBaseDir); |
| 40 | |
| 41 | auto PathFilename = (sys::path::filename(Path) + "." + Extension).str(); |
| 42 | sys::path::append(FullPath, PathFilename); |
| 43 | |
| 44 | return FullPath.str(); |
| 45 | } |
| 46 | |
| 47 | Expected<CoveragePrinter::OwnedStream> |
| 48 | CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension, |
| 49 | bool InToplevel) { |
| 50 | if (!Opts.hasOutputDirectory()) |
| 51 | return OwnedStream(&outs()); |
| 52 | |
| 53 | std::string FullPath = getOutputPath(Path, Extension, InToplevel); |
| 54 | |
| 55 | auto ParentDir = sys::path::parent_path(FullPath); |
| 56 | if (auto E = sys::fs::create_directories(ParentDir)) |
| 57 | return errorCodeToError(E); |
| 58 | |
| 59 | std::error_code E; |
| 60 | raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW); |
| 61 | auto OS = CoveragePrinter::OwnedStream(RawStream); |
| 62 | if (E) |
| 63 | return errorCodeToError(E); |
| 64 | return std::move(OS); |
| 65 | } |
| 66 | |
| 67 | std::unique_ptr<CoveragePrinter> |
| 68 | CoveragePrinter::create(const CoverageViewOptions &Opts) { |
| 69 | switch (Opts.Format) { |
| 70 | case CoverageViewOptions::OutputFormat::Text: |
| 71 | return llvm::make_unique<CoveragePrinterText>(Opts); |
| 72 | } |
Simon Pilgrim | 0fecee9 | 2016-06-28 21:02:41 +0000 | [diff] [blame] | 73 | llvm_unreachable("Unknown coverage output format!"); |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 76 | std::string SourceCoverageView::formatCount(uint64_t N) { |
Justin Bogner | d0ceebf | 2015-05-13 22:41:48 +0000 | [diff] [blame] | 77 | std::string Number = utostr(N); |
| 78 | int Len = Number.size(); |
| 79 | if (Len <= 3) |
| 80 | return Number; |
| 81 | int IntLen = Len % 3 == 0 ? 3 : Len % 3; |
| 82 | std::string Result(Number.data(), IntLen); |
| 83 | if (IntLen != 3) { |
| 84 | Result.push_back('.'); |
| 85 | Result += Number.substr(IntLen, 3 - IntLen); |
| 86 | } |
| 87 | Result.push_back(" kMGTPEZY"[(Len - 1) / 3]); |
| 88 | return Result; |
| 89 | } |
| 90 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 91 | bool SourceCoverageView::shouldRenderRegionMarkers( |
| 92 | bool LineHasMultipleRegions) const { |
| 93 | return getOptions().ShowRegionMarkers && |
| 94 | (!getOptions().ShowLineStatsOrRegionMarkers || LineHasMultipleRegions); |
| 95 | } |
| 96 | |
| 97 | bool SourceCoverageView::hasSubViews() const { |
| 98 | return !ExpansionSubViews.empty() || !InstantiationSubViews.empty(); |
| 99 | } |
| 100 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 101 | std::unique_ptr<SourceCoverageView> |
| 102 | SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File, |
| 103 | const CoverageViewOptions &Options, |
| 104 | coverage::CoverageData &&CoverageInfo) { |
| 105 | switch (Options.Format) { |
| 106 | case CoverageViewOptions::OutputFormat::Text: |
| 107 | return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options, |
| 108 | std::move(CoverageInfo)); |
| 109 | } |
| 110 | llvm_unreachable("Unknown coverage output format!"); |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 113 | void SourceCoverageView::addExpansion( |
| 114 | const coverage::CounterMappingRegion &Region, |
| 115 | std::unique_ptr<SourceCoverageView> View) { |
| 116 | ExpansionSubViews.emplace_back(Region, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 119 | void SourceCoverageView::addInstantiation( |
| 120 | StringRef FunctionName, unsigned Line, |
| 121 | std::unique_ptr<SourceCoverageView> View) { |
| 122 | InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 125 | void SourceCoverageView::print(raw_ostream &OS, bool WholeFile, |
| 126 | bool ShowSourceName, unsigned ViewDepth) { |
| 127 | if (ShowSourceName) |
| 128 | renderSourceName(OS); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 129 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 130 | renderViewHeader(OS); |
| 131 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 132 | // We need the expansions and instantiations sorted so we can go through them |
| 133 | // while we iterate lines. |
| 134 | std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end()); |
| 135 | std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end()); |
| 136 | auto NextESV = ExpansionSubViews.begin(); |
| 137 | auto EndESV = ExpansionSubViews.end(); |
| 138 | auto NextISV = InstantiationSubViews.begin(); |
| 139 | auto EndISV = InstantiationSubViews.end(); |
| 140 | |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 141 | // Get the coverage information for the file. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 142 | auto NextSegment = CoverageInfo.begin(); |
| 143 | auto EndSegment = CoverageInfo.end(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 144 | |
Justin Bogner | 13ba23b | 2014-09-19 08:13:16 +0000 | [diff] [blame] | 145 | unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 146 | const coverage::CoverageSegment *WrappedSegment = nullptr; |
| 147 | SmallVector<const coverage::CoverageSegment *, 8> LineSegments; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 148 | for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) { |
| 149 | // If we aren't rendering the whole file, we need to filter out the prologue |
| 150 | // and epilogue. |
| 151 | if (!WholeFile) { |
| 152 | if (NextSegment == EndSegment) |
| 153 | break; |
Justin Bogner | 13ba23b | 2014-09-19 08:13:16 +0000 | [diff] [blame] | 154 | else if (LI.line_number() < FirstLine) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 155 | continue; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 158 | // Collect the coverage information relevant to this line. |
| 159 | if (LineSegments.size()) |
| 160 | WrappedSegment = LineSegments.back(); |
| 161 | LineSegments.clear(); |
| 162 | while (NextSegment != EndSegment && NextSegment->Line == LI.line_number()) |
| 163 | LineSegments.push_back(&*NextSegment++); |
| 164 | |
| 165 | // Calculate a count to be for the line as a whole. |
Vedant Kumar | 60dcb48 | 2016-06-24 00:34:48 +0000 | [diff] [blame] | 166 | LineCoverageStats LineCount; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 167 | if (WrappedSegment && WrappedSegment->HasCount) |
| 168 | LineCount.addRegionCount(WrappedSegment->Count); |
| 169 | for (const auto *S : LineSegments) |
| 170 | if (S->HasCount && S->IsRegionEntry) |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 171 | LineCount.addRegionStartCount(S->Count); |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 172 | |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 173 | renderLinePrefix(OS, ViewDepth); |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 174 | if (getOptions().ShowLineStats) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 175 | renderLineCoverageColumn(OS, LineCount); |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 176 | if (getOptions().ShowLineNumbers) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 177 | renderLineNumberColumn(OS, LI.line_number()); |
| 178 | |
| 179 | // If there are expansion subviews, we want to highlight the first one. |
| 180 | unsigned ExpansionColumn = 0; |
| 181 | if (NextESV != EndESV && NextESV->getLine() == LI.line_number() && |
Vedant Kumar | 1c4f588 | 2016-06-24 00:41:26 +0000 | [diff] [blame] | 182 | getOptions().Colors) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 183 | ExpansionColumn = NextESV->getStartCol(); |
| 184 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 185 | // Display the source code for the current line. |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 186 | renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments, |
| 187 | ExpansionColumn, ViewDepth); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 188 | |
| 189 | // Show the region markers. |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 190 | if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions())) |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 191 | renderRegionMarkers(OS, LineSegments, ViewDepth); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 192 | |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 193 | // Show the expansions and instantiations for this line. |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 194 | bool RenderedSubView = false; |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 195 | for (; NextESV != EndESV && NextESV->getLine() == LI.line_number(); |
| 196 | ++NextESV) { |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 197 | renderViewDivider(OS, ViewDepth + 1); |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 198 | |
| 199 | // Re-render the current line and highlight the expansion range for |
| 200 | // this subview. |
| 201 | if (RenderedSubView) { |
| 202 | ExpansionColumn = NextESV->getStartCol(); |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 203 | renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment, |
| 204 | LineSegments, ExpansionColumn, ViewDepth); |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 205 | renderViewDivider(OS, ViewDepth + 1); |
| 206 | } |
| 207 | |
| 208 | renderExpansionView(OS, *NextESV, ViewDepth + 1); |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 209 | RenderedSubView = true; |
| 210 | } |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 211 | for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) { |
Vedant Kumar | 861a19c | 2016-06-26 02:45:13 +0000 | [diff] [blame] | 212 | renderViewDivider(OS, ViewDepth + 1); |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 213 | renderInstantiationView(OS, *NextISV, ViewDepth + 1); |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 214 | RenderedSubView = true; |
| 215 | } |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 216 | if (RenderedSubView) |
| 217 | renderViewDivider(OS, ViewDepth + 1); |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 218 | renderLineSuffix(OS, ViewDepth); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 219 | } |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame^] | 220 | |
| 221 | renderViewFooter(OS); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 222 | } |