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/Support/CommandLine.h"
16 #include "llvm/Support/GCOV.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/MemoryObject.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/system_error.h"
24 static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
25 cl::desc("SOURCEFILE"));
27 static cl::opt<bool> AllBlocks("a", cl::init(false),
28 cl::desc("Display all basic blocks"));
29 static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
31 static cl::opt<bool> BranchProb("b", cl::init(false),
32 cl::desc("Display branch probabilities"));
33 static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
35 static cl::opt<bool> BranchCount("c", cl::init(false),
36 cl::desc("Display branch counts instead "
37 "of percentages (requires -b)"));
38 static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
40 static cl::opt<bool> FuncSummary("f", cl::init(false),
41 cl::desc("Show coverage for each function"));
42 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
44 static cl::opt<bool> UncondBranch("u", cl::init(false),
45 cl::desc("Display unconditional branch info "
47 static cl::alias UncondBranchA("unconditional-branches",
48 cl::aliasopt(UncondBranch));
50 static cl::OptionCategory DebugCat("Internal and debugging options");
51 static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
52 cl::desc("Dump the gcov file to stderr"));
53 static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
54 cl::desc("Override inferred gcno file"));
55 static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
56 cl::desc("Override inferred gcda file"));
58 //===----------------------------------------------------------------------===//
59 int main(int argc, char **argv) {
60 // Print a stack trace if we signal out.
61 sys::PrintStackTraceOnErrorSignal();
62 PrettyStackTraceProgram X(argc, argv);
63 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
65 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
67 if (InputGCNO.empty())
68 InputGCNO = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcno";
69 if (InputGCDA.empty())
70 InputGCDA = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcda";
74 OwningPtr<MemoryBuffer> GCNO_Buff;
75 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
76 errs() << InputGCNO << ": " << ec.message() << "\n";
79 GCOVBuffer GCNO_GB(GCNO_Buff.get());
80 if (!GF.readGCNO(GCNO_GB)) {
81 errs() << "Invalid .gcno File!\n";
85 OwningPtr<MemoryBuffer> GCDA_Buff;
86 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
87 errs() << InputGCDA << ": " << ec.message() << "\n";
90 GCOVBuffer GCDA_GB(GCDA_Buff.get());
91 if (!GF.readGCDA(GCDA_GB)) {
92 errs() << "Invalid .gcda File!\n";
99 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
101 FileInfo FI(Options);
102 GF.collectLineCounts(FI);
103 FI.print(InputGCNO, InputGCDA);