xref: /llvm-project/llvm/tools/llvm-cov/CodeCoverage.cpp (revision 6e28bcdc27b222077085e2afb8c30f47e455e790)
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     DC.DemangledNames[Function.Name] = Symbols[I++];
455 }
456 
457 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
458                                            CoverageMapping *Coverage,
459                                            CoveragePrinter *Printer,
460                                            bool ShowFilenames) {
461   auto View = createSourceFileView(SourceFile, *Coverage);
462   if (!View) {
463     warning("The file '" + SourceFile + "' isn't covered.");
464     return;
465   }
466 
467   auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
468   if (Error E = OSOrErr.takeError()) {
469     error("Could not create view file!", toString(std::move(E)));
470     return;
471   }
472   auto OS = std::move(OSOrErr.get());
473 
474   View->print(*OS.get(), /*Wholefile=*/true,
475               /*ShowSourceName=*/ShowFilenames);
476   Printer->closeViewFile(std::move(OS));
477 }
478 
479 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
480   cl::opt<std::string> CovFilename(
481       cl::Positional, cl::desc("Covered executable or object file."));
482 
483   cl::list<std::string> CovFilenames(
484       "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
485       cl::CommaSeparated);
486 
487   cl::list<std::string> InputSourceFiles(
488       cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
489 
490   cl::opt<bool> DebugDumpCollectedPaths(
491       "dump-collected-paths", cl::Optional, cl::Hidden,
492       cl::desc("Show the collected paths to source files"));
493 
494   cl::opt<std::string, true> PGOFilename(
495       "instr-profile", cl::Required, cl::location(this->PGOFilename),
496       cl::desc(
497           "File with the profile data obtained after an instrumented run"));
498 
499   cl::opt<std::string> Arch(
500       "arch", cl::desc("architecture of the coverage mapping binary"));
501 
502   cl::opt<bool> DebugDump("dump", cl::Optional,
503                           cl::desc("Show internal debug dump"));
504 
505   cl::opt<CoverageViewOptions::OutputFormat> Format(
506       "format", cl::desc("Output format for line-based coverage reports"),
507       cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
508                             "Text output"),
509                  clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
510                             "HTML output")),
511       cl::init(CoverageViewOptions::OutputFormat::Text));
512 
513   cl::opt<bool> FilenameEquivalence(
514       "filename-equivalence", cl::Optional,
515       cl::desc("Treat source files as equivalent to paths in the coverage data "
516                "when the file names match, even if the full paths do not"));
517 
518   cl::OptionCategory FilteringCategory("Function filtering options");
519 
520   cl::list<std::string> NameFilters(
521       "name", cl::Optional,
522       cl::desc("Show code coverage only for functions with the given name"),
523       cl::ZeroOrMore, cl::cat(FilteringCategory));
524 
525   cl::list<std::string> NameRegexFilters(
526       "name-regex", cl::Optional,
527       cl::desc("Show code coverage only for functions that match the given "
528                "regular expression"),
529       cl::ZeroOrMore, cl::cat(FilteringCategory));
530 
531   cl::opt<double> RegionCoverageLtFilter(
532       "region-coverage-lt", cl::Optional,
533       cl::desc("Show code coverage only for functions with region coverage "
534                "less than the given threshold"),
535       cl::cat(FilteringCategory));
536 
537   cl::opt<double> RegionCoverageGtFilter(
538       "region-coverage-gt", cl::Optional,
539       cl::desc("Show code coverage only for functions with region coverage "
540                "greater than the given threshold"),
541       cl::cat(FilteringCategory));
542 
543   cl::opt<double> LineCoverageLtFilter(
544       "line-coverage-lt", cl::Optional,
545       cl::desc("Show code coverage only for functions with line coverage less "
546                "than the given threshold"),
547       cl::cat(FilteringCategory));
548 
549   cl::opt<double> LineCoverageGtFilter(
550       "line-coverage-gt", cl::Optional,
551       cl::desc("Show code coverage only for functions with line coverage "
552                "greater than the given threshold"),
553       cl::cat(FilteringCategory));
554 
555   cl::opt<cl::boolOrDefault> UseColor(
556       "use-color", cl::desc("Emit colored output (default=autodetect)"),
557       cl::init(cl::BOU_UNSET));
558 
559   cl::list<std::string> DemanglerOpts(
560       "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
561 
562   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
563     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
564     ViewOpts.Debug = DebugDump;
565     CompareFilenamesOnly = FilenameEquivalence;
566 
567     if (!CovFilename.empty())
568       ObjectFilenames.emplace_back(CovFilename);
569     for (const std::string &Filename : CovFilenames)
570       ObjectFilenames.emplace_back(Filename);
571     if (ObjectFilenames.empty()) {
572       errs() << "No filenames specified!\n";
573       ::exit(1);
574     }
575 
576     ViewOpts.Format = Format;
577     switch (ViewOpts.Format) {
578     case CoverageViewOptions::OutputFormat::Text:
579       ViewOpts.Colors = UseColor == cl::BOU_UNSET
580                             ? sys::Process::StandardOutHasColors()
581                             : UseColor == cl::BOU_TRUE;
582       break;
583     case CoverageViewOptions::OutputFormat::HTML:
584       if (UseColor == cl::BOU_FALSE)
585         errs() << "Color output cannot be disabled when generating html.\n";
586       ViewOpts.Colors = true;
587       break;
588     }
589 
590     // If a demangler is supplied, check if it exists and register it.
591     if (DemanglerOpts.size()) {
592       auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
593       if (!DemanglerPathOrErr) {
594         error("Could not find the demangler!",
595               DemanglerPathOrErr.getError().message());
596         return 1;
597       }
598       DemanglerOpts[0] = *DemanglerPathOrErr;
599       ViewOpts.DemanglerOpts.swap(DemanglerOpts);
600     }
601 
602     // Create the function filters
603     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
604       auto NameFilterer = new CoverageFilters;
605       for (const auto &Name : NameFilters)
606         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
607       for (const auto &Regex : NameRegexFilters)
608         NameFilterer->push_back(
609             llvm::make_unique<NameRegexCoverageFilter>(Regex));
610       Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
611     }
612     if (RegionCoverageLtFilter.getNumOccurrences() ||
613         RegionCoverageGtFilter.getNumOccurrences() ||
614         LineCoverageLtFilter.getNumOccurrences() ||
615         LineCoverageGtFilter.getNumOccurrences()) {
616       auto StatFilterer = new CoverageFilters;
617       if (RegionCoverageLtFilter.getNumOccurrences())
618         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
619             RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
620       if (RegionCoverageGtFilter.getNumOccurrences())
621         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
622             RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
623       if (LineCoverageLtFilter.getNumOccurrences())
624         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
625             LineCoverageFilter::LessThan, LineCoverageLtFilter));
626       if (LineCoverageGtFilter.getNumOccurrences())
627         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
628             RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
629       Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
630     }
631 
632     if (!Arch.empty() &&
633         Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
634       error("Unknown architecture: " + Arch);
635       return 1;
636     }
637     CoverageArch = Arch;
638 
639     for (const std::string &File : InputSourceFiles)
640       collectPaths(File);
641 
642     if (DebugDumpCollectedPaths) {
643       for (const std::string &SF : SourceFiles)
644         outs() << SF << '\n';
645       ::exit(0);
646     }
647 
648     return 0;
649   };
650 
651   switch (Cmd) {
652   case Show:
653     return show(argc, argv, commandLineParser);
654   case Report:
655     return report(argc, argv, commandLineParser);
656   case Export:
657     return export_(argc, argv, commandLineParser);
658   }
659   return 0;
660 }
661 
662 int CodeCoverageTool::show(int argc, const char **argv,
663                            CommandLineParserType commandLineParser) {
664 
665   cl::OptionCategory ViewCategory("Viewing options");
666 
667   cl::opt<bool> ShowLineExecutionCounts(
668       "show-line-counts", cl::Optional,
669       cl::desc("Show the execution counts for each line"), cl::init(true),
670       cl::cat(ViewCategory));
671 
672   cl::opt<bool> ShowRegions(
673       "show-regions", cl::Optional,
674       cl::desc("Show the execution counts for each region"),
675       cl::cat(ViewCategory));
676 
677   cl::opt<bool> ShowBestLineRegionsCounts(
678       "show-line-counts-or-regions", cl::Optional,
679       cl::desc("Show the execution counts for each line, or the execution "
680                "counts for each region on lines that have multiple regions"),
681       cl::cat(ViewCategory));
682 
683   cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
684                                cl::desc("Show expanded source regions"),
685                                cl::cat(ViewCategory));
686 
687   cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
688                                    cl::desc("Show function instantiations"),
689                                    cl::cat(ViewCategory));
690 
691   cl::opt<std::string> ShowOutputDirectory(
692       "output-dir", cl::init(""),
693       cl::desc("Directory in which coverage information is written out"));
694   cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
695                                  cl::aliasopt(ShowOutputDirectory));
696 
697   cl::opt<uint32_t> TabSize(
698       "tab-size", cl::init(2),
699       cl::desc(
700           "Set tab expansion size for html coverage reports (default = 2)"));
701 
702   cl::opt<std::string> ProjectTitle(
703       "project-title", cl::Optional,
704       cl::desc("Set project title for the coverage report"));
705 
706   auto Err = commandLineParser(argc, argv);
707   if (Err)
708     return Err;
709 
710   ViewOpts.ShowLineNumbers = true;
711   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
712                            !ShowRegions || ShowBestLineRegionsCounts;
713   ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
714   ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
715   ViewOpts.ShowExpandedRegions = ShowExpansions;
716   ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
717   ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
718   ViewOpts.TabSize = TabSize;
719   ViewOpts.ProjectTitle = ProjectTitle;
720 
721   if (ViewOpts.hasOutputDirectory()) {
722     if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
723       error("Could not create output directory!", E.message());
724       return 1;
725     }
726   }
727 
728   sys::fs::file_status Status;
729   if (sys::fs::status(PGOFilename, Status)) {
730     error("profdata file error: can not get the file status. \n");
731     return 1;
732   }
733 
734   auto ModifiedTime = Status.getLastModificationTime();
735   std::string ModifiedTimeStr = to_string(ModifiedTime);
736   size_t found = ModifiedTimeStr.rfind(':');
737   ViewOpts.CreatedTimeStr = (found != std::string::npos)
738                                 ? "Created: " + ModifiedTimeStr.substr(0, found)
739                                 : "Created: " + ModifiedTimeStr;
740 
741   auto Coverage = load();
742   if (!Coverage)
743     return 1;
744 
745   auto Printer = CoveragePrinter::create(ViewOpts);
746 
747   if (!Filters.empty()) {
748     auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
749     if (Error E = OSOrErr.takeError()) {
750       error("Could not create view file!", toString(std::move(E)));
751       return 1;
752     }
753     auto OS = std::move(OSOrErr.get());
754 
755     // Show functions.
756     for (const auto &Function : Coverage->getCoveredFunctions()) {
757       if (!Filters.matches(Function))
758         continue;
759 
760       auto mainView = createFunctionView(Function, *Coverage);
761       if (!mainView) {
762         warning("Could not read coverage for '" + Function.Name + "'.");
763         continue;
764       }
765 
766       mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
767     }
768 
769     Printer->closeViewFile(std::move(OS));
770     return 0;
771   }
772 
773   // Show files
774   bool ShowFilenames =
775       (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
776       (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
777 
778   if (SourceFiles.empty())
779     // Get the source files from the function coverage mapping.
780     for (StringRef Filename : Coverage->getUniqueSourceFiles())
781       SourceFiles.push_back(Filename);
782 
783   // Create an index out of the source files.
784   if (ViewOpts.hasOutputDirectory()) {
785     if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
786       error("Could not create index file!", toString(std::move(E)));
787       return 1;
788     }
789   }
790 
791   // FIXME: Sink the hardware_concurrency() == 1 check into ThreadPool.
792   if (!ViewOpts.hasOutputDirectory() ||
793       std::thread::hardware_concurrency() == 1) {
794     for (const std::string &SourceFile : SourceFiles)
795       writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
796                           ShowFilenames);
797   } else {
798     // In -output-dir mode, it's safe to use multiple threads to print files.
799     ThreadPool Pool;
800     for (const std::string &SourceFile : SourceFiles)
801       Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
802                  Coverage.get(), Printer.get(), ShowFilenames);
803     Pool.wait();
804   }
805 
806   return 0;
807 }
808 
809 int CodeCoverageTool::report(int argc, const char **argv,
810                              CommandLineParserType commandLineParser) {
811   auto Err = commandLineParser(argc, argv);
812   if (Err)
813     return Err;
814 
815   if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)
816     error("HTML output for summary reports is not yet supported.");
817 
818   auto Coverage = load();
819   if (!Coverage)
820     return 1;
821 
822   CoverageReport Report(ViewOpts, *Coverage.get());
823   if (SourceFiles.empty())
824     Report.renderFileReports(llvm::outs());
825   else
826     Report.renderFunctionReports(SourceFiles, llvm::outs());
827   return 0;
828 }
829 
830 int CodeCoverageTool::export_(int argc, const char **argv,
831                               CommandLineParserType commandLineParser) {
832 
833   auto Err = commandLineParser(argc, argv);
834   if (Err)
835     return Err;
836 
837   auto Coverage = load();
838   if (!Coverage) {
839     error("Could not load coverage information");
840     return 1;
841   }
842 
843   exportCoverageDataToJson(*Coverage.get(), outs());
844 
845   return 0;
846 }
847 
848 int showMain(int argc, const char *argv[]) {
849   CodeCoverageTool Tool;
850   return Tool.run(CodeCoverageTool::Show, argc, argv);
851 }
852 
853 int reportMain(int argc, const char *argv[]) {
854   CodeCoverageTool Tool;
855   return Tool.run(CodeCoverageTool::Report, argc, argv);
856 }
857 
858 int exportMain(int argc, const char *argv[]) {
859   CodeCoverageTool Tool;
860   return Tool.run(CodeCoverageTool::Export, argc, argv);
861 }
862