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