Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/CodeCoverage.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
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
// The 'CodeCoverageTool' class implements a command line tool to analyze and
11
// report coverage information using the profiling instrumentation and code
12
// coverage mapping.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "CoverageExporterJson.h"
17
#include "CoverageFilters.h"
18
#include "CoverageReport.h"
19
#include "CoverageSummaryInfo.h"
20
#include "CoverageViewOptions.h"
21
#include "RenderingSupport.h"
22
#include "SourceCoverageView.h"
23
#include "llvm/ADT/SmallString.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/ADT/Triple.h"
26
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
27
#include "llvm/ProfileData/InstrProfReader.h"
28
#include "llvm/Support/CommandLine.h"
29
#include "llvm/Support/FileSystem.h"
30
#include "llvm/Support/Format.h"
31
#include "llvm/Support/MemoryBuffer.h"
32
#include "llvm/Support/Path.h"
33
#include "llvm/Support/Process.h"
34
#include "llvm/Support/Program.h"
35
#include "llvm/Support/ScopedPrinter.h"
36
#include "llvm/Support/ThreadPool.h"
37
#include "llvm/Support/Threading.h"
38
#include "llvm/Support/ToolOutputFile.h"
39
40
#include <functional>
41
#include <map>
42
#include <system_error>
43
44
using namespace llvm;
45
using namespace coverage;
46
47
void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
48
                              const CoverageViewOptions &Options,
49
                              raw_ostream &OS);
50
51
namespace {
52
/// The implementation of the coverage tool.
53
class CodeCoverageTool {
54
public:
55
  enum Command {
56
    /// The show command.
57
    Show,
58
    /// The report command.
59
    Report,
60
    /// The export command.
61
    Export
62
  };
63
64
  int run(Command Cmd, int argc, const char **argv);
65
66
private:
67
  /// Print the error message to the error output stream.
68
  void error(const Twine &Message, StringRef Whence = "");
69
70
  /// Print the warning message to the error output stream.
71
  void warning(const Twine &Message, StringRef Whence = "");
72
73
  /// Convert \p Path into an absolute path and append it to the list
74
  /// of collected paths.
75
  void addCollectedPath(const std::string &Path);
76
77
  /// If \p Path is a regular file, collect the path. If it's a
78
  /// directory, recursively collect all of the paths within the directory.
79
  void collectPaths(const std::string &Path);
80
81
  /// Return a memory buffer for the given source file.
82
  ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
83
84
  /// Create source views for the expansions of the view.
85
  void attachExpansionSubViews(SourceCoverageView &View,
86
                               ArrayRef<ExpansionRecord> Expansions,
87
                               const CoverageMapping &Coverage);
88
89
  /// Create the source view of a particular function.
90
  std::unique_ptr<SourceCoverageView>
91
  createFunctionView(const FunctionRecord &Function,
92
                     const CoverageMapping &Coverage);
93
94
  /// Create the main source view of a particular source file.
95
  std::unique_ptr<SourceCoverageView>
96
  createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
97
98
  /// Load the coverage mapping data. Return nullptr if an error occurred.
99
  std::unique_ptr<CoverageMapping> load();
100
101
  /// Create a mapping from files in the Coverage data to local copies
102
  /// (path-equivalence).
103
  void remapPathNames(const CoverageMapping &Coverage);
104
105
  /// Remove input source files which aren't mapped by \p Coverage.
106
  void removeUnmappedInputs(const CoverageMapping &Coverage);
107
108
  /// If a demangler is available, demangle all symbol names.
109
  void demangleSymbols(const CoverageMapping &Coverage);
110
111
  /// Write out a source file view to the filesystem.
112
  void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
113
                           CoveragePrinter *Printer, bool ShowFilenames);
114
115
  typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
116
117
  int doShow(int argc, const char **argv,
118
             CommandLineParserType commandLineParser);
119
120
  int doReport(int argc, const char **argv,
121
               CommandLineParserType commandLineParser);
122
123
  int doExport(int argc, const char **argv,
124
               CommandLineParserType commandLineParser);
125
126
  std::vector<StringRef> ObjectFilenames;
127
  CoverageViewOptions ViewOpts;
128
  CoverageFiltersMatchAll Filters;
129
  CoverageFilters IgnoreFilenameFilters;
130
131
  /// The path to the indexed profile.
132
  std::string PGOFilename;
133
134
  /// A list of input source files.
135
  std::vector<std::string> SourceFiles;
136
137
  /// In -path-equivalence mode, this maps the absolute paths from the coverage
138
  /// mapping data to the input source files.
139
  StringMap<std::string> RemappedFilenames;
140
141
  /// The coverage data path to be remapped from, and the source path to be
142
  /// remapped to, when using -path-equivalence.
143
  Optional<std::pair<std::string, std::string>> PathRemapping;
144
145
  /// The architecture the coverage mapping data targets.
146
  std::vector<StringRef> CoverageArches;
147
148
  /// A cache for demangled symbols.
149
  DemangleCache DC;
150
151
  /// A lock which guards printing to stderr.
152
  std::mutex ErrsLock;
153
154
  /// A container for input source file buffers.
155
  std::mutex LoadedSourceFilesLock;
156
  std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
157
      LoadedSourceFiles;
158
159
  /// Whitelist from -name-whitelist to be used for filtering.
160
  std::unique_ptr<SpecialCaseList> NameWhitelist;
161
};
162
}
163
164
static std::string getErrorString(const Twine &Message, StringRef Whence,
165
37
                                  bool Warning) {
166
37
  std::string Str = (Warning ? 
"warning"15
:
"error"22
);
167
37
  Str += ": ";
168
37
  if (!Whence.empty())
169
17
    Str += Whence.str() + ": ";
170
37
  Str += Message.str() + "\n";
171
37
  return Str;
172
37
}
173
174
22
void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
175
22
  std::unique_lock<std::mutex> Guard{ErrsLock};
176
22
  ViewOpts.colored_ostream(errs(), raw_ostream::RED)
177
22
      << getErrorString(Message, Whence, false);
178
22
}
179
180
15
void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
181
15
  std::unique_lock<std::mutex> Guard{ErrsLock};
182
15
  ViewOpts.colored_ostream(errs(), raw_ostream::RED)
183
15
      << getErrorString(Message, Whence, true);
184
15
}
185
186
83
void CodeCoverageTool::addCollectedPath(const std::string &Path) {
187
83
  SmallString<128> EffectivePath(Path);
188
83
  if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
189
0
    error(EC.message(), Path);
190
0
    return;
191
0
  }
192
83
  sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
193
83
  if (!IgnoreFilenameFilters.matchesFilename(EffectivePath))
194
81
    SourceFiles.emplace_back(EffectivePath.str());
195
83
}
196
197
81
void CodeCoverageTool::collectPaths(const std::string &Path) {
198
81
  llvm::sys::fs::file_status Status;
199
81
  llvm::sys::fs::status(Path, Status);
200
81
  if (!llvm::sys::fs::exists(Status)) {
201
7
    if (PathRemapping)
202
7
      addCollectedPath(Path);
203
0
    else
204
0
      warning("Source file doesn't exist, proceeded by ignoring it.", Path);
205
7
    return;
206
7
  }
207
74
208
74
  if (llvm::sys::fs::is_regular_file(Status)) {
209
64
    addCollectedPath(Path);
210
64
    return;
211
64
  }
212
10
213
10
  if (llvm::sys::fs::is_directory(Status)) {
214
6
    std::error_code EC;
215
6
    for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
216
22
         F != E; 
F.increment(EC)16
) {
217
16
218
16
      if (EC) {
219
0
        warning(EC.message(), F->path());
220
0
        continue;
221
0
      }
222
16
223
16
      if (llvm::sys::fs::is_regular_file(F->path()))
224
12
        addCollectedPath(F->path());
225
16
    }
226
6
  }
227
10
}
228
229
ErrorOr<const MemoryBuffer &>
230
146
CodeCoverageTool::getSourceFile(StringRef SourceFile) {
231
146
  // If we've remapped filenames, look up the real location for this file.
232
146
  std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
233
146
  if (!RemappedFilenames.empty()) {
234
142
    auto Loc = RemappedFilenames.find(SourceFile);
235
142
    if (Loc != RemappedFilenames.end())
236
142
      SourceFile = Loc->second;
237
142
  }
238
146
  for (const auto &Files : LoadedSourceFiles)
239
188
    if (sys::fs::equivalent(SourceFile, Files.first))
240
10
      return *Files.second;
241
146
  auto Buffer = MemoryBuffer::getFile(SourceFile);
242
136
  if (auto EC = Buffer.getError()) {
243
14
    error(EC.message(), SourceFile);
244
14
    return EC;
245
14
  }
246
122
  LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
247
122
  return *LoadedSourceFiles.back().second;
248
122
}
249
250
void CodeCoverageTool::attachExpansionSubViews(
251
    SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
252
184
    const CoverageMapping &Coverage) {
253
184
  if (!ViewOpts.ShowExpandedRegions)
254
180
    return;
255
4
  for (const auto &Expansion : Expansions) {
256
3
    auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
257
3
    if (ExpansionCoverage.empty())
258
0
      continue;
259
3
    auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
260
3
    if (!SourceBuffer)
261
0
      continue;
262
3
263
3
    auto SubViewExpansions = ExpansionCoverage.getExpansions();
264
3
    auto SubView =
265
3
        SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
266
3
                                   ViewOpts, std::move(ExpansionCoverage));
267
3
    attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
268
3
    View.addExpansion(Expansion.Region, std::move(SubView));
269
3
  }
270
4
}
271
272
std::unique_ptr<SourceCoverageView>
273
CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
274
30
                                     const CoverageMapping &Coverage) {
275
30
  auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
276
30
  if (FunctionCoverage.empty())
277
0
    return nullptr;
278
30
  auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
279
30
  if (!SourceBuffer)
280
3
    return nullptr;
281
27
282
27
  auto Expansions = FunctionCoverage.getExpansions();
283
27
  auto View = SourceCoverageView::create(DC.demangle(Function.Name),
284
27
                                         SourceBuffer.get(), ViewOpts,
285
27
                                         std::move(FunctionCoverage));
286
27
  attachExpansionSubViews(*View, Expansions, Coverage);
287
27
288
27
  return View;
289
27
}
290
291
std::unique_ptr<SourceCoverageView>
292
CodeCoverageTool::createSourceFileView(StringRef SourceFile,
293
113
                                       const CoverageMapping &Coverage) {
294
113
  auto SourceBuffer = getSourceFile(SourceFile);
295
113
  if (!SourceBuffer)
296
11
    return nullptr;
297
102
  auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
298
102
  if (FileCoverage.empty())
299
0
    return nullptr;
300
102
301
102
  auto Expansions = FileCoverage.getExpansions();
302
102
  auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
303
102
                                         ViewOpts, std::move(FileCoverage));
304
102
  attachExpansionSubViews(*View, Expansions, Coverage);
305
102
  if (!ViewOpts.ShowFunctionInstantiations)
306
1
    return View;
307
101
308
124
  
for (const auto &Group : Coverage.getInstantiationGroups(SourceFile))101
{
309
124
    // Skip functions which have a single instantiation.
310
124
    if (Group.size() < 2)
311
98
      continue;
312
26
313
60
    
for (const FunctionRecord *Function : Group.getInstantiations())26
{
314
60
      std::unique_ptr<SourceCoverageView> SubView{nullptr};
315
60
316
60
      StringRef Funcname = DC.demangle(Function->Name);
317
60
318
60
      if (Function->ExecutionCount > 0) {
319
52
        auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
320
52
        auto SubViewExpansions = SubViewCoverage.getExpansions();
321
52
        SubView = SourceCoverageView::create(
322
52
            Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
323
52
        attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
324
52
      }
325
60
326
60
      unsigned FileID = Function->CountedRegions.front().FileID;
327
60
      unsigned Line = 0;
328
60
      for (const auto &CR : Function->CountedRegions)
329
446
        if (CR.FileID == FileID)
330
446
          Line = std::max(CR.LineEnd, Line);
331
60
      View->addInstantiation(Funcname, Line, std::move(SubView));
332
60
    }
333
26
  }
334
101
  return View;
335
101
}
336
337
114
static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
338
114
  sys::fs::file_status Status;
339
114
  if (sys::fs::status(LHS, Status))
340
0
    return false;
341
114
  auto LHSTime = Status.getLastModificationTime();
342
114
  if (sys::fs::status(RHS, Status))
343
0
    return false;
344
114
  auto RHSTime = Status.getLastModificationTime();
345
114
  return LHSTime > RHSTime;
346
114
}
347
348
111
std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
349
111
  for (StringRef ObjectFilename : ObjectFilenames)
350
114
    if (modifiedTimeGT(ObjectFilename, PGOFilename))
351
0
      warning("profile data may be out of date - object is newer",
352
0
              ObjectFilename);
353
111
  auto CoverageOrErr =
354
111
      CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches);
355
111
  if (Error E = CoverageOrErr.takeError()) {
356
2
    error("Failed to load coverage: " + toString(std::move(E)),
357
2
          join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
358
2
    return nullptr;
359
2
  }
360
109
  auto Coverage = std::move(CoverageOrErr.get());
361
109
  unsigned Mismatched = Coverage->getMismatchedCount();
362
109
  if (Mismatched) {
363
1
    warning(Twine(Mismatched) + " functions have mismatched data");
364
1
365
1
    if (ViewOpts.Debug) {
366
1
      for (const auto &HashMismatch : Coverage->getHashMismatches())
367
1
        errs() << "hash-mismatch: "
368
1
               << "No profile record found for '" << HashMismatch.first << "'"
369
1
               << " with hash = 0x" << Twine::utohexstr(HashMismatch.second)
370
1
               << '\n';
371
1
372
1
      for (const auto &CounterMismatch : Coverage->getCounterMismatches())
373
0
        errs() << "counter-mismatch: "
374
0
               << "Coverage mapping for " << CounterMismatch.first
375
0
               << " only has " << CounterMismatch.second
376
0
               << " valid counter expressions\n";
377
1
    }
378
1
  }
379
109
380
109
  remapPathNames(*Coverage);
381
109
382
109
  if (!SourceFiles.empty())
383
61
    removeUnmappedInputs(*Coverage);
384
109
385
109
  demangleSymbols(*Coverage);
386
109
387
109
  return Coverage;
388
109
}
389
390
109
void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
391
109
  if (!PathRemapping)
392
21
    return;
393
88
394
88
  // Convert remapping paths to native paths with trailing seperators.
395
176
  
auto nativeWithTrailing = [](StringRef Path) -> std::string 88
{
396
176
    if (Path.empty())
397
0
      return "";
398
176
    SmallString<128> NativePath;
399
176
    sys::path::native(Path, NativePath);
400
176
    if (!sys::path::is_separator(NativePath.back()))
401
173
      NativePath += sys::path::get_separator();
402
176
    return NativePath.c_str();
403
176
  };
404
88
  std::string RemapFrom = nativeWithTrailing(PathRemapping->first);
405
88
  std::string RemapTo = nativeWithTrailing(PathRemapping->second);
406
88
407
88
  // Create a mapping from coverage data file paths to local paths.
408
201
  for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
409
201
    SmallString<128> NativeFilename;
410
201
    sys::path::native(Filename, NativeFilename);
411
201
    if (NativeFilename.startswith(RemapFrom)) {
412
200
      RemappedFilenames[Filename] =
413
200
          RemapTo + NativeFilename.substr(RemapFrom.size()).str();
414
200
    }
415
201
  }
416
88
417
88
  // Convert input files from local paths to coverage data file paths.
418
88
  StringMap<std::string> InvRemappedFilenames;
419
88
  for (const auto &RemappedFilename : RemappedFilenames)
420
200
    InvRemappedFilenames[RemappedFilename.getValue()] = RemappedFilename.getKey();
421
88
422
88
  for (std::string &Filename : SourceFiles) {
423
68
    SmallString<128> NativeFilename;
424
68
    sys::path::native(Filename, NativeFilename);
425
68
    auto CovFileName = InvRemappedFilenames.find(NativeFilename);
426
68
    if (CovFileName != InvRemappedFilenames.end())
427
63
      Filename = CovFileName->second;
428
68
  }
429
88
}
430
431
61
void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
432
61
  std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
433
61
434
61
  auto UncoveredFilesIt = SourceFiles.end();
435
61
  // The user may have specified source files which aren't in the coverage
436
61
  // mapping. Filter these files away.
437
61
  UncoveredFilesIt = std::remove_if(
438
68
      SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
439
68
        return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
440
68
                                   SF);
441
68
      });
442
61
443
61
  SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
444
61
}
445
446
109
void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
447
109
  if (!ViewOpts.hasDemangler())
448
106
    return;
449
3
450
3
  // Pass function names to the demangler in a temporary file.
451
3
  int InputFD;
452
3
  SmallString<256> InputPath;
453
3
  std::error_code EC =
454
3
      sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
455
3
  if (EC) {
456
0
    error(InputPath, EC.message());
457
0
    return;
458
0
  }
459
3
  ToolOutputFile InputTOF{InputPath, InputFD};
460
3
461
3
  unsigned NumSymbols = 0;
462
9
  for (const auto &Function : Coverage.getCoveredFunctions()) {
463
9
    InputTOF.os() << Function.Name << '\n';
464
9
    ++NumSymbols;
465
9
  }
466
3
  InputTOF.os().close();
467
3
468
3
  // Use another temporary file to store the demangler's output.
469
3
  int OutputFD;
470
3
  SmallString<256> OutputPath;
471
3
  EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
472
3
                                    OutputPath);
473
3
  if (EC) {
474
0
    error(OutputPath, EC.message());
475
0
    return;
476
0
  }
477
3
  ToolOutputFile OutputTOF{OutputPath, OutputFD};
478
3
  OutputTOF.os().close();
479
3
480
3
  // Invoke the demangler.
481
3
  std::vector<StringRef> ArgsV;
482
3
  for (StringRef Arg : ViewOpts.DemanglerOpts)
483
6
    ArgsV.push_back(Arg);
484
3
  Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}};
485
3
  std::string ErrMsg;
486
3
  int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV,
487
3
                               /*env=*/None, Redirects, /*secondsToWait=*/0,
488
3
                               /*memoryLimit=*/0, &ErrMsg);
489
3
  if (RC) {
490
0
    error(ErrMsg, ViewOpts.DemanglerOpts[0]);
491
0
    return;
492
0
  }
493
3
494
3
  // Parse the demangler's output.
495
3
  auto BufOrError = MemoryBuffer::getFile(OutputPath);
496
3
  if (!BufOrError) {
497
0
    error(OutputPath, BufOrError.getError().message());
498
0
    return;
499
0
  }
500
3
501
3
  std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
502
3
503
3
  SmallVector<StringRef, 8> Symbols;
504
3
  StringRef DemanglerData = DemanglerBuf->getBuffer();
505
3
  DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
506
3
                      /*KeepEmpty=*/false);
507
3
  if (Symbols.size() != NumSymbols) {
508
0
    error("Demangler did not provide expected number of symbols");
509
0
    return;
510
0
  }
511
3
512
3
  // Cache the demangled names.
513
3
  unsigned I = 0;
514
3
  for (const auto &Function : Coverage.getCoveredFunctions())
515
9
    // On Windows, lines in the demangler's output file end with "\r\n".
516
9
    // Splitting by '\n' keeps '\r's, so cut them now.
517
9
    DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
518
3
}
519
520
void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
521
                                           CoverageMapping *Coverage,
522
                                           CoveragePrinter *Printer,
523
113
                                           bool ShowFilenames) {
524
113
  auto View = createSourceFileView(SourceFile, *Coverage);
525
113
  if (!View) {
526
11
    warning("The file '" + SourceFile + "' isn't covered.");
527
11
    return;
528
11
  }
529
102
530
102
  auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
531
102
  if (Error E = OSOrErr.takeError()) {
532
0
    error("Could not create view file!", toString(std::move(E)));
533
0
    return;
534
0
  }
535
102
  auto OS = std::move(OSOrErr.get());
536
102
537
102
  View->print(*OS.get(), /*Wholefile=*/true,
538
102
              /*ShowSourceName=*/ShowFilenames,
539
102
              /*ShowTitle=*/ViewOpts.hasOutputDirectory());
540
102
  Printer->closeViewFile(std::move(OS));
541
102
}
542
543
119
int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
544
119
  cl::opt<std::string> CovFilename(
545
119
      cl::Positional, cl::desc("Covered executable or object file."));
546
119
547
119
  cl::list<std::string> CovFilenames(
548
119
      "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
549
119
      cl::CommaSeparated);
550
119
551
119
  cl::list<std::string> InputSourceFiles(
552
119
      cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
553
119
554
119
  cl::opt<bool> DebugDumpCollectedPaths(
555
119
      "dump-collected-paths", cl::Optional, cl::Hidden,
556
119
      cl::desc("Show the collected paths to source files"));
557
119
558
119
  cl::opt<std::string, true> PGOFilename(
559
119
      "instr-profile", cl::Required, cl::location(this->PGOFilename),
560
119
      cl::desc(
561
119
          "File with the profile data obtained after an instrumented run"));
562
119
563
119
  cl::list<std::string> Arches(
564
119
      "arch", cl::desc("architectures of the coverage mapping binaries"));
565
119
566
119
  cl::opt<bool> DebugDump("dump", cl::Optional,
567
119
                          cl::desc("Show internal debug dump"));
568
119
569
119
  cl::opt<CoverageViewOptions::OutputFormat> Format(
570
119
      "format", cl::desc("Output format for line-based coverage reports"),
571
119
      cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
572
119
                            "Text output"),
573
119
                 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
574
119
                            "HTML output")),
575
119
      cl::init(CoverageViewOptions::OutputFormat::Text));
576
119
577
119
  cl::opt<std::string> PathRemap(
578
119
      "path-equivalence", cl::Optional,
579
119
      cl::desc("<from>,<to> Map coverage data paths to local source file "
580
119
               "paths"));
581
119
582
119
  cl::OptionCategory FilteringCategory("Function filtering options");
583
119
584
119
  cl::list<std::string> NameFilters(
585
119
      "name", cl::Optional,
586
119
      cl::desc("Show code coverage only for functions with the given name"),
587
119
      cl::ZeroOrMore, cl::cat(FilteringCategory));
588
119
589
119
  cl::list<std::string> NameFilterFiles(
590
119
      "name-whitelist", cl::Optional,
591
119
      cl::desc("Show code coverage only for functions listed in the given "
592
119
               "file"),
593
119
      cl::ZeroOrMore, cl::cat(FilteringCategory));
594
119
595
119
  cl::list<std::string> NameRegexFilters(
596
119
      "name-regex", cl::Optional,
597
119
      cl::desc("Show code coverage only for functions that match the given "
598
119
               "regular expression"),
599
119
      cl::ZeroOrMore, cl::cat(FilteringCategory));
600
119
601
119
  cl::list<std::string> IgnoreFilenameRegexFilters(
602
119
      "ignore-filename-regex", cl::Optional,
603
119
      cl::desc("Skip source code files with file paths that match the given "
604
119
               "regular expression"),
605
119
      cl::ZeroOrMore, cl::cat(FilteringCategory));
606
119
607
119
  cl::opt<double> RegionCoverageLtFilter(
608
119
      "region-coverage-lt", cl::Optional,
609
119
      cl::desc("Show code coverage only for functions with region coverage "
610
119
               "less than the given threshold"),
611
119
      cl::cat(FilteringCategory));
612
119
613
119
  cl::opt<double> RegionCoverageGtFilter(
614
119
      "region-coverage-gt", cl::Optional,
615
119
      cl::desc("Show code coverage only for functions with region coverage "
616
119
               "greater than the given threshold"),
617
119
      cl::cat(FilteringCategory));
618
119
619
119
  cl::opt<double> LineCoverageLtFilter(
620
119
      "line-coverage-lt", cl::Optional,
621
119
      cl::desc("Show code coverage only for functions with line coverage less "
622
119
               "than the given threshold"),
623
119
      cl::cat(FilteringCategory));
624
119
625
119
  cl::opt<double> LineCoverageGtFilter(
626
119
      "line-coverage-gt", cl::Optional,
627
119
      cl::desc("Show code coverage only for functions with line coverage "
628
119
               "greater than the given threshold"),
629
119
      cl::cat(FilteringCategory));
630
119
631
119
  cl::opt<cl::boolOrDefault> UseColor(
632
119
      "use-color", cl::desc("Emit colored output (default=autodetect)"),
633
119
      cl::init(cl::BOU_UNSET));
634
119
635
119
  cl::list<std::string> DemanglerOpts(
636
119
      "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
637
119
638
119
  cl::opt<bool> RegionSummary(
639
119
      "show-region-summary", cl::Optional,
640
119
      cl::desc("Show region statistics in summary table"),
641
119
      cl::init(true));
642
119
643
119
  cl::opt<bool> InstantiationSummary(
644
119
      "show-instantiation-summary", cl::Optional,
645
119
      cl::desc("Show instantiation statistics in summary table"));
646
119
647
119
  cl::opt<bool> SummaryOnly(
648
119
      "summary-only", cl::Optional,
649
119
      cl::desc("Export only summary information for each source file"));
650
119
651
119
  cl::opt<unsigned> NumThreads(
652
119
      "num-threads", cl::init(0),
653
119
      cl::desc("Number of merge threads to use (default: autodetect)"));
654
119
  cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
655
119
                        cl::aliasopt(NumThreads));
656
119
657
119
  auto commandLineParser = [&, this](int argc, const char **argv) -> int {
658
119
    cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
659
119
    ViewOpts.Debug = DebugDump;
660
119
661
119
    if (!CovFilename.empty())
662
117
      ObjectFilenames.emplace_back(CovFilename);
663
119
    for (const std::string &Filename : CovFilenames)
664
4
      ObjectFilenames.emplace_back(Filename);
665
119
    if (ObjectFilenames.empty()) {
666
1
      errs() << "No filenames specified!\n";
667
1
      ::exit(1);
668
1
    }
669
118
670
118
    ViewOpts.Format = Format;
671
118
    switch (ViewOpts.Format) {
672
118
    case CoverageViewOptions::OutputFormat::Text:
673
92
      ViewOpts.Colors = UseColor == cl::BOU_UNSET
674
92
                            ? sys::Process::StandardOutHasColors()
675
92
                            : 
UseColor == cl::BOU_TRUE0
;
676
92
      break;
677
118
    case CoverageViewOptions::OutputFormat::HTML:
678
26
      if (UseColor == cl::BOU_FALSE)
679
0
        errs() << "Color output cannot be disabled when generating html.\n";
680
26
      ViewOpts.Colors = true;
681
26
      break;
682
118
    }
683
118
684
118
    // If path-equivalence was given and is a comma seperated pair then set
685
118
    // PathRemapping.
686
118
    auto EquivPair = StringRef(PathRemap).split(',');
687
118
    if (!(EquivPair.first.empty() && 
EquivPair.second.empty()26
))
688
92
      PathRemapping = EquivPair;
689
118
690
118
    // If a demangler is supplied, check if it exists and register it.
691
118
    if (DemanglerOpts.size()) {
692
3
      auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
693
3
      if (!DemanglerPathOrErr) {
694
0
        error("Could not find the demangler!",
695
0
              DemanglerPathOrErr.getError().message());
696
0
        return 1;
697
0
      }
698
3
      DemanglerOpts[0] = *DemanglerPathOrErr;
699
3
      ViewOpts.DemanglerOpts.swap(DemanglerOpts);
700
3
    }
701
118
702
118
    // Read in -name-whitelist files.
703
118
    if (!NameFilterFiles.empty()) {
704
2
      std::string SpecialCaseListErr;
705
2
      NameWhitelist =
706
2
          SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr);
707
2
      if (!NameWhitelist)
708
0
        error(SpecialCaseListErr);
709
2
    }
710
118
711
118
    // Create the function filters
712
118
    if (!NameFilters.empty() || 
NameWhitelist101
||
!NameRegexFilters.empty()99
) {
713
21
      auto NameFilterer = llvm::make_unique<CoverageFilters>();
714
21
      for (const auto &Name : NameFilters)
715
21
        NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
716
21
      if (NameWhitelist)
717
2
        NameFilterer->push_back(
718
2
            llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
719
21
      for (const auto &Regex : NameRegexFilters)
720
2
        NameFilterer->push_back(
721
2
            llvm::make_unique<NameRegexCoverageFilter>(Regex));
722
21
      Filters.push_back(std::move(NameFilterer));
723
21
    }
724
118
725
118
    if (RegionCoverageLtFilter.getNumOccurrences() ||
726
118
        RegionCoverageGtFilter.getNumOccurrences() ||
727
118
        LineCoverageLtFilter.getNumOccurrences() ||
728
118
        LineCoverageGtFilter.getNumOccurrences()) {
729
0
      auto StatFilterer = llvm::make_unique<CoverageFilters>();
730
0
      if (RegionCoverageLtFilter.getNumOccurrences())
731
0
        StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
732
0
            RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
733
0
      if (RegionCoverageGtFilter.getNumOccurrences())
734
0
        StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
735
0
            RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
736
0
      if (LineCoverageLtFilter.getNumOccurrences())
737
0
        StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
738
0
            LineCoverageFilter::LessThan, LineCoverageLtFilter));
739
0
      if (LineCoverageGtFilter.getNumOccurrences())
740
0
        StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
741
0
            RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
742
0
      Filters.push_back(std::move(StatFilterer));
743
0
    }
744
118
745
118
    // Create the ignore filename filters.
746
118
    for (const auto &RE : IgnoreFilenameRegexFilters)
747
5
      IgnoreFilenameFilters.push_back(
748
5
          llvm::make_unique<NameRegexCoverageFilter>(RE));
749
118
750
118
    if (!Arches.empty()) {
751
8
      for (const std::string &Arch : Arches) {
752
8
        if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
753
1
          error("Unknown architecture: " + Arch);
754
1
          return 1;
755
1
        }
756
7
        CoverageArches.emplace_back(Arch);
757
7
      }
758
6
      
if (5
CoverageArches.size() != ObjectFilenames.size()5
) {
759
1
        error("Number of architectures doesn't match the number of objects");
760
1
        return 1;
761
1
      }
762
116
    }
763
116
764
116
    // IgnoreFilenameFilters are applied even when InputSourceFiles specified.
765
116
    for (const std::string &File : InputSourceFiles)
766
81
      collectPaths(File);
767
116
768
116
    if (DebugDumpCollectedPaths) {
769
3
      for (const std::string &SF : SourceFiles)
770
12
        outs() << SF << '\n';
771
3
      ::exit(0);
772
3
    }
773
113
774
113
    ViewOpts.ShowRegionSummary = RegionSummary;
775
113
    ViewOpts.ShowInstantiationSummary = InstantiationSummary;
776
113
    ViewOpts.ExportSummaryOnly = SummaryOnly;
777
113
    ViewOpts.NumThreads = NumThreads;
778
113
779
113
    return 0;
780
113
  };
781
119
782
119
  switch (Cmd) {
783
119
  case Show:
784
82
    return doShow(argc, argv, commandLineParser);
785
119
  case Report:
786
23
    return doReport(argc, argv, commandLineParser);
787
119
  case Export:
788
14
    return doExport(argc, argv, commandLineParser);
789
0
  }
790
0
  return 0;
791
0
}
792
793
int CodeCoverageTool::doShow(int argc, const char **argv,
794
82
                             CommandLineParserType commandLineParser) {
795
82
796
82
  cl::OptionCategory ViewCategory("Viewing options");
797
82
798
82
  cl::opt<bool> ShowLineExecutionCounts(
799
82
      "show-line-counts", cl::Optional,
800
82
      cl::desc("Show the execution counts for each line"), cl::init(true),
801
82
      cl::cat(ViewCategory));
802
82
803
82
  cl::opt<bool> ShowRegions(
804
82
      "show-regions", cl::Optional,
805
82
      cl::desc("Show the execution counts for each region"),
806
82
      cl::cat(ViewCategory));
807
82
808
82
  cl::opt<bool> ShowBestLineRegionsCounts(
809
82
      "show-line-counts-or-regions", cl::Optional,
810
82
      cl::desc("Show the execution counts for each line, or the execution "
811
82
               "counts for each region on lines that have multiple regions"),
812
82
      cl::cat(ViewCategory));
813
82
814
82
  cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
815
82
                               cl::desc("Show expanded source regions"),
816
82
                               cl::cat(ViewCategory));
817
82
818
82
  cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
819
82
                                   cl::desc("Show function instantiations"),
820
82
                                   cl::init(true), cl::cat(ViewCategory));
821
82
822
82
  cl::opt<std::string> ShowOutputDirectory(
823
82
      "output-dir", cl::init(""),
824
82
      cl::desc("Directory in which coverage information is written out"));
825
82
  cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
826
82
                                 cl::aliasopt(ShowOutputDirectory));
827
82
828
82
  cl::opt<uint32_t> TabSize(
829
82
      "tab-size", cl::init(2),
830
82
      cl::desc(
831
82
          "Set tab expansion size for html coverage reports (default = 2)"));
832
82
833
82
  cl::opt<std::string> ProjectTitle(
834
82
      "project-title", cl::Optional,
835
82
      cl::desc("Set project title for the coverage report"));
836
82
837
82
  auto Err = commandLineParser(argc, argv);
838
82
  if (Err)
839
2
    return Err;
840
80
841
80
  ViewOpts.ShowLineNumbers = true;
842
80
  ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
843
80
                           
!ShowRegions77
||
ShowBestLineRegionsCounts2
;
844
80
  ViewOpts.ShowRegionMarkers = ShowRegions || 
ShowBestLineRegionsCounts75
;
845
80
  ViewOpts.ShowExpandedRegions = ShowExpansions;
846
80
  ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
847
80
  ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
848
80
  ViewOpts.TabSize = TabSize;
849
80
  ViewOpts.ProjectTitle = ProjectTitle;
850
80
851
80
  if (ViewOpts.hasOutputDirectory()) {
852
31
    if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
853
0
      error("Could not create output directory!", E.message());
854
0
      return 1;
855
0
    }
856
80
  }
857
80
858
80
  sys::fs::file_status Status;
859
80
  if (sys::fs::status(PGOFilename, Status)) {
860
0
    error("profdata file error: can not get the file status. \n");
861
0
    return 1;
862
0
  }
863
80
864
80
  auto ModifiedTime = Status.getLastModificationTime();
865
80
  std::string ModifiedTimeStr = to_string(ModifiedTime);
866
80
  size_t found = ModifiedTimeStr.rfind(':');
867
80
  ViewOpts.CreatedTimeStr = (found != std::string::npos)
868
80
                                ? 
"Created: " + ModifiedTimeStr.substr(0, found)77
869
80
                                : 
"Created: " + ModifiedTimeStr3
;
870
80
871
80
  auto Coverage = load();
872
80
  if (!Coverage)
873
1
    return 1;
874
79
875
79
  auto Printer = CoveragePrinter::create(ViewOpts);
876
79
877
79
  if (SourceFiles.empty())
878
26
    // Get the source files from the function coverage mapping.
879
89
    
for (StringRef Filename : Coverage->getUniqueSourceFiles())26
{
880
89
      if (!IgnoreFilenameFilters.matchesFilename(Filename))
881
88
        SourceFiles.push_back(Filename);
882
89
    }
883
79
884
79
  // Create an index out of the source files.
885
79
  if (ViewOpts.hasOutputDirectory()) {
886
31
    if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
887
1
      error("Could not create index file!", toString(std::move(E)));
888
1
      return 1;
889
1
    }
890
78
  }
891
78
892
78
  if (!Filters.empty()) {
893
20
    // Build the map of filenames to functions.
894
20
    std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
895
20
        FilenameFunctionMap;
896
20
    for (const auto &SourceFile : SourceFiles)
897
26
      for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
898
57
        if (Filters.matches(*Coverage.get(), Function))
899
30
          FilenameFunctionMap[SourceFile].push_back(&Function);
900
20
901
20
    // Only print filter matching functions for each file.
902
21
    for (const auto &FileFunc : FilenameFunctionMap) {
903
21
      StringRef File = FileFunc.first;
904
21
      const auto &Functions = FileFunc.second;
905
21
906
21
      auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
907
21
      if (Error E = OSOrErr.takeError()) {
908
0
        error("Could not create view file!", toString(std::move(E)));
909
0
        return 1;
910
0
      }
911
21
      auto OS = std::move(OSOrErr.get());
912
21
913
21
      bool ShowTitle = ViewOpts.hasOutputDirectory();
914
30
      for (const auto *Function : Functions) {
915
30
        auto FunctionView = createFunctionView(*Function, *Coverage);
916
30
        if (!FunctionView) {
917
3
          warning("Could not read coverage for '" + Function->Name + "'.");
918
3
          continue;
919
3
        }
920
27
        FunctionView->print(*OS.get(), /*WholeFile=*/false,
921
27
                            /*ShowSourceName=*/true, ShowTitle);
922
27
        ShowTitle = false;
923
27
      }
924
21
925
21
      Printer->closeViewFile(std::move(OS));
926
21
    }
927
20
    return 0;
928
58
  }
929
58
930
58
  // Show files
931
58
  bool ShowFilenames =
932
58
      (SourceFiles.size() != 1) || 
ViewOpts.hasOutputDirectory()39
||
933
58
      
(ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)26
;
934
58
935
58
  auto NumThreads = ViewOpts.NumThreads;
936
58
937
58
  // If NumThreads is not specified, auto-detect a good default.
938
58
  if (NumThreads == 0)
939
44
    NumThreads =
940
44
        std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
941
44
                              unsigned(SourceFiles.size())));
942
58
943
58
  if (!ViewOpts.hasOutputDirectory() || 
NumThreads == 118
) {
944
52
    for (const std::string &SourceFile : SourceFiles)
945
98
      writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
946
98
                          ShowFilenames);
947
52
  } else {
948
6
    // In -output-dir mode, it's safe to use multiple threads to print files.
949
6
    ThreadPool Pool(NumThreads);
950
6
    for (const std::string &SourceFile : SourceFiles)
951
15
      Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
952
15
                 Coverage.get(), Printer.get(), ShowFilenames);
953
6
    Pool.wait();
954
6
  }
955
58
956
58
  return 0;
957
58
}
958
959
int CodeCoverageTool::doReport(int argc, const char **argv,
960
23
                               CommandLineParserType commandLineParser) {
961
23
  cl::opt<bool> ShowFunctionSummaries(
962
23
      "show-functions", cl::Optional, cl::init(false),
963
23
      cl::desc("Show coverage summaries for each function"));
964
23
965
23
  auto Err = commandLineParser(argc, argv);
966
23
  if (Err)
967
0
    return Err;
968
23
969
23
  if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
970
1
    error("HTML output for summary reports is not yet supported.");
971
1
    return 1;
972
1
  }
973
22
974
22
  auto Coverage = load();
975
22
  if (!Coverage)
976
1
    return 1;
977
21
978
21
  CoverageReport Report(ViewOpts, *Coverage.get());
979
21
  if (!ShowFunctionSummaries) {
980
16
    if (SourceFiles.empty())
981
13
      Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters);
982
3
    else
983
3
      Report.renderFileReports(llvm::outs(), SourceFiles);
984
16
  } else {
985
5
    if (SourceFiles.empty()) {
986
1
      error("Source files must be specified when -show-functions=true is "
987
1
            "specified");
988
1
      return 1;
989
1
    }
990
4
991
4
    Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
992
4
  }
993
21
  
return 020
;
994
21
}
995
996
int CodeCoverageTool::doExport(int argc, const char **argv,
997
14
                               CommandLineParserType commandLineParser) {
998
14
999
14
  auto Err = commandLineParser(argc, argv);
1000
14
  if (Err)
1001
0
    return Err;
1002
14
1003
14
  if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
1004
1
    error("Coverage data can only be exported as textual JSON.");
1005
1
    return 1;
1006
1
  }
1007
13
1008
13
  auto Coverage = load();
1009
13
  if (!Coverage) {
1010
0
    error("Could not load coverage information");
1011
0
    return 1;
1012
0
  }
1013
13
1014
13
  auto Exporter = CoverageExporterJson(*Coverage.get(), ViewOpts, outs());
1015
13
1016
13
  if (SourceFiles.empty())
1017
12
    Exporter.renderRoot(IgnoreFilenameFilters);
1018
1
  else
1019
1
    Exporter.renderRoot(SourceFiles);
1020
13
1021
13
  return 0;
1022
13
}
1023
1024
82
int showMain(int argc, const char *argv[]) {
1025
82
  CodeCoverageTool Tool;
1026
82
  return Tool.run(CodeCoverageTool::Show, argc, argv);
1027
82
}
1028
1029
23
int reportMain(int argc, const char *argv[]) {
1030
23
  CodeCoverageTool Tool;
1031
23
  return Tool.run(CodeCoverageTool::Report, argc, argv);
1032
23
}
1033
1034
14
int exportMain(int argc, const char *argv[]) {
1035
14
  CodeCoverageTool Tool;
1036
14
  return Tool.run(CodeCoverageTool::Export, argc, argv);
1037
14
}