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/ScopedPrinter.h" 34 #include "llvm/Support/ThreadPool.h" 35 #include "llvm/Support/ToolOutputFile.h" 36 #include <functional> 37 #include <system_error> 38 39 using namespace llvm; 40 using namespace coverage; 41 42 void exportCoverageDataToJson(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 Convert \p Path into an absolute path and append it to the list 68 /// of collected paths. 69 void addCollectedPath(const std::string &Path); 70 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); 74 75 /// \brief Return a memory buffer for the given source file. 76 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); 77 78 /// \brief Create source views for the expansions of the view. 79 void attachExpansionSubViews(SourceCoverageView &View, 80 ArrayRef<ExpansionRecord> Expansions, 81 const CoverageMapping &Coverage); 82 83 /// \brief Create the source view of a particular function. 84 std::unique_ptr<SourceCoverageView> 85 createFunctionView(const FunctionRecord &Function, 86 const CoverageMapping &Coverage); 87 88 /// \brief Create the main source view of a particular source file. 89 std::unique_ptr<SourceCoverageView> 90 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage); 91 92 /// \brief Load the coverage mapping data. Return nullptr if an error occured. 93 std::unique_ptr<CoverageMapping> load(); 94 95 /// \brief Remove input source files which aren't mapped by \p Coverage. 96 void removeUnmappedInputs(const CoverageMapping &Coverage); 97 98 /// \brief If a demangler is available, demangle all symbol names. 99 void demangleSymbols(const CoverageMapping &Coverage); 100 101 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym. 102 StringRef getSymbolForHumans(StringRef Sym) const; 103 104 /// \brief Write out a source file view to the filesystem. 105 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage, 106 CoveragePrinter *Printer, bool ShowFilenames); 107 108 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType; 109 110 int show(int argc, const char **argv, 111 CommandLineParserType commandLineParser); 112 113 int report(int argc, const char **argv, 114 CommandLineParserType commandLineParser); 115 116 int export_(int argc, const char **argv, 117 CommandLineParserType commandLineParser); 118 119 std::vector<StringRef> ObjectFilenames; 120 CoverageViewOptions ViewOpts; 121 CoverageFiltersMatchAll Filters; 122 123 /// The path to the indexed profile. 124 std::string PGOFilename; 125 126 /// A list of input source files. 127 std::vector<std::string> SourceFiles; 128 129 /// Whether or not we're in -filename-equivalence mode. 130 bool CompareFilenamesOnly; 131 132 /// In -filename-equivalence mode, this maps absolute paths from the 133 /// coverage mapping data to input source files. 134 StringMap<std::string> RemappedFilenames; 135 136 /// The architecture the coverage mapping data targets. 137 std::string CoverageArch; 138 139 /// A cache for demangled symbol names. 140 StringMap<std::string> DemangledNames; 141 142 /// Errors and warnings which have not been printed. 143 std::mutex ErrsLock; 144 145 /// A container for input source file buffers. 146 std::mutex LoadedSourceFilesLock; 147 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> 148 LoadedSourceFiles; 149 }; 150 } 151 152 static std::string getErrorString(const Twine &Message, StringRef Whence, 153 bool Warning) { 154 std::string Str = (Warning ? "warning" : "error"); 155 Str += ": "; 156 if (!Whence.empty()) 157 Str += Whence.str() + ": "; 158 Str += Message.str() + "\n"; 159 return Str; 160 } 161 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); 166 } 167 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); 172 } 173 174 void CodeCoverageTool::addCollectedPath(const std::string &Path) { 175 if (CompareFilenamesOnly) { 176 SourceFiles.emplace_back(Path); 177 } else { 178 SmallString<128> EffectivePath(Path); 179 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) { 180 error(EC.message(), Path); 181 return; 182 } 183 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true); 184 SourceFiles.emplace_back(EffectivePath.str()); 185 } 186 } 187 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); 194 else 195 error("Missing source file", Path); 196 return; 197 } 198 199 if (llvm::sys::fs::is_regular_file(Status)) { 200 addCollectedPath(Path); 201 return; 202 } 203 204 if (llvm::sys::fs::is_directory(Status)) { 205 std::error_code EC; 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()); 210 } 211 if (EC) 212 warning(EC.message(), Path); 213 } 214 } 215 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; 224 } 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); 231 return EC; 232 } 233 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get())); 234 return *LoadedSourceFiles.back().second; 235 } 236 237 void CodeCoverageTool::attachExpansionSubViews( 238 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions, 239 const CoverageMapping &Coverage) { 240 if (!ViewOpts.ShowExpandedRegions) 241 return; 242 for (const auto &Expansion : Expansions) { 243 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); 244 if (ExpansionCoverage.empty()) 245 continue; 246 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); 247 if (!SourceBuffer) 248 continue; 249 250 auto SubViewExpansions = ExpansionCoverage.getExpansions(); 251 auto SubView = 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)); 256 } 257 } 258 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()) 264 return nullptr; 265 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); 266 if (!SourceBuffer) 267 return nullptr; 268 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); 274 275 return View; 276 } 277 278 std::unique_ptr<SourceCoverageView> 279 CodeCoverageTool::createSourceFileView(StringRef SourceFile, 280 const CoverageMapping &Coverage) { 281 auto SourceBuffer = getSourceFile(SourceFile); 282 if (!SourceBuffer) 283 return nullptr; 284 auto FileCoverage = Coverage.getCoverageForFile(SourceFile); 285 if (FileCoverage.empty()) 286 return nullptr; 287 288 auto Expansions = FileCoverage.getExpansions(); 289 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), 290 ViewOpts, std::move(FileCoverage)); 291 attachExpansionSubViews(*View, Expansions, Coverage); 292 293 for (const auto *Function : Coverage.getInstantiations(SourceFile)) { 294 std::unique_ptr<SourceCoverageView> SubView{nullptr}; 295 296 StringRef Funcname = getSymbolForHumans(Function->Name); 297 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); 304 } 305 306 unsigned FileID = Function->CountedRegions.front().FileID; 307 unsigned Line = 0; 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)); 312 } 313 return View; 314 } 315 316 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) { 317 sys::fs::file_status Status; 318 if (sys::fs::status(LHS, Status)) 319 return false; 320 auto LHSTime = Status.getLastModificationTime(); 321 if (sys::fs::status(RHS, Status)) 322 return false; 323 auto RHSTime = Status.getLastModificationTime(); 324 return LHSTime > RHSTime; 325 } 326 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", 331 ObjectFilename); 332 auto CoverageOrErr = 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(), ", ")); 337 return nullptr; 338 } 339 auto Coverage = std::move(CoverageOrErr.get()); 340 unsigned Mismatched = Coverage->getMismatchedCount(); 341 if (Mismatched) 342 warning(utostr(Mismatched) + " functions have mismatched data"); 343 344 if (!SourceFiles.empty()) 345 removeUnmappedInputs(*Coverage); 346 347 demangleSymbols(*Coverage); 348 349 return Coverage; 350 } 351 352 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) { 353 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles(); 354 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(), 362 SF); 363 }); 364 } else { 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; 370 SF = CF; 371 break; 372 } 373 } 374 } 375 UncoveredFilesIt = std::remove_if( 376 SourceFiles.begin(), SourceFiles.end(), 377 [&](const std::string &SF) { return !RemappedFilenames.count(SF); }); 378 } 379 380 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end()); 381 } 382 383 void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) { 384 if (!ViewOpts.hasDemangler()) 385 return; 386 387 // Pass function names to the demangler in a temporary file. 388 int InputFD; 389 SmallString<256> InputPath; 390 std::error_code EC = 391 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath); 392 if (EC) { 393 error(InputPath, EC.message()); 394 return; 395 } 396 tool_output_file InputTOF{InputPath, InputFD}; 397 398 unsigned NumSymbols = 0; 399 for (const auto &Function : Coverage.getCoveredFunctions()) { 400 InputTOF.os() << Function.Name << '\n'; 401 ++NumSymbols; 402 } 403 InputTOF.os().close(); 404 405 // Use another temporary file to store the demangler's output. 406 int OutputFD; 407 SmallString<256> OutputPath; 408 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD, 409 OutputPath); 410 if (EC) { 411 error(OutputPath, EC.message()); 412 return; 413 } 414 tool_output_file OutputTOF{OutputPath, OutputFD}; 415 OutputTOF.os().close(); 416 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(); 424 StringRef StderrRef; 425 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef}; 426 std::string ErrMsg; 427 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(), 428 /*env=*/nullptr, Redirects, /*secondsToWait=*/0, 429 /*memoryLimit=*/0, &ErrMsg); 430 if (RC) { 431 error(ErrMsg, ViewOpts.DemanglerOpts[0]); 432 return; 433 } 434 435 // Parse the demangler's output. 436 auto BufOrError = MemoryBuffer::getFile(OutputPath); 437 if (!BufOrError) { 438 error(OutputPath, BufOrError.getError().message()); 439 return; 440 } 441 442 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError); 443 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"); 450 return; 451 } 452 453 // Cache the demangled names. 454 unsigned I = 0; 455 for (const auto &Function : Coverage.getCoveredFunctions()) 456 DemangledNames[Function.Name] = Symbols[I++]; 457 } 458 459 StringRef CodeCoverageTool::getSymbolForHumans(StringRef Sym) const { 460 const auto DemangledName = DemangledNames.find(Sym); 461 if (DemangledName == DemangledNames.end()) 462 return Sym; 463 return DemangledName->getValue(); 464 } 465 466 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile, 467 CoverageMapping *Coverage, 468 CoveragePrinter *Printer, 469 bool ShowFilenames) { 470 auto View = createSourceFileView(SourceFile, *Coverage); 471 if (!View) { 472 warning("The file '" + SourceFile + "' isn't covered."); 473 return; 474 } 475 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))); 479 return; 480 } 481 auto OS = std::move(OSOrErr.get()); 482 483 View->print(*OS.get(), /*Wholefile=*/true, 484 /*ShowSourceName=*/ShowFilenames); 485 Printer->closeViewFile(std::move(OS)); 486 } 487 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.")); 491 492 cl::list<std::string> CovFilenames( 493 "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore, 494 cl::CommaSeparated); 495 496 cl::list<std::string> InputSourceFiles( 497 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); 498 499 cl::opt<bool> DebugDumpCollectedPaths( 500 "dump-collected-paths", cl::Optional, cl::Hidden, 501 cl::desc("Show the collected paths to source files")); 502 503 cl::opt<std::string, true> PGOFilename( 504 "instr-profile", cl::Required, cl::location(this->PGOFilename), 505 cl::desc( 506 "File with the profile data obtained after an instrumented run")); 507 508 cl::opt<std::string> Arch( 509 "arch", cl::desc("architecture of the coverage mapping binary")); 510 511 cl::opt<bool> DebugDump("dump", cl::Optional, 512 cl::desc("Show internal debug dump")); 513 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", 517 "Text output"), 518 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", 519 "HTML output")), 520 cl::init(CoverageViewOptions::OutputFormat::Text)); 521 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")); 526 527 cl::OptionCategory FilteringCategory("Function filtering options"); 528 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)); 533 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)); 539 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)); 545 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)); 551 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)); 557 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)); 563 564 cl::opt<cl::boolOrDefault> UseColor( 565 "use-color", cl::desc("Emit colored output (default=autodetect)"), 566 cl::init(cl::BOU_UNSET)); 567 568 cl::list<std::string> DemanglerOpts( 569 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>")); 570 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; 575 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 error("No filenames specified!"); 582 ::exit(1); 583 } 584 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; 591 break; 592 case CoverageViewOptions::OutputFormat::HTML: 593 if (UseColor == cl::BOU_FALSE) 594 error("Color output cannot be disabled when generating html."); 595 ViewOpts.Colors = true; 596 break; 597 } 598 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()); 605 return 1; 606 } 607 DemanglerOpts[0] = *DemanglerPathOrErr; 608 ViewOpts.DemanglerOpts.swap(DemanglerOpts); 609 } 610 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)); 620 } 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)); 639 } 640 641 if (!Arch.empty() && 642 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) { 643 error("Unknown architecture: " + Arch); 644 return 1; 645 } 646 CoverageArch = Arch; 647 648 for (const std::string &File : InputSourceFiles) 649 collectPaths(File); 650 651 if (DebugDumpCollectedPaths) { 652 for (const std::string &SF : SourceFiles) 653 outs() << SF << '\n'; 654 ::exit(0); 655 } 656 657 return 0; 658 }; 659 660 switch (Cmd) { 661 case Show: 662 return show(argc, argv, commandLineParser); 663 case Report: 664 return report(argc, argv, commandLineParser); 665 case Export: 666 return export_(argc, argv, commandLineParser); 667 } 668 return 0; 669 } 670 671 int CodeCoverageTool::show(int argc, const char **argv, 672 CommandLineParserType commandLineParser) { 673 674 cl::OptionCategory ViewCategory("Viewing options"); 675 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)); 680 681 cl::opt<bool> ShowRegions( 682 "show-regions", cl::Optional, 683 cl::desc("Show the execution counts for each region"), 684 cl::cat(ViewCategory)); 685 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)); 691 692 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, 693 cl::desc("Show expanded source regions"), 694 cl::cat(ViewCategory)); 695 696 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, 697 cl::desc("Show function instantiations"), 698 cl::cat(ViewCategory)); 699 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)); 705 706 cl::opt<uint32_t> TabSize( 707 "tab-size", cl::init(2), 708 cl::desc( 709 "Set tab expansion size for html coverage reports (default = 2)")); 710 711 cl::opt<std::string> ProjectTitle( 712 "project-title", cl::Optional, 713 cl::desc("Set project title for the coverage report")); 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 // 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(), 805 ShowFilenames); 806 } else { 807 // In -output-dir mode, it's safe to use multiple threads to print files. 808 ThreadPool Pool; 809 for (const std::string &SourceFile : SourceFiles) 810 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile, 811 Coverage.get(), Printer.get(), ShowFilenames); 812 Pool.wait(); 813 } 814 815 return 0; 816 } 817 818 int CodeCoverageTool::report(int argc, const char **argv, 819 CommandLineParserType commandLineParser) { 820 auto Err = commandLineParser(argc, argv); 821 if (Err) 822 return Err; 823 824 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) 825 error("HTML output for summary reports is not yet supported."); 826 827 auto Coverage = load(); 828 if (!Coverage) 829 return 1; 830 831 CoverageReport Report(ViewOpts, *Coverage.get()); 832 if (SourceFiles.empty()) 833 Report.renderFileReports(llvm::outs()); 834 else 835 Report.renderFunctionReports(SourceFiles, llvm::outs()); 836 return 0; 837 } 838 839 int CodeCoverageTool::export_(int argc, const char **argv, 840 CommandLineParserType commandLineParser) { 841 842 auto Err = commandLineParser(argc, argv); 843 if (Err) 844 return Err; 845 846 auto Coverage = load(); 847 if (!Coverage) { 848 error("Could not load coverage information"); 849 return 1; 850 } 851 852 exportCoverageDataToJson(*Coverage.get(), outs()); 853 854 return 0; 855 } 856 857 int showMain(int argc, const char *argv[]) { 858 CodeCoverageTool Tool; 859 return Tool.run(CodeCoverageTool::Show, argc, argv); 860 } 861 862 int reportMain(int argc, const char *argv[]) { 863 CodeCoverageTool Tool; 864 return Tool.run(CodeCoverageTool::Report, argc, argv); 865 } 866 867 int exportMain(int argc, const char *argv[]) { 868 CodeCoverageTool Tool; 869 return Tool.run(CodeCoverageTool::Export, argc, argv); 870 } 871