xref: /llvm-project/llvm/tools/llvm-cov/llvm-cov.cpp (revision f720bf64d4bb572aa06092af301dee108f889731)
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 // 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/Path.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/system_error.h"
23 using namespace llvm;
24 
25 static cl::opt<std::string>
26 InputFilename(cl::Positional, cl::desc("source filename"), cl::init(""));
27 
28 static cl::opt<bool>
29 DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file"));
30 
31 static cl::opt<std::string>
32 InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
33 
34 static cl::opt<std::string>
35 InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
36 
37 
38 //===----------------------------------------------------------------------===//
39 int main(int argc, char **argv) {
40   // Print a stack trace if we signal out.
41   sys::PrintStackTraceOnErrorSignal();
42   PrettyStackTraceProgram X(argc, argv);
43   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
44 
45   cl::ParseCommandLineOptions(argc, argv, "llvm cov\n");
46 
47   if (InputFilename.empty()) {
48     // FIXME: Error out here.
49   }
50 
51   sys::Path SrcFile(InputFilename);
52 
53   sys::Path GCNOFile(SrcFile);
54   GCNOFile.eraseSuffix();
55   GCNOFile.appendSuffix(".gcno");
56 
57   sys::Path GCDAFile(SrcFile);
58   GCDAFile.eraseSuffix();
59   GCDAFile.appendSuffix(".gcda");
60 
61   sys::Path OutputFile(SrcFile);
62   OutputFile.appendSuffix(".gcov");
63 
64   GCOVFile GF;
65   if (InputGCNO.empty())
66     errs() << " " << argv[0] << ": No gcov input file!\n";
67 
68   OwningPtr<MemoryBuffer> GCNO_Buff;
69   if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
70     errs() << InputGCNO << ": " << ec.message() << "\n";
71     return 1;
72   }
73   GCOVBuffer GCNO_GB(GCNO_Buff.take());
74   if (!GF.read(GCNO_GB)) {
75     errs() << "Invalid .gcno File!\n";
76     return 1;
77   }
78 
79   if (!InputGCDA.empty()) {
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.take());
86     if (!GF.read(GCDA_GB)) {
87       errs() << "Invalid .gcda File!\n";
88       return 1;
89     }
90   }
91 
92 
93   if (DumpGCOV)
94     GF.dump();
95 
96   FileInfo FI;
97   GF.collectLineCounts(FI);
98   return 0;
99 }
100