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