Coverage Report

Created: 2017-09-21 03:39

/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
76
void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
26
76
  if (OS == &outs())
27
36
    return;
28
40
  delete OS;
29
40
}
30
31
std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
32
                                           bool InToplevel,
33
59
                                           bool Relative) const {
34
59
  assert(Extension.size() && "The file extension may not be empty");
35
59
36
59
  SmallString<256> FullPath;
37
59
38
59
  if (!Relative)
39
40
    FullPath.append(Opts.ShowOutputDirectory);
40
59
41
59
  if (!InToplevel)
42
26
    sys::path::append(FullPath, getCoverageDir());
43
59
44
59
  SmallString<256> ParentPath = sys::path::parent_path(Path);
45
59
  sys::path::remove_dots(ParentPath, /*remove_dot_dots=*/true);
46
59
  sys::path::append(FullPath, sys::path::relative_path(ParentPath));
47
59
48
59
  auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
49
59
  sys::path::append(FullPath, PathFilename);
50
59
  sys::path::native(FullPath);
51
59
52
59
  return FullPath.str();
53
59
}
54
55
Expected<CoveragePrinter::OwnedStream>
56
CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
57
76
                                    bool InToplevel) const {
58
76
  if (!Opts.hasOutputDirectory())
59
36
    return OwnedStream(&outs());
60
40
61
40
  std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
62
40
63
40
  auto ParentDir = sys::path::parent_path(FullPath);
64
40
  if (auto E = sys::fs::create_directories(ParentDir))
65
0
    return errorCodeToError(E);
66
40
67
40
  std::error_code E;
68
40
  raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW);
69
40
  auto OS = CoveragePrinter::OwnedStream(RawStream);
70
40
  if (E)
71
1
    return errorCodeToError(E);
72
39
  return std::move(OS);
73
39
}
74
75
std::unique_ptr<CoveragePrinter>
76
59
CoveragePrinter::create(const CoverageViewOptions &Opts) {
77
59
  switch (Opts.Format) {
78
42
  case CoverageViewOptions::OutputFormat::Text:
79
42
    return llvm::make_unique<CoveragePrinterText>(Opts);
80
17
  case CoverageViewOptions::OutputFormat::HTML:
81
17
    return llvm::make_unique<CoveragePrinterHTML>(Opts);
82
0
  }
83
0
  
llvm_unreachable0
("Unknown coverage output format!");
84
0
}
85
86
58
unsigned SourceCoverageView::getFirstUncoveredLineNo() {
87
58
  const auto MinSegIt =
88
283
      find_if(CoverageInfo, [](const coverage::CoverageSegment &S) {
89
261
        return S.HasCount && S.Count == 0;
90
283
      });
91
58
92
58
  // There is no uncovered line, return zero.
93
58
  if (MinSegIt == CoverageInfo.end())
94
14
    return 0;
95
44
96
44
  return (*MinSegIt).Line;
97
44
}
98
99
796
std::string SourceCoverageView::formatCount(uint64_t N) {
100
796
  std::string Number = utostr(N);
101
796
  int Len = Number.size();
102
796
  if (Len <= 3)
103
757
    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
796
  Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
111
796
  return Result;
112
796
}
113
114
bool SourceCoverageView::shouldRenderRegionMarkers(
115
3.05k
    CoverageSegmentArray Segments) const {
116
3.05k
  if (!getOptions().ShowRegionMarkers)
117
2.86k
    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.48k
bool SourceCoverageView::hasSubViews() const {
129
1.48k
  return !ExpansionSubViews.empty() || !InstantiationSubViews.empty();
130
1.48k
}
131
132
std::unique_ptr<SourceCoverageView>
133
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
134
                           const CoverageViewOptions &Options,
135
73
                           coverage::CoverageData &&CoverageInfo) {
136
73
  switch (Options.Format) {
137
52
  case CoverageViewOptions::OutputFormat::Text:
138
52
    return llvm::make_unique<SourceCoverageViewText>(
139
52
        SourceName, File, Options, std::move(CoverageInfo));
140
21
  case CoverageViewOptions::OutputFormat::HTML:
141
21
    return llvm::make_unique<SourceCoverageViewHTML>(
142
21
        SourceName, File, Options, std::move(CoverageInfo));
143
0
  }
144
0
  
llvm_unreachable0
("Unknown coverage output format!");
145
0
}
146
147
54
std::string SourceCoverageView::getSourceName() const {
148
54
  SmallString<128> SourceText(SourceName);
149
54
  sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
150
54
  sys::path::native(SourceText);
151
54
  return SourceText.str();
152
54
}
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
73
                               bool ShowSourceName, unsigned ViewDepth) {
168
73
  if (
WholeFile && 73
getOptions().hasOutputDirectory()40
)
169
11
    renderTitle(OS, "Coverage Report");
170
73
171
73
  renderViewHeader(OS);
172
73
173
73
  if (ShowSourceName)
174
54
    renderSourceName(OS, WholeFile);
175
73
176
73
  renderTableHeader(OS, (ViewDepth > 0) ? 
015
:
getFirstUncoveredLineNo()58
,
177
73
                    ViewDepth);
178
73
179
73
  // We need the expansions and instantiations sorted so we can go through them
180
73
  // while we iterate lines.
181
73
  std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
182
73
  std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
183
73
  auto NextESV = ExpansionSubViews.begin();
184
73
  auto EndESV = ExpansionSubViews.end();
185
73
  auto NextISV = InstantiationSubViews.begin();
186
73
  auto EndISV = InstantiationSubViews.end();
187
73
188
73
  // Get the coverage information for the file.
189
73
  auto NextSegment = CoverageInfo.begin();
190
73
  auto EndSegment = CoverageInfo.end();
191
73
192
73
  unsigned FirstLine = NextSegment != EndSegment ? 
NextSegment->Line73
:
00
;
193
73
  const coverage::CoverageSegment *WrappedSegment = nullptr;
194
73
  SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
195
2.59k
  for (line_iterator LI(File, /*SkipBlanks=*/false); 
!LI.is_at_eof()2.59k
;
++LI2.52k
) {
196
2.55k
    // If we aren't rendering the whole file, we need to filter out the prologue
197
2.55k
    // and epilogue.
198
2.55k
    if (
!WholeFile2.55k
) {
199
452
      if (NextSegment == EndSegment)
200
33
        break;
201
419
      else 
if (419
LI.line_number() < FirstLine419
)
202
195
        continue;
203
2.32k
    }
204
2.32k
205
2.32k
    // Collect the coverage information relevant to this line.
206
2.32k
    
if (2.32k
LineSegments.size()2.32k
)
207
451
      WrappedSegment = LineSegments.back();
208
2.32k
    LineSegments.clear();
209
3.18k
    while (
NextSegment != EndSegment && 3.18k
NextSegment->Line == LI.line_number()1.98k
)
210
861
      LineSegments.push_back(&*NextSegment++);
211
2.32k
212
2.32k
    renderLinePrefix(OS, ViewDepth);
213
2.32k
    if (getOptions().ShowLineNumbers)
214
2.32k
      renderLineNumberColumn(OS, LI.line_number());
215
2.32k
216
2.32k
    LineCoverageStats LineCount{LineSegments, WrappedSegment};
217
2.32k
    if (getOptions().ShowLineStats)
218
2.27k
      renderLineCoverageColumn(OS, LineCount);
219
2.32k
220
2.32k
    // If there are expansion subviews, we want to highlight the first one.
221
2.32k
    unsigned ExpansionColumn = 0;
222
2.32k
    if (
NextESV != EndESV && 2.32k
NextESV->getLine() == LI.line_number()29
&&
223
3
        getOptions().Colors)
224
0
      ExpansionColumn = NextESV->getStartCol();
225
2.32k
226
2.32k
    // Display the source code for the current line.
227
2.32k
    renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
228
2.32k
               ExpansionColumn, ViewDepth);
229
2.32k
230
2.32k
    // Show the region markers.
231
2.32k
    if (shouldRenderRegionMarkers(LineSegments))
232
11
      renderRegionMarkers(OS, LineSegments, ViewDepth);
233
2.32k
234
2.32k
    // Show the expansions and instantiations for this line.
235
2.32k
    bool RenderedSubView = false;
236
2.32k
    for (; 
NextESV != EndESV && 2.32k
NextESV->getLine() == LI.line_number()30
;
237
2.32k
         
++NextESV3
) {
238
3
      renderViewDivider(OS, ViewDepth + 1);
239
3
240
3
      // Re-render the current line and highlight the expansion range for
241
3
      // this subview.
242
3
      if (
RenderedSubView3
) {
243
0
        ExpansionColumn = NextESV->getStartCol();
244
0
        renderExpansionSite(OS, {*LI, LI.line_number()}, WrappedSegment,
245
0
                            LineSegments, ExpansionColumn, ViewDepth);
246
0
        renderViewDivider(OS, ViewDepth + 1);
247
0
      }
248
3
249
3
      renderExpansionView(OS, *NextESV, ViewDepth + 1);
250
3
      RenderedSubView = true;
251
3
    }
252
2.34k
    for (; 
NextISV != EndISV && 2.34k
NextISV->Line == LI.line_number()134
;
++NextISV20
) {
253
20
      renderViewDivider(OS, ViewDepth + 1);
254
20
      renderInstantiationView(OS, *NextISV, ViewDepth + 1);
255
20
      RenderedSubView = true;
256
20
    }
257
2.32k
    if (RenderedSubView)
258
13
      renderViewDivider(OS, ViewDepth + 1);
259
2.55k
    renderLineSuffix(OS, ViewDepth);
260
2.55k
  }
261
73
262
73
  renderViewFooter(OS);
263
73
}