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