f5498916940bb14d57a42ebfa659b921ab334473
[oota-llvm.git] / tools / llvm-cov / llvm-cov.cpp
1 //===- llvm-cov.cpp - 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.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
22 using namespace llvm;
23
24 static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
25                                        cl::desc("SOURCEFILE"));
26
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));
30
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));
34
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));
39
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));
43
44 static cl::opt<bool> UncondBranch("u", cl::init(false),
45                                   cl::desc("Display unconditional branch info "
46                                            "(requires -b)"));
47 static cl::alias UncondBranchA("unconditional-branches",
48                                cl::aliasopt(UncondBranch));
49
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"));
57
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.
64
65   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
66
67   if (InputGCNO.empty())
68     InputGCNO = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcno";
69   if (InputGCDA.empty())
70     InputGCDA = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcda";
71
72   GCOVFile GF;
73
74   OwningPtr<MemoryBuffer> GCNO_Buff;
75   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
76     errs() << InputGCNO << ": " << ec.message() << "\n";
77     return 1;
78   }
79   GCOVBuffer GCNO_GB(GCNO_Buff.get());
80   if (!GF.readGCNO(GCNO_GB)) {
81     errs() << "Invalid .gcno File!\n";
82     return 1;
83   }
84
85   OwningPtr<MemoryBuffer> GCDA_Buff;
86   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
87     errs() << InputGCDA << ": " << ec.message() << "\n";
88     return 1;
89   }
90   GCOVBuffer GCDA_GB(GCDA_Buff.get());
91   if (!GF.readGCDA(GCDA_GB)) {
92     errs() << "Invalid .gcda File!\n";
93     return 1;
94   }
95
96   if (DumpGCOV)
97     GF.dump();
98
99   GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
100                       UncondBranch);
101   FileInfo FI(Options);
102   GF.collectLineCounts(FI);
103   FI.print(InputGCNO, InputGCDA);
104   return 0;
105 }