blob: cebaf63adb12967489e27cd898d1fcf1e222dfcd [file] [log] [blame]
Alex Lorenz6c7a6a12014-08-22 22:56:03 +00001//===- 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 Kumara6154f92016-06-25 05:48:54 +00009///
10/// \file This class implements rendering for code coverage of source code.
11///
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000012//===----------------------------------------------------------------------===//
13
14#include "SourceCoverageView.h"
Vedant Kumar55c8c002016-07-06 21:44:05 +000015#include "SourceCoverageViewHTML.h"
Vedant Kumara9bf3122016-06-25 02:58:30 +000016#include "SourceCoverageViewText.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000017#include "llvm/ADT/SmallString.h"
Justin Bogner98f0f262015-05-13 22:41:48 +000018#include "llvm/ADT/StringExtras.h"
Vedant Kumar40a78f82016-06-28 02:09:39 +000019#include "llvm/Support/FileSystem.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000020#include "llvm/Support/LineIterator.h"
Vedant Kumar40a78f82016-06-28 02:09:39 +000021#include "llvm/Support/Path.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000022
23using namespace llvm;
24
Vedant Kumar028d73c2016-06-28 16:12:24 +000025void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
26 if (OS == &outs())
27 return;
28 delete OS;
29}
30
31std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
Vedant Kumar85e096d2016-09-09 01:32:51 +000032 bool InToplevel,
33 bool Relative) const {
Jordan Rupprechtf6ea1292018-12-20 00:57:06 +000034 assert(!Extension.empty() && "The file extension may not be empty");
Vedant Kumar028d73c2016-06-28 16:12:24 +000035
Vedant Kumard378d972016-06-29 21:55:46 +000036 SmallString<256> FullPath;
37
38 if (!Relative)
39 FullPath.append(Opts.ShowOutputDirectory);
40
Vedant Kumar028d73c2016-06-28 16:12:24 +000041 if (!InToplevel)
42 sys::path::append(FullPath, getCoverageDir());
43
Vedant Kumar651e3ff2016-06-29 16:22:12 +000044 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 Kumar028d73c2016-06-28 16:12:24 +000047
48 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
49 sys::path::append(FullPath, PathFilename);
Ying Yi696fea92016-08-30 07:01:37 +000050 sys::path::native(FullPath);
Vedant Kumar028d73c2016-06-28 16:12:24 +000051
52 return FullPath.str();
53}
54
55Expected<CoveragePrinter::OwnedStream>
56CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
Vedant Kumar85e096d2016-09-09 01:32:51 +000057 bool InToplevel) const {
Vedant Kumar028d73c2016-06-28 16:12:24 +000058 if (!Opts.hasOutputDirectory())
59 return OwnedStream(&outs());
60
Vedant Kumard378d972016-06-29 21:55:46 +000061 std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
Vedant Kumar028d73c2016-06-28 16:12:24 +000062
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;
Zachary Turner03bcb212018-06-07 19:58:58 +000068 raw_ostream *RawStream =
69 new raw_fd_ostream(FullPath, E, sys::fs::FA_Read | sys::fs::FA_Write);
Vedant Kumar028d73c2016-06-28 16:12:24 +000070 auto OS = CoveragePrinter::OwnedStream(RawStream);
71 if (E)
72 return errorCodeToError(E);
73 return std::move(OS);
74}
75
76std::unique_ptr<CoveragePrinter>
77CoveragePrinter::create(const CoverageViewOptions &Opts) {
78 switch (Opts.Format) {
79 case CoverageViewOptions::OutputFormat::Text:
80 return llvm::make_unique<CoveragePrinterText>(Opts);
Vedant Kumar55c8c002016-07-06 21:44:05 +000081 case CoverageViewOptions::OutputFormat::HTML:
82 return llvm::make_unique<CoveragePrinterHTML>(Opts);
Max Morozfcdc2672018-11-09 16:10:44 +000083 case CoverageViewOptions::OutputFormat::Lcov:
84 // Unreachable because CodeCoverage.cpp should terminate with an error
85 // before we get here.
86 llvm_unreachable("Lcov format is not supported!");
Vedant Kumar028d73c2016-06-28 16:12:24 +000087 }
Simon Pilgrim24aa4762016-06-28 21:02:41 +000088 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar028d73c2016-06-28 16:12:24 +000089}
90
Ying Yif73cd142016-09-06 19:31:18 +000091unsigned SourceCoverageView::getFirstUncoveredLineNo() {
Vedant Kumar34ea0de2017-10-18 23:58:27 +000092 const auto MinSegIt = find_if(CoverageInfo, [](const CoverageSegment &S) {
93 return S.HasCount && S.Count == 0;
94 });
Vedant Kumar2fbca6b2017-09-18 23:37:27 +000095
Ying Yif73cd142016-09-06 19:31:18 +000096 // There is no uncovered line, return zero.
Vedant Kumar2fbca6b2017-09-18 23:37:27 +000097 if (MinSegIt == CoverageInfo.end())
98 return 0;
99
100 return (*MinSegIt).Line;
Ying Yif73cd142016-09-06 19:31:18 +0000101}
102
Vedant Kumara9bf3122016-06-25 02:58:30 +0000103std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bogner98f0f262015-05-13 22:41:48 +0000104 std::string Number = utostr(N);
105 int Len = Number.size();
106 if (Len <= 3)
107 return Number;
108 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
109 std::string Result(Number.data(), IntLen);
110 if (IntLen != 3) {
111 Result.push_back('.');
112 Result += Number.substr(IntLen, 3 - IntLen);
113 }
114 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
115 return Result;
116}
117
Vedant Kumarcd29c6b2016-06-29 00:38:21 +0000118bool SourceCoverageView::shouldRenderRegionMarkers(
Vedant Kumar5a74fe72017-11-09 02:33:44 +0000119 const LineCoverageStats &LCS) const {
Vedant Kumar2cefdfa2017-09-08 18:44:46 +0000120 if (!getOptions().ShowRegionMarkers)
121 return false;
122
Vedant Kumar5a74fe72017-11-09 02:33:44 +0000123 CoverageSegmentArray Segments = LCS.getLineSegments();
124 if (Segments.empty())
125 return false;
126 for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
127 const auto *CurSeg = Segments[I];
128 if (!CurSeg->IsRegionEntry || CurSeg->Count == LCS.getExecutionCount())
129 continue;
130 return true;
131 }
Vedant Kumar2cefdfa2017-09-08 18:44:46 +0000132 return false;
Vedant Kumarcd29c6b2016-06-29 00:38:21 +0000133}
134
135bool SourceCoverageView::hasSubViews() const {
136 return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
137}
138
Vedant Kumar028d73c2016-06-28 16:12:24 +0000139std::unique_ptr<SourceCoverageView>
140SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
141 const CoverageViewOptions &Options,
Vedant Kumar34ea0de2017-10-18 23:58:27 +0000142 CoverageData &&CoverageInfo) {
Vedant Kumar028d73c2016-06-28 16:12:24 +0000143 switch (Options.Format) {
144 case CoverageViewOptions::OutputFormat::Text:
Ying Yi1461e992016-08-24 14:27:23 +0000145 return llvm::make_unique<SourceCoverageViewText>(
Vedant Kumard2e36ca2016-09-08 00:56:48 +0000146 SourceName, File, Options, std::move(CoverageInfo));
Vedant Kumar55c8c002016-07-06 21:44:05 +0000147 case CoverageViewOptions::OutputFormat::HTML:
Ying Yi1461e992016-08-24 14:27:23 +0000148 return llvm::make_unique<SourceCoverageViewHTML>(
Vedant Kumard2e36ca2016-09-08 00:56:48 +0000149 SourceName, File, Options, std::move(CoverageInfo));
Max Morozfcdc2672018-11-09 16:10:44 +0000150 case CoverageViewOptions::OutputFormat::Lcov:
151 // Unreachable because CodeCoverage.cpp should terminate with an error
152 // before we get here.
153 llvm_unreachable("Lcov format is not supported!");
Vedant Kumar028d73c2016-06-28 16:12:24 +0000154 }
155 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar40a78f82016-06-28 02:09:39 +0000156}
157
Vedant Kumard2e36ca2016-09-08 00:56:48 +0000158std::string SourceCoverageView::getSourceName() const {
159 SmallString<128> SourceText(SourceName);
Ying Yi84f34c02016-09-06 21:41:38 +0000160 sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
161 sys::path::native(SourceText);
Vedant Kumard2e36ca2016-09-08 00:56:48 +0000162 return SourceText.str();
163}
164
Vedant Kumara9bf3122016-06-25 02:58:30 +0000165void SourceCoverageView::addExpansion(
Vedant Kumar34ea0de2017-10-18 23:58:27 +0000166 const CounterMappingRegion &Region,
Vedant Kumara9bf3122016-06-25 02:58:30 +0000167 std::unique_ptr<SourceCoverageView> View) {
168 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000169}
170
Vedant Kumara9bf3122016-06-25 02:58:30 +0000171void SourceCoverageView::addInstantiation(
172 StringRef FunctionName, unsigned Line,
173 std::unique_ptr<SourceCoverageView> View) {
174 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000175}
176
Vedant Kumara9bf3122016-06-25 02:58:30 +0000177void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
Sean Eveson078c2c32017-09-28 10:07:30 +0000178 bool ShowSourceName, bool ShowTitle,
179 unsigned ViewDepth) {
180 if (ShowTitle)
Vedant Kumar7551d482016-09-15 04:45:59 +0000181 renderTitle(OS, "Coverage Report");
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000182
Vedant Kumarcd29c6b2016-06-29 00:38:21 +0000183 renderViewHeader(OS);
184
Ying Yi1461e992016-08-24 14:27:23 +0000185 if (ShowSourceName)
Vedant Kumara8edd762016-09-10 19:37:26 +0000186 renderSourceName(OS, WholeFile);
Ying Yi1461e992016-08-24 14:27:23 +0000187
Vedant Kumar4cb35092016-09-15 06:49:13 +0000188 renderTableHeader(OS, (ViewDepth > 0) ? 0 : getFirstUncoveredLineNo(),
189 ViewDepth);
Vedant Kumara8edd762016-09-10 19:37:26 +0000190
Justin Bognerd1b4e602014-09-17 05:33:20 +0000191 // We need the expansions and instantiations sorted so we can go through them
192 // while we iterate lines.
Vedant Kumar7e809502017-10-24 20:03:37 +0000193 std::stable_sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
194 std::stable_sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
Justin Bognerd1b4e602014-09-17 05:33:20 +0000195 auto NextESV = ExpansionSubViews.begin();
196 auto EndESV = ExpansionSubViews.end();
197 auto NextISV = InstantiationSubViews.begin();
198 auto EndISV = InstantiationSubViews.end();
199
Justin Bognerb3edb562014-09-17 18:23:47 +0000200 // Get the coverage information for the file.
Vedant Kumar53719452017-10-14 02:27:29 +0000201 auto StartSegment = CoverageInfo.begin();
Justin Bogner9eb38162014-09-20 15:31:56 +0000202 auto EndSegment = CoverageInfo.end();
Vedant Kumar53719452017-10-14 02:27:29 +0000203 LineCoverageIterator LCI{CoverageInfo, 1};
204 LineCoverageIterator LCIEnd = LCI.getEnd();
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000205
Vedant Kumar53719452017-10-14 02:27:29 +0000206 unsigned FirstLine = StartSegment != EndSegment ? StartSegment->Line : 0;
207 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof();
208 ++LI, ++LCI) {
Justin Bognerb3edb562014-09-17 18:23:47 +0000209 // If we aren't rendering the whole file, we need to filter out the prologue
210 // and epilogue.
211 if (!WholeFile) {
Vedant Kumar53719452017-10-14 02:27:29 +0000212 if (LCI == LCIEnd)
Justin Bognerb3edb562014-09-17 18:23:47 +0000213 break;
Justin Bogner42b96882014-09-19 08:13:16 +0000214 else if (LI.line_number() < FirstLine)
Justin Bognerb3edb562014-09-17 18:23:47 +0000215 continue;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000216 }
217
Vedant Kumara9bf3122016-06-25 02:58:30 +0000218 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1bbbb812016-06-24 00:41:26 +0000219 if (getOptions().ShowLineNumbers)
Justin Bognerb3edb562014-09-17 18:23:47 +0000220 renderLineNumberColumn(OS, LI.line_number());
Vedant Kumarc7e789c2017-08-04 00:36:24 +0000221
Ying Yi17a2f3f2016-08-09 19:53:35 +0000222 if (getOptions().ShowLineStats)
Vedant Kumar53719452017-10-14 02:27:29 +0000223 renderLineCoverageColumn(OS, *LCI);
Justin Bognerb3edb562014-09-17 18:23:47 +0000224
225 // If there are expansion subviews, we want to highlight the first one.
226 unsigned ExpansionColumn = 0;
227 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1bbbb812016-06-24 00:41:26 +0000228 getOptions().Colors)
Justin Bognerb3edb562014-09-17 18:23:47 +0000229 ExpansionColumn = NextESV->getStartCol();
230
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000231 // Display the source code for the current line.
Vedant Kumard35bb382017-10-18 18:52:28 +0000232 renderLine(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn, ViewDepth);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000233
234 // Show the region markers.
Vedant Kumar5a74fe72017-11-09 02:33:44 +0000235 if (shouldRenderRegionMarkers(*LCI))
Vedant Kumard35bb382017-10-18 18:52:28 +0000236 renderRegionMarkers(OS, *LCI, ViewDepth);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000237
Justin Bognerd1b4e602014-09-17 05:33:20 +0000238 // Show the expansions and instantiations for this line.
Justin Bognerd1b4e602014-09-17 05:33:20 +0000239 bool RenderedSubView = false;
Justin Bognerb3edb562014-09-17 18:23:47 +0000240 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
241 ++NextESV) {
Vedant Kumara9bf3122016-06-25 02:58:30 +0000242 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumare4356612016-06-26 02:45:13 +0000243
244 // Re-render the current line and highlight the expansion range for
245 // this subview.
246 if (RenderedSubView) {
247 ExpansionColumn = NextESV->getStartCol();
Vedant Kumard35bb382017-10-18 18:52:28 +0000248 renderExpansionSite(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn,
249 ViewDepth);
Vedant Kumare4356612016-06-26 02:45:13 +0000250 renderViewDivider(OS, ViewDepth + 1);
251 }
252
253 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bognerd1b4e602014-09-17 05:33:20 +0000254 RenderedSubView = true;
255 }
Justin Bognerb3edb562014-09-17 18:23:47 +0000256 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumare4356612016-06-26 02:45:13 +0000257 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumara9bf3122016-06-25 02:58:30 +0000258 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bognerd1b4e602014-09-17 05:33:20 +0000259 RenderedSubView = true;
260 }
Vedant Kumara9bf3122016-06-25 02:58:30 +0000261 if (RenderedSubView)
262 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarcd29c6b2016-06-29 00:38:21 +0000263 renderLineSuffix(OS, ViewDepth);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000264 }
Vedant Kumarcd29c6b2016-06-29 00:38:21 +0000265
266 renderViewFooter(OS);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000267}