xref: /llvm-project/llvm/tools/llvm-cov/llvm-cov.cpp (revision 342714c11ca26281453fc3ee0c5e5bafd044031a)
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<bool>
25 DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file"));
26 
27 static cl::opt<std::string>
28 InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
29 
30 static cl::opt<std::string>
31 InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
32 
33 static cl::opt<bool>
34 AllBlocks("a", cl::init(false), cl::desc("display all block info"));
35 
36 static cl::opt<bool>
37 BranchProb("b", cl::init(false), cl::desc("display branch info"));
38 
39 //===----------------------------------------------------------------------===//
40 int main(int argc, char **argv) {
41   // Print a stack trace if we signal out.
42   sys::PrintStackTraceOnErrorSignal();
43   PrettyStackTraceProgram X(argc, argv);
44   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
45 
46   cl::ParseCommandLineOptions(argc, argv, "llvm coverage tool\n");
47 
48   GCOVFile GF;
49   if (InputGCNO.empty())
50     errs() << " " << argv[0] << ": No gcov input file!\n";
51 
52   OwningPtr<MemoryBuffer> GCNO_Buff;
53   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
54     errs() << InputGCNO << ": " << ec.message() << "\n";
55     return 1;
56   }
57   GCOVBuffer GCNO_GB(GCNO_Buff.get());
58   if (!GF.readGCNO(GCNO_GB)) {
59     errs() << "Invalid .gcno File!\n";
60     return 1;
61   }
62 
63   if (!InputGCDA.empty()) {
64     OwningPtr<MemoryBuffer> GCDA_Buff;
65     if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
66       errs() << InputGCDA << ": " << ec.message() << "\n";
67       return 1;
68     }
69     GCOVBuffer GCDA_GB(GCDA_Buff.get());
70     if (!GF.readGCDA(GCDA_GB)) {
71       errs() << "Invalid .gcda File!\n";
72       return 1;
73     }
74   }
75 
76   if (DumpGCOV)
77     GF.dump();
78 
79   GCOVOptions Options(AllBlocks, BranchProb);
80   FileInfo FI(Options);
81   GF.collectLineCounts(FI);
82   FI.print(InputGCNO, InputGCDA);
83   return 0;
84 }
85