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