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