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/SmallString.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/Support/FileSystem.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::Grouping, 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::Grouping, 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::Grouping, 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> LongNames("l", cl::Grouping, cl::init(false), 43 cl::desc("Prefix filenames with the main file")); 44 static cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames)); 45 46 static cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false), 47 cl::desc("Show coverage for each function")); 48 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary)); 49 50 static cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false), 51 cl::desc("Do not output any .gcov files")); 52 static cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput)); 53 54 static cl::opt<std::string> 55 ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""), 56 cl::desc("Find objects in DIR or based on FILE's path")); 57 static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir)); 58 static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir)); 59 60 static cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false), 61 cl::desc("Preserve path components")); 62 static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths)); 63 64 static cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false), 65 cl::desc("Display unconditional branch info " 66 "(requires -b)")); 67 static cl::alias UncondBranchA("unconditional-branches", 68 cl::aliasopt(UncondBranch)); 69 70 static cl::OptionCategory DebugCat("Internal and debugging options"); 71 static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat), 72 cl::desc("Dump the gcov file to stderr")); 73 static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""), 74 cl::desc("Override inferred gcno file")); 75 static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""), 76 cl::desc("Override inferred gcda file")); 77 78 //===----------------------------------------------------------------------===// 79 int main(int argc, char **argv) { 80 // Print a stack trace if we signal out. 81 sys::PrintStackTraceOnErrorSignal(); 82 PrettyStackTraceProgram X(argc, argv); 83 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 84 85 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); 86 87 SmallString<128> CoverageFileStem(ObjectDir); 88 if (CoverageFileStem.empty()) { 89 // If no directory was specified with -o, look next to the source file. 90 CoverageFileStem = sys::path::parent_path(SourceFile); 91 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); 92 } else if (sys::fs::is_directory(ObjectDir)) 93 // A directory name was given. Use it and the source file name. 94 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); 95 else 96 // A file was given. Ignore the source file and look next to this file. 97 sys::path::replace_extension(CoverageFileStem, ""); 98 99 if (InputGCNO.empty()) 100 InputGCNO = (CoverageFileStem.str() + ".gcno").str(); 101 if (InputGCDA.empty()) 102 InputGCDA = (CoverageFileStem.str() + ".gcda").str(); 103 104 GCOVFile GF; 105 106 std::unique_ptr<MemoryBuffer> GCNO_Buff; 107 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) { 108 errs() << InputGCNO << ": " << ec.message() << "\n"; 109 return 1; 110 } 111 GCOVBuffer GCNO_GB(GCNO_Buff.get()); 112 if (!GF.readGCNO(GCNO_GB)) { 113 errs() << "Invalid .gcno File!\n"; 114 return 1; 115 } 116 117 std::unique_ptr<MemoryBuffer> GCDA_Buff; 118 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) { 119 if (ec != errc::no_such_file_or_directory) { 120 errs() << InputGCDA << ": " << ec.message() << "\n"; 121 return 1; 122 } 123 // Clear the filename to make it clear we didn't read anything. 124 InputGCDA = "-"; 125 } else { 126 GCOVBuffer GCDA_GB(GCDA_Buff.get()); 127 if (!GF.readGCDA(GCDA_GB)) { 128 errs() << "Invalid .gcda File!\n"; 129 return 1; 130 } 131 } 132 133 if (DumpGCOV) 134 GF.dump(); 135 136 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary, 137 PreservePaths, UncondBranch, LongNames, NoOutput); 138 FileInfo FI(Options); 139 GF.collectLineCounts(FI); 140 FI.print(SourceFile, InputGCNO, InputGCDA); 141 return 0; 142 } 143