1 //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // llvm-cov is a command line tools to analyze and report coverage information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/GCOV.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/MemoryObject.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/Signals.h"
23 #include <system_error>
25 using std::error_code;
27 static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
28 cl::desc("SOURCEFILE"));
30 static cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
31 cl::desc("Display all basic blocks"));
32 static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
34 static cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
35 cl::desc("Display branch probabilities"));
36 static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
38 static cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
39 cl::desc("Display branch counts instead "
40 "of percentages (requires -b)"));
41 static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
43 static cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
44 cl::desc("Prefix filenames with the main file"));
45 static cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
47 static cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
48 cl::desc("Show coverage for each function"));
49 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
51 static cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
52 cl::desc("Do not output any .gcov files"));
53 static cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
55 static cl::opt<std::string>
56 ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""),
57 cl::desc("Find objects in DIR or based on FILE's path"));
58 static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
59 static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
61 static cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
62 cl::desc("Preserve path components"));
63 static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
65 static cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
66 cl::desc("Display unconditional branch info "
68 static cl::alias UncondBranchA("unconditional-branches",
69 cl::aliasopt(UncondBranch));
71 static cl::OptionCategory DebugCat("Internal and debugging options");
72 static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
73 cl::desc("Dump the gcov file to stderr"));
74 static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
75 cl::desc("Override inferred gcno file"));
76 static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
77 cl::desc("Override inferred gcda file"));
79 //===----------------------------------------------------------------------===//
80 int main(int argc, char **argv) {
81 // Print a stack trace if we signal out.
82 sys::PrintStackTraceOnErrorSignal();
83 PrettyStackTraceProgram X(argc, argv);
84 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
86 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
88 SmallString<128> CoverageFileStem(ObjectDir);
89 if (CoverageFileStem.empty()) {
90 // If no directory was specified with -o, look next to the source file.
91 CoverageFileStem = sys::path::parent_path(SourceFile);
92 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
93 } else if (sys::fs::is_directory(ObjectDir))
94 // A directory name was given. Use it and the source file name.
95 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
97 // A file was given. Ignore the source file and look next to this file.
98 sys::path::replace_extension(CoverageFileStem, "");
100 if (InputGCNO.empty())
101 InputGCNO = (CoverageFileStem.str() + ".gcno").str();
102 if (InputGCDA.empty())
103 InputGCDA = (CoverageFileStem.str() + ".gcda").str();
107 std::unique_ptr<MemoryBuffer> GCNO_Buff;
108 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
109 errs() << InputGCNO << ": " << ec.message() << "\n";
112 GCOVBuffer GCNO_GB(GCNO_Buff.get());
113 if (!GF.readGCNO(GCNO_GB)) {
114 errs() << "Invalid .gcno File!\n";
118 std::unique_ptr<MemoryBuffer> GCDA_Buff;
119 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
120 if (ec != std::errc::no_such_file_or_directory) {
121 errs() << InputGCDA << ": " << ec.message() << "\n";
124 // Clear the filename to make it clear we didn't read anything.
127 GCOVBuffer GCDA_GB(GCDA_Buff.get());
128 if (!GF.readGCDA(GCDA_GB)) {
129 errs() << "Invalid .gcda File!\n";
137 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
138 PreservePaths, UncondBranch, LongNames, NoOutput);
139 FileInfo FI(Options);
140 GF.collectLineCounts(FI);
141 FI.print(SourceFile, InputGCNO, InputGCDA);