blob: 6b7f50ffc7e71185ddc5e1013b318e21939f8f6e [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 Kumarf9151b92016-06-25 02:58:30 +000015#include "SourceCoverageViewText.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "llvm/ADT/SmallString.h"
Justin Bognerd0ceebf2015-05-13 22:41:48 +000017#include "llvm/ADT/StringExtras.h"
Vedant Kumar7937ef32016-06-28 02:09:39 +000018#include "llvm/Support/FileSystem.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include "llvm/Support/LineIterator.h"
Vedant Kumar7937ef32016-06-28 02:09:39 +000020#include "llvm/Support/Path.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000021
22using namespace llvm;
23
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000024void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
25 if (OS == &outs())
26 return;
27 delete OS;
28}
29
30std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
31 bool InToplevel) {
32 assert(Extension.size() && "The file extension may not be empty");
33
34 SmallString<256> FullPath(Opts.ShowOutputDirectory);
35 if (!InToplevel)
36 sys::path::append(FullPath, getCoverageDir());
37
38 auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path));
39 sys::path::append(FullPath, PathBaseDir);
40
41 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
42 sys::path::append(FullPath, PathFilename);
43
44 return FullPath.str();
45}
46
47Expected<CoveragePrinter::OwnedStream>
48CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
49 bool InToplevel) {
50 if (!Opts.hasOutputDirectory())
51 return OwnedStream(&outs());
52
53 std::string FullPath = getOutputPath(Path, Extension, InToplevel);
54
55 auto ParentDir = sys::path::parent_path(FullPath);
56 if (auto E = sys::fs::create_directories(ParentDir))
57 return errorCodeToError(E);
58
59 std::error_code E;
60 raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW);
61 auto OS = CoveragePrinter::OwnedStream(RawStream);
62 if (E)
63 return errorCodeToError(E);
64 return std::move(OS);
65}
66
67std::unique_ptr<CoveragePrinter>
68CoveragePrinter::create(const CoverageViewOptions &Opts) {
69 switch (Opts.Format) {
70 case CoverageViewOptions::OutputFormat::Text:
71 return llvm::make_unique<CoveragePrinterText>(Opts);
72 }
73}
74
Vedant Kumarf9151b92016-06-25 02:58:30 +000075std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +000076 std::string Number = utostr(N);
77 int Len = Number.size();
78 if (Len <= 3)
79 return Number;
80 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
81 std::string Result(Number.data(), IntLen);
82 if (IntLen != 3) {
83 Result.push_back('.');
84 Result += Number.substr(IntLen, 3 - IntLen);
85 }
86 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
87 return Result;
88}
89
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000090std::unique_ptr<SourceCoverageView>
91SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
92 const CoverageViewOptions &Options,
93 coverage::CoverageData &&CoverageInfo) {
94 switch (Options.Format) {
95 case CoverageViewOptions::OutputFormat::Text:
96 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
97 std::move(CoverageInfo));
98 }
99 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000100}
101
Vedant Kumarf9151b92016-06-25 02:58:30 +0000102void SourceCoverageView::addExpansion(
103 const coverage::CounterMappingRegion &Region,
104 std::unique_ptr<SourceCoverageView> View) {
105 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000106}
107
Vedant Kumarf9151b92016-06-25 02:58:30 +0000108void SourceCoverageView::addInstantiation(
109 StringRef FunctionName, unsigned Line,
110 std::unique_ptr<SourceCoverageView> View) {
111 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000112}
113
Vedant Kumarf9151b92016-06-25 02:58:30 +0000114void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
115 bool ShowSourceName, unsigned ViewDepth) {
116 if (ShowSourceName)
117 renderSourceName(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000118
Justin Bogner5e1400a2014-09-17 05:33:20 +0000119 // We need the expansions and instantiations sorted so we can go through them
120 // while we iterate lines.
121 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
122 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
123 auto NextESV = ExpansionSubViews.begin();
124 auto EndESV = ExpansionSubViews.end();
125 auto NextISV = InstantiationSubViews.begin();
126 auto EndISV = InstantiationSubViews.end();
127
Justin Bognerfe357c02014-09-17 18:23:47 +0000128 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000129 auto NextSegment = CoverageInfo.begin();
130 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000131
Justin Bogner13ba23b2014-09-19 08:13:16 +0000132 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000133 const coverage::CoverageSegment *WrappedSegment = nullptr;
134 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000135 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
136 // If we aren't rendering the whole file, we need to filter out the prologue
137 // and epilogue.
138 if (!WholeFile) {
139 if (NextSegment == EndSegment)
140 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000141 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000142 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000143 }
144
Justin Bognerfe357c02014-09-17 18:23:47 +0000145 // Collect the coverage information relevant to this line.
146 if (LineSegments.size())
147 WrappedSegment = LineSegments.back();
148 LineSegments.clear();
149 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
150 LineSegments.push_back(&*NextSegment++);
151
152 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000153 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000154 if (WrappedSegment && WrappedSegment->HasCount)
155 LineCount.addRegionCount(WrappedSegment->Count);
156 for (const auto *S : LineSegments)
157 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000158 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000159
Vedant Kumarf9151b92016-06-25 02:58:30 +0000160 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000161 if (getOptions().ShowLineStats)
Justin Bognerfe357c02014-09-17 18:23:47 +0000162 renderLineCoverageColumn(OS, LineCount);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000163 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000164 renderLineNumberColumn(OS, LI.line_number());
165
166 // If there are expansion subviews, we want to highlight the first one.
167 unsigned ExpansionColumn = 0;
168 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000169 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000170 ExpansionColumn = NextESV->getStartCol();
171
Alex Lorenze82d89c2014-08-22 22:56:03 +0000172 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000173 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
174 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000175
176 // Show the region markers.
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000177 if (getOptions().ShowRegionMarkers &&
178 (!getOptions().ShowLineStatsOrRegionMarkers ||
179 LineCount.hasMultipleRegions()) &&
Justin Bognerfe357c02014-09-17 18:23:47 +0000180 !LineSegments.empty()) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000181 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000182 }
183
Justin Bogner5e1400a2014-09-17 05:33:20 +0000184 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000185 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000186 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
187 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000188 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000189
190 // Re-render the current line and highlight the expansion range for
191 // this subview.
192 if (RenderedSubView) {
193 ExpansionColumn = NextESV->getStartCol();
194 renderExpansionSite(
195 OS, *NextESV, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
196 ExpansionColumn, ViewDepth);
197 renderViewDivider(OS, ViewDepth + 1);
198 }
199
200 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000201 RenderedSubView = true;
202 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000203 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000204 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000205 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000206 RenderedSubView = true;
207 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000208 if (RenderedSubView)
209 renderViewDivider(OS, ViewDepth + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000210 }
211}