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