[llvm-objcopy] [COFF] Fix warnings abuilt missing field initialization. NFC.
[llvm-complete.git] / tools / llvm-cov / gcov.cpp
blob7776f2aa9a6844efe0ae52112a53bad704d60ce3
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ProfileData/GCOV.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include <system_error>
21 using namespace llvm;
23 static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
24 const std::string &InputGCNO,
25 const std::string &InputGCDA, bool DumpGCOV,
26 const GCOV::Options &Options) {
27 SmallString<128> CoverageFileStem(ObjectDir);
28 if (CoverageFileStem.empty()) {
29 // If no directory was specified with -o, look next to the source file.
30 CoverageFileStem = sys::path::parent_path(SourceFile);
31 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
32 } else if (sys::fs::is_directory(ObjectDir))
33 // A directory name was given. Use it and the source file name.
34 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
35 else
36 // A file was given. Ignore the source file and look next to this file.
37 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;
47 ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff =
48 MemoryBuffer::getFileOrSTDIN(GCNO);
49 if (std::error_code EC = GCNO_Buff.getError()) {
50 errs() << GCNO << ": " << EC.message() << "\n";
51 return;
53 GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
54 if (!GF.readGCNO(GCNO_GB)) {
55 errs() << "Invalid .gcno File!\n";
56 return;
59 ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =
60 MemoryBuffer::getFileOrSTDIN(GCDA);
61 if (std::error_code EC = GCDA_Buff.getError()) {
62 if (EC != errc::no_such_file_or_directory) {
63 errs() << GCDA << ": " << EC.message() << "\n";
64 return;
66 // Clear the filename to make it clear we didn't read anything.
67 GCDA = "-";
68 } else {
69 GCOVBuffer GCDA_GB(GCDA_Buff.get().get());
70 if (!GF.readGCDA(GCDA_GB)) {
71 errs() << "Invalid .gcda File!\n";
72 return;
76 if (DumpGCOV)
77 GF.print(errs());
79 FileInfo FI(Options);
80 GF.collectLineCounts(FI);
81 FI.print(llvm::outs(), SourceFile, GCNO, GCDA);
84 int gcovMain(int argc, const char *argv[]) {
85 cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
86 cl::desc("SOURCEFILE"));
88 cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
89 cl::desc("Display all basic blocks"));
90 cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
92 cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
93 cl::desc("Display branch probabilities"));
94 cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
96 cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
97 cl::desc("Display branch counts instead "
98 "of percentages (requires -b)"));
99 cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
101 cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
102 cl::desc("Prefix filenames with the main file"));
103 cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
105 cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
106 cl::desc("Show coverage for each function"));
107 cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
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));
113 cl::opt<std::string> ObjectDir(
114 "o", cl::value_desc("DIR|FILE"), cl::init(""),
115 cl::desc("Find objects in DIR or based on FILE's path"));
116 cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
117 cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
119 cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
120 cl::desc("Preserve path components"));
121 cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
123 cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
124 cl::desc("Display unconditional branch info "
125 "(requires -b)"));
126 cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
128 cl::OptionCategory DebugCat("Internal and debugging options");
129 cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
130 cl::desc("Dump the gcov file to stderr"));
131 cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
132 cl::desc("Override inferred gcno file"));
133 cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
134 cl::desc("Override inferred gcda file"));
136 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
138 GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
139 PreservePaths, UncondBranch, LongNames, NoOutput);
141 for (const auto &SourceFile : SourceFiles)
142 reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,
143 Options);
144 return 0;