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