1 //===- 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/ADT/SmallString.h" 16 #include "llvm/Support/CommandLine.h" 17 #include "llvm/Support/GCOV.h" 18 #include "llvm/Support/ManagedStatic.h" 19 #include "llvm/Support/MemoryObject.h" 20 #include "llvm/Support/Path.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<std::string> SourceFile(cl::Positional, cl::Required, 27 cl::desc("SOURCEFILE")); 28 29 static cl::opt<bool> AllBlocks("a", cl::init(false), 30 cl::desc("Display all basic blocks")); 31 static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks)); 32 33 static cl::opt<bool> BranchProb("b", cl::init(false), 34 cl::desc("Display branch probabilities")); 35 static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb)); 36 37 static cl::opt<bool> BranchCount("c", cl::init(false), 38 cl::desc("Display branch counts instead " 39 "of percentages (requires -b)")); 40 static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount)); 41 42 static cl::opt<bool> FuncSummary("f", cl::init(false), 43 cl::desc("Show coverage for each function")); 44 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary)); 45 46 static cl::opt<std::string> ObjectDir("o", cl::value_desc("DIR"), cl::init(""), 47 cl::desc("Search for objects in DIR")); 48 static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir)); 49 50 static cl::opt<bool> PreservePaths("p", cl::init(false), 51 cl::desc("Preserve path components")); 52 static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths)); 53 54 static cl::opt<bool> UncondBranch("u", cl::init(false), 55 cl::desc("Display unconditional branch info " 56 "(requires -b)")); 57 static cl::alias UncondBranchA("unconditional-branches", 58 cl::aliasopt(UncondBranch)); 59 60 static cl::OptionCategory DebugCat("Internal and debugging options"); 61 static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat), 62 cl::desc("Dump the gcov file to stderr")); 63 static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""), 64 cl::desc("Override inferred gcno file")); 65 static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""), 66 cl::desc("Override inferred gcda file")); 67 68 //===----------------------------------------------------------------------===// 69 int main(int argc, char **argv) { 70 // Print a stack trace if we signal out. 71 sys::PrintStackTraceOnErrorSignal(); 72 PrettyStackTraceProgram X(argc, argv); 73 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 74 75 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); 76 77 SmallString<128> CoverageFileStem(ObjectDir); 78 if (CoverageFileStem.empty()) 79 CoverageFileStem = sys::path::parent_path(SourceFile); 80 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); 81 82 if (InputGCNO.empty()) 83 InputGCNO = (CoverageFileStem.str() + ".gcno").str(); 84 if (InputGCDA.empty()) 85 InputGCDA = (CoverageFileStem.str() + ".gcda").str(); 86 87 GCOVFile GF; 88 89 OwningPtr<MemoryBuffer> GCNO_Buff; 90 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) { 91 errs() << InputGCNO << ": " << ec.message() << "\n"; 92 return 1; 93 } 94 GCOVBuffer GCNO_GB(GCNO_Buff.get()); 95 if (!GF.readGCNO(GCNO_GB)) { 96 errs() << "Invalid .gcno File!\n"; 97 return 1; 98 } 99 100 OwningPtr<MemoryBuffer> GCDA_Buff; 101 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) { 102 if (ec != errc::no_such_file_or_directory) { 103 errs() << InputGCDA << ": " << ec.message() << "\n"; 104 return 1; 105 } 106 // Clear the filename to make it clear we didn't read anything. 107 InputGCDA = "-"; 108 } else { 109 GCOVBuffer GCDA_GB(GCDA_Buff.get()); 110 if (!GF.readGCDA(GCDA_GB)) { 111 errs() << "Invalid .gcda File!\n"; 112 return 1; 113 } 114 } 115 116 if (DumpGCOV) 117 GF.dump(); 118 119 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary, 120 PreservePaths, UncondBranch); 121 FileInfo FI(Options); 122 GF.collectLineCounts(FI); 123 FI.print(InputGCNO, InputGCDA); 124 return 0; 125 } 126