Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 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 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 10 | // llvm-cov is a command line tools to analyze and report coverage information. |
| 11 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/OwningPtr.h" |
| 15 | #include "llvm/Support/CommandLine.h" |
Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/GCOV.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
| 18 | #include "llvm/Support/MemoryObject.h" |
| 19 | #include "llvm/Support/PrettyStackTrace.h" |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Signals.h" |
| 22 | #include "llvm/Support/system_error.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | static cl::opt<bool> |
| 26 | DumpGCOV("dump", cl::init(false), cl::desc("dump gcov file")); |
| 27 | |
| 28 | static cl::opt<std::string> |
| 29 | InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init("")); |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init("")); |
| 33 | |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 34 | static cl::opt<std::string> |
| 35 | OutputFile("o", cl::desc("<output llvm-cov file>"), cl::init("-")); |
| 36 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 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 | |
Yuchen Wu | 9194d7b | 2013-10-31 02:01:24 +0000 | [diff] [blame] | 45 | cl::ParseCommandLineOptions(argc, argv, "llvm coverage tool\n"); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 46 | |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 47 | std::string ErrorInfo; |
| 48 | raw_fd_ostream OS(OutputFile.c_str(), ErrorInfo); |
| 49 | if (!ErrorInfo.empty()) |
| 50 | errs() << ErrorInfo << "\n"; |
| 51 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 52 | GCOVFile GF; |
| 53 | if (InputGCNO.empty()) |
| 54 | errs() << " " << argv[0] << ": No gcov input file!\n"; |
| 55 | |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 56 | OwningPtr<MemoryBuffer> GCNO_Buff; |
| 57 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 58 | errs() << InputGCNO << ": " << ec.message() << "\n"; |
| 59 | return 1; |
| 60 | } |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 61 | GCOVBuffer GCNO_GB(GCNO_Buff.take()); |
| 62 | if (!GF.read(GCNO_GB)) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 63 | errs() << "Invalid .gcno File!\n"; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | if (!InputGCDA.empty()) { |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 68 | OwningPtr<MemoryBuffer> GCDA_Buff; |
| 69 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 70 | errs() << InputGCDA << ": " << ec.message() << "\n"; |
| 71 | return 1; |
| 72 | } |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 73 | GCOVBuffer GCDA_GB(GCDA_Buff.take()); |
| 74 | if (!GF.read(GCDA_GB)) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 75 | errs() << "Invalid .gcda File!\n"; |
| 76 | return 1; |
| 77 | } |
| 78 | } |
| 79 | |
Yuchen Wu | e68c5f8 | 2013-11-05 01:56:29 +0000 | [diff] [blame^] | 80 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 81 | if (DumpGCOV) |
| 82 | GF.dump(); |
| 83 | |
| 84 | FileInfo FI; |
| 85 | GF.collectLineCounts(FI); |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 86 | FI.print(OS, InputGCNO, InputGCDA); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |