gcov.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, -1, /*RequiresNullTerminator=*/false);
  46. if (std::error_code EC = GCNO_Buff.getError()) {
  47. errs() << GCNO << ": " << EC.message() << "\n";
  48. return;
  49. }
  50. GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
  51. if (!GF.readGCNO(GCNO_GB)) {
  52. errs() << "Invalid .gcno File!\n";
  53. return;
  54. }
  55. ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =
  56. MemoryBuffer::getFileOrSTDIN(GCDA, -1, /*RequiresNullTerminator=*/false);
  57. if (std::error_code EC = GCDA_Buff.getError()) {
  58. if (EC != errc::no_such_file_or_directory) {
  59. errs() << GCDA << ": " << EC.message() << "\n";
  60. return;
  61. }
  62. // Clear the filename to make it clear we didn't read anything.
  63. GCDA = "-";
  64. } else {
  65. GCOVBuffer gcda_buf(GCDA_Buff.get().get());
  66. if (!gcda_buf.readGCDAFormat())
  67. errs() << GCDA << ":not a gcov data file\n";
  68. else if (!GF.readGCDA(gcda_buf))
  69. errs() << "Invalid .gcda File!\n";
  70. }
  71. if (DumpGCOV)
  72. GF.print(errs());
  73. gcovOneInput(Options, SourceFile, GCNO, GCDA, GF);
  74. }
  75. int gcovMain(int argc, const char *argv[]) {
  76. cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
  77. cl::desc("SOURCEFILE"));
  78. cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
  79. cl::desc("Display all basic blocks"));
  80. cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
  81. cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
  82. cl::desc("Display branch probabilities"));
  83. cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
  84. cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
  85. cl::desc("Display branch counts instead "
  86. "of percentages (requires -b)"));
  87. cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
  88. cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
  89. cl::desc("Prefix filenames with the main file"));
  90. cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
  91. cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
  92. cl::desc("Show coverage for each function"));
  93. cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
  94. // Supported by gcov 4.9~8. gcov 9 (GCC r265587) removed --intermediate-format
  95. // and -i was changed to mean --json-format. We consider this format still
  96. // useful and support -i.
  97. cl::opt<bool> Intermediate(
  98. "intermediate-format", cl::init(false),
  99. cl::desc("Output .gcov in intermediate text format"));
  100. cl::alias IntermediateA("i", cl::desc("Alias for --intermediate-format"),
  101. cl::Grouping, cl::NotHidden,
  102. cl::aliasopt(Intermediate));
  103. cl::opt<bool> Demangle("demangled-names", cl::init(false),
  104. cl::desc("Demangle function names"));
  105. cl::alias DemangleA("m", cl::desc("Alias for --demangled-names"),
  106. cl::Grouping, cl::NotHidden, cl::aliasopt(Demangle));
  107. cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
  108. cl::desc("Do not output any .gcov files"));
  109. cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
  110. cl::opt<std::string> ObjectDir(
  111. "o", cl::value_desc("DIR|FILE"), cl::init(""),
  112. cl::desc("Find objects in DIR or based on FILE's path"));
  113. cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
  114. cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
  115. cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
  116. cl::desc("Preserve path components"));
  117. cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
  118. cl::opt<bool> RelativeOnly(
  119. "r", cl::Grouping,
  120. cl::desc("Only dump files with relative paths or absolute paths with the "
  121. "prefix specified by -s"));
  122. cl::alias RelativeOnlyA("relative-only", cl::aliasopt(RelativeOnly));
  123. cl::opt<std::string> SourcePrefix("s", cl::desc("Source prefix to elide"));
  124. cl::alias SourcePrefixA("source-prefix", cl::aliasopt(SourcePrefix));
  125. cl::opt<bool> UseStdout("t", cl::Grouping, cl::init(false),
  126. cl::desc("Print to stdout"));
  127. cl::alias UseStdoutA("stdout", cl::aliasopt(UseStdout));
  128. cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
  129. cl::desc("Display unconditional branch info "
  130. "(requires -b)"));
  131. cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
  132. cl::opt<bool> HashFilenames("x", cl::Grouping, cl::init(false),
  133. cl::desc("Hash long pathnames"));
  134. cl::alias HashFilenamesA("hash-filenames", cl::aliasopt(HashFilenames));
  135. cl::OptionCategory DebugCat("Internal and debugging options");
  136. cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
  137. cl::desc("Dump the gcov file to stderr"));
  138. cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
  139. cl::desc("Override inferred gcno file"));
  140. cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
  141. cl::desc("Override inferred gcda file"));
  142. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  143. GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
  144. PreservePaths, UncondBranch, Intermediate, LongNames,
  145. Demangle, NoOutput, RelativeOnly, UseStdout,
  146. HashFilenames, SourcePrefix);
  147. for (const auto &SourceFile : SourceFiles)
  148. reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,
  149. Options);
  150. return 0;
  151. }