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