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/PrettyStackTrace.h" 20 #include "llvm/Support/raw_ostream.h" 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 34 static cl::opt<std::string> 35 OutputFile("o", cl::desc("<output llvm-cov 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 coverage tool\n"); 46 47 std::string ErrorInfo; 48 raw_fd_ostream OS(OutputFile.c_str(), ErrorInfo); 49 if (!ErrorInfo.empty()) 50 errs() << ErrorInfo << "\n"; 51 52 GCOVFile GF; 53 if (InputGCNO.empty()) 54 errs() << " " << argv[0] << ": No gcov input file!\n"; 55 56 OwningPtr<MemoryBuffer> GCNO_Buff; 57 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) { 58 errs() << InputGCNO << ": " << ec.message() << "\n"; 59 return 1; 60 } 61 GCOVBuffer GCNO_GB(GCNO_Buff.take()); 62 if (!GF.read(GCNO_GB)) { 63 errs() << "Invalid .gcno File!\n"; 64 return 1; 65 } 66 67 if (!InputGCDA.empty()) { 68 OwningPtr<MemoryBuffer> GCDA_Buff; 69 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) { 70 errs() << InputGCDA << ": " << ec.message() << "\n"; 71 return 1; 72 } 73 GCOVBuffer GCDA_GB(GCDA_Buff.take()); 74 if (!GF.read(GCDA_GB)) { 75 errs() << "Invalid .gcda File!\n"; 76 return 1; 77 } 78 } 79 80 81 if (DumpGCOV) 82 GF.dump(); 83 84 FileInfo FI; 85 GF.collectLineCounts(FI); 86 FI.print(OS, InputGCNO, InputGCDA); 87 return 0; 88 } 89