blob: 08eb545a29c578b2b6c7f8a24c5ec33ce4149bc8 [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,
Vedant Kumard6d192c2016-06-29 21:55:46 +000031 bool InToplevel, bool Relative) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000032 assert(Extension.size() && "The file extension may not be empty");
33
Vedant Kumard6d192c2016-06-29 21:55:46 +000034 SmallString<256> FullPath;
35
36 if (!Relative)
37 FullPath.append(Opts.ShowOutputDirectory);
38
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000039 if (!InToplevel)
40 sys::path::append(FullPath, getCoverageDir());
41
Vedant Kumar4a54abe2016-06-29 16:22:12 +000042 SmallString<256> ParentPath = sys::path::parent_path(Path);
43 sys::path::remove_dots(ParentPath, /*remove_dot_dots=*/true);
44 sys::path::append(FullPath, sys::path::relative_path(ParentPath));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000045
46 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
47 sys::path::append(FullPath, PathFilename);
48
49 return FullPath.str();
50}
51
52Expected<CoveragePrinter::OwnedStream>
53CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
54 bool InToplevel) {
55 if (!Opts.hasOutputDirectory())
56 return OwnedStream(&outs());
57
Vedant Kumard6d192c2016-06-29 21:55:46 +000058 std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000059
60 auto ParentDir = sys::path::parent_path(FullPath);
61 if (auto E = sys::fs::create_directories(ParentDir))
62 return errorCodeToError(E);
63
64 std::error_code E;
65 raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW);
66 auto OS = CoveragePrinter::OwnedStream(RawStream);
67 if (E)
68 return errorCodeToError(E);
69 return std::move(OS);
70}
71
72std::unique_ptr<CoveragePrinter>
73CoveragePrinter::create(const CoverageViewOptions &Opts) {
74 switch (Opts.Format) {
75 case CoverageViewOptions::OutputFormat::Text:
76 return llvm::make_unique<CoveragePrinterText>(Opts);
77 }
Simon Pilgrim0fecee92016-06-28 21:02:41 +000078 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000079}
80
Vedant Kumarf9151b92016-06-25 02:58:30 +000081std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +000082 std::string Number = utostr(N);
83 int Len = Number.size();
84 if (Len <= 3)
85 return Number;
86 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
87 std::string Result(Number.data(), IntLen);
88 if (IntLen != 3) {
89 Result.push_back('.');
90 Result += Number.substr(IntLen, 3 - IntLen);
91 }
92 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
93 return Result;
94}
95
Vedant Kumar8d74cb22016-06-29 00:38:21 +000096bool SourceCoverageView::shouldRenderRegionMarkers(
97 bool LineHasMultipleRegions) const {
98 return getOptions().ShowRegionMarkers &&
99 (!getOptions().ShowLineStatsOrRegionMarkers || LineHasMultipleRegions);
100}
101
102bool SourceCoverageView::hasSubViews() const {
103 return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
104}
105
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000106std::unique_ptr<SourceCoverageView>
107SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
108 const CoverageViewOptions &Options,
109 coverage::CoverageData &&CoverageInfo) {
110 switch (Options.Format) {
111 case CoverageViewOptions::OutputFormat::Text:
112 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
113 std::move(CoverageInfo));
114 }
115 llvm_unreachable("Unknown coverage output format!");
Vedant Kumar7937ef32016-06-28 02:09:39 +0000116}
117
Vedant Kumarf9151b92016-06-25 02:58:30 +0000118void SourceCoverageView::addExpansion(
119 const coverage::CounterMappingRegion &Region,
120 std::unique_ptr<SourceCoverageView> View) {
121 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000122}
123
Vedant Kumarf9151b92016-06-25 02:58:30 +0000124void SourceCoverageView::addInstantiation(
125 StringRef FunctionName, unsigned Line,
126 std::unique_ptr<SourceCoverageView> View) {
127 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000128}
129
Vedant Kumarf9151b92016-06-25 02:58:30 +0000130void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
131 bool ShowSourceName, unsigned ViewDepth) {
132 if (ShowSourceName)
133 renderSourceName(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000134
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000135 renderViewHeader(OS);
136
Justin Bogner5e1400a2014-09-17 05:33:20 +0000137 // We need the expansions and instantiations sorted so we can go through them
138 // while we iterate lines.
139 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
140 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
141 auto NextESV = ExpansionSubViews.begin();
142 auto EndESV = ExpansionSubViews.end();
143 auto NextISV = InstantiationSubViews.begin();
144 auto EndISV = InstantiationSubViews.end();
145
Justin Bognerfe357c02014-09-17 18:23:47 +0000146 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000147 auto NextSegment = CoverageInfo.begin();
148 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000149
Justin Bogner13ba23b2014-09-19 08:13:16 +0000150 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000151 const coverage::CoverageSegment *WrappedSegment = nullptr;
152 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000153 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
154 // If we aren't rendering the whole file, we need to filter out the prologue
155 // and epilogue.
156 if (!WholeFile) {
157 if (NextSegment == EndSegment)
158 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000159 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000160 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000161 }
162
Justin Bognerfe357c02014-09-17 18:23:47 +0000163 // Collect the coverage information relevant to this line.
164 if (LineSegments.size())
165 WrappedSegment = LineSegments.back();
166 LineSegments.clear();
167 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
168 LineSegments.push_back(&*NextSegment++);
169
170 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000171 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000172 if (WrappedSegment && WrappedSegment->HasCount)
173 LineCount.addRegionCount(WrappedSegment->Count);
174 for (const auto *S : LineSegments)
175 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000176 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000177
Vedant Kumarf9151b92016-06-25 02:58:30 +0000178 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000179 if (getOptions().ShowLineStats)
Justin Bognerfe357c02014-09-17 18:23:47 +0000180 renderLineCoverageColumn(OS, LineCount);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000181 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000182 renderLineNumberColumn(OS, LI.line_number());
183
184 // If there are expansion subviews, we want to highlight the first one.
185 unsigned ExpansionColumn = 0;
186 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000187 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000188 ExpansionColumn = NextESV->getStartCol();
189
Alex Lorenze82d89c2014-08-22 22:56:03 +0000190 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000191 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
192 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000193
194 // Show the region markers.
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000195 if (shouldRenderRegionMarkers(LineCount.hasMultipleRegions()))
Vedant Kumarf9151b92016-06-25 02:58:30 +0000196 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000197
Justin Bogner5e1400a2014-09-17 05:33:20 +0000198 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000199 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000200 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
201 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000202 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000203
204 // Re-render the current line and highlight the expansion range for
205 // this subview.
206 if (RenderedSubView) {
207 ExpansionColumn = NextESV->getStartCol();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000208 renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
209 LineSegments, ExpansionColumn, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000210 renderViewDivider(OS, ViewDepth + 1);
211 }
212
213 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000214 RenderedSubView = true;
215 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000216 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000217 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000218 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000219 RenderedSubView = true;
220 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000221 if (RenderedSubView)
222 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000223 renderLineSuffix(OS, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000224 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000225
226 renderViewFooter(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000227}