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 "CoverageSummaryInfo.h"
19 #include "CoverageViewOptions.h"
20 #include "RenderingSupport.h"
21 #include "SourceCoverageView.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
26 #include "llvm/ProfileData/InstrProfReader.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Format.h"
30 #include "llvm/Support/MemoryBuffer.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/Process.h"
33 #include "llvm/Support/Program.h"
34 #include "llvm/Support/ScopedPrinter.h"
35 #include "llvm/Support/Threading.h"
36 #include "llvm/Support/ThreadPool.h"
37 #include "llvm/Support/ToolOutputFile.h"
39 #include <system_error>
42 using namespace coverage;
44 void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
48 /// \brief The implementation of the coverage tool.
49 class CodeCoverageTool {
52 /// \brief The show command.
54 /// \brief The report command.
56 /// \brief The export command.
60 int run(Command Cmd, int argc, const char **argv);
63 /// \brief Print the error message to the error output stream.
64 void error(const Twine &Message, StringRef Whence = "");
66 /// \brief Print the warning message to the error output stream.
67 void warning(const Twine &Message, StringRef Whence = "");
69 /// \brief Convert \p Path into an absolute path and append it to the list
70 /// of collected paths.
71 void addCollectedPath(const std::string &Path);
73 /// \brief If \p Path is a regular file, collect the path. If it's a
74 /// directory, recursively collect all of the paths within the directory.
75 void collectPaths(const std::string &Path);
77 /// \brief Return a memory buffer for the given source file.
78 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
80 /// \brief Create source views for the expansions of the view.
81 void attachExpansionSubViews(SourceCoverageView &View,
82 ArrayRef<ExpansionRecord> Expansions,
83 const CoverageMapping &Coverage);
85 /// \brief Create the source view of a particular function.
86 std::unique_ptr<SourceCoverageView>
87 createFunctionView(const FunctionRecord &Function,
88 const CoverageMapping &Coverage);
90 /// \brief Create the main source view of a particular source file.
91 std::unique_ptr<SourceCoverageView>
92 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
94 /// \brief Load the coverage mapping data. Return nullptr if an error occurred.
95 std::unique_ptr<CoverageMapping> load();
97 /// \brief Remove input source files which aren't mapped by \p Coverage.
98 void removeUnmappedInputs(const CoverageMapping &Coverage);
100 /// \brief If a demangler is available, demangle all symbol names.
101 void demangleSymbols(const CoverageMapping &Coverage);
103 /// \brief Write out a source file view to the filesystem.
104 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
105 CoveragePrinter *Printer, bool ShowFilenames);
107 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
109 int show(int argc, const char **argv,
110 CommandLineParserType commandLineParser);
112 int report(int argc, const char **argv,
113 CommandLineParserType commandLineParser);
115 int export_(int argc, const char **argv,
116 CommandLineParserType commandLineParser);
118 std::vector<StringRef> ObjectFilenames;
119 CoverageViewOptions ViewOpts;
120 CoverageFiltersMatchAll Filters;
122 /// The path to the indexed profile.
123 std::string PGOFilename;
125 /// A list of input source files.
126 std::vector<std::string> SourceFiles;
128 /// Whether or not we're in -filename-equivalence mode.
129 bool CompareFilenamesOnly;
131 /// In -filename-equivalence mode, this maps absolute paths from the
132 /// coverage mapping data to input source files.
133 StringMap<std::string> RemappedFilenames;
135 /// The architecture the coverage mapping data targets.
136 std::string CoverageArch;
138 /// A cache for demangled symbols.
141 /// A lock which guards printing to stderr.
144 /// A container for input source file buffers.
145 std::mutex LoadedSourceFilesLock;
146 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
151 static std::string getErrorString(const Twine &Message, StringRef Whence,
153 std::string Str = (Warning ? "warning" : "error");
156 Str += Whence.str() + ": ";
157 Str += Message.str() + "\n";
161 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
162 std::unique_lock<std::mutex> Guard{ErrsLock};
163 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
164 << getErrorString(Message, Whence, false);
167 void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
168 std::unique_lock<std::mutex> Guard{ErrsLock};
169 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
170 << getErrorString(Message, Whence, true);
173 void CodeCoverageTool::addCollectedPath(const std::string &Path) {
174 if (CompareFilenamesOnly) {
175 SourceFiles.emplace_back(Path);
177 SmallString<128> EffectivePath(Path);
178 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
179 error(EC.message(), Path);
182 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
183 SourceFiles.emplace_back(EffectivePath.str());
187 void CodeCoverageTool::collectPaths(const std::string &Path) {
188 llvm::sys::fs::file_status Status;
189 llvm::sys::fs::status(Path, Status);
190 if (!llvm::sys::fs::exists(Status)) {
191 if (CompareFilenamesOnly)
192 addCollectedPath(Path);
194 error("Missing source file", Path);
198 if (llvm::sys::fs::is_regular_file(Status)) {
199 addCollectedPath(Path);
203 if (llvm::sys::fs::is_directory(Status)) {
205 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
206 F != E && !EC; F.increment(EC)) {
207 if (llvm::sys::fs::is_regular_file(F->path()))
208 addCollectedPath(F->path());
211 warning(EC.message(), Path);
215 ErrorOr<const MemoryBuffer &>
216 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
217 // If we've remapped filenames, look up the real location for this file.
218 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
219 if (!RemappedFilenames.empty()) {
220 auto Loc = RemappedFilenames.find(SourceFile);
221 if (Loc != RemappedFilenames.end())
222 SourceFile = Loc->second;
224 for (const auto &Files : LoadedSourceFiles)
225 if (sys::fs::equivalent(SourceFile, Files.first))
226 return *Files.second;
227 auto Buffer = MemoryBuffer::getFile(SourceFile);
228 if (auto EC = Buffer.getError()) {
229 error(EC.message(), SourceFile);
232 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
233 return *LoadedSourceFiles.back().second;
236 void CodeCoverageTool::attachExpansionSubViews(
237 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
238 const CoverageMapping &Coverage) {
239 if (!ViewOpts.ShowExpandedRegions)
241 for (const auto &Expansion : Expansions) {
242 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
243 if (ExpansionCoverage.empty())
245 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
249 auto SubViewExpansions = ExpansionCoverage.getExpansions();
251 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
252 ViewOpts, std::move(ExpansionCoverage));
253 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
254 View.addExpansion(Expansion.Region, std::move(SubView));
258 std::unique_ptr<SourceCoverageView>
259 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
260 const CoverageMapping &Coverage) {
261 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
262 if (FunctionCoverage.empty())
264 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
268 auto Expansions = FunctionCoverage.getExpansions();
269 auto View = SourceCoverageView::create(DC.demangle(Function.Name),
270 SourceBuffer.get(), ViewOpts,
271 std::move(FunctionCoverage));
272 attachExpansionSubViews(*View, Expansions, Coverage);
277 std::unique_ptr<SourceCoverageView>
278 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
279 const CoverageMapping &Coverage) {
280 auto SourceBuffer = getSourceFile(SourceFile);
283 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
284 if (FileCoverage.empty())
287 auto Expansions = FileCoverage.getExpansions();
288 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
289 ViewOpts, std::move(FileCoverage));
290 attachExpansionSubViews(*View, Expansions, Coverage);
292 for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
293 std::unique_ptr<SourceCoverageView> SubView{nullptr};
295 StringRef Funcname = DC.demangle(Function->Name);
297 if (Function->ExecutionCount > 0) {
298 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
299 auto SubViewExpansions = SubViewCoverage.getExpansions();
300 SubView = SourceCoverageView::create(
301 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
302 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
305 unsigned FileID = Function->CountedRegions.front().FileID;
307 for (const auto &CR : Function->CountedRegions)
308 if (CR.FileID == FileID)
309 Line = std::max(CR.LineEnd, Line);
310 View->addInstantiation(Funcname, Line, std::move(SubView));
315 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
316 sys::fs::file_status Status;
317 if (sys::fs::status(LHS, Status))
319 auto LHSTime = Status.getLastModificationTime();
320 if (sys::fs::status(RHS, Status))
322 auto RHSTime = Status.getLastModificationTime();
323 return LHSTime > RHSTime;
326 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
327 for (StringRef ObjectFilename : ObjectFilenames)
328 if (modifiedTimeGT(ObjectFilename, PGOFilename))
329 warning("profile data may be out of date - object is newer",
332 CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArch);
333 if (Error E = CoverageOrErr.takeError()) {
334 error("Failed to load coverage: " + toString(std::move(E)),
335 join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
338 auto Coverage = std::move(CoverageOrErr.get());
339 unsigned Mismatched = Coverage->getMismatchedCount();
341 warning(utostr(Mismatched) + " functions have mismatched data");
343 if (!SourceFiles.empty())
344 removeUnmappedInputs(*Coverage);
346 demangleSymbols(*Coverage);
351 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
352 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
354 auto UncoveredFilesIt = SourceFiles.end();
355 if (!CompareFilenamesOnly) {
356 // The user may have specified source files which aren't in the coverage
357 // mapping. Filter these files away.
358 UncoveredFilesIt = std::remove_if(
359 SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
360 return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
364 for (auto &SF : SourceFiles) {
365 StringRef SFBase = sys::path::filename(SF);
366 for (const auto &CF : CoveredFiles) {
367 if (SFBase == sys::path::filename(CF)) {
368 RemappedFilenames[CF] = SF;
374 UncoveredFilesIt = std::remove_if(
375 SourceFiles.begin(), SourceFiles.end(),
376 [&](const std::string &SF) { return !RemappedFilenames.count(SF); });
379 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
382 void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
383 if (!ViewOpts.hasDemangler())
386 // Pass function names to the demangler in a temporary file.
388 SmallString<256> InputPath;
390 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
392 error(InputPath, EC.message());
395 tool_output_file InputTOF{InputPath, InputFD};
397 unsigned NumSymbols = 0;
398 for (const auto &Function : Coverage.getCoveredFunctions()) {
399 InputTOF.os() << Function.Name << '\n';
402 InputTOF.os().close();
404 // Use another temporary file to store the demangler's output.
406 SmallString<256> OutputPath;
407 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
410 error(OutputPath, EC.message());
413 tool_output_file OutputTOF{OutputPath, OutputFD};
414 OutputTOF.os().close();
416 // Invoke the demangler.
417 std::vector<const char *> ArgsV;
418 for (const std::string &Arg : ViewOpts.DemanglerOpts)
419 ArgsV.push_back(Arg.c_str());
420 ArgsV.push_back(nullptr);
421 StringRef InputPathRef = InputPath.str();
422 StringRef OutputPathRef = OutputPath.str();
424 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
426 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
427 /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
428 /*memoryLimit=*/0, &ErrMsg);
430 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
434 // Parse the demangler's output.
435 auto BufOrError = MemoryBuffer::getFile(OutputPath);
437 error(OutputPath, BufOrError.getError().message());
441 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
443 SmallVector<StringRef, 8> Symbols;
444 StringRef DemanglerData = DemanglerBuf->getBuffer();
445 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
446 /*KeepEmpty=*/false);
447 if (Symbols.size() != NumSymbols) {
448 error("Demangler did not provide expected number of symbols");
452 // Cache the demangled names.
454 for (const auto &Function : Coverage.getCoveredFunctions())
455 // On Windows, lines in the demangler's output file end with "\r\n".
456 // Splitting by '\n' keeps '\r's, so cut them now.
457 DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
460 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
461 CoverageMapping *Coverage,
462 CoveragePrinter *Printer,
463 bool ShowFilenames) {
464 auto View = createSourceFileView(SourceFile, *Coverage);
466 warning("The file '" + SourceFile + "' isn't covered.");
470 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
471 if (Error E = OSOrErr.takeError()) {
472 error("Could not create view file!", toString(std::move(E)));
475 auto OS = std::move(OSOrErr.get());
477 View->print(*OS.get(), /*Wholefile=*/true,
478 /*ShowSourceName=*/ShowFilenames);
479 Printer->closeViewFile(std::move(OS));
482 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
483 cl::opt<std::string> CovFilename(
484 cl::Positional, cl::desc("Covered executable or object file."));
486 cl::list<std::string> CovFilenames(
487 "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
490 cl::list<std::string> InputSourceFiles(
491 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
493 cl::opt<bool> DebugDumpCollectedPaths(
494 "dump-collected-paths", cl::Optional, cl::Hidden,
495 cl::desc("Show the collected paths to source files"));
497 cl::opt<std::string, true> PGOFilename(
498 "instr-profile", cl::Required, cl::location(this->PGOFilename),
500 "File with the profile data obtained after an instrumented run"));
502 cl::opt<std::string> Arch(
503 "arch", cl::desc("architecture of the coverage mapping binary"));
505 cl::opt<bool> DebugDump("dump", cl::Optional,
506 cl::desc("Show internal debug dump"));
508 cl::opt<CoverageViewOptions::OutputFormat> Format(
509 "format", cl::desc("Output format for line-based coverage reports"),
510 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
512 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
514 cl::init(CoverageViewOptions::OutputFormat::Text));
516 cl::opt<bool> FilenameEquivalence(
517 "filename-equivalence", cl::Optional,
518 cl::desc("Treat source files as equivalent to paths in the coverage data "
519 "when the file names match, even if the full paths do not"));
521 cl::OptionCategory FilteringCategory("Function filtering options");
523 cl::list<std::string> NameFilters(
524 "name", cl::Optional,
525 cl::desc("Show code coverage only for functions with the given name"),
526 cl::ZeroOrMore, cl::cat(FilteringCategory));
528 cl::list<std::string> NameRegexFilters(
529 "name-regex", cl::Optional,
530 cl::desc("Show code coverage only for functions that match the given "
531 "regular expression"),
532 cl::ZeroOrMore, cl::cat(FilteringCategory));
534 cl::opt<double> RegionCoverageLtFilter(
535 "region-coverage-lt", cl::Optional,
536 cl::desc("Show code coverage only for functions with region coverage "
537 "less than the given threshold"),
538 cl::cat(FilteringCategory));
540 cl::opt<double> RegionCoverageGtFilter(
541 "region-coverage-gt", cl::Optional,
542 cl::desc("Show code coverage only for functions with region coverage "
543 "greater than the given threshold"),
544 cl::cat(FilteringCategory));
546 cl::opt<double> LineCoverageLtFilter(
547 "line-coverage-lt", cl::Optional,
548 cl::desc("Show code coverage only for functions with line coverage less "
549 "than the given threshold"),
550 cl::cat(FilteringCategory));
552 cl::opt<double> LineCoverageGtFilter(
553 "line-coverage-gt", cl::Optional,
554 cl::desc("Show code coverage only for functions with line coverage "
555 "greater than the given threshold"),
556 cl::cat(FilteringCategory));
558 cl::opt<cl::boolOrDefault> UseColor(
559 "use-color", cl::desc("Emit colored output (default=autodetect)"),
560 cl::init(cl::BOU_UNSET));
562 cl::list<std::string> DemanglerOpts(
563 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
565 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
566 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
567 ViewOpts.Debug = DebugDump;
568 CompareFilenamesOnly = FilenameEquivalence;
570 if (!CovFilename.empty())
571 ObjectFilenames.emplace_back(CovFilename);
572 for (const std::string &Filename : CovFilenames)
573 ObjectFilenames.emplace_back(Filename);
574 if (ObjectFilenames.empty()) {
575 errs() << "No filenames specified!\n";
579 ViewOpts.Format = Format;
580 switch (ViewOpts.Format) {
581 case CoverageViewOptions::OutputFormat::Text:
582 ViewOpts.Colors = UseColor == cl::BOU_UNSET
583 ? sys::Process::StandardOutHasColors()
584 : UseColor == cl::BOU_TRUE;
586 case CoverageViewOptions::OutputFormat::HTML:
587 if (UseColor == cl::BOU_FALSE)
588 errs() << "Color output cannot be disabled when generating html.\n";
589 ViewOpts.Colors = true;
593 // If a demangler is supplied, check if it exists and register it.
594 if (DemanglerOpts.size()) {
595 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
596 if (!DemanglerPathOrErr) {
597 error("Could not find the demangler!",
598 DemanglerPathOrErr.getError().message());
601 DemanglerOpts[0] = *DemanglerPathOrErr;
602 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
605 // Create the function filters
606 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
607 auto NameFilterer = new CoverageFilters;
608 for (const auto &Name : NameFilters)
609 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
610 for (const auto &Regex : NameRegexFilters)
611 NameFilterer->push_back(
612 llvm::make_unique<NameRegexCoverageFilter>(Regex));
613 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
615 if (RegionCoverageLtFilter.getNumOccurrences() ||
616 RegionCoverageGtFilter.getNumOccurrences() ||
617 LineCoverageLtFilter.getNumOccurrences() ||
618 LineCoverageGtFilter.getNumOccurrences()) {
619 auto StatFilterer = new CoverageFilters;
620 if (RegionCoverageLtFilter.getNumOccurrences())
621 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
622 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
623 if (RegionCoverageGtFilter.getNumOccurrences())
624 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
625 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
626 if (LineCoverageLtFilter.getNumOccurrences())
627 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
628 LineCoverageFilter::LessThan, LineCoverageLtFilter));
629 if (LineCoverageGtFilter.getNumOccurrences())
630 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
631 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
632 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
636 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
637 error("Unknown architecture: " + Arch);
642 for (const std::string &File : InputSourceFiles)
645 if (DebugDumpCollectedPaths) {
646 for (const std::string &SF : SourceFiles)
647 outs() << SF << '\n';
656 return show(argc, argv, commandLineParser);
658 return report(argc, argv, commandLineParser);
660 return export_(argc, argv, commandLineParser);
665 int CodeCoverageTool::show(int argc, const char **argv,
666 CommandLineParserType commandLineParser) {
668 cl::OptionCategory ViewCategory("Viewing options");
670 cl::opt<bool> ShowLineExecutionCounts(
671 "show-line-counts", cl::Optional,
672 cl::desc("Show the execution counts for each line"), cl::init(true),
673 cl::cat(ViewCategory));
675 cl::opt<bool> ShowRegions(
676 "show-regions", cl::Optional,
677 cl::desc("Show the execution counts for each region"),
678 cl::cat(ViewCategory));
680 cl::opt<bool> ShowBestLineRegionsCounts(
681 "show-line-counts-or-regions", cl::Optional,
682 cl::desc("Show the execution counts for each line, or the execution "
683 "counts for each region on lines that have multiple regions"),
684 cl::cat(ViewCategory));
686 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
687 cl::desc("Show expanded source regions"),
688 cl::cat(ViewCategory));
690 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
691 cl::desc("Show function instantiations"),
692 cl::cat(ViewCategory));
694 cl::opt<std::string> ShowOutputDirectory(
695 "output-dir", cl::init(""),
696 cl::desc("Directory in which coverage information is written out"));
697 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
698 cl::aliasopt(ShowOutputDirectory));
700 cl::opt<uint32_t> TabSize(
701 "tab-size", cl::init(2),
703 "Set tab expansion size for html coverage reports (default = 2)"));
705 cl::opt<std::string> ProjectTitle(
706 "project-title", cl::Optional,
707 cl::desc("Set project title for the coverage report"));
709 cl::opt<unsigned> NumThreads(
710 "num-threads", cl::init(0),
711 cl::desc("Number of merge threads to use (default: autodetect)"));
712 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
713 cl::aliasopt(NumThreads));
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 // If NumThreads is not specified, auto-detect a good default.
803 std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
804 unsigned(SourceFiles.size())));
806 if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
807 for (const std::string &SourceFile : SourceFiles)
808 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
811 // In -output-dir mode, it's safe to use multiple threads to print files.
812 ThreadPool Pool(NumThreads);
813 for (const std::string &SourceFile : SourceFiles)
814 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
815 Coverage.get(), Printer.get(), ShowFilenames);
822 int CodeCoverageTool::report(int argc, const char **argv,
823 CommandLineParserType commandLineParser) {
824 cl::opt<bool> ShowFunctionSummaries(
825 "show-functions", cl::Optional, cl::init(false),
826 cl::desc("Show coverage summaries for each function"));
828 auto Err = commandLineParser(argc, argv);
832 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
833 error("HTML output for summary reports is not yet supported.");
837 auto Coverage = load();
841 CoverageReport Report(ViewOpts, *Coverage.get());
842 if (!ShowFunctionSummaries)
843 Report.renderFileReports(llvm::outs());
845 Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
849 int CodeCoverageTool::export_(int argc, const char **argv,
850 CommandLineParserType commandLineParser) {
852 auto Err = commandLineParser(argc, argv);
856 if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
857 error("Coverage data can only be exported as textual JSON.");
861 auto Coverage = load();
863 error("Could not load coverage information");
867 exportCoverageDataToJson(*Coverage.get(), outs());
872 int showMain(int argc, const char *argv[]) {
873 CodeCoverageTool Tool;
874 return Tool.run(CodeCoverageTool::Show, argc, argv);
877 int reportMain(int argc, const char *argv[]) {
878 CodeCoverageTool Tool;
879 return Tool.run(CodeCoverageTool::Report, argc, argv);
882 int exportMain(int argc, const char *argv[]) {
883 CodeCoverageTool Tool;
884 return Tool.run(CodeCoverageTool::Export, argc, argv);