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