Coverage Report

Created: 2019-01-18 03:29

/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.empty() && "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
76
  case CoverageViewOptions::OutputFormat::Lcov:
84
0
    // Unreachable because CodeCoverage.cpp should terminate with an error
85
0
    // before we get here.
86
0
    llvm_unreachable("Lcov format is not supported!");
87
0
  }
88
0
  llvm_unreachable("Unknown coverage output format!");
89
0
}
90
91
129
unsigned SourceCoverageView::getFirstUncoveredLineNo() {
92
708
  const auto MinSegIt = find_if(CoverageInfo, [](const CoverageSegment &S) {
93
708
    return S.HasCount && 
S.Count == 0637
;
94
708
  });
95
129
96
129
  // There is no uncovered line, return zero.
97
129
  if (MinSegIt == CoverageInfo.end())
98
63
    return 0;
99
66
100
66
  return (*MinSegIt).Line;
101
66
}
102
103
1.58k
std::string SourceCoverageView::formatCount(uint64_t N) {
104
1.58k
  std::string Number = utostr(N);
105
1.58k
  int Len = Number.size();
106
1.58k
  if (Len <= 3)
107
1.23k
    return Number;
108
351
  int IntLen = Len % 3 == 0 ? 
384
:
Len % 3267
;
109
351
  std::string Result(Number.data(), IntLen);
110
351
  if (IntLen != 3) {
111
267
    Result.push_back('.');
112
267
    Result += Number.substr(IntLen, 3 - IntLen);
113
267
  }
114
351
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
115
351
  return Result;
116
351
}
117
118
bool SourceCoverageView::shouldRenderRegionMarkers(
119
4.04k
    const LineCoverageStats &LCS) const {
120
4.04k
  if (!getOptions().ShowRegionMarkers)
121
3.85k
    return false;
122
185
123
185
  CoverageSegmentArray Segments = LCS.getLineSegments();
124
185
  if (Segments.empty())
125
102
    return false;
126
157
  
for (unsigned I = 0, E = Segments.size() - 1; 83
I < E;
++I74
) {
127
82
    const auto *CurSeg = Segments[I];
128
82
    if (!CurSeg->IsRegionEntry || 
CurSeg->Count == LCS.getExecutionCount()51
)
129
74
      continue;
130
8
    return true;
131
8
  }
132
83
  
return false75
;
133
83
}
134
135
2.23k
bool SourceCoverageView::hasSubViews() const {
136
2.23k
  return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
137
2.23k
}
138
139
std::unique_ptr<SourceCoverageView>
140
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
141
                           const CoverageViewOptions &Options,
142
184
                           CoverageData &&CoverageInfo) {
143
184
  switch (Options.Format) {
144
184
  case CoverageViewOptions::OutputFormat::Text:
145
111
    return llvm::make_unique<SourceCoverageViewText>(
146
111
        SourceName, File, Options, std::move(CoverageInfo));
147
184
  case CoverageViewOptions::OutputFormat::HTML:
148
73
    return llvm::make_unique<SourceCoverageViewHTML>(
149
73
        SourceName, File, Options, std::move(CoverageInfo));
150
184
  case CoverageViewOptions::OutputFormat::Lcov:
151
0
    // Unreachable because CodeCoverage.cpp should terminate with an error
152
0
    // before we get here.
153
0
    llvm_unreachable("Lcov format is not supported!");
154
0
  }
155
0
  llvm_unreachable("Unknown coverage output format!");
156
0
}
157
158
160
std::string SourceCoverageView::getSourceName() const {
159
160
  SmallString<128> SourceText(SourceName);
160
160
  sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
161
160
  sys::path::native(SourceText);
162
160
  return SourceText.str();
163
160
}
164
165
void SourceCoverageView::addExpansion(
166
    const CounterMappingRegion &Region,
167
3
    std::unique_ptr<SourceCoverageView> View) {
168
3
  ExpansionSubViews.emplace_back(Region, std::move(View));
169
3
}
170
171
void SourceCoverageView::addInstantiation(
172
    StringRef FunctionName, unsigned Line,
173
60
    std::unique_ptr<SourceCoverageView> View) {
174
60
  InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
175
60
}
176
177
void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
178
                               bool ShowSourceName, bool ShowTitle,
179
184
                               unsigned ViewDepth) {
180
184
  if (ShowTitle)
181
53
    renderTitle(OS, "Coverage Report");
182
184
183
184
  renderViewHeader(OS);
184
184
185
184
  if (ShowSourceName)
186
160
    renderSourceName(OS, WholeFile);
187
184
188
184
  renderTableHeader(OS, (ViewDepth > 0) ? 
055
:
getFirstUncoveredLineNo()129
,
189
184
                    ViewDepth);
190
184
191
184
  // We need the expansions and instantiations sorted so we can go through them
192
184
  // while we iterate lines.
193
184
  std::stable_sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
194
184
  std::stable_sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
195
184
  auto NextESV = ExpansionSubViews.begin();
196
184
  auto EndESV = ExpansionSubViews.end();
197
184
  auto NextISV = InstantiationSubViews.begin();
198
184
  auto EndISV = InstantiationSubViews.end();
199
184
200
184
  // Get the coverage information for the file.
201
184
  auto StartSegment = CoverageInfo.begin();
202
184
  auto EndSegment = CoverageInfo.end();
203
184
  LineCoverageIterator LCI{CoverageInfo, 1};
204
184
  LineCoverageIterator LCIEnd = LCI.getEnd();
205
184
206
184
  unsigned FirstLine = StartSegment != EndSegment ? StartSegment->Line : 
00
;
207
3.48k
  for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof();
208
3.33k
       
++LI, ++LCI3.29k
) {
209
3.33k
    // If we aren't rendering the whole file, we need to filter out the prologue
210
3.33k
    // and epilogue.
211
3.33k
    if (!WholeFile) {
212
937
      if (LCI == LCIEnd)
213
36
        break;
214
901
      else if (LI.line_number() < FirstLine)
215
333
        continue;
216
2.96k
    }
217
2.96k
218
2.96k
    renderLinePrefix(OS, ViewDepth);
219
2.96k
    if (getOptions().ShowLineNumbers)
220
2.96k
      renderLineNumberColumn(OS, LI.line_number());
221
2.96k
222
2.96k
    if (getOptions().ShowLineStats)
223
2.91k
      renderLineCoverageColumn(OS, *LCI);
224
2.96k
225
2.96k
    // If there are expansion subviews, we want to highlight the first one.
226
2.96k
    unsigned ExpansionColumn = 0;
227
2.96k
    if (NextESV != EndESV && 
NextESV->getLine() == LI.line_number()29
&&
228
2.96k
        
getOptions().Colors3
)
229
0
      ExpansionColumn = NextESV->getStartCol();
230
2.96k
231
2.96k
    // Display the source code for the current line.
232
2.96k
    renderLine(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn, ViewDepth);
233
2.96k
234
2.96k
    // Show the region markers.
235
2.96k
    if (shouldRenderRegionMarkers(*LCI))
236
6
      renderRegionMarkers(OS, *LCI, ViewDepth);
237
2.96k
238
2.96k
    // Show the expansions and instantiations for this line.
239
2.96k
    bool RenderedSubView = false;
240
2.96k
    for (; NextESV != EndESV && 
NextESV->getLine() == LI.line_number()30
;
241
2.96k
         
++NextESV3
) {
242
3
      renderViewDivider(OS, ViewDepth + 1);
243
3
244
3
      // Re-render the current line and highlight the expansion range for
245
3
      // this subview.
246
3
      if (RenderedSubView) {
247
0
        ExpansionColumn = NextESV->getStartCol();
248
0
        renderExpansionSite(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn,
249
0
                            ViewDepth);
250
0
        renderViewDivider(OS, ViewDepth + 1);
251
0
      }
252
3
253
3
      renderExpansionView(OS, *NextESV, ViewDepth + 1);
254
3
      RenderedSubView = true;
255
3
    }
256
3.02k
    for (; NextISV != EndISV && 
NextISV->Line == LI.line_number()302
;
++NextISV60
) {
257
60
      renderViewDivider(OS, ViewDepth + 1);
258
60
      renderInstantiationView(OS, *NextISV, ViewDepth + 1);
259
60
      RenderedSubView = true;
260
60
    }
261
2.96k
    if (RenderedSubView)
262
29
      renderViewDivider(OS, ViewDepth + 1);
263
2.96k
    renderLineSuffix(OS, ViewDepth);
264
2.96k
  }
265
184
266
184
  renderViewFooter(OS);
267
184
}