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