1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // The 'CodeCoverageTool' class implements a command line tool to analyze and 10 // report coverage information using the profiling instrumentation and code 11 // coverage mapping. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CoverageExporterJson.h" 16 #include "CoverageExporterLcov.h" 17 #include "CoverageFilters.h" 18 #include "CoverageReport.h" 19 #include "CoverageSummaryInfo.h" 20 #include "CoverageViewOptions.h" 21 #include "RenderingSupport.h" 22 #include "SourceCoverageView.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/ADT/Triple.h" 26 #include "llvm/ProfileData/Coverage/CoverageMapping.h" 27 #include "llvm/ProfileData/InstrProfReader.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/FileSystem.h" 30 #include "llvm/Support/Format.h" 31 #include "llvm/Support/MemoryBuffer.h" 32 #include "llvm/Support/Path.h" 33 #include "llvm/Support/Process.h" 34 #include "llvm/Support/Program.h" 35 #include "llvm/Support/ScopedPrinter.h" 36 #include "llvm/Support/SpecialCaseList.h" 37 #include "llvm/Support/ThreadPool.h" 38 #include "llvm/Support/Threading.h" 39 #include "llvm/Support/ToolOutputFile.h" 40 #include "llvm/Support/VirtualFileSystem.h" 41 42 #include <functional> 43 #include <map> 44 #include <system_error> 45 46 using namespace llvm; 47 using namespace coverage; 48 49 void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping, 50 const CoverageViewOptions &Options, 51 raw_ostream &OS); 52 53 namespace { 54 /// The implementation of the coverage tool. 55 class CodeCoverageTool { 56 public: 57 enum Command { 58 /// The show command. 59 Show, 60 /// The report command. 61 Report, 62 /// The export command. 63 Export 64 }; 65 66 int run(Command Cmd, int argc, const char **argv); 67 68 private: 69 /// Print the error message to the error output stream. 70 void error(const Twine &Message, StringRef Whence = ""); 71 72 /// Print the warning message to the error output stream. 73 void warning(const Twine &Message, StringRef Whence = ""); 74 75 /// Convert \p Path into an absolute path and append it to the list 76 /// of collected paths. 77 void addCollectedPath(const std::string &Path); 78 79 /// If \p Path is a regular file, collect the path. If it's a 80 /// directory, recursively collect all of the paths within the directory. 81 void collectPaths(const std::string &Path); 82 83 /// Check if the two given files are the same file. 84 bool isEquivalentFile(StringRef FilePath1, StringRef FilePath2); 85 86 /// Retrieve a file status with a cache. 87 Optional<sys::fs::file_status> getFileStatus(StringRef FilePath); 88 89 /// Return a memory buffer for the given source file. 90 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); 91 92 /// Create source views for the expansions of the view. 93 void attachExpansionSubViews(SourceCoverageView &View, 94 ArrayRef<ExpansionRecord> Expansions, 95 const CoverageMapping &Coverage); 96 97 /// Create source views for the branches of the view. 98 void attachBranchSubViews(SourceCoverageView &View, StringRef SourceName, 99 ArrayRef<CountedRegion> Branches, 100 const MemoryBuffer &File, 101 CoverageData &CoverageInfo); 102 103 /// Create the source view of a particular function. 104 std::unique_ptr<SourceCoverageView> 105 createFunctionView(const FunctionRecord &Function, 106 const CoverageMapping &Coverage); 107 108 /// Create the main source view of a particular source file. 109 std::unique_ptr<SourceCoverageView> 110 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage); 111 112 /// Load the coverage mapping data. Return nullptr if an error occurred. 113 std::unique_ptr<CoverageMapping> load(); 114 115 /// Create a mapping from files in the Coverage data to local copies 116 /// (path-equivalence). 117 void remapPathNames(const CoverageMapping &Coverage); 118 119 /// Remove input source files which aren't mapped by \p Coverage. 120 void removeUnmappedInputs(const CoverageMapping &Coverage); 121 122 /// If a demangler is available, demangle all symbol names. 123 void demangleSymbols(const CoverageMapping &Coverage); 124 125 /// Write out a source file view to the filesystem. 126 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage, 127 CoveragePrinter *Printer, bool ShowFilenames); 128 129 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType; 130 131 int doShow(int argc, const char **argv, 132 CommandLineParserType commandLineParser); 133 134 int doReport(int argc, const char **argv, 135 CommandLineParserType commandLineParser); 136 137 int doExport(int argc, const char **argv, 138 CommandLineParserType commandLineParser); 139 140 std::vector<StringRef> ObjectFilenames; 141 CoverageViewOptions ViewOpts; 142 CoverageFiltersMatchAll Filters; 143 CoverageFilters IgnoreFilenameFilters; 144 145 /// True if InputSourceFiles are provided. 146 bool HadSourceFiles = false; 147 148 /// The path to the indexed profile. 149 std::string PGOFilename; 150 151 /// A list of input source files. 152 std::vector<std::string> SourceFiles; 153 154 /// In -path-equivalence mode, this maps the absolute paths from the coverage 155 /// mapping data to the input source files. 156 StringMap<std::string> RemappedFilenames; 157 158 /// The coverage data path to be remapped from, and the source path to be 159 /// remapped to, when using -path-equivalence. 160 Optional<std::pair<std::string, std::string>> PathRemapping; 161 162 /// File status cache used when finding the same file. 163 StringMap<Optional<sys::fs::file_status>> FileStatusCache; 164 165 /// The architecture the coverage mapping data targets. 166 std::vector<StringRef> CoverageArches; 167 168 /// A cache for demangled symbols. 169 DemangleCache DC; 170 171 /// A lock which guards printing to stderr. 172 std::mutex ErrsLock; 173 174 /// A container for input source file buffers. 175 std::mutex LoadedSourceFilesLock; 176 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> 177 LoadedSourceFiles; 178 179 /// Allowlist from -name-allowlist to be used for filtering. 180 std::unique_ptr<SpecialCaseList> NameAllowlist; 181 }; 182 } 183 184 static std::string getErrorString(const Twine &Message, StringRef Whence, 185 bool Warning) { 186 std::string Str = (Warning ? "warning" : "error"); 187 Str += ": "; 188 if (!Whence.empty()) 189 Str += Whence.str() + ": "; 190 Str += Message.str() + "\n"; 191 return Str; 192 } 193 194 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) { 195 std::unique_lock<std::mutex> Guard{ErrsLock}; 196 ViewOpts.colored_ostream(errs(), raw_ostream::RED) 197 << getErrorString(Message, Whence, false); 198 } 199 200 void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) { 201 std::unique_lock<std::mutex> Guard{ErrsLock}; 202 ViewOpts.colored_ostream(errs(), raw_ostream::RED) 203 << getErrorString(Message, Whence, true); 204 } 205 206 void CodeCoverageTool::addCollectedPath(const std::string &Path) { 207 SmallString<128> EffectivePath(Path); 208 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) { 209 error(EC.message(), Path); 210 return; 211 } 212 sys::path::remove_dots(EffectivePath, /*remove_dot_dot=*/true); 213 if (!IgnoreFilenameFilters.matchesFilename(EffectivePath)) 214 SourceFiles.emplace_back(EffectivePath.str()); 215 HadSourceFiles = !SourceFiles.empty(); 216 } 217 218 void CodeCoverageTool::collectPaths(const std::string &Path) { 219 llvm::sys::fs::file_status Status; 220 llvm::sys::fs::status(Path, Status); 221 if (!llvm::sys::fs::exists(Status)) { 222 if (PathRemapping) 223 addCollectedPath(Path); 224 else 225 warning("Source file doesn't exist, proceeded by ignoring it.", Path); 226 return; 227 } 228 229 if (llvm::sys::fs::is_regular_file(Status)) { 230 addCollectedPath(Path); 231 return; 232 } 233 234 if (llvm::sys::fs::is_directory(Status)) { 235 std::error_code EC; 236 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E; 237 F != E; F.increment(EC)) { 238 239 auto Status = F->status(); 240 if (!Status) { 241 warning(Status.getError().message(), F->path()); 242 continue; 243 } 244 245 if (Status->type() == llvm::sys::fs::file_type::regular_file) 246 addCollectedPath(F->path()); 247 } 248 } 249 } 250 251 Optional<sys::fs::file_status> 252 CodeCoverageTool::getFileStatus(StringRef FilePath) { 253 auto It = FileStatusCache.try_emplace(FilePath); 254 auto &CachedStatus = It.first->getValue(); 255 if (!It.second) 256 return CachedStatus; 257 258 sys::fs::file_status Status; 259 if (!sys::fs::status(FilePath, Status)) 260 CachedStatus = Status; 261 return CachedStatus; 262 } 263 264 bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1, 265 StringRef FilePath2) { 266 auto Status1 = getFileStatus(FilePath1); 267 auto Status2 = getFileStatus(FilePath2); 268 return Status1.hasValue() && Status2.hasValue() && 269 sys::fs::equivalent(Status1.getValue(), Status2.getValue()); 270 } 271 272 ErrorOr<const MemoryBuffer &> 273 CodeCoverageTool::getSourceFile(StringRef SourceFile) { 274 // If we've remapped filenames, look up the real location for this file. 275 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock}; 276 if (!RemappedFilenames.empty()) { 277 auto Loc = RemappedFilenames.find(SourceFile); 278 if (Loc != RemappedFilenames.end()) 279 SourceFile = Loc->second; 280 } 281 for (const auto &Files : LoadedSourceFiles) 282 if (isEquivalentFile(SourceFile, Files.first)) 283 return *Files.second; 284 auto Buffer = MemoryBuffer::getFile(SourceFile); 285 if (auto EC = Buffer.getError()) { 286 error(EC.message(), SourceFile); 287 return EC; 288 } 289 LoadedSourceFiles.emplace_back(std::string(SourceFile), 290 std::move(Buffer.get())); 291 return *LoadedSourceFiles.back().second; 292 } 293 294 void CodeCoverageTool::attachExpansionSubViews( 295 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions, 296 const CoverageMapping &Coverage) { 297 if (!ViewOpts.ShowExpandedRegions) 298 return; 299 for (const auto &Expansion : Expansions) { 300 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); 301 if (ExpansionCoverage.empty()) 302 continue; 303 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); 304 if (!SourceBuffer) 305 continue; 306 307 auto SubViewBranches = ExpansionCoverage.getBranches(); 308 auto SubViewExpansions = ExpansionCoverage.getExpansions(); 309 auto SubView = 310 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(), 311 ViewOpts, std::move(ExpansionCoverage)); 312 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); 313 attachBranchSubViews(*SubView, Expansion.Function.Name, SubViewBranches, 314 SourceBuffer.get(), ExpansionCoverage); 315 View.addExpansion(Expansion.Region, std::move(SubView)); 316 } 317 } 318 319 void CodeCoverageTool::attachBranchSubViews(SourceCoverageView &View, 320 StringRef SourceName, 321 ArrayRef<CountedRegion> Branches, 322 const MemoryBuffer &File, 323 CoverageData &CoverageInfo) { 324 if (!ViewOpts.ShowBranchCounts && !ViewOpts.ShowBranchPercents) 325 return; 326 327 const auto *NextBranch = Branches.begin(); 328 const auto *EndBranch = Branches.end(); 329 330 // Group branches that have the same line number into the same subview. 331 while (NextBranch != EndBranch) { 332 std::vector<CountedRegion> ViewBranches; 333 unsigned CurrentLine = NextBranch->LineStart; 334 335 while (NextBranch != EndBranch && CurrentLine == NextBranch->LineStart) 336 ViewBranches.push_back(*NextBranch++); 337 338 if (!ViewBranches.empty()) { 339 auto SubView = SourceCoverageView::create(SourceName, File, ViewOpts, 340 std::move(CoverageInfo)); 341 View.addBranch(CurrentLine, ViewBranches, std::move(SubView)); 342 } 343 } 344 } 345 346 std::unique_ptr<SourceCoverageView> 347 CodeCoverageTool::createFunctionView(const FunctionRecord &Function, 348 const CoverageMapping &Coverage) { 349 auto FunctionCoverage = Coverage.getCoverageForFunction(Function); 350 if (FunctionCoverage.empty()) 351 return nullptr; 352 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); 353 if (!SourceBuffer) 354 return nullptr; 355 356 auto Branches = FunctionCoverage.getBranches(); 357 auto Expansions = FunctionCoverage.getExpansions(); 358 auto View = SourceCoverageView::create(DC.demangle(Function.Name), 359 SourceBuffer.get(), ViewOpts, 360 std::move(FunctionCoverage)); 361 attachExpansionSubViews(*View, Expansions, Coverage); 362 attachBranchSubViews(*View, DC.demangle(Function.Name), Branches, 363 SourceBuffer.get(), FunctionCoverage); 364 365 return View; 366 } 367 368 std::unique_ptr<SourceCoverageView> 369 CodeCoverageTool::createSourceFileView(StringRef SourceFile, 370 const CoverageMapping &Coverage) { 371 auto SourceBuffer = getSourceFile(SourceFile); 372 if (!SourceBuffer) 373 return nullptr; 374 auto FileCoverage = Coverage.getCoverageForFile(SourceFile); 375 if (FileCoverage.empty()) 376 return nullptr; 377 378 auto Branches = FileCoverage.getBranches(); 379 auto Expansions = FileCoverage.getExpansions(); 380 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), 381 ViewOpts, std::move(FileCoverage)); 382 attachExpansionSubViews(*View, Expansions, Coverage); 383 attachBranchSubViews(*View, SourceFile, Branches, SourceBuffer.get(), 384 FileCoverage); 385 if (!ViewOpts.ShowFunctionInstantiations) 386 return View; 387 388 for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) { 389 // Skip functions which have a single instantiation. 390 if (Group.size() < 2) 391 continue; 392 393 for (const FunctionRecord *Function : Group.getInstantiations()) { 394 std::unique_ptr<SourceCoverageView> SubView{nullptr}; 395 396 StringRef Funcname = DC.demangle(Function->Name); 397 398 if (Function->ExecutionCount > 0) { 399 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function); 400 auto SubViewExpansions = SubViewCoverage.getExpansions(); 401 auto SubViewBranches = SubViewCoverage.getBranches(); 402 SubView = SourceCoverageView::create( 403 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage)); 404 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); 405 attachBranchSubViews(*SubView, SourceFile, SubViewBranches, 406 SourceBuffer.get(), SubViewCoverage); 407 } 408 409 unsigned FileID = Function->CountedRegions.front().FileID; 410 unsigned Line = 0; 411 for (const auto &CR : Function->CountedRegions) 412 if (CR.FileID == FileID) 413 Line = std::max(CR.LineEnd, Line); 414 View->addInstantiation(Funcname, Line, std::move(SubView)); 415 } 416 } 417 return View; 418 } 419 420 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) { 421 sys::fs::file_status Status; 422 if (sys::fs::status(LHS, Status)) 423 return false; 424 auto LHSTime = Status.getLastModificationTime(); 425 if (sys::fs::status(RHS, Status)) 426 return false; 427 auto RHSTime = Status.getLastModificationTime(); 428 return LHSTime > RHSTime; 429 } 430 431 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() { 432 for (StringRef ObjectFilename : ObjectFilenames) 433 if (modifiedTimeGT(ObjectFilename, PGOFilename)) 434 warning("profile data may be out of date - object is newer", 435 ObjectFilename); 436 auto CoverageOrErr = 437 CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches, 438 ViewOpts.CompilationDirectory); 439 if (Error E = CoverageOrErr.takeError()) { 440 error("Failed to load coverage: " + toString(std::move(E)), 441 join(ObjectFilenames.begin(), ObjectFilenames.end(), ", ")); 442 return nullptr; 443 } 444 auto Coverage = std::move(CoverageOrErr.get()); 445 unsigned Mismatched = Coverage->getMismatchedCount(); 446 if (Mismatched) { 447 warning(Twine(Mismatched) + " functions have mismatched data"); 448 449 if (ViewOpts.Debug) { 450 for (const auto &HashMismatch : Coverage->getHashMismatches()) 451 errs() << "hash-mismatch: " 452 << "No profile record found for '" << HashMismatch.first << "'" 453 << " with hash = 0x" << Twine::utohexstr(HashMismatch.second) 454 << '\n'; 455 } 456 } 457 458 remapPathNames(*Coverage); 459 460 if (!SourceFiles.empty()) 461 removeUnmappedInputs(*Coverage); 462 463 demangleSymbols(*Coverage); 464 465 return Coverage; 466 } 467 468 void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) { 469 if (!PathRemapping) 470 return; 471 472 // Convert remapping paths to native paths with trailing seperators. 473 auto nativeWithTrailing = [](StringRef Path) -> std::string { 474 if (Path.empty()) 475 return ""; 476 SmallString<128> NativePath; 477 sys::path::native(Path, NativePath); 478 sys::path::remove_dots(NativePath, true); 479 if (!NativePath.empty() && !sys::path::is_separator(NativePath.back())) 480 NativePath += sys::path::get_separator(); 481 return NativePath.c_str(); 482 }; 483 std::string RemapFrom = nativeWithTrailing(PathRemapping->first); 484 std::string RemapTo = nativeWithTrailing(PathRemapping->second); 485 486 // Create a mapping from coverage data file paths to local paths. 487 for (StringRef Filename : Coverage.getUniqueSourceFiles()) { 488 SmallString<128> NativeFilename; 489 sys::path::native(Filename, NativeFilename); 490 sys::path::remove_dots(NativeFilename, true); 491 if (NativeFilename.startswith(RemapFrom)) { 492 RemappedFilenames[Filename] = 493 RemapTo + NativeFilename.substr(RemapFrom.size()).str(); 494 } 495 } 496 497 // Convert input files from local paths to coverage data file paths. 498 StringMap<std::string> InvRemappedFilenames; 499 for (const auto &RemappedFilename : RemappedFilenames) 500 InvRemappedFilenames[RemappedFilename.getValue()] = 501 std::string(RemappedFilename.getKey()); 502 503 for (std::string &Filename : SourceFiles) { 504 SmallString<128> NativeFilename; 505 sys::path::native(Filename, NativeFilename); 506 auto CovFileName = InvRemappedFilenames.find(NativeFilename); 507 if (CovFileName != InvRemappedFilenames.end()) 508 Filename = CovFileName->second; 509 } 510 } 511 512 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) { 513 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles(); 514 515 // The user may have specified source files which aren't in the coverage 516 // mapping. Filter these files away. 517 llvm::erase_if(SourceFiles, [&](const std::string &SF) { 518 return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(), SF); 519 }); 520 } 521 522 void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) { 523 if (!ViewOpts.hasDemangler()) 524 return; 525 526 // Pass function names to the demangler in a temporary file. 527 int InputFD; 528 SmallString<256> InputPath; 529 std::error_code EC = 530 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath); 531 if (EC) { 532 error(InputPath, EC.message()); 533 return; 534 } 535 ToolOutputFile InputTOF{InputPath, InputFD}; 536 537 unsigned NumSymbols = 0; 538 for (const auto &Function : Coverage.getCoveredFunctions()) { 539 InputTOF.os() << Function.Name << '\n'; 540 ++NumSymbols; 541 } 542 InputTOF.os().close(); 543 544 // Use another temporary file to store the demangler's output. 545 int OutputFD; 546 SmallString<256> OutputPath; 547 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD, 548 OutputPath); 549 if (EC) { 550 error(OutputPath, EC.message()); 551 return; 552 } 553 ToolOutputFile OutputTOF{OutputPath, OutputFD}; 554 OutputTOF.os().close(); 555 556 // Invoke the demangler. 557 std::vector<StringRef> ArgsV; 558 for (StringRef Arg : ViewOpts.DemanglerOpts) 559 ArgsV.push_back(Arg); 560 Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}}; 561 std::string ErrMsg; 562 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV, 563 /*env=*/None, Redirects, /*secondsToWait=*/0, 564 /*memoryLimit=*/0, &ErrMsg); 565 if (RC) { 566 error(ErrMsg, ViewOpts.DemanglerOpts[0]); 567 return; 568 } 569 570 // Parse the demangler's output. 571 auto BufOrError = MemoryBuffer::getFile(OutputPath); 572 if (!BufOrError) { 573 error(OutputPath, BufOrError.getError().message()); 574 return; 575 } 576 577 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError); 578 579 SmallVector<StringRef, 8> Symbols; 580 StringRef DemanglerData = DemanglerBuf->getBuffer(); 581 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols, 582 /*KeepEmpty=*/false); 583 if (Symbols.size() != NumSymbols) { 584 error("Demangler did not provide expected number of symbols"); 585 return; 586 } 587 588 // Cache the demangled names. 589 unsigned I = 0; 590 for (const auto &Function : Coverage.getCoveredFunctions()) 591 // On Windows, lines in the demangler's output file end with "\r\n". 592 // Splitting by '\n' keeps '\r's, so cut them now. 593 DC.DemangledNames[Function.Name] = std::string(Symbols[I++].rtrim()); 594 } 595 596 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile, 597 CoverageMapping *Coverage, 598 CoveragePrinter *Printer, 599 bool ShowFilenames) { 600 auto View = createSourceFileView(SourceFile, *Coverage); 601 if (!View) { 602 warning("The file '" + SourceFile + "' isn't covered."); 603 return; 604 } 605 606 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false); 607 if (Error E = OSOrErr.takeError()) { 608 error("Could not create view file!", toString(std::move(E))); 609 return; 610 } 611 auto OS = std::move(OSOrErr.get()); 612 613 View->print(*OS.get(), /*Wholefile=*/true, 614 /*ShowSourceName=*/ShowFilenames, 615 /*ShowTitle=*/ViewOpts.hasOutputDirectory()); 616 Printer->closeViewFile(std::move(OS)); 617 } 618 619 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { 620 cl::opt<std::string> CovFilename( 621 cl::Positional, cl::desc("Covered executable or object file.")); 622 623 cl::list<std::string> CovFilenames( 624 "object", cl::desc("Coverage executable or object file")); 625 626 cl::opt<bool> DebugDumpCollectedObjects( 627 "dump-collected-objects", cl::Optional, cl::Hidden, 628 cl::desc("Show the collected coverage object files")); 629 630 cl::list<std::string> InputSourceFiles(cl::Positional, 631 cl::desc("<Source files>")); 632 633 cl::opt<bool> DebugDumpCollectedPaths( 634 "dump-collected-paths", cl::Optional, cl::Hidden, 635 cl::desc("Show the collected paths to source files")); 636 637 cl::opt<std::string, true> PGOFilename( 638 "instr-profile", cl::Required, cl::location(this->PGOFilename), 639 cl::desc( 640 "File with the profile data obtained after an instrumented run")); 641 642 cl::list<std::string> Arches( 643 "arch", cl::desc("architectures of the coverage mapping binaries")); 644 645 cl::opt<bool> DebugDump("dump", cl::Optional, 646 cl::desc("Show internal debug dump")); 647 648 cl::opt<CoverageViewOptions::OutputFormat> Format( 649 "format", cl::desc("Output format for line-based coverage reports"), 650 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text", 651 "Text output"), 652 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", 653 "HTML output"), 654 clEnumValN(CoverageViewOptions::OutputFormat::Lcov, "lcov", 655 "lcov tracefile output")), 656 cl::init(CoverageViewOptions::OutputFormat::Text)); 657 658 cl::opt<std::string> PathRemap( 659 "path-equivalence", cl::Optional, 660 cl::desc("<from>,<to> Map coverage data paths to local source file " 661 "paths")); 662 663 cl::OptionCategory FilteringCategory("Function filtering options"); 664 665 cl::list<std::string> NameFilters( 666 "name", cl::Optional, 667 cl::desc("Show code coverage only for functions with the given name"), 668 cl::cat(FilteringCategory)); 669 670 cl::list<std::string> NameFilterFiles( 671 "name-allowlist", cl::Optional, 672 cl::desc("Show code coverage only for functions listed in the given " 673 "file"), 674 cl::cat(FilteringCategory)); 675 676 // Allow for accepting previous option name. 677 cl::list<std::string> NameFilterFilesDeprecated( 678 "name-whitelist", cl::Optional, cl::Hidden, 679 cl::desc("Show code coverage only for functions listed in the given " 680 "file. Deprecated, use -name-allowlist instead"), 681 cl::cat(FilteringCategory)); 682 683 cl::list<std::string> NameRegexFilters( 684 "name-regex", cl::Optional, 685 cl::desc("Show code coverage only for functions that match the given " 686 "regular expression"), 687 cl::cat(FilteringCategory)); 688 689 cl::list<std::string> IgnoreFilenameRegexFilters( 690 "ignore-filename-regex", cl::Optional, 691 cl::desc("Skip source code files with file paths that match the given " 692 "regular expression"), 693 cl::cat(FilteringCategory)); 694 695 cl::opt<double> RegionCoverageLtFilter( 696 "region-coverage-lt", cl::Optional, 697 cl::desc("Show code coverage only for functions with region coverage " 698 "less than the given threshold"), 699 cl::cat(FilteringCategory)); 700 701 cl::opt<double> RegionCoverageGtFilter( 702 "region-coverage-gt", cl::Optional, 703 cl::desc("Show code coverage only for functions with region coverage " 704 "greater than the given threshold"), 705 cl::cat(FilteringCategory)); 706 707 cl::opt<double> LineCoverageLtFilter( 708 "line-coverage-lt", cl::Optional, 709 cl::desc("Show code coverage only for functions with line coverage less " 710 "than the given threshold"), 711 cl::cat(FilteringCategory)); 712 713 cl::opt<double> LineCoverageGtFilter( 714 "line-coverage-gt", cl::Optional, 715 cl::desc("Show code coverage only for functions with line coverage " 716 "greater than the given threshold"), 717 cl::cat(FilteringCategory)); 718 719 cl::opt<cl::boolOrDefault> UseColor( 720 "use-color", cl::desc("Emit colored output (default=autodetect)"), 721 cl::init(cl::BOU_UNSET)); 722 723 cl::list<std::string> DemanglerOpts( 724 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>")); 725 726 cl::opt<bool> RegionSummary( 727 "show-region-summary", cl::Optional, 728 cl::desc("Show region statistics in summary table"), 729 cl::init(true)); 730 731 cl::opt<bool> BranchSummary( 732 "show-branch-summary", cl::Optional, 733 cl::desc("Show branch condition statistics in summary table"), 734 cl::init(true)); 735 736 cl::opt<bool> InstantiationSummary( 737 "show-instantiation-summary", cl::Optional, 738 cl::desc("Show instantiation statistics in summary table")); 739 740 cl::opt<bool> SummaryOnly( 741 "summary-only", cl::Optional, 742 cl::desc("Export only summary information for each source file")); 743 744 cl::opt<unsigned> NumThreads( 745 "num-threads", cl::init(0), 746 cl::desc("Number of merge threads to use (default: autodetect)")); 747 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), 748 cl::aliasopt(NumThreads)); 749 750 cl::opt<std::string> CompilationDirectory( 751 "compilation-dir", cl::init(""), 752 cl::desc("Directory used as a base for relative coverage mapping paths")); 753 754 auto commandLineParser = [&, this](int argc, const char **argv) -> int { 755 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); 756 ViewOpts.Debug = DebugDump; 757 758 if (!CovFilename.empty()) 759 ObjectFilenames.emplace_back(CovFilename); 760 for (const std::string &Filename : CovFilenames) 761 ObjectFilenames.emplace_back(Filename); 762 if (ObjectFilenames.empty()) { 763 errs() << "No filenames specified!\n"; 764 ::exit(1); 765 } 766 767 if (DebugDumpCollectedObjects) { 768 for (StringRef OF : ObjectFilenames) 769 outs() << OF << '\n'; 770 ::exit(0); 771 } 772 773 ViewOpts.Format = Format; 774 switch (ViewOpts.Format) { 775 case CoverageViewOptions::OutputFormat::Text: 776 ViewOpts.Colors = UseColor == cl::BOU_UNSET 777 ? sys::Process::StandardOutHasColors() 778 : UseColor == cl::BOU_TRUE; 779 break; 780 case CoverageViewOptions::OutputFormat::HTML: 781 if (UseColor == cl::BOU_FALSE) 782 errs() << "Color output cannot be disabled when generating html.\n"; 783 ViewOpts.Colors = true; 784 break; 785 case CoverageViewOptions::OutputFormat::Lcov: 786 if (UseColor == cl::BOU_TRUE) 787 errs() << "Color output cannot be enabled when generating lcov.\n"; 788 ViewOpts.Colors = false; 789 break; 790 } 791 792 // If path-equivalence was given and is a comma seperated pair then set 793 // PathRemapping. 794 if (!PathRemap.empty()) { 795 auto EquivPair = StringRef(PathRemap).split(','); 796 if (EquivPair.first.empty() || EquivPair.second.empty()) { 797 error("invalid argument '" + PathRemap + 798 "', must be in format 'from,to'", 799 "-path-equivalence"); 800 return 1; 801 } 802 803 PathRemapping = {std::string(EquivPair.first), 804 std::string(EquivPair.second)}; 805 } 806 807 // If a demangler is supplied, check if it exists and register it. 808 if (!DemanglerOpts.empty()) { 809 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]); 810 if (!DemanglerPathOrErr) { 811 error("Could not find the demangler!", 812 DemanglerPathOrErr.getError().message()); 813 return 1; 814 } 815 DemanglerOpts[0] = *DemanglerPathOrErr; 816 ViewOpts.DemanglerOpts.swap(DemanglerOpts); 817 } 818 819 // Read in -name-allowlist files. 820 if (!NameFilterFiles.empty() || !NameFilterFilesDeprecated.empty()) { 821 std::string SpecialCaseListErr; 822 if (!NameFilterFiles.empty()) 823 NameAllowlist = SpecialCaseList::create( 824 NameFilterFiles, *vfs::getRealFileSystem(), SpecialCaseListErr); 825 if (!NameFilterFilesDeprecated.empty()) 826 NameAllowlist = SpecialCaseList::create(NameFilterFilesDeprecated, 827 *vfs::getRealFileSystem(), 828 SpecialCaseListErr); 829 830 if (!NameAllowlist) 831 error(SpecialCaseListErr); 832 } 833 834 // Create the function filters 835 if (!NameFilters.empty() || NameAllowlist || !NameRegexFilters.empty()) { 836 auto NameFilterer = std::make_unique<CoverageFilters>(); 837 for (const auto &Name : NameFilters) 838 NameFilterer->push_back(std::make_unique<NameCoverageFilter>(Name)); 839 if (NameAllowlist) { 840 if (!NameFilterFiles.empty()) 841 NameFilterer->push_back( 842 std::make_unique<NameAllowlistCoverageFilter>(*NameAllowlist)); 843 if (!NameFilterFilesDeprecated.empty()) 844 NameFilterer->push_back( 845 std::make_unique<NameWhitelistCoverageFilter>(*NameAllowlist)); 846 } 847 for (const auto &Regex : NameRegexFilters) 848 NameFilterer->push_back( 849 std::make_unique<NameRegexCoverageFilter>(Regex)); 850 Filters.push_back(std::move(NameFilterer)); 851 } 852 853 if (RegionCoverageLtFilter.getNumOccurrences() || 854 RegionCoverageGtFilter.getNumOccurrences() || 855 LineCoverageLtFilter.getNumOccurrences() || 856 LineCoverageGtFilter.getNumOccurrences()) { 857 auto StatFilterer = std::make_unique<CoverageFilters>(); 858 if (RegionCoverageLtFilter.getNumOccurrences()) 859 StatFilterer->push_back(std::make_unique<RegionCoverageFilter>( 860 RegionCoverageFilter::LessThan, RegionCoverageLtFilter)); 861 if (RegionCoverageGtFilter.getNumOccurrences()) 862 StatFilterer->push_back(std::make_unique<RegionCoverageFilter>( 863 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter)); 864 if (LineCoverageLtFilter.getNumOccurrences()) 865 StatFilterer->push_back(std::make_unique<LineCoverageFilter>( 866 LineCoverageFilter::LessThan, LineCoverageLtFilter)); 867 if (LineCoverageGtFilter.getNumOccurrences()) 868 StatFilterer->push_back(std::make_unique<LineCoverageFilter>( 869 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter)); 870 Filters.push_back(std::move(StatFilterer)); 871 } 872 873 // Create the ignore filename filters. 874 for (const auto &RE : IgnoreFilenameRegexFilters) 875 IgnoreFilenameFilters.push_back( 876 std::make_unique<NameRegexCoverageFilter>(RE)); 877 878 if (!Arches.empty()) { 879 for (const std::string &Arch : Arches) { 880 if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) { 881 error("Unknown architecture: " + Arch); 882 return 1; 883 } 884 CoverageArches.emplace_back(Arch); 885 } 886 if (CoverageArches.size() == 1) 887 CoverageArches.insert(CoverageArches.end(), ObjectFilenames.size() - 1, 888 CoverageArches[0]); 889 if (CoverageArches.size() != ObjectFilenames.size()) { 890 error("Number of architectures doesn't match the number of objects"); 891 return 1; 892 } 893 } 894 895 // IgnoreFilenameFilters are applied even when InputSourceFiles specified. 896 for (const std::string &File : InputSourceFiles) 897 collectPaths(File); 898 899 if (DebugDumpCollectedPaths) { 900 for (const std::string &SF : SourceFiles) 901 outs() << SF << '\n'; 902 ::exit(0); 903 } 904 905 ViewOpts.ShowBranchSummary = BranchSummary; 906 ViewOpts.ShowRegionSummary = RegionSummary; 907 ViewOpts.ShowInstantiationSummary = InstantiationSummary; 908 ViewOpts.ExportSummaryOnly = SummaryOnly; 909 ViewOpts.NumThreads = NumThreads; 910 ViewOpts.CompilationDirectory = CompilationDirectory; 911 912 return 0; 913 }; 914 915 switch (Cmd) { 916 case Show: 917 return doShow(argc, argv, commandLineParser); 918 case Report: 919 return doReport(argc, argv, commandLineParser); 920 case Export: 921 return doExport(argc, argv, commandLineParser); 922 } 923 return 0; 924 } 925 926 int CodeCoverageTool::doShow(int argc, const char **argv, 927 CommandLineParserType commandLineParser) { 928 929 cl::OptionCategory ViewCategory("Viewing options"); 930 931 cl::opt<bool> ShowLineExecutionCounts( 932 "show-line-counts", cl::Optional, 933 cl::desc("Show the execution counts for each line"), cl::init(true), 934 cl::cat(ViewCategory)); 935 936 cl::opt<bool> ShowRegions( 937 "show-regions", cl::Optional, 938 cl::desc("Show the execution counts for each region"), 939 cl::cat(ViewCategory)); 940 941 cl::opt<CoverageViewOptions::BranchOutputType> ShowBranches( 942 "show-branches", cl::Optional, 943 cl::desc("Show coverage for branch conditions"), cl::cat(ViewCategory), 944 cl::values(clEnumValN(CoverageViewOptions::BranchOutputType::Count, 945 "count", "Show True/False counts"), 946 clEnumValN(CoverageViewOptions::BranchOutputType::Percent, 947 "percent", "Show True/False percent")), 948 cl::init(CoverageViewOptions::BranchOutputType::Off)); 949 950 cl::opt<bool> ShowBestLineRegionsCounts( 951 "show-line-counts-or-regions", cl::Optional, 952 cl::desc("Show the execution counts for each line, or the execution " 953 "counts for each region on lines that have multiple regions"), 954 cl::cat(ViewCategory)); 955 956 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, 957 cl::desc("Show expanded source regions"), 958 cl::cat(ViewCategory)); 959 960 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, 961 cl::desc("Show function instantiations"), 962 cl::init(true), cl::cat(ViewCategory)); 963 964 cl::opt<std::string> ShowOutputDirectory( 965 "output-dir", cl::init(""), 966 cl::desc("Directory in which coverage information is written out")); 967 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"), 968 cl::aliasopt(ShowOutputDirectory)); 969 970 cl::opt<uint32_t> TabSize( 971 "tab-size", cl::init(2), 972 cl::desc( 973 "Set tab expansion size for html coverage reports (default = 2)")); 974 975 cl::opt<std::string> ProjectTitle( 976 "project-title", cl::Optional, 977 cl::desc("Set project title for the coverage report")); 978 979 cl::opt<std::string> CovWatermark( 980 "coverage-watermark", cl::Optional, 981 cl::desc("<high>,<low> value indicate thresholds for high and low" 982 "coverage watermark")); 983 984 auto Err = commandLineParser(argc, argv); 985 if (Err) 986 return Err; 987 988 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) { 989 error("Lcov format should be used with 'llvm-cov export'."); 990 return 1; 991 } 992 993 ViewOpts.HighCovWatermark = 100.0; 994 ViewOpts.LowCovWatermark = 80.0; 995 if (!CovWatermark.empty()) { 996 auto WaterMarkPair = StringRef(CovWatermark).split(','); 997 if (WaterMarkPair.first.empty() || WaterMarkPair.second.empty()) { 998 error("invalid argument '" + CovWatermark + 999 "', must be in format 'high,low'", 1000 "-coverage-watermark"); 1001 return 1; 1002 } 1003 1004 char *EndPointer = nullptr; 1005 ViewOpts.HighCovWatermark = 1006 strtod(WaterMarkPair.first.begin(), &EndPointer); 1007 if (EndPointer != WaterMarkPair.first.end()) { 1008 error("invalid number '" + WaterMarkPair.first + 1009 "', invalid value for 'high'", 1010 "-coverage-watermark"); 1011 return 1; 1012 } 1013 1014 ViewOpts.LowCovWatermark = 1015 strtod(WaterMarkPair.second.begin(), &EndPointer); 1016 if (EndPointer != WaterMarkPair.second.end()) { 1017 error("invalid number '" + WaterMarkPair.second + 1018 "', invalid value for 'low'", 1019 "-coverage-watermark"); 1020 return 1; 1021 } 1022 1023 if (ViewOpts.HighCovWatermark > 100 || ViewOpts.LowCovWatermark < 0 || 1024 ViewOpts.HighCovWatermark <= ViewOpts.LowCovWatermark) { 1025 error( 1026 "invalid number range '" + CovWatermark + 1027 "', must be both high and low should be between 0-100, and high " 1028 "> low", 1029 "-coverage-watermark"); 1030 return 1; 1031 } 1032 } 1033 1034 ViewOpts.ShowLineNumbers = true; 1035 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 || 1036 !ShowRegions || ShowBestLineRegionsCounts; 1037 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; 1038 ViewOpts.ShowExpandedRegions = ShowExpansions; 1039 ViewOpts.ShowBranchCounts = 1040 ShowBranches == CoverageViewOptions::BranchOutputType::Count; 1041 ViewOpts.ShowBranchPercents = 1042 ShowBranches == CoverageViewOptions::BranchOutputType::Percent; 1043 ViewOpts.ShowFunctionInstantiations = ShowInstantiations; 1044 ViewOpts.ShowOutputDirectory = ShowOutputDirectory; 1045 ViewOpts.TabSize = TabSize; 1046 ViewOpts.ProjectTitle = ProjectTitle; 1047 1048 if (ViewOpts.hasOutputDirectory()) { 1049 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) { 1050 error("Could not create output directory!", E.message()); 1051 return 1; 1052 } 1053 } 1054 1055 sys::fs::file_status Status; 1056 if (std::error_code EC = sys::fs::status(PGOFilename, Status)) { 1057 error("Could not read profile data!", EC.message()); 1058 return 1; 1059 } 1060 1061 auto ModifiedTime = Status.getLastModificationTime(); 1062 std::string ModifiedTimeStr = to_string(ModifiedTime); 1063 size_t found = ModifiedTimeStr.rfind(':'); 1064 ViewOpts.CreatedTimeStr = (found != std::string::npos) 1065 ? "Created: " + ModifiedTimeStr.substr(0, found) 1066 : "Created: " + ModifiedTimeStr; 1067 1068 auto Coverage = load(); 1069 if (!Coverage) 1070 return 1; 1071 1072 auto Printer = CoveragePrinter::create(ViewOpts); 1073 1074 if (SourceFiles.empty() && !HadSourceFiles) 1075 // Get the source files from the function coverage mapping. 1076 for (StringRef Filename : Coverage->getUniqueSourceFiles()) { 1077 if (!IgnoreFilenameFilters.matchesFilename(Filename)) 1078 SourceFiles.push_back(std::string(Filename)); 1079 } 1080 1081 // Create an index out of the source files. 1082 if (ViewOpts.hasOutputDirectory()) { 1083 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) { 1084 error("Could not create index file!", toString(std::move(E))); 1085 return 1; 1086 } 1087 } 1088 1089 if (!Filters.empty()) { 1090 // Build the map of filenames to functions. 1091 std::map<llvm::StringRef, std::vector<const FunctionRecord *>> 1092 FilenameFunctionMap; 1093 for (const auto &SourceFile : SourceFiles) 1094 for (const auto &Function : Coverage->getCoveredFunctions(SourceFile)) 1095 if (Filters.matches(*Coverage.get(), Function)) 1096 FilenameFunctionMap[SourceFile].push_back(&Function); 1097 1098 // Only print filter matching functions for each file. 1099 for (const auto &FileFunc : FilenameFunctionMap) { 1100 StringRef File = FileFunc.first; 1101 const auto &Functions = FileFunc.second; 1102 1103 auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false); 1104 if (Error E = OSOrErr.takeError()) { 1105 error("Could not create view file!", toString(std::move(E))); 1106 return 1; 1107 } 1108 auto OS = std::move(OSOrErr.get()); 1109 1110 bool ShowTitle = ViewOpts.hasOutputDirectory(); 1111 for (const auto *Function : Functions) { 1112 auto FunctionView = createFunctionView(*Function, *Coverage); 1113 if (!FunctionView) { 1114 warning("Could not read coverage for '" + Function->Name + "'."); 1115 continue; 1116 } 1117 FunctionView->print(*OS.get(), /*WholeFile=*/false, 1118 /*ShowSourceName=*/true, ShowTitle); 1119 ShowTitle = false; 1120 } 1121 1122 Printer->closeViewFile(std::move(OS)); 1123 } 1124 return 0; 1125 } 1126 1127 // Show files 1128 bool ShowFilenames = 1129 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() || 1130 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML); 1131 1132 ThreadPoolStrategy S = hardware_concurrency(ViewOpts.NumThreads); 1133 if (ViewOpts.NumThreads == 0) { 1134 // If NumThreads is not specified, create one thread for each input, up to 1135 // the number of hardware cores. 1136 S = heavyweight_hardware_concurrency(SourceFiles.size()); 1137 S.Limit = true; 1138 } 1139 1140 if (!ViewOpts.hasOutputDirectory() || S.ThreadsRequested == 1) { 1141 for (const std::string &SourceFile : SourceFiles) 1142 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(), 1143 ShowFilenames); 1144 } else { 1145 // In -output-dir mode, it's safe to use multiple threads to print files. 1146 ThreadPool Pool(S); 1147 for (const std::string &SourceFile : SourceFiles) 1148 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile, 1149 Coverage.get(), Printer.get(), ShowFilenames); 1150 Pool.wait(); 1151 } 1152 1153 return 0; 1154 } 1155 1156 int CodeCoverageTool::doReport(int argc, const char **argv, 1157 CommandLineParserType commandLineParser) { 1158 cl::opt<bool> ShowFunctionSummaries( 1159 "show-functions", cl::Optional, cl::init(false), 1160 cl::desc("Show coverage summaries for each function")); 1161 1162 auto Err = commandLineParser(argc, argv); 1163 if (Err) 1164 return Err; 1165 1166 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) { 1167 error("HTML output for summary reports is not yet supported."); 1168 return 1; 1169 } else if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) { 1170 error("Lcov format should be used with 'llvm-cov export'."); 1171 return 1; 1172 } 1173 1174 auto Coverage = load(); 1175 if (!Coverage) 1176 return 1; 1177 1178 CoverageReport Report(ViewOpts, *Coverage.get()); 1179 if (!ShowFunctionSummaries) { 1180 if (SourceFiles.empty()) 1181 Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters); 1182 else 1183 Report.renderFileReports(llvm::outs(), SourceFiles); 1184 } else { 1185 if (SourceFiles.empty()) { 1186 error("Source files must be specified when -show-functions=true is " 1187 "specified"); 1188 return 1; 1189 } 1190 1191 Report.renderFunctionReports(SourceFiles, DC, llvm::outs()); 1192 } 1193 return 0; 1194 } 1195 1196 int CodeCoverageTool::doExport(int argc, const char **argv, 1197 CommandLineParserType commandLineParser) { 1198 1199 cl::OptionCategory ExportCategory("Exporting options"); 1200 1201 cl::opt<bool> SkipExpansions("skip-expansions", cl::Optional, 1202 cl::desc("Don't export expanded source regions"), 1203 cl::cat(ExportCategory)); 1204 1205 cl::opt<bool> SkipFunctions("skip-functions", cl::Optional, 1206 cl::desc("Don't export per-function data"), 1207 cl::cat(ExportCategory)); 1208 1209 auto Err = commandLineParser(argc, argv); 1210 if (Err) 1211 return Err; 1212 1213 ViewOpts.SkipExpansions = SkipExpansions; 1214 ViewOpts.SkipFunctions = SkipFunctions; 1215 1216 if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text && 1217 ViewOpts.Format != CoverageViewOptions::OutputFormat::Lcov) { 1218 error("Coverage data can only be exported as textual JSON or an " 1219 "lcov tracefile."); 1220 return 1; 1221 } 1222 1223 auto Coverage = load(); 1224 if (!Coverage) { 1225 error("Could not load coverage information"); 1226 return 1; 1227 } 1228 1229 std::unique_ptr<CoverageExporter> Exporter; 1230 1231 switch (ViewOpts.Format) { 1232 case CoverageViewOptions::OutputFormat::Text: 1233 Exporter = std::make_unique<CoverageExporterJson>(*Coverage.get(), 1234 ViewOpts, outs()); 1235 break; 1236 case CoverageViewOptions::OutputFormat::HTML: 1237 // Unreachable because we should have gracefully terminated with an error 1238 // above. 1239 llvm_unreachable("Export in HTML is not supported!"); 1240 case CoverageViewOptions::OutputFormat::Lcov: 1241 Exporter = std::make_unique<CoverageExporterLcov>(*Coverage.get(), 1242 ViewOpts, outs()); 1243 break; 1244 } 1245 1246 if (SourceFiles.empty()) 1247 Exporter->renderRoot(IgnoreFilenameFilters); 1248 else 1249 Exporter->renderRoot(SourceFiles); 1250 1251 return 0; 1252 } 1253 1254 int showMain(int argc, const char *argv[]) { 1255 CodeCoverageTool Tool; 1256 return Tool.run(CodeCoverageTool::Show, argc, argv); 1257 } 1258 1259 int reportMain(int argc, const char *argv[]) { 1260 CodeCoverageTool Tool; 1261 return Tool.run(CodeCoverageTool::Report, argc, argv); 1262 } 1263 1264 int exportMain(int argc, const char *argv[]) { 1265 CodeCoverageTool Tool; 1266 return Tool.run(CodeCoverageTool::Export, argc, argv); 1267 } 1268