gcov.cpp revision 281049
1//===- gcov.cpp - GCOV compatible 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/Errc.h" 17#include "llvm/Support/FileSystem.h" 18#include "llvm/Support/GCOV.h" 19#include "llvm/Support/ManagedStatic.h" 20#include "llvm/Support/Path.h" 21#include "llvm/Support/PrettyStackTrace.h" 22#include "llvm/Support/Signals.h" 23#include <system_error> 24using namespace llvm; 25 26void reportCoverage(StringRef SourceFile, StringRef ObjectDir, 27 const std::string &InputGCNO, const std::string &InputGCDA, 28 bool DumpGCOV, const GCOVOptions &Options) { 29 SmallString<128> CoverageFileStem(ObjectDir); 30 if (CoverageFileStem.empty()) { 31 // If no directory was specified with -o, look next to the source file. 32 CoverageFileStem = sys::path::parent_path(SourceFile); 33 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); 34 } else if (sys::fs::is_directory(ObjectDir)) 35 // A directory name was given. Use it and the source file name. 36 sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); 37 else 38 // A file was given. Ignore the source file and look next to this file. 39 sys::path::replace_extension(CoverageFileStem, ""); 40 41 std::string GCNO = InputGCNO.empty() 42 ? std::string(CoverageFileStem.str()) + ".gcno" 43 : InputGCNO; 44 std::string GCDA = InputGCDA.empty() 45 ? std::string(CoverageFileStem.str()) + ".gcda" 46 : InputGCDA; 47 GCOVFile GF; 48 49 ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff = 50 MemoryBuffer::getFileOrSTDIN(GCNO); 51 if (std::error_code EC = GCNO_Buff.getError()) { 52 errs() << GCNO << ": " << EC.message() << "\n"; 53 return; 54 } 55 GCOVBuffer GCNO_GB(GCNO_Buff.get().get()); 56 if (!GF.readGCNO(GCNO_GB)) { 57 errs() << "Invalid .gcno File!\n"; 58 return; 59 } 60 61 ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff = 62 MemoryBuffer::getFileOrSTDIN(GCDA); 63 if (std::error_code EC = GCDA_Buff.getError()) { 64 if (EC != errc::no_such_file_or_directory) { 65 errs() << GCDA << ": " << EC.message() << "\n"; 66 return; 67 } 68 // Clear the filename to make it clear we didn't read anything. 69 GCDA = "-"; 70 } else { 71 GCOVBuffer GCDA_GB(GCDA_Buff.get().get()); 72 if (!GF.readGCDA(GCDA_GB)) { 73 errs() << "Invalid .gcda File!\n"; 74 return; 75 } 76 } 77 78 if (DumpGCOV) 79 GF.dump(); 80 81 FileInfo FI(Options); 82 GF.collectLineCounts(FI); 83 FI.print(SourceFile, GCNO, GCDA); 84} 85 86int gcovMain(int argc, const char *argv[]) { 87 // Print a stack trace if we signal out. 88 sys::PrintStackTraceOnErrorSignal(); 89 PrettyStackTraceProgram X(argc, argv); 90 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 91 92 cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore, 93 cl::desc("SOURCEFILE")); 94 95 cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false), 96 cl::desc("Display all basic blocks")); 97 cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks)); 98 99 cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false), 100 cl::desc("Display branch probabilities")); 101 cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb)); 102 103 cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false), 104 cl::desc("Display branch counts instead " 105 "of percentages (requires -b)")); 106 cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount)); 107 108 cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false), 109 cl::desc("Prefix filenames with the main file")); 110 cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames)); 111 112 cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false), 113 cl::desc("Show coverage for each function")); 114 cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary)); 115 116 cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false), 117 cl::desc("Do not output any .gcov files")); 118 cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput)); 119 120 cl::opt<std::string> ObjectDir( 121 "o", cl::value_desc("DIR|FILE"), cl::init(""), 122 cl::desc("Find objects in DIR or based on FILE's path")); 123 cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir)); 124 cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir)); 125 126 cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false), 127 cl::desc("Preserve path components")); 128 cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths)); 129 130 cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false), 131 cl::desc("Display unconditional branch info " 132 "(requires -b)")); 133 cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch)); 134 135 cl::OptionCategory DebugCat("Internal and debugging options"); 136 cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat), 137 cl::desc("Dump the gcov file to stderr")); 138 cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""), 139 cl::desc("Override inferred gcno file")); 140 cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""), 141 cl::desc("Override inferred gcda file")); 142 143 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); 144 145 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary, 146 PreservePaths, UncondBranch, LongNames, NoOutput); 147 148 for (const auto &SourceFile : SourceFiles) 149 reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV, 150 Options); 151 return 0; 152} 153