xref: /llvm-project/llvm/tools/llvm-cov/llvm-cov.cpp (revision 3714065a9412a1bee3c2d8931720940e5fe172da)
1 //===- tools/llvm-cov/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 //
11 // llvm-cov is a command line tools to analyze and report coverage information.
12 //
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "GCOVReader.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/MemoryObject.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/Signals.h"
23 #include "llvm/Support/system_error.h"
24 using namespace llvm;
25 
26 static cl::opt<bool>
27 DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file"));
28 
29 static cl::opt<std::string>
30 InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
31 
32 static cl::opt<std::string>
33 InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
34 
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 cov\n");
44 
45 
46   GCOVFile GF;
47   if (InputGCNO.empty())
48     errs() << " " << argv[0] << ": No gcov input file!\n";
49 
50   OwningPtr<MemoryBuffer> Buff;
51   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, Buff)) {
52     errs() << InputGCNO << ": " << ec.message() << "\n";
53     return 1;
54   }
55   GCOVBuffer GB(Buff.take());
56 
57   if (!GF.read(GB)) {
58     errs() << "Invalid .gcno File!\n";
59     return 1;
60   }
61 
62   if (!InputGCDA.empty()) {
63     OwningPtr<MemoryBuffer> Buff2;
64     if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, Buff2)) {
65       errs() << InputGCDA << ": " << ec.message() << "\n";
66       return 1;
67     }
68     GCOVBuffer GB2(Buff2.take());
69 
70     if (!GF.read(GB2)) {
71       errs() << "Invalid .gcda File!\n";
72       return 1;
73     }
74   }
75 
76 
77   if (DumpGCOV)
78     GF.dump();
79 
80   FileInfo FI;
81   GF.collectLineCounts(FI);
82   return 0;
83 }
84