blob: 23c86e32dcc537499d53f1bc148996e9d6cae6cb [file] [log] [blame]
Alex Lorenze82d89c2014-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 Kumaree5a5e92016-06-25 05:48:54 +00009///
10/// \file This class implements rendering for code coverage of source code.
11///
Alex Lorenze82d89c2014-08-22 22:56:03 +000012//===----------------------------------------------------------------------===//
13
14#include "SourceCoverageView.h"
Vedant Kumar4c010922016-07-06 21:44:05 +000015#include "SourceCoverageViewHTML.h"
Vedant Kumarf9151b92016-06-25 02:58:30 +000016#include "SourceCoverageViewText.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include "llvm/ADT/SmallString.h"
Justin Bognerd0ceebf2015-05-13 22:41:48 +000018#include "llvm/ADT/StringExtras.h"
Vedant Kumar7937ef32016-06-28 02:09:39 +000019#include "llvm/Support/FileSystem.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000020#include "llvm/Support/LineIterator.h"
Vedant Kumar7937ef32016-06-28 02:09:39 +000021#include "llvm/Support/Path.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000022
23using namespace llvm;
24
Vedant Kumar9cbad2c2016-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 Kumaraae0ba72016-09-09 01:32:51 +000032 bool InToplevel,
33 bool Relative) const {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000034 assert(Extension.size() && "The file extension may not be empty");
35
Vedant Kumard6d192c2016-06-29 21:55:46 +000036 SmallString<256> FullPath;
37
38 if (!Relative)
39 FullPath.append(Opts.ShowOutputDirectory);
40
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000041 if (!InToplevel)
42 sys::path::append(FullPath, getCoverageDir());
43
Vedant Kumar4a54abe2016-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 Kumar9cbad2c2016-06-28 16:12:24 +000047
48 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
49 sys::path::append(FullPath, PathFilename);
Ying Yi76eb2192016-08-30 07:01:37 +000050 sys::path::native(FullPath);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000051
52 return FullPath.str();
53}
54
55Expected<CoveragePrinter::OwnedStream>
56CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
Vedant Kumaraae0ba72016-09-09 01:32:51 +000057 bool InToplevel) const {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000058 if (!Opts.hasOutputDirectory())
59 return OwnedStream(&outs());
60
Vedant Kumard6d192c2016-06-29 21:55:46 +000061 std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
Vedant Kumar9cbad2c2016-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;
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
75std::unique_ptr<CoveragePrinter>
76CoveragePrinter::create(const CoverageViewOptions &Opts) {
77 switch (Opts.Format) {
78 case CoverageViewOptions::OutputFormat::Text:
79 return llvm::make_unique<CoveragePrinterText>(Opts);
Vedant Kumar4c010922016-07-06 21:44:05 +000080 case CoverageViewOptions::OutputFormat::HTML:
81 return llvm::make_unique<CoveragePrinterHTML>(Opts);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000082 }
Simon Pilgrim0fecee92016-06-28 21:02:41 +000083 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000084}
85
Ying Yid36b47c2016-09-06 19:31:18 +000086unsigned 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 Kumarf9151b92016-06-25 02:58:30 +0000105std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +0000106 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 Kumar8d74cb22016-06-29 00:38:21 +0000120bool SourceCoverageView::shouldRenderRegionMarkers(
121 bool LineHasMultipleRegions) const {
122 return getOptions().ShowRegionMarkers &&
123 (!getOptions().ShowLineStatsOrRegionMarkers || LineHasMultipleRegions);
124}
125
126bool SourceCoverageView::hasSubViews() const {
127 return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
128}
129
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000130std::unique_ptr<SourceCoverageView>
131SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
132 const CoverageViewOptions &Options,
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000133 coverage::CoverageData &&CoverageInfo) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000134 switch (Options.Format) {
135 case CoverageViewOptions::OutputFormat::Text:
Ying Yi84dc9712016-08-24 14:27:23 +0000136 return llvm::make_unique<SourceCoverageViewText>(
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000137 SourceName, File, Options, std::move(CoverageInfo));
Vedant Kumar4c010922016-07-06 21:44:05 +0000138 case CoverageViewOptions::OutputFormat::HTML:
Ying Yi84dc9712016-08-24 14:27:23 +0000139 return llvm::make_unique<SourceCoverageViewHTML>(
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000140 SourceName, File, Options, std::move(CoverageInfo));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000141 }
142 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000143}
144
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000145std::string SourceCoverageView::getSourceName() const {
146 SmallString<128> SourceText(SourceName);
Ying Yi24e91bd2016-09-06 21:41:38 +0000147 sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
148 sys::path::native(SourceText);
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000149 return SourceText.str();
150}
151
152std::string SourceCoverageView::getVerboseSourceName() const {
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000153 return getSourceName() + " (Binary: " +
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000154 sys::path::filename(getOptions().ObjectFilename).str() + ")";
Ying Yi24e91bd2016-09-06 21:41:38 +0000155}
156
Vedant Kumarf9151b92016-06-25 02:58:30 +0000157void SourceCoverageView::addExpansion(
158 const coverage::CounterMappingRegion &Region,
159 std::unique_ptr<SourceCoverageView> View) {
160 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000161}
162
Vedant Kumarf9151b92016-06-25 02:58:30 +0000163void SourceCoverageView::addInstantiation(
164 StringRef FunctionName, unsigned Line,
165 std::unique_ptr<SourceCoverageView> View) {
166 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000167}
168
Vedant Kumarf9151b92016-06-25 02:58:30 +0000169void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
170 bool ShowSourceName, unsigned ViewDepth) {
Ying Yi84dc9712016-08-24 14:27:23 +0000171 if (WholeFile)
Vedant Kumar7b9e9bb2016-09-10 19:37:20 +0000172 renderCellInTitle(OS, "Coverage Report");
Alex Lorenze82d89c2014-08-22 22:56:03 +0000173
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000174 renderViewHeader(OS);
175
Ying Yid36b47c2016-09-06 19:31:18 +0000176 unsigned FirstUncoveredLineNo = 0;
177 if (WholeFile)
178 FirstUncoveredLineNo = getFirstUncoveredLineNo();
179
Ying Yi84dc9712016-08-24 14:27:23 +0000180 if (ShowSourceName)
Ying Yid36b47c2016-09-06 19:31:18 +0000181 renderSourceName(OS, WholeFile, FirstUncoveredLineNo);
Ying Yi84dc9712016-08-24 14:27:23 +0000182
183 renderTableHeader(OS, ViewDepth);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000184 // We need the expansions and instantiations sorted so we can go through them
185 // while we iterate lines.
186 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
187 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
188 auto NextESV = ExpansionSubViews.begin();
189 auto EndESV = ExpansionSubViews.end();
190 auto NextISV = InstantiationSubViews.begin();
191 auto EndISV = InstantiationSubViews.end();
192
Justin Bognerfe357c02014-09-17 18:23:47 +0000193 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000194 auto NextSegment = CoverageInfo.begin();
195 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000196
Justin Bogner13ba23b2014-09-19 08:13:16 +0000197 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000198 const coverage::CoverageSegment *WrappedSegment = nullptr;
199 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000200 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
201 // If we aren't rendering the whole file, we need to filter out the prologue
202 // and epilogue.
203 if (!WholeFile) {
204 if (NextSegment == EndSegment)
205 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000206 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000207 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000208 }
209
Justin Bognerfe357c02014-09-17 18:23:47 +0000210 // Collect the coverage information relevant to this line.
211 if (LineSegments.size())
212 WrappedSegment = LineSegments.back();
213 LineSegments.clear();
214 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
215 LineSegments.push_back(&*NextSegment++);
216
217 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000218 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000219 if (WrappedSegment && WrappedSegment->HasCount)
220 LineCount.addRegionCount(WrappedSegment->Count);
221 for (const auto *S : LineSegments)
222 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000223 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000224
Vedant Kumarf9151b92016-06-25 02:58:30 +0000225 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000226 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000227 renderLineNumberColumn(OS, LI.line_number());
Ying Yi6b1f5f82016-08-09 19:53:35 +0000228 if (getOptions().ShowLineStats)
229 renderLineCoverageColumn(OS, LineCount);
Justin Bognerfe357c02014-09-17 18:23:47 +0000230
231 // If there are expansion subviews, we want to highlight the first one.
232 unsigned ExpansionColumn = 0;
233 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000234 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000235 ExpansionColumn = NextESV->getStartCol();
236
Alex Lorenze82d89c2014-08-22 22:56:03 +0000237 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000238 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
239 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000240
241 // Show the region markers.
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000242 if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions()))
Vedant Kumarf9151b92016-06-25 02:58:30 +0000243 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000244
Justin Bogner5e1400a2014-09-17 05:33:20 +0000245 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000246 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000247 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
248 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000249 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000250
251 // Re-render the current line and highlight the expansion range for
252 // this subview.
253 if (RenderedSubView) {
254 ExpansionColumn = NextESV->getStartCol();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000255 renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
256 LineSegments, ExpansionColumn, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000257 renderViewDivider(OS, ViewDepth + 1);
258 }
259
260 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000261 RenderedSubView = true;
262 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000263 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000264 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000265 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000266 RenderedSubView = true;
267 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000268 if (RenderedSubView)
269 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000270 renderLineSuffix(OS, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000271 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000272
273 renderViewFooter(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000274}