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/OwningPtr.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/GCOV.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/MemoryObject.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/system_error.h"
27 static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
28 cl::desc("SOURCEFILE"));
30 static cl::opt<bool> AllBlocks("a", 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::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::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> FuncSummary("f", cl::init(false),
44 cl::desc("Show coverage for each function"));
45 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
47 static cl::opt<std::string>
48 ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""),
49 cl::desc("Find objects in DIR or based on FILE's path"));
50 static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
51 static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
53 static cl::opt<bool> PreservePaths("p", cl::init(false),
54 cl::desc("Preserve path components"));
55 static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
57 static cl::opt<bool> UncondBranch("u", cl::init(false),
58 cl::desc("Display unconditional branch info "
60 static cl::alias UncondBranchA("unconditional-branches",
61 cl::aliasopt(UncondBranch));
63 static cl::OptionCategory DebugCat("Internal and debugging options");
64 static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
65 cl::desc("Dump the gcov file to stderr"));
66 static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
67 cl::desc("Override inferred gcno file"));
68 static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
69 cl::desc("Override inferred gcda file"));
71 //===----------------------------------------------------------------------===//
72 int main(int argc, char **argv) {
73 // Print a stack trace if we signal out.
74 sys::PrintStackTraceOnErrorSignal();
75 PrettyStackTraceProgram X(argc, argv);
76 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
78 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
80 SmallString<128> CoverageFileStem(ObjectDir);
81 if (CoverageFileStem.empty()) {
82 // If no directory was specified with -o, look next to the source file.
83 CoverageFileStem = sys::path::parent_path(SourceFile);
84 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
85 } else if (sys::fs::is_directory(ObjectDir))
86 // A directory name was given. Use it and the source file name.
87 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
89 // A file was given. Ignore the source file and look next to this file.
90 sys::path::replace_extension(CoverageFileStem, "");
92 if (InputGCNO.empty())
93 InputGCNO = (CoverageFileStem.str() + ".gcno").str();
94 if (InputGCDA.empty())
95 InputGCDA = (CoverageFileStem.str() + ".gcda").str();
99 OwningPtr<MemoryBuffer> GCNO_Buff;
100 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
101 errs() << InputGCNO << ": " << ec.message() << "\n";
104 GCOVBuffer GCNO_GB(GCNO_Buff.get());
105 if (!GF.readGCNO(GCNO_GB)) {
106 errs() << "Invalid .gcno File!\n";
110 OwningPtr<MemoryBuffer> GCDA_Buff;
111 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
112 if (ec != errc::no_such_file_or_directory) {
113 errs() << InputGCDA << ": " << ec.message() << "\n";
116 // Clear the filename to make it clear we didn't read anything.
119 GCOVBuffer GCDA_GB(GCDA_Buff.get());
120 if (!GF.readGCDA(GCDA_GB)) {
121 errs() << "Invalid .gcda File!\n";
129 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
130 PreservePaths, UncondBranch);
131 FileInfo FI(Options);
132 GF.collectLineCounts(FI);
133 FI.print(InputGCNO, InputGCDA);