Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
171
void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
26
171
  if (OS == &outs())
27
70
    return;
28
101
  delete OS;
29
101
}
30
31
std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
32
                                           bool InToplevel,
33
160
                                           bool Relative) const {
34
160
  assert(Extension.size() && "The file extension may not be empty");
35
160
36
160
  SmallString<256> FullPath;
37
160
38
160
  if (!Relative)
39
101
    FullPath.append(Opts.ShowOutputDirectory);
40
160
41
160
  if (!InToplevel)
42
112
    sys::path::append(FullPath, getCoverageDir());
43
160
44
160
  SmallString<256> ParentPath = sys::path::parent_path(Path);
45
160
  sys::path::remove_dots(ParentPath, /*remove_dot_dots=*/true);
46
160
  sys::path::append(FullPath, sys::path::relative_path(ParentPath));
47
160
48
160
  auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
49
160
  sys::path::append(FullPath, PathFilename);
50
160
  sys::path::native(FullPath);
51
160
52
160
  return FullPath.str();
53
160
}
54
55
Expected<CoveragePrinter::OwnedStream>
56
CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
57
171
                                    bool InToplevel) const {
58
171
  if (!Opts.hasOutputDirectory())
59
70
    return OwnedStream(&outs());
60
101
61
101
  std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
62
101
63
101
  auto ParentDir = sys::path::parent_path(FullPath);
64
101
  if (auto E = sys::fs::create_directories(ParentDir))
65
0
    return errorCodeToError(E);
66
101
67
101
  std::error_code E;
68
101
  raw_ostream *RawStream =
69
101
      new raw_fd_ostream(FullPath, E, sys::fs::FA_Read | sys::fs::FA_Write);
70
101
  auto OS = CoveragePrinter::OwnedStream(RawStream);
71
101
  if (E)
72
1
    return errorCodeToError(E);
73
100
  return std::move(OS);
74
100
}
75
76
std::unique_ptr<CoveragePrinter>
77
76
CoveragePrinter::create(const CoverageViewOptions &Opts) {
78
76
  switch (Opts.Format) {
79
76
  case CoverageViewOptions::OutputFormat::Text:
80
52
    return llvm::make_unique<CoveragePrinterText>(Opts);
81
76
  case CoverageViewOptions::OutputFormat::HTML:
82
24
    return llvm::make_unique<CoveragePrinterHTML>(Opts);
83
0
  }
84
0
  llvm_unreachable("Unknown coverage output format!");
85
0
}
86
87
129
unsigned SourceCoverageView::getFirstUncoveredLineNo() {
88
708
  const auto MinSegIt = find_if(CoverageInfo, [](const CoverageSegment &S) {
89
708
    return S.HasCount && 
S.Count == 0637
;
90
708
  });
91
129
92
129
  // There is no uncovered line, return zero.
93
129
  if (MinSegIt == CoverageInfo.end())
94
63
    return 0;
95
66
96
66
  return (*MinSegIt).Line;
97
66
}
98
99
1.58k
std::string SourceCoverageView::formatCount(uint64_t N) {
100
1.58k
  std::string Number = utostr(N);
101
1.58k
  int Len = Number.size();
102
1.58k
  if (Len <= 3)
103
1.23k
    return Number;
104
351
  int IntLen = Len % 3 == 0 ? 
384
:
Len % 3267
;
105
351
  std::string Result(Number.data(), IntLen);
106
351
  if (IntLen != 3) {
107
267
    Result.push_back('.');
108
267
    Result += Number.substr(IntLen, 3 - IntLen);
109
267
  }
110
351
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
111
351
  return Result;
112
351
}
113
114
bool SourceCoverageView::shouldRenderRegionMarkers(
115
4.08k
    const LineCoverageStats &LCS) const {
116
4.08k
  if (!getOptions().ShowRegionMarkers)
117
3.89k
    return false;
118
191
119
191
  CoverageSegmentArray Segments = LCS.getLineSegments();
120
191
  if (Segments.empty())
121
108
    return false;
122
157
  
for (unsigned I = 0, E = Segments.size() - 1; 83
I < E;
++I74
) {
123
82
    const auto *CurSeg = Segments[I];
124
82
    if (!CurSeg->IsRegionEntry || 
CurSeg->Count == LCS.getExecutionCount()51
)
125
74
      continue;
126
8
    return true;
127
8
  }
128
83
  
return false75
;
129
83
}
130
131
2.24k
bool SourceCoverageView::hasSubViews() const {
132
2.24k
  return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
133
2.24k
}
134
135
std::unique_ptr<SourceCoverageView>
136
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
137
                           const CoverageViewOptions &Options,
138
184
                           CoverageData &&CoverageInfo) {
139
184
  switch (Options.Format) {
140
184
  case CoverageViewOptions::OutputFormat::Text:
141
111
    return llvm::make_unique<SourceCoverageViewText>(
142
111
        SourceName, File, Options, std::move(CoverageInfo));
143
184
  case CoverageViewOptions::OutputFormat::HTML:
144
73
    return llvm::make_unique<SourceCoverageViewHTML>(
145
73
        SourceName, File, Options, std::move(CoverageInfo));
146
0
  }
147
0
  llvm_unreachable("Unknown coverage output format!");
148
0
}
149
150
160
std::string SourceCoverageView::getSourceName() const {
151
160
  SmallString<128> SourceText(SourceName);
152
160
  sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
153
160
  sys::path::native(SourceText);
154
160
  return SourceText.str();
155
160
}
156
157
void SourceCoverageView::addExpansion(
158
    const CounterMappingRegion &Region,
159
3
    std::unique_ptr<SourceCoverageView> View) {
160
3
  ExpansionSubViews.emplace_back(Region, std::move(View));
161
3
}
162
163
void SourceCoverageView::addInstantiation(
164
    StringRef FunctionName, unsigned Line,
165
60
    std::unique_ptr<SourceCoverageView> View) {
166
60
  InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
167
60
}
168
169
void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
170
                               bool ShowSourceName, bool ShowTitle,
171
184
                               unsigned ViewDepth) {
172
184
  if (ShowTitle)
173
53
    renderTitle(OS, "Coverage Report");
174
184
175
184
  renderViewHeader(OS);
176
184
177
184
  if (ShowSourceName)
178
160
    renderSourceName(OS, WholeFile);
179
184
180
184
  renderTableHeader(OS, (ViewDepth > 0) ? 
055
:
getFirstUncoveredLineNo()129
,
181
184
                    ViewDepth);
182
184
183
184
  // We need the expansions and instantiations sorted so we can go through them
184
184
  // while we iterate lines.
185
184
  std::stable_sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
186
184
  std::stable_sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
187
184
  auto NextESV = ExpansionSubViews.begin();
188
184
  auto EndESV = ExpansionSubViews.end();
189
184
  auto NextISV = InstantiationSubViews.begin();
190
184
  auto EndISV = InstantiationSubViews.end();
191
184
192
184
  // Get the coverage information for the file.
193
184
  auto StartSegment = CoverageInfo.begin();
194
184
  auto EndSegment = CoverageInfo.end();
195
184
  LineCoverageIterator LCI{CoverageInfo, 1};
196
184
  LineCoverageIterator LCIEnd = LCI.getEnd();
197
184
198
184
  unsigned FirstLine = StartSegment != EndSegment ? StartSegment->Line : 
00
;
199
3.51k
  for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof();
200
3.37k
       
++LI, ++LCI3.33k
) {
201
3.37k
    // If we aren't rendering the whole file, we need to filter out the prologue
202
3.37k
    // and epilogue.
203
3.37k
    if (!WholeFile) {
204
937
      if (LCI == LCIEnd)
205
36
        break;
206
901
      else if (LI.line_number() < FirstLine)
207
333
        continue;
208
3.00k
    }
209
3.00k
210
3.00k
    renderLinePrefix(OS, ViewDepth);
211
3.00k
    if (getOptions().ShowLineNumbers)
212
3.00k
      renderLineNumberColumn(OS, LI.line_number());
213
3.00k
214
3.00k
    if (getOptions().ShowLineStats)
215
2.94k
      renderLineCoverageColumn(OS, *LCI);
216
3.00k
217
3.00k
    // If there are expansion subviews, we want to highlight the first one.
218
3.00k
    unsigned ExpansionColumn = 0;
219
3.00k
    if (NextESV != EndESV && 
NextESV->getLine() == LI.line_number()29
&&
220
3.00k
        
getOptions().Colors3
)
221
0
      ExpansionColumn = NextESV->getStartCol();
222
3.00k
223
3.00k
    // Display the source code for the current line.
224
3.00k
    renderLine(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn, ViewDepth);
225
3.00k
226
3.00k
    // Show the region markers.
227
3.00k
    if (shouldRenderRegionMarkers(*LCI))
228
6
      renderRegionMarkers(OS, *LCI, ViewDepth);
229
3.00k
230
3.00k
    // Show the expansions and instantiations for this line.
231
3.00k
    bool RenderedSubView = false;
232
3.00k
    for (; NextESV != EndESV && 
NextESV->getLine() == LI.line_number()30
;
233
3.00k
         
++NextESV3
) {
234
3
      renderViewDivider(OS, ViewDepth + 1);
235
3
236
3
      // Re-render the current line and highlight the expansion range for
237
3
      // this subview.
238
3
      if (RenderedSubView) {
239
0
        ExpansionColumn = NextESV->getStartCol();
240
0
        renderExpansionSite(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn,
241
0
                            ViewDepth);
242
0
        renderViewDivider(OS, ViewDepth + 1);
243
0
      }
244
3
245
3
      renderExpansionView(OS, *NextESV, ViewDepth + 1);
246
3
      RenderedSubView = true;
247
3
    }
248
3.06k
    for (; NextISV != EndISV && 
NextISV->Line == LI.line_number()302
;
++NextISV60
) {
249
60
      renderViewDivider(OS, ViewDepth + 1);
250
60
      renderInstantiationView(OS, *NextISV, ViewDepth + 1);
251
60
      RenderedSubView = true;
252
60
    }
253
3.00k
    if (RenderedSubView)
254
29
      renderViewDivider(OS, ViewDepth + 1);
255
3.00k
    renderLineSuffix(OS, ViewDepth);
256
3.00k
  }
257
184
258
184
  renderViewFooter(OS);
259
184
}