blob: d6de961c70b65f917d47929166b4eff566a21f31 [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 Kumarf9151b92016-06-25 02:58:30 +000024std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +000025 std::string Number = utostr(N);
26 int Len = Number.size();
27 if (Len <= 3)
28 return Number;
29 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
30 std::string Result(Number.data(), IntLen);
31 if (IntLen != 3) {
32 Result.push_back('.');
33 Result += Number.substr(IntLen, 3 - IntLen);
34 }
35 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
36 return Result;
37}
38
Vedant Kumar7937ef32016-06-28 02:09:39 +000039void SourceCoverageView::StreamDestructor::operator()(raw_ostream *OS) const {
40 if (OS == &outs())
41 return;
42 delete OS;
43}
44
45/// \brief Create a file at ``Dir/ToplevelDir/@Path.Extension``. If
46/// \p ToplevelDir is empty, its path component is skipped.
47static Expected<SourceCoverageView::OwnedStream>
48createFileInDirectory(StringRef Dir, StringRef ToplevelDir, StringRef Path,
49 StringRef Extension) {
50 assert(Extension.size() && "The file extension may not be empty");
51
52 SmallString<256> FullPath(Dir);
53 if (!ToplevelDir.empty())
54 sys::path::append(FullPath, ToplevelDir);
55
56 auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path));
57 sys::path::append(FullPath, PathBaseDir);
58
59 if (auto E = sys::fs::create_directories(FullPath))
60 return errorCodeToError(E);
61
62 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
63 sys::path::append(FullPath, PathFilename);
64
65 std::error_code E;
66 auto OS = SourceCoverageView::OwnedStream(
67 new raw_fd_ostream(FullPath, E, sys::fs::F_RW));
68 if (E)
69 return errorCodeToError(E);
70 return std::move(OS);
71}
72
73Expected<SourceCoverageView::OwnedStream>
74SourceCoverageView::createOutputStream(const CoverageViewOptions &Opts,
75 StringRef Path, StringRef Extension,
76 bool InToplevel) {
Vedant Kumarde717e62016-06-28 16:12:12 +000077 if (!Opts.hasOutputDirectory())
Vedant Kumar7937ef32016-06-28 02:09:39 +000078 return OwnedStream(&outs());
79
80 return createFileInDirectory(Opts.ShowOutputDirectory,
81 InToplevel ? "" : "coverage", Path, Extension);
82}
83
Vedant Kumarf9151b92016-06-25 02:58:30 +000084void SourceCoverageView::addExpansion(
85 const coverage::CounterMappingRegion &Region,
86 std::unique_ptr<SourceCoverageView> View) {
87 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +000088}
89
Vedant Kumarf9151b92016-06-25 02:58:30 +000090void SourceCoverageView::addInstantiation(
91 StringRef FunctionName, unsigned Line,
92 std::unique_ptr<SourceCoverageView> View) {
93 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +000094}
95
Vedant Kumarf9151b92016-06-25 02:58:30 +000096std::unique_ptr<SourceCoverageView>
97SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
98 const CoverageViewOptions &Options,
99 coverage::CoverageData &&CoverageInfo) {
Vedant Kumarebe84012016-06-28 16:12:15 +0000100 switch (Options.Format) {
Vedant Kumar635c83c2016-06-28 00:15:54 +0000101 case CoverageViewOptions::OutputFormat::Text:
102 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
103 std::move(CoverageInfo));
104 }
Simon Pilgrimd95a1722016-06-28 12:55:35 +0000105 llvm_unreachable("Unknown coverage output format!");
Alex Lorenze82d89c2014-08-22 22:56:03 +0000106}
107
Vedant Kumarf9151b92016-06-25 02:58:30 +0000108void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
109 bool ShowSourceName, unsigned ViewDepth) {
110 if (ShowSourceName)
111 renderSourceName(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000112
Justin Bogner5e1400a2014-09-17 05:33:20 +0000113 // We need the expansions and instantiations sorted so we can go through them
114 // while we iterate lines.
115 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
116 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
117 auto NextESV = ExpansionSubViews.begin();
118 auto EndESV = ExpansionSubViews.end();
119 auto NextISV = InstantiationSubViews.begin();
120 auto EndISV = InstantiationSubViews.end();
121
Justin Bognerfe357c02014-09-17 18:23:47 +0000122 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +0000123 auto NextSegment = CoverageInfo.begin();
124 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000125
Justin Bogner13ba23b2014-09-19 08:13:16 +0000126 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +0000127 const coverage::CoverageSegment *WrappedSegment = nullptr;
128 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +0000129 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
130 // If we aren't rendering the whole file, we need to filter out the prologue
131 // and epilogue.
132 if (!WholeFile) {
133 if (NextSegment == EndSegment)
134 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000135 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000136 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000137 }
138
Justin Bognerfe357c02014-09-17 18:23:47 +0000139 // Collect the coverage information relevant to this line.
140 if (LineSegments.size())
141 WrappedSegment = LineSegments.back();
142 LineSegments.clear();
143 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
144 LineSegments.push_back(&*NextSegment++);
145
146 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +0000147 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +0000148 if (WrappedSegment && WrappedSegment->HasCount)
149 LineCount.addRegionCount(WrappedSegment->Count);
150 for (const auto *S : LineSegments)
151 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000152 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000153
Vedant Kumarf9151b92016-06-25 02:58:30 +0000154 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000155 if (getOptions().ShowLineStats)
Justin Bognerfe357c02014-09-17 18:23:47 +0000156 renderLineCoverageColumn(OS, LineCount);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000157 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000158 renderLineNumberColumn(OS, LI.line_number());
159
160 // If there are expansion subviews, we want to highlight the first one.
161 unsigned ExpansionColumn = 0;
162 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000163 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000164 ExpansionColumn = NextESV->getStartCol();
165
Alex Lorenze82d89c2014-08-22 22:56:03 +0000166 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000167 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
168 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000169
170 // Show the region markers.
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000171 if (getOptions().ShowRegionMarkers &&
172 (!getOptions().ShowLineStatsOrRegionMarkers ||
173 LineCount.hasMultipleRegions()) &&
Justin Bognerfe357c02014-09-17 18:23:47 +0000174 !LineSegments.empty()) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000175 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000176 }
177
Justin Bogner5e1400a2014-09-17 05:33:20 +0000178 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000179 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000180 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
181 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000182 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000183
184 // Re-render the current line and highlight the expansion range for
185 // this subview.
186 if (RenderedSubView) {
187 ExpansionColumn = NextESV->getStartCol();
188 renderExpansionSite(
189 OS, *NextESV, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
190 ExpansionColumn, ViewDepth);
191 renderViewDivider(OS, ViewDepth + 1);
192 }
193
194 renderExpansionView(OS, *NextESV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000195 RenderedSubView = true;
196 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000197 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000198 renderViewDivider(OS, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000199 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000200 RenderedSubView = true;
201 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000202 if (RenderedSubView)
203 renderViewDivider(OS, ViewDepth + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000204 }
205}