1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The 'CodeCoverageTool' class implements a command line tool to analyze and
11 // report coverage information using the profiling instrumentation and code
14 //===----------------------------------------------------------------------===//
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/ScopedPrinter.h"
34 #include "llvm/Support/ThreadPool.h"
35 #include "llvm/Support/ToolOutputFile.h"
37 #include <system_error>
40 using namespace coverage;
42 void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
46 /// \brief The implementation of the coverage tool.
47 class CodeCoverageTool {
50 /// \brief The show command.
52 /// \brief The report command.
54 /// \brief The export command.
58 int run(Command Cmd, int argc, const char **argv);
61 /// \brief Print the error message to the error output stream.
62 void error(const Twine &Message, StringRef Whence = "");
64 /// \brief Print the warning message to the error output stream.
65 void warning(const Twine &Message, StringRef Whence = "");
67 /// \brief Convert \p Path into an absolute path and append it to the list
68 /// of collected paths.
69 void addCollectedPath(const std::string &Path);
71 /// \brief If \p Path is a regular file, collect the path. If it's a
72 /// directory, recursively collect all of the paths within the directory.
73 void collectPaths(const std::string &Path);
75 /// \brief Return a memory buffer for the given source file.
76 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
78 /// \brief Create source views for the expansions of the view.
79 void attachExpansionSubViews(SourceCoverageView &View,
80 ArrayRef<ExpansionRecord> Expansions,
81 const CoverageMapping &Coverage);
83 /// \brief Create the source view of a particular function.
84 std::unique_ptr<SourceCoverageView>
85 createFunctionView(const FunctionRecord &Function,
86 const CoverageMapping &Coverage);
88 /// \brief Create the main source view of a particular source file.
89 std::unique_ptr<SourceCoverageView>
90 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
92 /// \brief Load the coverage mapping data. Return nullptr if an error occurred.
93 std::unique_ptr<CoverageMapping> load();
95 /// \brief Remove input source files which aren't mapped by \p Coverage.
96 void removeUnmappedInputs(const CoverageMapping &Coverage);
98 /// \brief If a demangler is available, demangle all symbol names.
99 void demangleSymbols(const CoverageMapping &Coverage);
101 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
102 StringRef getSymbolForHumans(StringRef Sym) const;
104 /// \brief Write out a source file view to the filesystem.
105 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
106 CoveragePrinter *Printer, bool ShowFilenames);
108 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
110 int show(int argc, const char **argv,
111 CommandLineParserType commandLineParser);
113 int report(int argc, const char **argv,
114 CommandLineParserType commandLineParser);
116 int export_(int argc, const char **argv,
117 CommandLineParserType commandLineParser);
119 std::vector<StringRef> ObjectFilenames;
120 CoverageViewOptions ViewOpts;
121 CoverageFiltersMatchAll Filters;
123 /// The path to the indexed profile.
124 std::string PGOFilename;
126 /// A list of input source files.
127 std::vector<std::string> SourceFiles;
129 /// Whether or not we're in -filename-equivalence mode.
130 bool CompareFilenamesOnly;
132 /// In -filename-equivalence mode, this maps absolute paths from the
133 /// coverage mapping data to input source files.
134 StringMap<std::string> RemappedFilenames;
136 /// The architecture the coverage mapping data targets.
137 std::string CoverageArch;
139 /// A cache for demangled symbol names.
140 StringMap<std::string> DemangledNames;
142 /// Errors and warnings which have not been printed.
145 /// A container for input source file buffers.
146 std::mutex LoadedSourceFilesLock;
147 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
152 static std::string getErrorString(const Twine &Message, StringRef Whence,
154 std::string Str = (Warning ? "warning" : "error");
157 Str += Whence.str() + ": ";
158 Str += Message.str() + "\n";
162 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
163 std::unique_lock<std::mutex> Guard{ErrsLock};
164 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
165 << getErrorString(Message, Whence, false);
168 void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
169 std::unique_lock<std::mutex> Guard{ErrsLock};
170 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
171 << getErrorString(Message, Whence, true);
174 void CodeCoverageTool::addCollectedPath(const std::string &Path) {
175 if (CompareFilenamesOnly) {
176 SourceFiles.emplace_back(Path);
178 SmallString<128> EffectivePath(Path);
179 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
180 error(EC.message(), Path);
183 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
184 SourceFiles.emplace_back(EffectivePath.str());
188 void CodeCoverageTool::collectPaths(const std::string &Path) {
189 llvm::sys::fs::file_status Status;
190 llvm::sys::fs::status(Path, Status);
191 if (!llvm::sys::fs::exists(Status)) {
192 if (CompareFilenamesOnly)
193 addCollectedPath(Path);
195 error("Missing source file", Path);
199 if (llvm::sys::fs::is_regular_file(Status)) {
200 addCollectedPath(Path);
204 if (llvm::sys::fs::is_directory(Status)) {
206 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
207 F != E && !EC; F.increment(EC)) {
208 if (llvm::sys::fs::is_regular_file(F->path()))
209 addCollectedPath(F->path());
212 warning(EC.message(), Path);
216 ErrorOr<const MemoryBuffer &>
217 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
218 // If we've remapped filenames, look up the real location for this file.
219 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
220 if (!RemappedFilenames.empty()) {
221 auto Loc = RemappedFilenames.find(SourceFile);
222 if (Loc != RemappedFilenames.end())
223 SourceFile = Loc->second;
225 for (const auto &Files : LoadedSourceFiles)
226 if (sys::fs::equivalent(SourceFile, Files.first))
227 return *Files.second;
228 auto Buffer = MemoryBuffer::getFile(SourceFile);
229 if (auto EC = Buffer.getError()) {
230 error(EC.message(), SourceFile);
233 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
234 return *LoadedSourceFiles.back().second;
237 void CodeCoverageTool::attachExpansionSubViews(
238 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
239 const CoverageMapping &Coverage) {
240 if (!ViewOpts.ShowExpandedRegions)
242 for (const auto &Expansion : Expansions) {
243 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
244 if (ExpansionCoverage.empty())
246 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
250 auto SubViewExpansions = ExpansionCoverage.getExpansions();
252 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
253 ViewOpts, std::move(ExpansionCoverage));
254 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
255 View.addExpansion(Expansion.Region, std::move(SubView));
259 std::unique_ptr<SourceCoverageView>
260 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
261 const CoverageMapping &Coverage) {
262 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
263 if (FunctionCoverage.empty())
265 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
269 auto Expansions = FunctionCoverage.getExpansions();
270 auto View = SourceCoverageView::create(getSymbolForHumans(Function.Name),
271 SourceBuffer.get(), ViewOpts,
272 std::move(FunctionCoverage));
273 attachExpansionSubViews(*View, Expansions, Coverage);
278 std::unique_ptr<SourceCoverageView>
279 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
280 const CoverageMapping &Coverage) {
281 auto SourceBuffer = getSourceFile(SourceFile);
284 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
285 if (FileCoverage.empty())
288 auto Expansions = FileCoverage.getExpansions();
289 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
290 ViewOpts, std::move(FileCoverage));
291 attachExpansionSubViews(*View, Expansions, Coverage);
293 for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
294 std::unique_ptr<SourceCoverageView> SubView{nullptr};
296 StringRef Funcname = getSymbolForHumans(Function->Name);
298 if (Function->ExecutionCount > 0) {
299 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
300 auto SubViewExpansions = SubViewCoverage.getExpansions();
301 SubView = SourceCoverageView::create(
302 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
303 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
306 unsigned FileID = Function->CountedRegions.front().FileID;
308 for (const auto &CR : Function->CountedRegions)
309 if (CR.FileID == FileID)
310 Line = std::max(CR.LineEnd, Line);
311 View->addInstantiation(Funcname, Line, std::move(SubView));
316 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
317 sys::fs::file_status Status;
318 if (sys::fs::status(LHS, Status))
320 auto LHSTime = Status.getLastModificationTime();
321 if (sys::fs::status(RHS, Status))
323 auto RHSTime = Status.getLastModificationTime();
324 return LHSTime > RHSTime;
327 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
328 for (StringRef ObjectFilename : ObjectFilenames)
329 if (modifiedTimeGT(ObjectFilename, PGOFilename))
330 warning("profile data may be out of date - object is newer",
333 CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArch);
334 if (Error E = CoverageOrErr.takeError()) {
335 error("Failed to load coverage: " + toString(std::move(E)),
336 join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
339 auto Coverage = std::move(CoverageOrErr.get());
340 unsigned Mismatched = Coverage->getMismatchedCount();
342 warning(utostr(Mismatched) + " functions have mismatched data");
344 if (!SourceFiles.empty())
345 removeUnmappedInputs(*Coverage);
347 demangleSymbols(*Coverage);
352 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
353 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
355 auto UncoveredFilesIt = SourceFiles.end();
356 if (!CompareFilenamesOnly) {
357 // The user may have specified source files which aren't in the coverage
358 // mapping. Filter these files away.
359 UncoveredFilesIt = std::remove_if(
360 SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
361 return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
365 for (auto &SF : SourceFiles) {
366 StringRef SFBase = sys::path::filename(SF);
367 for (const auto &CF : CoveredFiles) {
368 if (SFBase == sys::path::filename(CF)) {
369 RemappedFilenames[CF] = SF;
375 UncoveredFilesIt = std::remove_if(
376 SourceFiles.begin(), SourceFiles.end(),
377 [&](const std::string &SF) { return !RemappedFilenames.count(SF); });
380 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
383 void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
384 if (!ViewOpts.hasDemangler())
387 // Pass function names to the demangler in a temporary file.
389 SmallString<256> InputPath;
391 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
393 error(InputPath, EC.message());
396 tool_output_file InputTOF{InputPath, InputFD};
398 unsigned NumSymbols = 0;
399 for (const auto &Function : Coverage.getCoveredFunctions()) {
400 InputTOF.os() << Function.Name << '\n';
403 InputTOF.os().close();
405 // Use another temporary file to store the demangler's output.
407 SmallString<256> OutputPath;
408 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
411 error(OutputPath, EC.message());
414 tool_output_file OutputTOF{OutputPath, OutputFD};
415 OutputTOF.os().close();
417 // Invoke the demangler.
418 std::vector<const char *> ArgsV;
419 for (const std::string &Arg : ViewOpts.DemanglerOpts)
420 ArgsV.push_back(Arg.c_str());
421 ArgsV.push_back(nullptr);
422 StringRef InputPathRef = InputPath.str();
423 StringRef OutputPathRef = OutputPath.str();
425 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
427 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
428 /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
429 /*memoryLimit=*/0, &ErrMsg);
431 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
435 // Parse the demangler's output.
436 auto BufOrError = MemoryBuffer::getFile(OutputPath);
438 error(OutputPath, BufOrError.getError().message());
442 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
444 SmallVector<StringRef, 8> Symbols;
445 StringRef DemanglerData = DemanglerBuf->getBuffer();
446 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
447 /*KeepEmpty=*/false);
448 if (Symbols.size() != NumSymbols) {
449 error("Demangler did not provide expected number of symbols");
453 // Cache the demangled names.
455 for (const auto &Function : Coverage.getCoveredFunctions())
456 DemangledNames[Function.Name] = Symbols[I++];
459 StringRef CodeCoverageTool::getSymbolForHumans(StringRef Sym) const {
460 const auto DemangledName = DemangledNames.find(Sym);
461 if (DemangledName == DemangledNames.end())
463 return DemangledName->getValue();
466 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
467 CoverageMapping *Coverage,
468 CoveragePrinter *Printer,
469 bool ShowFilenames) {
470 auto View = createSourceFileView(SourceFile, *Coverage);
472 warning("The file '" + SourceFile + "' isn't covered.");
476 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
477 if (Error E = OSOrErr.takeError()) {
478 error("Could not create view file!", toString(std::move(E)));
481 auto OS = std::move(OSOrErr.get());
483 View->print(*OS.get(), /*Wholefile=*/true,
484 /*ShowSourceName=*/ShowFilenames);
485 Printer->closeViewFile(std::move(OS));
488 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
489 cl::opt<std::string> CovFilename(
490 cl::Positional, cl::desc("Covered executable or object file."));
492 cl::list<std::string> CovFilenames(
493 "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
496 cl::list<std::string> InputSourceFiles(
497 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
499 cl::opt<bool> DebugDumpCollectedPaths(
500 "dump-collected-paths", cl::Optional, cl::Hidden,
501 cl::desc("Show the collected paths to source files"));
503 cl::opt<std::string, true> PGOFilename(
504 "instr-profile", cl::Required, cl::location(this->PGOFilename),
506 "File with the profile data obtained after an instrumented run"));
508 cl::opt<std::string> Arch(
509 "arch", cl::desc("architecture of the coverage mapping binary"));
511 cl::opt<bool> DebugDump("dump", cl::Optional,
512 cl::desc("Show internal debug dump"));
514 cl::opt<CoverageViewOptions::OutputFormat> Format(
515 "format", cl::desc("Output format for line-based coverage reports"),
516 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
518 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
520 cl::init(CoverageViewOptions::OutputFormat::Text));
522 cl::opt<bool> FilenameEquivalence(
523 "filename-equivalence", cl::Optional,
524 cl::desc("Treat source files as equivalent to paths in the coverage data "
525 "when the file names match, even if the full paths do not"));
527 cl::OptionCategory FilteringCategory("Function filtering options");
529 cl::list<std::string> NameFilters(
530 "name", cl::Optional,
531 cl::desc("Show code coverage only for functions with the given name"),
532 cl::ZeroOrMore, cl::cat(FilteringCategory));
534 cl::list<std::string> NameRegexFilters(
535 "name-regex", cl::Optional,
536 cl::desc("Show code coverage only for functions that match the given "
537 "regular expression"),
538 cl::ZeroOrMore, cl::cat(FilteringCategory));
540 cl::opt<double> RegionCoverageLtFilter(
541 "region-coverage-lt", cl::Optional,
542 cl::desc("Show code coverage only for functions with region coverage "
543 "less than the given threshold"),
544 cl::cat(FilteringCategory));
546 cl::opt<double> RegionCoverageGtFilter(
547 "region-coverage-gt", cl::Optional,
548 cl::desc("Show code coverage only for functions with region coverage "
549 "greater than the given threshold"),
550 cl::cat(FilteringCategory));
552 cl::opt<double> LineCoverageLtFilter(
553 "line-coverage-lt", cl::Optional,
554 cl::desc("Show code coverage only for functions with line coverage less "
555 "than the given threshold"),
556 cl::cat(FilteringCategory));
558 cl::opt<double> LineCoverageGtFilter(
559 "line-coverage-gt", cl::Optional,
560 cl::desc("Show code coverage only for functions with line coverage "
561 "greater than the given threshold"),
562 cl::cat(FilteringCategory));
564 cl::opt<cl::boolOrDefault> UseColor(
565 "use-color", cl::desc("Emit colored output (default=autodetect)"),
566 cl::init(cl::BOU_UNSET));
568 cl::list<std::string> DemanglerOpts(
569 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
571 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
572 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
573 ViewOpts.Debug = DebugDump;
574 CompareFilenamesOnly = FilenameEquivalence;
576 if (!CovFilename.empty())
577 ObjectFilenames.emplace_back(CovFilename);
578 for (const std::string &Filename : CovFilenames)
579 ObjectFilenames.emplace_back(Filename);
580 if (ObjectFilenames.empty()) {
581 errs() << "No filenames specified!\n";
585 ViewOpts.Format = Format;
586 switch (ViewOpts.Format) {
587 case CoverageViewOptions::OutputFormat::Text:
588 ViewOpts.Colors = UseColor == cl::BOU_UNSET
589 ? sys::Process::StandardOutHasColors()
590 : UseColor == cl::BOU_TRUE;
592 case CoverageViewOptions::OutputFormat::HTML:
593 if (UseColor == cl::BOU_FALSE)
594 errs() << "Color output cannot be disabled when generating html.\n";
595 ViewOpts.Colors = true;
599 // If a demangler is supplied, check if it exists and register it.
600 if (DemanglerOpts.size()) {
601 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
602 if (!DemanglerPathOrErr) {
603 error("Could not find the demangler!",
604 DemanglerPathOrErr.getError().message());
607 DemanglerOpts[0] = *DemanglerPathOrErr;
608 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
611 // Create the function filters
612 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
613 auto NameFilterer = new CoverageFilters;
614 for (const auto &Name : NameFilters)
615 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
616 for (const auto &Regex : NameRegexFilters)
617 NameFilterer->push_back(
618 llvm::make_unique<NameRegexCoverageFilter>(Regex));
619 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
621 if (RegionCoverageLtFilter.getNumOccurrences() ||
622 RegionCoverageGtFilter.getNumOccurrences() ||
623 LineCoverageLtFilter.getNumOccurrences() ||
624 LineCoverageGtFilter.getNumOccurrences()) {
625 auto StatFilterer = new CoverageFilters;
626 if (RegionCoverageLtFilter.getNumOccurrences())
627 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
628 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
629 if (RegionCoverageGtFilter.getNumOccurrences())
630 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
631 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
632 if (LineCoverageLtFilter.getNumOccurrences())
633 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
634 LineCoverageFilter::LessThan, LineCoverageLtFilter));
635 if (LineCoverageGtFilter.getNumOccurrences())
636 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
637 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
638 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
642 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
643 error("Unknown architecture: " + Arch);
648 for (const std::string &File : InputSourceFiles)
651 if (DebugDumpCollectedPaths) {
652 for (const std::string &SF : SourceFiles)
653 outs() << SF << '\n';
662 return show(argc, argv, commandLineParser);
664 return report(argc, argv, commandLineParser);
666 return export_(argc, argv, commandLineParser);
671 int CodeCoverageTool::show(int argc, const char **argv,
672 CommandLineParserType commandLineParser) {
674 cl::OptionCategory ViewCategory("Viewing options");
676 cl::opt<bool> ShowLineExecutionCounts(
677 "show-line-counts", cl::Optional,
678 cl::desc("Show the execution counts for each line"), cl::init(true),
679 cl::cat(ViewCategory));
681 cl::opt<bool> ShowRegions(
682 "show-regions", cl::Optional,
683 cl::desc("Show the execution counts for each region"),
684 cl::cat(ViewCategory));
686 cl::opt<bool> ShowBestLineRegionsCounts(
687 "show-line-counts-or-regions", cl::Optional,
688 cl::desc("Show the execution counts for each line, or the execution "
689 "counts for each region on lines that have multiple regions"),
690 cl::cat(ViewCategory));
692 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
693 cl::desc("Show expanded source regions"),
694 cl::cat(ViewCategory));
696 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
697 cl::desc("Show function instantiations"),
698 cl::cat(ViewCategory));
700 cl::opt<std::string> ShowOutputDirectory(
701 "output-dir", cl::init(""),
702 cl::desc("Directory in which coverage information is written out"));
703 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
704 cl::aliasopt(ShowOutputDirectory));
706 cl::opt<uint32_t> TabSize(
707 "tab-size", cl::init(2),
709 "Set tab expansion size for html coverage reports (default = 2)"));
711 cl::opt<std::string> ProjectTitle(
712 "project-title", cl::Optional,
713 cl::desc("Set project title for the coverage report"));
715 auto Err = commandLineParser(argc, argv);
719 ViewOpts.ShowLineNumbers = true;
720 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
721 !ShowRegions || ShowBestLineRegionsCounts;
722 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
723 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
724 ViewOpts.ShowExpandedRegions = ShowExpansions;
725 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
726 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
727 ViewOpts.TabSize = TabSize;
728 ViewOpts.ProjectTitle = ProjectTitle;
730 if (ViewOpts.hasOutputDirectory()) {
731 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
732 error("Could not create output directory!", E.message());
737 sys::fs::file_status Status;
738 if (sys::fs::status(PGOFilename, Status)) {
739 error("profdata file error: can not get the file status. \n");
743 auto ModifiedTime = Status.getLastModificationTime();
744 std::string ModifiedTimeStr = to_string(ModifiedTime);
745 size_t found = ModifiedTimeStr.rfind(':');
746 ViewOpts.CreatedTimeStr = (found != std::string::npos)
747 ? "Created: " + ModifiedTimeStr.substr(0, found)
748 : "Created: " + ModifiedTimeStr;
750 auto Coverage = load();
754 auto Printer = CoveragePrinter::create(ViewOpts);
756 if (!Filters.empty()) {
757 auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
758 if (Error E = OSOrErr.takeError()) {
759 error("Could not create view file!", toString(std::move(E)));
762 auto OS = std::move(OSOrErr.get());
765 for (const auto &Function : Coverage->getCoveredFunctions()) {
766 if (!Filters.matches(Function))
769 auto mainView = createFunctionView(Function, *Coverage);
771 warning("Could not read coverage for '" + Function.Name + "'.");
775 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
778 Printer->closeViewFile(std::move(OS));
784 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
785 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
787 if (SourceFiles.empty())
788 // Get the source files from the function coverage mapping.
789 for (StringRef Filename : Coverage->getUniqueSourceFiles())
790 SourceFiles.push_back(Filename);
792 // Create an index out of the source files.
793 if (ViewOpts.hasOutputDirectory()) {
794 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
795 error("Could not create index file!", toString(std::move(E)));
800 // FIXME: Sink the hardware_concurrency() == 1 check into ThreadPool.
801 if (!ViewOpts.hasOutputDirectory() ||
802 std::thread::hardware_concurrency() == 1) {
803 for (const std::string &SourceFile : SourceFiles)
804 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
807 // In -output-dir mode, it's safe to use multiple threads to print files.
809 for (const std::string &SourceFile : SourceFiles)
810 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
811 Coverage.get(), Printer.get(), ShowFilenames);
818 int CodeCoverageTool::report(int argc, const char **argv,
819 CommandLineParserType commandLineParser) {
820 auto Err = commandLineParser(argc, argv);
824 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)
825 error("HTML output for summary reports is not yet supported.");
827 auto Coverage = load();
831 CoverageReport Report(ViewOpts, *Coverage.get());
832 if (SourceFiles.empty())
833 Report.renderFileReports(llvm::outs());
835 Report.renderFunctionReports(SourceFiles, llvm::outs());
839 int CodeCoverageTool::export_(int argc, const char **argv,
840 CommandLineParserType commandLineParser) {
842 auto Err = commandLineParser(argc, argv);
846 auto Coverage = load();
848 error("Could not load coverage information");
852 exportCoverageDataToJson(*Coverage.get(), outs());
857 int showMain(int argc, const char *argv[]) {
858 CodeCoverageTool Tool;
859 return Tool.run(CodeCoverageTool::Show, argc, argv);
862 int reportMain(int argc, const char *argv[]) {
863 CodeCoverageTool Tool;
864 return Tool.run(CodeCoverageTool::Report, argc, argv);
867 int exportMain(int argc, const char *argv[]) {
868 CodeCoverageTool Tool;
869 return Tool.run(CodeCoverageTool::Export, argc, argv);