blob: baf7c148bb800224b773a7d3ea63ad9644f8136b [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,
112 coverage::CoverageData &&CoverageInfo) {
113 switch (Options.Format) {
114 case CoverageViewOptions::OutputFormat::Text:
115 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
116 std::move(CoverageInfo));
Vedant Kumar4c010922016-07-06 21:44:05 +0000117 case CoverageViewOptions::OutputFormat::HTML:
118 return llvm::make_unique<SourceCoverageViewHTML>(SourceName, File, Options,
119 std::move(CoverageInfo));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000120 }
121 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000122}
123
Vedant Kumarf9151b92016-06-25 02:58:30 +0000124void SourceCoverageView::addExpansion(
125 const coverage::CounterMappingRegion &Region,
126 std::unique_ptr<SourceCoverageView> View) {
127 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000128}
129
Vedant Kumarf9151b92016-06-25 02:58:30 +0000130void SourceCoverageView::addInstantiation(
131 StringRef FunctionName, unsigned Line,
132 std::unique_ptr<SourceCoverageView> View) {
133 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000134}
135
Vedant Kumarf9151b92016-06-25 02:58:30 +0000136void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
137 bool ShowSourceName, unsigned ViewDepth) {
138 if (ShowSourceName)
139 renderSourceName(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000140
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000141 renderViewHeader(OS);
142
Justin Bogner5e1400a2014-09-17 05:33:20 +0000143 // We need the expansions and instantiations sorted so we can go through them
144 // while we iterate lines.
145 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
146 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
147 auto NextESV = ExpansionSubViews.begin();
148 auto EndESV = ExpansionSubViews.end();
149 auto NextISV = InstantiationSubViews.begin();
150 auto EndISV = InstantiationSubViews.end();
151
Justin Bognerfe357c02014-09-17 18:23:47 +0000152 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000153 auto NextSegment = CoverageInfo.begin();
154 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000155
Justin Bogner13ba23b2014-09-19 08:13:16 +0000156 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000157 const coverage::CoverageSegment *WrappedSegment = nullptr;
158 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000159 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
160 // If we aren't rendering the whole file, we need to filter out the prologue
161 // and epilogue.
162 if (!WholeFile) {
163 if (NextSegment == EndSegment)
164 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000165 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000166 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000167 }
168
Justin Bognerfe357c02014-09-17 18:23:47 +0000169 // Collect the coverage information relevant to this line.
170 if (LineSegments.size())
171 WrappedSegment = LineSegments.back();
172 LineSegments.clear();
173 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
174 LineSegments.push_back(&*NextSegment++);
175
176 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000177 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000178 if (WrappedSegment && WrappedSegment->HasCount)
179 LineCount.addRegionCount(WrappedSegment->Count);
180 for (const auto *S : LineSegments)
181 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000182 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000183
Vedant Kumarf9151b92016-06-25 02:58:30 +0000184 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000185 if (getOptions().ShowLineStats)
Justin Bognerfe357c02014-09-17 18:23:47 +0000186 renderLineCoverageColumn(OS, LineCount);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000187 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000188 renderLineNumberColumn(OS, LI.line_number());
189
190 // If there are expansion subviews, we want to highlight the first one.
191 unsigned ExpansionColumn = 0;
192 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000193 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000194 ExpansionColumn = NextESV->getStartCol();
195
Alex Lorenze82d89c2014-08-22 22:56:03 +0000196 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000197 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
198 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000199
200 // Show the region markers.
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000201 if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions()))
Vedant Kumarf9151b92016-06-25 02:58:30 +0000202 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000203
Justin Bogner5e1400a2014-09-17 05:33:20 +0000204 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000205 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000206 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
207 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000208 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000209
210 // Re-render the current line and highlight the expansion range for
211 // this subview.
212 if (RenderedSubView) {
213 ExpansionColumn = NextESV->getStartCol();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000214 renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
215 LineSegments, ExpansionColumn, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000216 renderViewDivider(OS, ViewDepth + 1);
217 }
218
219 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000220 RenderedSubView = true;
221 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000222 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000223 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000224 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000225 RenderedSubView = true;
226 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000227 if (RenderedSubView)
228 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000229 renderLineSuffix(OS, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000230 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000231
232 renderViewFooter(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000233}