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 "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" 38 #include <functional> 39 #include <system_error> 40 41 using namespace llvm; 42 using namespace coverage; 43 44 void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping, 45 raw_ostream &OS); 46 47 namespace { 48 /// \brief The implementation of the coverage tool. 49 class CodeCoverageTool { 50 public: 51 enum Command { 52 /// \brief The show command. 53 Show, 54 /// \brief The report command. 55 Report, 56 /// \brief The export command. 57 Export 58 }; 59 60 int run(Command Cmd, int argc, const char **argv); 61 62 private: 63 /// \brief Print the error message to the error output stream. 64 void error(const Twine &Message, StringRef Whence = ""); 65 66 /// \brief Print the warning message to the error output stream. 67 void warning(const Twine &Message, StringRef Whence = ""); 68 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); 72 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); 76 77 /// \brief Return a memory buffer for the given source file. 78 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); 79 80 /// \brief Create source views for the expansions of the view. 81 void attachExpansionSubViews(SourceCoverageView &View, 82 ArrayRef<ExpansionRecord> Expansions, 83 const CoverageMapping &Coverage); 84 85 /// \brief Create the source view of a particular function. 86 std::unique_ptr<SourceCoverageView> 87 createFunctionView(const FunctionRecord &Function, 88 const CoverageMapping &Coverage); 89 90 /// \brief Create the main source view of a particular source file. 91 std::unique_ptr<SourceCoverageView> 92 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage); 93 94 /// \brief Load the coverage mapping data. Return nullptr if an error occurred. 95 std::unique_ptr<CoverageMapping> load(); 96 97 /// \brief Remove input source files which aren't mapped by \p Coverage. 98 void removeUnmappedInputs(const CoverageMapping &Coverage); 99 100 /// \brief If a demangler is available, demangle all symbol names. 101 void demangleSymbols(const CoverageMapping &Coverage); 102 103 /// \brief Write out a source file view to the filesystem. 104 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage, 105 CoveragePrinter *Printer, bool ShowFilenames); 106 107 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType; 108 109 int show(int argc, const char **argv, 110 CommandLineParserType commandLineParser); 111 112 int report(int argc, const char **argv, 113 CommandLineParserType commandLineParser); 114 115 int export_(int argc, const char **argv, 116 CommandLineParserType commandLineParser); 117 118 std::vector<StringRef> ObjectFilenames; 119 CoverageViewOptions ViewOpts; 120 CoverageFiltersMatchAll Filters; 121 122 /// The path to the indexed profile. 123 std::string PGOFilename; 124 125 /// A list of input source files. 126 std::vector<std::string> SourceFiles; 127 128 /// Whether or not we're in -filename-equivalence mode. 129 bool CompareFilenamesOnly; 130 131 /// In -filename-equivalence mode, this maps absolute paths from the 132 /// coverage mapping data to input source files. 133 StringMap<std::string> RemappedFilenames; 134 135 /// The architecture the coverage mapping data targets. 136 std::string CoverageArch; 137 138 /// A cache for demangled symbols. 139 DemangleCache DC; 140 141 /// A lock which guards printing to stderr. 142 std::mutex ErrsLock; 143 144 /// A container for input source file buffers. 145 std::mutex LoadedSourceFilesLock; 146 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> 147 LoadedSourceFiles; 148 }; 149 } 150 151 static std::string getErrorString(const Twine &Message, StringRef Whence, 152 bool Warning) { 153 std::string Str = (Warning ? "warning" : "error"); 154 Str += ": "; 155 if (!Whence.empty()) 156 Str += Whence.str() + ": "; 157 Str += Message.str() + "\n"; 158 return Str; 159 } 160 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); 165 } 166 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); 171 } 172 173 void CodeCoverageTool::addCollectedPath(const std::string &Path) { 174 if (CompareFilenamesOnly) { 175 SourceFiles.emplace_back(Path); 176 } else { 177 SmallString<128> EffectivePath(Path); 178 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) { 179 error(EC.message(), Path); 180 return; 181 } 182 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true); 183 SourceFiles.emplace_back(EffectivePath.str()); 184 } 185 } 186 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); 193 else 194 error("Missing source file", Path); 195 return; 196 } 197 198 if (llvm::sys::fs::is_regular_file(Status)) { 199 addCollectedPath(Path); 200 return; 201 } 202 203 if (llvm::sys::fs::is_directory(Status)) { 204 std::error_code EC; 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()); 209 } 210 if (EC) 211 warning(EC.message(), Path); 212 } 213 } 214 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; 223 } 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); 230 return EC; 231 } 232 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get())); 233 return *LoadedSourceFiles.back().second; 234 } 235 236 void CodeCoverageTool::attachExpansionSubViews( 237 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions, 238 const CoverageMapping &Coverage) { 239 if (!ViewOpts.ShowExpandedRegions) 240 return; 241 for (const auto &Expansion : Expansions) { 242 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); 243 if (ExpansionCoverage.empty()) 244 continue; 245 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); 246 if (!SourceBuffer) 247 continue; 248 249 auto SubViewExpansions = ExpansionCoverage.getExpansions(); 250 auto SubView = 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)); 255 } 256 } 257 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()) 263 return nullptr; 264 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); 265 if (!SourceBuffer) 266 return nullptr; 267 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); 273 274 return View; 275 } 276 277 std::unique_ptr<SourceCoverageView> 278 CodeCoverageTool::createSourceFileView(StringRef SourceFile, 279 const CoverageMapping &Coverage) { 280 auto SourceBuffer = getSourceFile(SourceFile); 281 if (!SourceBuffer) 282 return nullptr; 283 auto FileCoverage = Coverage.getCoverageForFile(SourceFile); 284 if (FileCoverage.empty()) 285 return nullptr; 286 287 auto Expansions = FileCoverage.getExpansions(); 288 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), 289 ViewOpts, std::move(FileCoverage)); 290 attachExpansionSubViews(*View, Expansions, Coverage); 291 292 for (const auto *Function : Coverage.getInstantiations(SourceFile)) { 293 std::unique_ptr<SourceCoverageView> SubView{nullptr}; 294 295 StringRef Funcname = DC.demangle(Function->Name); 296 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); 303 } 304 305 unsigned FileID = Function->CountedRegions.front().FileID; 306 unsigned Line = 0; 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)); 311 } 312 return View; 313 } 314 315 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) { 316 sys::fs::file_status Status; 317 if (sys::fs::status(LHS, Status)) 318 return false; 319 auto LHSTime = Status.getLastModificationTime(); 320 if (sys::fs::status(RHS, Status)) 321 return false; 322 auto RHSTime = Status.getLastModificationTime(); 323 return LHSTime > RHSTime; 324 } 325 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", 330 ObjectFilename); 331 auto CoverageOrErr = 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(), ", ")); 336 return nullptr; 337 } 338 auto Coverage = std::move(CoverageOrErr.get()); 339 unsigned Mismatched = Coverage->getMismatchedCount(); 340 if (Mismatched) 341 warning(utostr(Mismatched) + " functions have mismatched data"); 342 343 if (!SourceFiles.empty()) 344 removeUnmappedInputs(*Coverage); 345 346 demangleSymbols(*Coverage); 347 348 return Coverage; 349 } 350 351 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) { 352 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles(); 353 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(), 361 SF); 362 }); 363 } else { 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; 369 SF = CF; 370 break; 371 } 372 } 373 } 374 UncoveredFilesIt = std::remove_if( 375 SourceFiles.begin(), SourceFiles.end(), 376 [&](const std::string &SF) { return !RemappedFilenames.count(SF); }); 377 } 378 379 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end()); 380 } 381 382 void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) { 383 if (!ViewOpts.hasDemangler()) 384 return; 385 386 // Pass function names to the demangler in a temporary file. 387 int InputFD; 388 SmallString<256> InputPath; 389 std::error_code EC = 390 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath); 391 if (EC) { 392 error(InputPath, EC.message()); 393 return; 394 } 395 tool_output_file InputTOF{InputPath, InputFD}; 396 397 unsigned NumSymbols = 0; 398 for (const auto &Function : Coverage.getCoveredFunctions()) { 399 InputTOF.os() << Function.Name << '\n'; 400 ++NumSymbols; 401 } 402 InputTOF.os().close(); 403 404 // Use another temporary file to store the demangler's output. 405 int OutputFD; 406 SmallString<256> OutputPath; 407 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD, 408 OutputPath); 409 if (EC) { 410 error(OutputPath, EC.message()); 411 return; 412 } 413 tool_output_file OutputTOF{OutputPath, OutputFD}; 414 OutputTOF.os().close(); 415 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(); 423 StringRef StderrRef; 424 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef}; 425 std::string ErrMsg; 426 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(), 427 /*env=*/nullptr, Redirects, /*secondsToWait=*/0, 428 /*memoryLimit=*/0, &ErrMsg); 429 if (RC) { 430 error(ErrMsg, ViewOpts.DemanglerOpts[0]); 431 return; 432 } 433 434 // Parse the demangler's output. 435 auto BufOrError = MemoryBuffer::getFile(OutputPath); 436 if (!BufOrError) { 437 error(OutputPath, BufOrError.getError().message()); 438 return; 439 } 440 441 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError); 442 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"); 449 return; 450 } 451 452 // Cache the demangled names. 453 unsigned I = 0; 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(); 458 } 459 460 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile, 461 CoverageMapping *Coverage, 462 CoveragePrinter *Printer, 463 bool ShowFilenames) { 464 auto View = createSourceFileView(SourceFile, *Coverage); 465 if (!View) { 466 warning("The file '" + SourceFile + "' isn't covered."); 467 return; 468 } 469 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))); 473 return; 474 } 475 auto OS = std::move(OSOrErr.get()); 476 477 View->print(*OS.get(), /*Wholefile=*/true, 478 /*ShowSourceName=*/ShowFilenames); 479 Printer->closeViewFile(std::move(OS)); 480 } 481 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.")); 485 486 cl::list<std::string> CovFilenames( 487 "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore, 488 cl::CommaSeparated); 489 490 cl::list<std::string> InputSourceFiles( 491 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); 492 493 cl::opt<bool> DebugDumpCollectedPaths( 494 "dump-collected-paths", cl::Optional, cl::Hidden, 495 cl::desc("Show the collected paths to source files")); 496 497 cl::opt<std::string, true> PGOFilename( 498 "instr-profile", cl::Required, cl::location(this->PGOFilename), 499 cl::desc( 500 "File with the profile data obtained after an instrumented run")); 501 502 cl::opt<std::string> Arch( 503 "arch", cl::desc("architecture of the coverage mapping binary")); 504 505 cl::opt<bool> DebugDump("dump", cl::Optional, 506 cl::desc("Show internal debug dump")); 507 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", 511 "Text output"), 512 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", 513 "HTML output")), 514 cl::init(CoverageViewOptions::OutputFormat::Text)); 515 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")); 520 521 cl::OptionCategory FilteringCategory("Function filtering options"); 522 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)); 527 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)); 533 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)); 539 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)); 545 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)); 551 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)); 557 558 cl::opt<cl::boolOrDefault> UseColor( 559 "use-color", cl::desc("Emit colored output (default=autodetect)"), 560 cl::init(cl::BOU_UNSET)); 561 562 cl::list<std::string> DemanglerOpts( 563 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>")); 564 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; 569 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"; 576 ::exit(1); 577 } 578 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; 585 break; 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; 590 break; 591 } 592 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()); 599 return 1; 600 } 601 DemanglerOpts[0] = *DemanglerPathOrErr; 602 ViewOpts.DemanglerOpts.swap(DemanglerOpts); 603 } 604 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)); 614 } 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)); 633 } 634 635 if (!Arch.empty() && 636 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) { 637 error("Unknown architecture: " + Arch); 638 return 1; 639 } 640 CoverageArch = Arch; 641 642 for (const std::string &File : InputSourceFiles) 643 collectPaths(File); 644 645 if (DebugDumpCollectedPaths) { 646 for (const std::string &SF : SourceFiles) 647 outs() << SF << '\n'; 648 ::exit(0); 649 } 650 651 return 0; 652 }; 653 654 switch (Cmd) { 655 case Show: 656 return show(argc, argv, commandLineParser); 657 case Report: 658 return report(argc, argv, commandLineParser); 659 case Export: 660 return export_(argc, argv, commandLineParser); 661 } 662 return 0; 663 } 664 665 int CodeCoverageTool::show(int argc, const char **argv, 666 CommandLineParserType commandLineParser) { 667 668 cl::OptionCategory ViewCategory("Viewing options"); 669 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)); 674 675 cl::opt<bool> ShowRegions( 676 "show-regions", cl::Optional, 677 cl::desc("Show the execution counts for each region"), 678 cl::cat(ViewCategory)); 679 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)); 685 686 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, 687 cl::desc("Show expanded source regions"), 688 cl::cat(ViewCategory)); 689 690 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, 691 cl::desc("Show function instantiations"), 692 cl::cat(ViewCategory)); 693 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)); 699 700 cl::opt<uint32_t> TabSize( 701 "tab-size", cl::init(2), 702 cl::desc( 703 "Set tab expansion size for html coverage reports (default = 2)")); 704 705 cl::opt<std::string> ProjectTitle( 706 "project-title", cl::Optional, 707 cl::desc("Set project title for the coverage report")); 708 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)); 714 715 auto Err = commandLineParser(argc, argv); 716 if (Err) 717 return Err; 718 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; 729 730 if (ViewOpts.hasOutputDirectory()) { 731 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) { 732 error("Could not create output directory!", E.message()); 733 return 1; 734 } 735 } 736 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"); 740 return 1; 741 } 742 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; 749 750 auto Coverage = load(); 751 if (!Coverage) 752 return 1; 753 754 auto Printer = CoveragePrinter::create(ViewOpts); 755 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))); 760 return 1; 761 } 762 auto OS = std::move(OSOrErr.get()); 763 764 // Show functions. 765 for (const auto &Function : Coverage->getCoveredFunctions()) { 766 if (!Filters.matches(Function)) 767 continue; 768 769 auto mainView = createFunctionView(Function, *Coverage); 770 if (!mainView) { 771 warning("Could not read coverage for '" + Function.Name + "'."); 772 continue; 773 } 774 775 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true); 776 } 777 778 Printer->closeViewFile(std::move(OS)); 779 return 0; 780 } 781 782 // Show files 783 bool ShowFilenames = 784 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() || 785 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML); 786 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); 791 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))); 796 return 1; 797 } 798 } 799 800 // If NumThreads is not specified, auto-detect a good default. 801 if (NumThreads == 0) 802 NumThreads = 803 std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(), 804 unsigned(SourceFiles.size()))); 805 806 if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) { 807 for (const std::string &SourceFile : SourceFiles) 808 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(), 809 ShowFilenames); 810 } else { 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); 816 Pool.wait(); 817 } 818 819 return 0; 820 } 821 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")); 827 828 auto Err = commandLineParser(argc, argv); 829 if (Err) 830 return Err; 831 832 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) { 833 error("HTML output for summary reports is not yet supported."); 834 return 1; 835 } 836 837 auto Coverage = load(); 838 if (!Coverage) 839 return 1; 840 841 CoverageReport Report(ViewOpts, *Coverage.get()); 842 if (!ShowFunctionSummaries) 843 Report.renderFileReports(llvm::outs()); 844 else 845 Report.renderFunctionReports(SourceFiles, DC, llvm::outs()); 846 return 0; 847 } 848 849 int CodeCoverageTool::export_(int argc, const char **argv, 850 CommandLineParserType commandLineParser) { 851 852 auto Err = commandLineParser(argc, argv); 853 if (Err) 854 return Err; 855 856 if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) { 857 error("Coverage data can only be exported as textual JSON."); 858 return 1; 859 } 860 861 auto Coverage = load(); 862 if (!Coverage) { 863 error("Could not load coverage information"); 864 return 1; 865 } 866 867 exportCoverageDataToJson(*Coverage.get(), outs()); 868 869 return 0; 870 } 871 872 int showMain(int argc, const char *argv[]) { 873 CodeCoverageTool Tool; 874 return Tool.run(CodeCoverageTool::Show, argc, argv); 875 } 876 877 int reportMain(int argc, const char *argv[]) { 878 CodeCoverageTool Tool; 879 return Tool.run(CodeCoverageTool::Report, argc, argv); 880 } 881 882 int exportMain(int argc, const char *argv[]) { 883 CodeCoverageTool Tool; 884 return Tool.run(CodeCoverageTool::Export, argc, argv); 885 } 886