gcov.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===- gcov.cpp - GCOV compatible LLVM coverage tool ----------------------===//
  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. // llvm-cov is a command line tools to analyze and report coverage information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/Errc.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Support/GCOV.h"
  18. #include "llvm/Support/ManagedStatic.h"
  19. #include "llvm/Support/Path.h"
  20. #include "llvm/Support/PrettyStackTrace.h"
  21. #include "llvm/Support/Signals.h"
  22. #include <system_error>
  23. using namespace llvm;
  24. static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
  25. const std::string &InputGCNO,
  26. const std::string &InputGCDA, bool DumpGCOV,
  27. const GCOVOptions &Options) {
  28. SmallString<128> CoverageFileStem(ObjectDir);
  29. if (CoverageFileStem.empty()) {
  30. // If no directory was specified with -o, look next to the source file.
  31. CoverageFileStem = sys::path::parent_path(SourceFile);
  32. sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
  33. } else if (sys::fs::is_directory(ObjectDir))
  34. // A directory name was given. Use it and the source file name.
  35. sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
  36. else
  37. // A file was given. Ignore the source file and look next to this file.
  38. sys::path::replace_extension(CoverageFileStem, "");
  39. std::string GCNO = InputGCNO.empty()
  40. ? std::string(CoverageFileStem.str()) + ".gcno"
  41. : InputGCNO;
  42. std::string GCDA = InputGCDA.empty()
  43. ? std::string(CoverageFileStem.str()) + ".gcda"
  44. : InputGCDA;
  45. GCOVFile GF;
  46. ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff =
  47. MemoryBuffer::getFileOrSTDIN(GCNO);
  48. if (std::error_code EC = GCNO_Buff.getError()) {
  49. errs() << GCNO << ": " << EC.message() << "\n";
  50. return;
  51. }
  52. GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
  53. if (!GF.readGCNO(GCNO_GB)) {
  54. errs() << "Invalid .gcno File!\n";
  55. return;
  56. }
  57. ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =
  58. MemoryBuffer::getFileOrSTDIN(GCDA);
  59. if (std::error_code EC = GCDA_Buff.getError()) {
  60. if (EC != errc::no_such_file_or_directory) {
  61. errs() << GCDA << ": " << EC.message() << "\n";
  62. return;
  63. }
  64. // Clear the filename to make it clear we didn't read anything.
  65. GCDA = "-";
  66. } else {
  67. GCOVBuffer GCDA_GB(GCDA_Buff.get().get());
  68. if (!GF.readGCDA(GCDA_GB)) {
  69. errs() << "Invalid .gcda File!\n";
  70. return;
  71. }
  72. }
  73. if (DumpGCOV)
  74. GF.dump();
  75. FileInfo FI(Options);
  76. GF.collectLineCounts(FI);
  77. FI.print(llvm::outs(), SourceFile, GCNO, GCDA);
  78. }
  79. int gcovMain(int argc, const char *argv[]) {
  80. // Print a stack trace if we signal out.
  81. sys::PrintStackTraceOnErrorSignal();
  82. PrettyStackTraceProgram X(argc, argv);
  83. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  84. cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
  85. cl::desc("SOURCEFILE"));
  86. cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
  87. cl::desc("Display all basic blocks"));
  88. cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
  89. cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
  90. cl::desc("Display branch probabilities"));
  91. cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
  92. cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
  93. cl::desc("Display branch counts instead "
  94. "of percentages (requires -b)"));
  95. cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
  96. cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
  97. cl::desc("Prefix filenames with the main file"));
  98. cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
  99. cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
  100. cl::desc("Show coverage for each function"));
  101. cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
  102. cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
  103. cl::desc("Do not output any .gcov files"));
  104. cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
  105. cl::opt<std::string> ObjectDir(
  106. "o", cl::value_desc("DIR|FILE"), cl::init(""),
  107. cl::desc("Find objects in DIR or based on FILE's path"));
  108. cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
  109. cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
  110. cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
  111. cl::desc("Preserve path components"));
  112. cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
  113. cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
  114. cl::desc("Display unconditional branch info "
  115. "(requires -b)"));
  116. cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
  117. cl::OptionCategory DebugCat("Internal and debugging options");
  118. cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
  119. cl::desc("Dump the gcov file to stderr"));
  120. cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
  121. cl::desc("Override inferred gcno file"));
  122. cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
  123. cl::desc("Override inferred gcda file"));
  124. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  125. GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
  126. PreservePaths, UncondBranch, LongNames, NoOutput);
  127. for (const auto &SourceFile : SourceFiles)
  128. reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,
  129. Options);
  130. return 0;
  131. }