blob: 23f725fccdccc94b0ae14968690ffcaa6f57a000 [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);
49
50 return FullPath.str();
51}
52
53Expected<CoveragePrinter::OwnedStream>
54CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
55 bool InToplevel) {
56 if (!Opts.hasOutputDirectory())
57 return OwnedStream(&outs());
58
Vedant Kumard6d192c2016-06-29 21:55:46 +000059 std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000060
61 auto ParentDir = sys::path::parent_path(FullPath);
62 if (auto E = sys::fs::create_directories(ParentDir))
63 return errorCodeToError(E);
64
65 std::error_code E;
66 raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW);
67 auto OS = CoveragePrinter::OwnedStream(RawStream);
68 if (E)
69 return errorCodeToError(E);
70 return std::move(OS);
71}
72
73std::unique_ptr<CoveragePrinter>
74CoveragePrinter::create(const CoverageViewOptions &Opts) {
75 switch (Opts.Format) {
76 case CoverageViewOptions::OutputFormat::Text:
77 return llvm::make_unique<CoveragePrinterText>(Opts);
Vedant Kumar4c010922016-07-06 21:44:05 +000078 case CoverageViewOptions::OutputFormat::HTML:
79 return llvm::make_unique<CoveragePrinterHTML>(Opts);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000080 }
Simon Pilgrim0fecee92016-06-28 21:02:41 +000081 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000082}
83
Vedant Kumarf9151b92016-06-25 02:58:30 +000084std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +000085 std::string Number = utostr(N);
86 int Len = Number.size();
87 if (Len <= 3)
88 return Number;
89 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
90 std::string Result(Number.data(), IntLen);
91 if (IntLen != 3) {
92 Result.push_back('.');
93 Result += Number.substr(IntLen, 3 - IntLen);
94 }
95 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
96 return Result;
97}
98
Vedant Kumar8d74cb22016-06-29 00:38:21 +000099bool SourceCoverageView::shouldRenderRegionMarkers(
100 bool LineHasMultipleRegions) const {
101 return getOptions().ShowRegionMarkers &&
102 (!getOptions().ShowLineStatsOrRegionMarkers || LineHasMultipleRegions);
103}
104
105bool SourceCoverageView::hasSubViews() const {
106 return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
107}
108
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000109std::unique_ptr<SourceCoverageView>
110SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
111 const CoverageViewOptions &Options,
Ying Yi84dc9712016-08-24 14:27:23 +0000112 coverage::CoverageData &&CoverageInfo,
113 bool FunctionView) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000114 switch (Options.Format) {
115 case CoverageViewOptions::OutputFormat::Text:
Ying Yi84dc9712016-08-24 14:27:23 +0000116 return llvm::make_unique<SourceCoverageViewText>(
117 SourceName, File, Options, std::move(CoverageInfo), FunctionView);
Vedant Kumar4c010922016-07-06 21:44:05 +0000118 case CoverageViewOptions::OutputFormat::HTML:
Ying Yi84dc9712016-08-24 14:27:23 +0000119 return llvm::make_unique<SourceCoverageViewHTML>(
120 SourceName, File, Options, std::move(CoverageInfo), FunctionView);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000121 }
122 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000123}
124
Vedant Kumarf9151b92016-06-25 02:58:30 +0000125void SourceCoverageView::addExpansion(
126 const coverage::CounterMappingRegion &Region,
127 std::unique_ptr<SourceCoverageView> View) {
128 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000129}
130
Vedant Kumarf9151b92016-06-25 02:58:30 +0000131void SourceCoverageView::addInstantiation(
132 StringRef FunctionName, unsigned Line,
133 std::unique_ptr<SourceCoverageView> View) {
134 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000135}
136
Vedant Kumarf9151b92016-06-25 02:58:30 +0000137void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
138 bool ShowSourceName, unsigned ViewDepth) {
Ying Yi84dc9712016-08-24 14:27:23 +0000139 if (WholeFile)
140 renderCellInTitle(OS, "Code Coverage Report");
Alex Lorenze82d89c2014-08-22 22:56:03 +0000141
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000142 renderViewHeader(OS);
143
Ying Yi84dc9712016-08-24 14:27:23 +0000144 if (ShowSourceName)
145 renderSourceName(OS, WholeFile);
146
147 renderTableHeader(OS, ViewDepth);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000148 // We need the expansions and instantiations sorted so we can go through them
149 // while we iterate lines.
150 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
151 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
152 auto NextESV = ExpansionSubViews.begin();
153 auto EndESV = ExpansionSubViews.end();
154 auto NextISV = InstantiationSubViews.begin();
155 auto EndISV = InstantiationSubViews.end();
156
Justin Bognerfe357c02014-09-17 18:23:47 +0000157 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000158 auto NextSegment = CoverageInfo.begin();
159 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000160
Justin Bogner13ba23b2014-09-19 08:13:16 +0000161 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000162 const coverage::CoverageSegment *WrappedSegment = nullptr;
163 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000164 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
165 // If we aren't rendering the whole file, we need to filter out the prologue
166 // and epilogue.
167 if (!WholeFile) {
168 if (NextSegment == EndSegment)
169 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000170 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000171 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000172 }
173
Justin Bognerfe357c02014-09-17 18:23:47 +0000174 // Collect the coverage information relevant to this line.
175 if (LineSegments.size())
176 WrappedSegment = LineSegments.back();
177 LineSegments.clear();
178 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
179 LineSegments.push_back(&*NextSegment++);
180
181 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000182 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000183 if (WrappedSegment && WrappedSegment->HasCount)
184 LineCount.addRegionCount(WrappedSegment->Count);
185 for (const auto *S : LineSegments)
186 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000187 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000188
Vedant Kumarf9151b92016-06-25 02:58:30 +0000189 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000190 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000191 renderLineNumberColumn(OS, LI.line_number());
Ying Yi6b1f5f82016-08-09 19:53:35 +0000192 if (getOptions().ShowLineStats)
193 renderLineCoverageColumn(OS, LineCount);
Justin Bognerfe357c02014-09-17 18:23:47 +0000194
195 // If there are expansion subviews, we want to highlight the first one.
196 unsigned ExpansionColumn = 0;
197 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000198 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000199 ExpansionColumn = NextESV->getStartCol();
200
Alex Lorenze82d89c2014-08-22 22:56:03 +0000201 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000202 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
203 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000204
205 // Show the region markers.
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000206 if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions()))
Vedant Kumarf9151b92016-06-25 02:58:30 +0000207 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000208
Justin Bogner5e1400a2014-09-17 05:33:20 +0000209 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000210 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000211 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
212 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000213 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000214
215 // Re-render the current line and highlight the expansion range for
216 // this subview.
217 if (RenderedSubView) {
218 ExpansionColumn = NextESV->getStartCol();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000219 renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
220 LineSegments, ExpansionColumn, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000221 renderViewDivider(OS, ViewDepth + 1);
222 }
223
224 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000225 RenderedSubView = true;
226 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000227 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000228 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000229 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000230 RenderedSubView = true;
231 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000232 if (RenderedSubView)
233 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000234 renderLineSuffix(OS, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000235 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000236
237 renderViewFooter(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000238}