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