gcov.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===- gcov.cpp - GCOV compatible LLVM coverage tool ----------------------===//
  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. // llvm-cov is a command line tools to analyze and report coverage information.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ProfileData/GCOV.h"
  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/Path.h"
  18. #include <system_error>
  19. using namespace llvm;
  20. static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
  21. const std::string &InputGCNO,
  22. const std::string &InputGCDA, bool DumpGCOV,
  23. const GCOV::Options &Options) {
  24. SmallString<128> CoverageFileStem(ObjectDir);
  25. if (CoverageFileStem.empty()) {
  26. // If no directory was specified with -o, look next to the source file.
  27. CoverageFileStem = sys::path::parent_path(SourceFile);
  28. sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
  29. } else if (sys::fs::is_directory(ObjectDir))
  30. // A directory name was given. Use it and the source file name.
  31. sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
  32. else
  33. // A file was given. Ignore the source file and look next to this file.
  34. sys::path::replace_extension(CoverageFileStem, "");
  35. std::string GCNO = InputGCNO.empty()
  36. ? std::string(CoverageFileStem.str()) + ".gcno"
  37. : InputGCNO;
  38. std::string GCDA = InputGCDA.empty()
  39. ? std::string(CoverageFileStem.str()) + ".gcda"
  40. : InputGCDA;
  41. GCOVFile GF;
  42. // Open .gcda and .gcda without requiring a NUL terminator. The concurrent
  43. // modification may nullify the NUL terminator condition.
  44. ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff =
  45. MemoryBuffer::getFileOrSTDIN(GCNO, /*IsText=*/false,
  46. /*RequiresNullTerminator=*/false);
  47. if (std::error_code EC = GCNO_Buff.getError()) {
  48. errs() << GCNO << ": " << EC.message() << "\n";
  49. return;
  50. }
  51. GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
  52. if (!GF.readGCNO(GCNO_GB)) {
  53. errs() << "Invalid .gcno File!\n";
  54. return;
  55. }
  56. ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =
  57. MemoryBuffer::getFileOrSTDIN(GCDA, /*IsText=*/false,
  58. /*RequiresNullTerminator=*/false);
  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_buf(GCDA_Buff.get().get());
  68. if (!gcda_buf.readGCDAFormat())
  69. errs() << GCDA << ":not a gcov data file\n";
  70. else if (!GF.readGCDA(gcda_buf))
  71. errs() << "Invalid .gcda File!\n";
  72. }
  73. if (DumpGCOV)
  74. GF.print(errs());
  75. gcovOneInput(Options, SourceFile, GCNO, GCDA, GF);
  76. }
  77. int gcovMain(int argc, const char *argv[]) {
  78. cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
  79. cl::desc("SOURCEFILE"));
  80. cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
  81. cl::desc("Display all basic blocks"));
  82. cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
  83. cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
  84. cl::desc("Display branch probabilities"));
  85. cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
  86. cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
  87. cl::desc("Display branch counts instead "
  88. "of percentages (requires -b)"));
  89. cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
  90. cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
  91. cl::desc("Prefix filenames with the main file"));
  92. cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
  93. cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
  94. cl::desc("Show coverage for each function"));
  95. cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
  96. // Supported by gcov 4.9~8. gcov 9 (GCC r265587) removed --intermediate-format
  97. // and -i was changed to mean --json-format. We consider this format still
  98. // useful and support -i.
  99. cl::opt<bool> Intermediate(
  100. "intermediate-format", cl::init(false),
  101. cl::desc("Output .gcov in intermediate text format"));
  102. cl::alias IntermediateA("i", cl::desc("Alias for --intermediate-format"),
  103. cl::Grouping, cl::NotHidden,
  104. cl::aliasopt(Intermediate));
  105. cl::opt<bool> Demangle("demangled-names", cl::init(false),
  106. cl::desc("Demangle function names"));
  107. cl::alias DemangleA("m", cl::desc("Alias for --demangled-names"),
  108. cl::Grouping, cl::NotHidden, cl::aliasopt(Demangle));
  109. cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
  110. cl::desc("Do not output any .gcov files"));
  111. cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
  112. cl::opt<std::string> ObjectDir(
  113. "o", cl::value_desc("DIR|FILE"), cl::init(""),
  114. cl::desc("Find objects in DIR or based on FILE's path"));
  115. cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
  116. cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
  117. cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
  118. cl::desc("Preserve path components"));
  119. cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
  120. cl::opt<bool> RelativeOnly(
  121. "r", cl::Grouping,
  122. cl::desc("Only dump files with relative paths or absolute paths with the "
  123. "prefix specified by -s"));
  124. cl::alias RelativeOnlyA("relative-only", cl::aliasopt(RelativeOnly));
  125. cl::opt<std::string> SourcePrefix("s", cl::desc("Source prefix to elide"));
  126. cl::alias SourcePrefixA("source-prefix", cl::aliasopt(SourcePrefix));
  127. cl::opt<bool> UseStdout("t", cl::Grouping, cl::init(false),
  128. cl::desc("Print to stdout"));
  129. cl::alias UseStdoutA("stdout", cl::aliasopt(UseStdout));
  130. cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
  131. cl::desc("Display unconditional branch info "
  132. "(requires -b)"));
  133. cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
  134. cl::opt<bool> HashFilenames("x", cl::Grouping, cl::init(false),
  135. cl::desc("Hash long pathnames"));
  136. cl::alias HashFilenamesA("hash-filenames", cl::aliasopt(HashFilenames));
  137. cl::OptionCategory DebugCat("Internal and debugging options");
  138. cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
  139. cl::desc("Dump the gcov file to stderr"));
  140. cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
  141. cl::desc("Override inferred gcno file"));
  142. cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
  143. cl::desc("Override inferred gcda file"));
  144. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  145. GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
  146. PreservePaths, UncondBranch, Intermediate, LongNames,
  147. Demangle, NoOutput, RelativeOnly, UseStdout,
  148. HashFilenames, SourcePrefix);
  149. for (const auto &SourceFile : SourceFiles)
  150. reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,
  151. Options);
  152. return 0;
  153. }