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