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