xref: /llvm-project/llvm/tools/llvm-cov/llvm-cov.cpp (revision 8c6bb5f4d4bddf1a41471dbd115d37b21d982abe)
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 //===----------------------------------------------------------------------===//
37 int main(int argc, char **argv) {
38   // Print a stack trace if we signal out.
39   sys::PrintStackTraceOnErrorSignal();
40   PrettyStackTraceProgram X(argc, argv);
41   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
42 
43   cl::ParseCommandLineOptions(argc, argv, "llvm coverage tool\n");
44 
45   GCOVFile GF;
46   if (InputGCNO.empty())
47     errs() << " " << argv[0] << ": No gcov input file!\n";
48 
49   OwningPtr<MemoryBuffer> GCNO_Buff;
50   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
51     errs() << InputGCNO << ": " << ec.message() << "\n";
52     return 1;
53   }
54   GCOVBuffer GCNO_GB(GCNO_Buff.get());
55   if (!GF.readGCNO(GCNO_GB)) {
56     errs() << "Invalid .gcno File!\n";
57     return 1;
58   }
59 
60   if (!InputGCDA.empty()) {
61     OwningPtr<MemoryBuffer> GCDA_Buff;
62     if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
63       errs() << InputGCDA << ": " << ec.message() << "\n";
64       return 1;
65     }
66     GCOVBuffer GCDA_GB(GCDA_Buff.get());
67     if (!GF.readGCDA(GCDA_GB)) {
68       errs() << "Invalid .gcda File!\n";
69       return 1;
70     }
71   }
72 
73   if (DumpGCOV)
74     GF.dump();
75 
76   FileInfo FI;
77   GF.collectLineCounts(FI);
78   FI.print(InputGCNO, InputGCDA, GCOVOptions(AllBlocks));
79   return 0;
80 }
81