blob: ad6c67181548b59fcdb93fe4f3e796c7af8673f6 [file] [log] [blame]
Devang Patel37140652011-09-28 18:50:00 +00001//===- 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//
Devang Patel37140652011-09-28 18:50:00 +000010// llvm-cov is a command line tools to analyze and report coverage information.
11//
Devang Patel37140652011-09-28 18:50:00 +000012//===----------------------------------------------------------------------===//
13
Devang Patel37140652011-09-28 18:50:00 +000014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/Support/CommandLine.h"
Devang Patel8dfb6552011-10-04 17:24:48 +000016#include "llvm/Support/GCOV.h"
Devang Patel37140652011-09-28 18:50:00 +000017#include "llvm/Support/ManagedStatic.h"
18#include "llvm/Support/MemoryObject.h"
19#include "llvm/Support/PrettyStackTrace.h"
Yuchen Wudbcf1972013-11-02 00:09:17 +000020#include "llvm/Support/raw_ostream.h"
Devang Patel37140652011-09-28 18:50:00 +000021#include "llvm/Support/Signals.h"
22#include "llvm/Support/system_error.h"
23using namespace llvm;
24
25static cl::opt<bool>
26DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file"));
27
28static cl::opt<std::string>
29InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
30
31static cl::opt<std::string>
32InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
33
Yuchen Wudbcf1972013-11-02 00:09:17 +000034static cl::opt<std::string>
35OutputFile("o", cl::desc("<output llvm-cov file>"), cl::init("-"));
36
Devang Patel37140652011-09-28 18:50:00 +000037
38//===----------------------------------------------------------------------===//
39int 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
Yuchen Wu9194d7b2013-10-31 02:01:24 +000045 cl::ParseCommandLineOptions(argc, argv, "llvm coverage tool\n");
Devang Patel37140652011-09-28 18:50:00 +000046
Yuchen Wudbcf1972013-11-02 00:09:17 +000047 std::string ErrorInfo;
48 raw_fd_ostream OS(OutputFile.c_str(), ErrorInfo);
49 if (!ErrorInfo.empty())
50 errs() << ErrorInfo << "\n";
51
Devang Patel37140652011-09-28 18:50:00 +000052 GCOVFile GF;
53 if (InputGCNO.empty())
54 errs() << " " << argv[0] << ": No gcov input file!\n";
55
Devang Patela9e8a252011-09-29 16:46:47 +000056 OwningPtr<MemoryBuffer> GCNO_Buff;
57 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
Devang Patel37140652011-09-28 18:50:00 +000058 errs() << InputGCNO << ": " << ec.message() << "\n";
59 return 1;
60 }
Devang Patela9e8a252011-09-29 16:46:47 +000061 GCOVBuffer GCNO_GB(GCNO_Buff.take());
62 if (!GF.read(GCNO_GB)) {
Devang Patel37140652011-09-28 18:50:00 +000063 errs() << "Invalid .gcno File!\n";
64 return 1;
65 }
66
67 if (!InputGCDA.empty()) {
Devang Patela9e8a252011-09-29 16:46:47 +000068 OwningPtr<MemoryBuffer> GCDA_Buff;
69 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
Devang Patel37140652011-09-28 18:50:00 +000070 errs() << InputGCDA << ": " << ec.message() << "\n";
71 return 1;
72 }
Devang Patela9e8a252011-09-29 16:46:47 +000073 GCOVBuffer GCDA_GB(GCDA_Buff.take());
74 if (!GF.read(GCDA_GB)) {
Devang Patel37140652011-09-28 18:50:00 +000075 errs() << "Invalid .gcda File!\n";
76 return 1;
77 }
78 }
79
Yuchen Wue68c5f82013-11-05 01:56:29 +000080
Devang Patel37140652011-09-28 18:50:00 +000081 if (DumpGCOV)
82 GF.dump();
83
84 FileInfo FI;
85 GF.collectLineCounts(FI);
Yuchen Wudbcf1972013-11-02 00:09:17 +000086 FI.print(OS, InputGCNO, InputGCDA);
Devang Patel37140652011-09-28 18:50:00 +000087 return 0;
88}