xref: /llvm-project/llvm/tools/llvm-cov/llvm-cov.cpp (revision a99a3902a5c170a3ee184bd9e6efddd9dc604c9f)
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>
28 DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file"));
29 
30 static cl::opt<std::string>
31 InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
32 
33 static cl::opt<std::string>
34 InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
35 
36 static cl::opt<bool>
37 AllBlocks("a", cl::init(false), cl::desc("display all block info"));
38 
39 static cl::opt<bool>
40 BranchInfo("b", cl::init(false), cl::desc("display branch info"));
41 
42 static cl::opt<bool>
43 BranchCount("c", cl::init(false), cl::desc("display branch counts instead of \
44                                             probabilities (requires -b)"));
45 
46 static cl::opt<bool>
47 FuncCoverage("f", cl::init(false), cl::desc("output function coverage"));
48 
49 static cl::opt<bool>
50 UncondBranch("u", cl::init(false), cl::desc("display unconditional branch info \
51                                              (requires -b)"));
52 
53 //===----------------------------------------------------------------------===//
54 int main(int argc, char **argv) {
55   // Print a stack trace if we signal out.
56   sys::PrintStackTraceOnErrorSignal();
57   PrettyStackTraceProgram X(argc, argv);
58   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
59 
60   cl::ParseCommandLineOptions(argc, argv, "llvm coverage tool\n");
61 
62   if (InputGCNO.empty())
63     InputGCNO = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcno";
64   if (InputGCDA.empty())
65     InputGCDA = SourceFile.substr(0, SourceFile.rfind(".")) + ".gcda";
66 
67   GCOVFile GF;
68 
69   OwningPtr<MemoryBuffer> GCNO_Buff;
70   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
71     errs() << InputGCNO << ": " << ec.message() << "\n";
72     return 1;
73   }
74   GCOVBuffer GCNO_GB(GCNO_Buff.get());
75   if (!GF.readGCNO(GCNO_GB)) {
76     errs() << "Invalid .gcno File!\n";
77     return 1;
78   }
79 
80   OwningPtr<MemoryBuffer> GCDA_Buff;
81   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
82     errs() << InputGCDA << ": " << ec.message() << "\n";
83     return 1;
84   }
85   GCOVBuffer GCDA_GB(GCDA_Buff.get());
86   if (!GF.readGCDA(GCDA_GB)) {
87     errs() << "Invalid .gcda File!\n";
88     return 1;
89   }
90 
91   if (DumpGCOV)
92     GF.dump();
93 
94   GCOVOptions Options(AllBlocks, BranchInfo, BranchCount, FuncCoverage,
95                       UncondBranch);
96   FileInfo FI(Options);
97   GF.collectLineCounts(FI);
98   FI.print(InputGCNO, InputGCDA);
99   return 0;
100 }
101