blob: 47980ab095257ee8ca5e2694930caa3ec40f1cca [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 Kumard6d192c2016-06-29 21:55:46 +000032 bool InToplevel, bool Relative) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000033 assert(Extension.size() && "The file extension may not be empty");
34
Vedant Kumard6d192c2016-06-29 21:55:46 +000035 SmallString<256> FullPath;
36
37 if (!Relative)
38 FullPath.append(Opts.ShowOutputDirectory);
39
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000040 if (!InToplevel)
41 sys::path::append(FullPath, getCoverageDir());
42
Vedant Kumar4a54abe2016-06-29 16:22:12 +000043 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 Kumar9cbad2c2016-06-28 16:12:24 +000046
47 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
48 sys::path::append(FullPath, PathFilename);
Ying Yi76eb2192016-08-30 07:01:37 +000049 sys::path::native(FullPath);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000050
51 return FullPath.str();
52}
53
54Expected<CoveragePrinter::OwnedStream>
55CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
56 bool InToplevel) {
57 if (!Opts.hasOutputDirectory())
58 return OwnedStream(&outs());
59
Vedant Kumard6d192c2016-06-29 21:55:46 +000060 std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000061
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
74std::unique_ptr<CoveragePrinter>
75CoveragePrinter::create(const CoverageViewOptions &Opts) {
76 switch (Opts.Format) {
77 case CoverageViewOptions::OutputFormat::Text:
78 return llvm::make_unique<CoveragePrinterText>(Opts);
Vedant Kumar4c010922016-07-06 21:44:05 +000079 case CoverageViewOptions::OutputFormat::HTML:
80 return llvm::make_unique<CoveragePrinterHTML>(Opts);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000081 }
Simon Pilgrim0fecee92016-06-28 21:02:41 +000082 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000083}
84
Ying Yid36b47c2016-09-06 19:31:18 +000085unsigned SourceCoverageView::getFirstUncoveredLineNo() {
86 auto CheckIfUncovered = [](const coverage::CoverageSegment &S) {
87 return S.HasCount && S.Count == 0;
88 };
89 // L is less than R if (1) it's an uncovered segment (has a 0 count), and (2)
90 // either R is not an uncovered segment, or L has a lower line number than R.
91 const auto MinSegIt =
92 std::min_element(CoverageInfo.begin(), CoverageInfo.end(),
93 [CheckIfUncovered](const coverage::CoverageSegment &L,
94 const coverage::CoverageSegment &R) {
95 return (CheckIfUncovered(L) &&
96 (!CheckIfUncovered(R) || (L.Line < R.Line)));
97 });
98 if (CheckIfUncovered(*MinSegIt))
99 return (*MinSegIt).Line;
100 // There is no uncovered line, return zero.
101 return 0;
102}
103
Vedant Kumarf9151b92016-06-25 02:58:30 +0000104std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +0000105 std::string Number = utostr(N);
106 int Len = Number.size();
107 if (Len <= 3)
108 return Number;
109 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
110 std::string Result(Number.data(), IntLen);
111 if (IntLen != 3) {
112 Result.push_back('.');
113 Result += Number.substr(IntLen, 3 - IntLen);
114 }
115 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
116 return Result;
117}
118
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000119bool SourceCoverageView::shouldRenderRegionMarkers(
120 bool LineHasMultipleRegions) const {
121 return getOptions().ShowRegionMarkers &&
122 (!getOptions().ShowLineStatsOrRegionMarkers || LineHasMultipleRegions);
123}
124
125bool SourceCoverageView::hasSubViews() const {
126 return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
127}
128
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000129std::unique_ptr<SourceCoverageView>
130SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
131 const CoverageViewOptions &Options,
Ying Yi84dc9712016-08-24 14:27:23 +0000132 coverage::CoverageData &&CoverageInfo,
133 bool FunctionView) {
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>(
137 SourceName, File, Options, std::move(CoverageInfo), FunctionView);
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>(
140 SourceName, File, Options, std::move(CoverageInfo), FunctionView);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000141 }
142 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000143}
144
Ying Yi24e91bd2016-09-06 21:41:38 +0000145std::string SourceCoverageView::getNativeSourceName() const {
146 std::string SourceFile = isFunctionView() ? "Function: " : "Source: ";
147 SourceFile += getSourceName().str();
148 SmallString<128> SourceText(SourceFile);
149 sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
150 sys::path::native(SourceText);
151 return SourceText.c_str();
152}
153
Vedant Kumarf9151b92016-06-25 02:58:30 +0000154void SourceCoverageView::addExpansion(
155 const coverage::CounterMappingRegion &Region,
156 std::unique_ptr<SourceCoverageView> View) {
157 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000158}
159
Vedant Kumarf9151b92016-06-25 02:58:30 +0000160void SourceCoverageView::addInstantiation(
161 StringRef FunctionName, unsigned Line,
162 std::unique_ptr<SourceCoverageView> View) {
163 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000164}
165
Vedant Kumarf9151b92016-06-25 02:58:30 +0000166void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
167 bool ShowSourceName, unsigned ViewDepth) {
Ying Yi84dc9712016-08-24 14:27:23 +0000168 if (WholeFile)
169 renderCellInTitle(OS, "Code Coverage Report");
Alex Lorenze82d89c2014-08-22 22:56:03 +0000170
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000171 renderViewHeader(OS);
172
Ying Yid36b47c2016-09-06 19:31:18 +0000173 unsigned FirstUncoveredLineNo = 0;
174 if (WholeFile)
175 FirstUncoveredLineNo = getFirstUncoveredLineNo();
176
Ying Yi84dc9712016-08-24 14:27:23 +0000177 if (ShowSourceName)
Ying Yid36b47c2016-09-06 19:31:18 +0000178 renderSourceName(OS, WholeFile, FirstUncoveredLineNo);
Ying Yi84dc9712016-08-24 14:27:23 +0000179
180 renderTableHeader(OS, ViewDepth);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000181 // We need the expansions and instantiations sorted so we can go through them
182 // while we iterate lines.
183 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
184 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
185 auto NextESV = ExpansionSubViews.begin();
186 auto EndESV = ExpansionSubViews.end();
187 auto NextISV = InstantiationSubViews.begin();
188 auto EndISV = InstantiationSubViews.end();
189
Justin Bognerfe357c02014-09-17 18:23:47 +0000190 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000191 auto NextSegment = CoverageInfo.begin();
192 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000193
Justin Bogner13ba23b2014-09-19 08:13:16 +0000194 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000195 const coverage::CoverageSegment *WrappedSegment = nullptr;
196 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000197 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
198 // If we aren't rendering the whole file, we need to filter out the prologue
199 // and epilogue.
200 if (!WholeFile) {
201 if (NextSegment == EndSegment)
202 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000203 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000204 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000205 }
206
Justin Bognerfe357c02014-09-17 18:23:47 +0000207 // Collect the coverage information relevant to this line.
208 if (LineSegments.size())
209 WrappedSegment = LineSegments.back();
210 LineSegments.clear();
211 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
212 LineSegments.push_back(&*NextSegment++);
213
214 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000215 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000216 if (WrappedSegment && WrappedSegment->HasCount)
217 LineCount.addRegionCount(WrappedSegment->Count);
218 for (const auto *S : LineSegments)
219 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000220 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000221
Vedant Kumarf9151b92016-06-25 02:58:30 +0000222 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000223 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000224 renderLineNumberColumn(OS, LI.line_number());
Ying Yi6b1f5f82016-08-09 19:53:35 +0000225 if (getOptions().ShowLineStats)
226 renderLineCoverageColumn(OS, LineCount);
Justin Bognerfe357c02014-09-17 18:23:47 +0000227
228 // If there are expansion subviews, we want to highlight the first one.
229 unsigned ExpansionColumn = 0;
230 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000231 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000232 ExpansionColumn = NextESV->getStartCol();
233
Alex Lorenze82d89c2014-08-22 22:56:03 +0000234 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000235 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
236 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000237
238 // Show the region markers.
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000239 if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions()))
Vedant Kumarf9151b92016-06-25 02:58:30 +0000240 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000241
Justin Bogner5e1400a2014-09-17 05:33:20 +0000242 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000243 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000244 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
245 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000246 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000247
248 // Re-render the current line and highlight the expansion range for
249 // this subview.
250 if (RenderedSubView) {
251 ExpansionColumn = NextESV->getStartCol();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000252 renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
253 LineSegments, ExpansionColumn, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000254 renderViewDivider(OS, ViewDepth + 1);
255 }
256
257 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000258 RenderedSubView = true;
259 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000260 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000261 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000262 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000263 RenderedSubView = true;
264 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000265 if (RenderedSubView)
266 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000267 renderLineSuffix(OS, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000268 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000269
270 renderViewFooter(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000271}