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