Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/llvm-cov/SourceCoverageView.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- 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
//===----------------------------------------------------------------------===//
9
///
10
/// \file This class implements rendering for code coverage of source code.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "SourceCoverageView.h"
15
#include "SourceCoverageViewHTML.h"
16
#include "SourceCoverageViewText.h"
17
#include "llvm/ADT/SmallString.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/Support/FileSystem.h"
20
#include "llvm/Support/LineIterator.h"
21
#include "llvm/Support/Path.h"
22
23
using namespace llvm;
24
25
97
void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
26
97
  if (OS == &outs())
27
35
    return;
28
62
  delete OS;
29
62
}
30
31
std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
32
                                           bool InToplevel,
33
91
                                           bool Relative) const {
34
91
  assert(Extension.size() && "The file extension may not be empty");
35
91
36
91
  SmallString<256> FullPath;
37
91
38
91
  if (!Relative)
39
62
    FullPath.append(Opts.ShowOutputDirectory);
40
91
41
91
  if (!InToplevel)
42
52
    sys::path::append(FullPath, getCoverageDir());
43
91
44
91
  SmallString<256> ParentPath = sys::path::parent_path(Path);
45
91
  sys::path::remove_dots(ParentPath, /*remove_dot_dots=*/true);
46
91
  sys::path::append(FullPath, sys::path::relative_path(ParentPath));
47
91
48
91
  auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
49
91
  sys::path::append(FullPath, PathFilename);
50
91
  sys::path::native(FullPath);
51
91
52
91
  return FullPath.str();
53
91
}
54
55
Expected<CoveragePrinter::OwnedStream>
56
CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
57
97
                                    bool InToplevel) const {
58
97
  if (!Opts.hasOutputDirectory())
59
35
    return OwnedStream(&outs());
60
62
61
62
  std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
62
62
63
62
  auto ParentDir = sys::path::parent_path(FullPath);
64
62
  if (auto E = sys::fs::create_directories(ParentDir))
65
0
    return errorCodeToError(E);
66
62
67
62
  std::error_code E;
68
62
  raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW);
69
62
  auto OS = CoveragePrinter::OwnedStream(RawStream);
70
62
  if (E)
71
1
    return errorCodeToError(E);
72
61
  return std::move(OS);
73
61
}
74
75
std::unique_ptr<CoveragePrinter>
76
63
CoveragePrinter::create(const CoverageViewOptions &Opts) {
77
63
  switch (Opts.Format) {
78
44
  case CoverageViewOptions::OutputFormat::Text:
79
44
    return llvm::make_unique<CoveragePrinterText>(Opts);
80
19
  case CoverageViewOptions::OutputFormat::HTML:
81
19
    return llvm::make_unique<CoveragePrinterHTML>(Opts);
82
0
  }
83
0
  
llvm_unreachable0
("Unknown coverage output format!");
84
0
}
85
86
64
unsigned SourceCoverageView::getFirstUncoveredLineNo() {
87
64
  const auto MinSegIt =
88
293
      find_if(CoverageInfo, [](const coverage::CoverageSegment &S) {
89
267
        return S.HasCount && S.Count == 0;
90
293
      });
91
64
92
64
  // There is no uncovered line, return zero.
93
64
  if (MinSegIt == CoverageInfo.end())
94
18
    return 0;
95
46
96
46
  return (*MinSegIt).Line;
97
46
}
98
99
818
std::string SourceCoverageView::formatCount(uint64_t N) {
100
818
  std::string Number = utostr(N);
101
818
  int Len = Number.size();
102
818
  if (Len <= 3)
103
779
    return Number;
104
39
  
int IntLen = Len % 3 == 0 ? 39
38
:
Len % 331
;
105
39
  std::string Result(Number.data(), IntLen);
106
39
  if (
IntLen != 339
) {
107
31
    Result.push_back('.');
108
31
    Result += Number.substr(IntLen, 3 - IntLen);
109
31
  }
110
818
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
111
818
  return Result;
112
818
}
113
114
bool SourceCoverageView::shouldRenderRegionMarkers(
115
3.08k
    CoverageSegmentArray Segments) const {
116
3.08k
  if (!getOptions().ShowRegionMarkers)
117
2.89k
    return false;
118
193
119
193
  // Render the region markers if there's more than one count to show.
120
193
  unsigned RegionCount = 0;
121
193
  for (const auto *S : Segments)
122
122
    
if (122
S->IsRegionEntry122
)
123
68
      
if (68
++RegionCount > 168
)
124
13
        return true;
125
180
  return false;
126
180
}
127
128
1.50k
bool SourceCoverageView::hasSubViews() const {
129
1.50k
  return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
130
1.50k
}
131
132
std::unique_ptr<SourceCoverageView>
133
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
134
                           const CoverageViewOptions &Options,
135
79
                           coverage::CoverageData &&CoverageInfo) {
136
79
  switch (Options.Format) {
137
55
  case CoverageViewOptions::OutputFormat::Text:
138
55
    return llvm::make_unique<SourceCoverageViewText>(
139
55
        SourceName, File, Options, std::move(CoverageInfo));
140
24
  case CoverageViewOptions::OutputFormat::HTML:
141
24
    return llvm::make_unique<SourceCoverageViewHTML>(
142
24
        SourceName, File, Options, std::move(CoverageInfo));
143
0
  }
144
0
  
llvm_unreachable0
("Unknown coverage output format!");
145
0
}
146
147
55
std::string SourceCoverageView::getSourceName() const {
148
55
  SmallString<128> SourceText(SourceName);
149
55
  sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
150
55
  sys::path::native(SourceText);
151
55
  return SourceText.str();
152
55
}
153
154
void SourceCoverageView::addExpansion(
155
    const coverage::CounterMappingRegion &Region,
156
3
    std::unique_ptr<SourceCoverageView> View) {
157
3
  ExpansionSubViews.emplace_back(Region, std::move(View));
158
3
}
159
160
void SourceCoverageView::addInstantiation(
161
    StringRef FunctionName, unsigned Line,
162
20
    std::unique_ptr<SourceCoverageView> View) {
163
20
  InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
164
20
}
165
166
void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
167
                               bool ShowSourceName, bool ShowTitle,
168
79
                               unsigned ViewDepth) {
169
79
  if (ShowTitle)
170
28
    renderTitle(OS, "Coverage Report");
171
79
172
79
  renderViewHeader(OS);
173
79
174
79
  if (ShowSourceName)
175
55
    renderSourceName(OS, WholeFile);
176
79
177
79
  renderTableHeader(OS, (ViewDepth > 0) ? 
015
:
getFirstUncoveredLineNo()64
,
178
79
                    ViewDepth);
179
79
180
79
  // We need the expansions and instantiations sorted so we can go through them
181
79
  // while we iterate lines.
182
79
  std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
183
79
  std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
184
79
  auto NextESV = ExpansionSubViews.begin();
185
79
  auto EndESV = ExpansionSubViews.end();
186
79
  auto NextISV = InstantiationSubViews.begin();
187
79
  auto EndISV = InstantiationSubViews.end();
188
79
189
79
  // Get the coverage information for the file.
190
79
  auto NextSegment = CoverageInfo.begin();
191
79
  auto EndSegment = CoverageInfo.end();
192
79
193
79
  unsigned FirstLine = NextSegment != EndSegment ? 
NextSegment->Line79
:
00
;
194
79
  const coverage::CoverageSegment *WrappedSegment = nullptr;
195
79
  SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
196
2.63k
  for (line_iterator LI(File, /*SkipBlanks=*/false); 
!LI.is_at_eof()2.63k
;
++LI2.55k
) {
197
2.59k
    // If we aren't rendering the whole file, we need to filter out the prologue
198
2.59k
    // and epilogue.
199
2.59k
    if (
!WholeFile2.59k
) {
200
492
      if (NextSegment == EndSegment)
201
35
        break;
202
457
      else 
if (457
LI.line_number() < FirstLine457
)
203
211
        continue;
204
2.34k
    }
205
2.34k
206
2.34k
    // Collect the coverage information relevant to this line.
207
2.34k
    
if (2.34k
LineSegments.size()2.34k
)
208
457
      WrappedSegment = LineSegments.back();
209
2.34k
    LineSegments.clear();
210
3.22k
    while (
NextSegment != EndSegment && 3.22k
NextSegment->Line == LI.line_number()2.01k
)
211
873
      LineSegments.push_back(&*NextSegment++);
212
2.34k
213
2.34k
    renderLinePrefix(OS, ViewDepth);
214
2.34k
    if (getOptions().ShowLineNumbers)
215
2.34k
      renderLineNumberColumn(OS, LI.line_number());
216
2.34k
217
2.34k
    LineCoverageStats LineCount{LineSegments, WrappedSegment};
218
2.34k
    if (getOptions().ShowLineStats)
219
2.29k
      renderLineCoverageColumn(OS, LineCount);
220
2.34k
221
2.34k
    // If there are expansion subviews, we want to highlight the first one.
222
2.34k
    unsigned ExpansionColumn = 0;
223
2.34k
    if (
NextESV != EndESV && 2.34k
NextESV->getLine() == LI.line_number()29
&&
224
3
        getOptions().Colors)
225
0
      ExpansionColumn = NextESV->getStartCol();
226
2.34k
227
2.34k
    // Display the source code for the current line.
228
2.34k
    renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
229
2.34k
               ExpansionColumn, ViewDepth);
230
2.34k
231
2.34k
    // Show the region markers.
232
2.34k
    if (shouldRenderRegionMarkers(LineSegments))
233
11
      renderRegionMarkers(OS, LineSegments, ViewDepth);
234
2.34k
235
2.34k
    // Show the expansions and instantiations for this line.
236
2.34k
    bool RenderedSubView = false;
237
2.35k
    for (; 
NextESV != EndESV && 2.35k
NextESV->getLine() == LI.line_number()30
;
238
2.34k
         
++NextESV3
) {
239
3
      renderViewDivider(OS, ViewDepth + 1);
240
3
241
3
      // Re-render the current line and highlight the expansion range for
242
3
      // this subview.
243
3
      if (
RenderedSubView3
) {
244
0
        ExpansionColumn = NextESV->getStartCol();
245
0
        renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
246
0
                            LineSegments, ExpansionColumn, ViewDepth);
247
0
        renderViewDivider(OS, ViewDepth + 1);
248
0
      }
249
3
250
3
      renderExpansionView(OS, *NextESV, ViewDepth + 1);
251
3
      RenderedSubView = true;
252
3
    }
253
2.36k
    for (; 
NextISV != EndISV && 2.36k
NextISV->Line == LI.line_number()134
;
++NextISV20
) {
254
20
      renderViewDivider(OS, ViewDepth + 1);
255
20
      renderInstantiationView(OS, *NextISV, ViewDepth + 1);
256
20
      RenderedSubView = true;
257
20
    }
258
2.34k
    if (RenderedSubView)
259
13
      renderViewDivider(OS, ViewDepth + 1);
260
2.59k
    renderLineSuffix(OS, ViewDepth);
261
2.59k
  }
262
79
263
79
  renderViewFooter(OS);
264
79
}