| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1 | //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===// |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 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 | d02c42b | 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 | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 14 | #include "llvm/ADT/SmallString.h" |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 15 | #include "llvm/Support/CommandLine.h" |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 16 | #include "llvm/Support/FileSystem.h" |
| Devang Patel | 58c6200 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 17 | #include "llvm/Support/GCOV.h" |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
| 19 | #include "llvm/Support/MemoryObject.h" |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 20 | #include "llvm/Support/Path.h" |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/PrettyStackTrace.h" |
| 22 | #include "llvm/Support/Signals.h" |
| 23 | #include "llvm/Support/system_error.h" |
| 24 | using namespace llvm; |
| 25 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 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)); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 45 | |
| 46 | static cl::opt<std::string> |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 47 | ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""), |
| 48 | cl::desc("Find objects in DIR or based on FILE's path")); |
| 49 | static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir)); |
| 50 | static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir)); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 51 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 52 | static cl::opt<bool> PreservePaths("p", cl::init(false), |
| 53 | cl::desc("Preserve path components")); |
| 54 | static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths)); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 55 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 56 | static cl::opt<bool> UncondBranch("u", cl::init(false), |
| 57 | cl::desc("Display unconditional branch info " |
| 58 | "(requires -b)")); |
| 59 | static cl::alias UncondBranchA("unconditional-branches", |
| 60 | cl::aliasopt(UncondBranch)); |
| Yuchen Wu | daaa8b7 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 61 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 62 | static cl::OptionCategory DebugCat("Internal and debugging options"); |
| 63 | static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat), |
| 64 | cl::desc("Dump the gcov file to stderr")); |
| 65 | static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""), |
| 66 | cl::desc("Override inferred gcno file")); |
| 67 | static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""), |
| 68 | cl::desc("Override inferred gcda file")); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 69 | |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | int main(int argc, char **argv) { |
| 72 | // Print a stack trace if we signal out. |
| 73 | sys::PrintStackTraceOnErrorSignal(); |
| 74 | PrettyStackTraceProgram X(argc, argv); |
| 75 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 76 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 77 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 78 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 79 | SmallString<128> CoverageFileStem(ObjectDir); |
| 80 | if (CoverageFileStem.empty()) { |
| 81 | // If no directory was specified with -o, look next to the source file. |
| 82 | CoverageFileStem = sys::path::parent_path(SourceFile); |
| 83 | sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); |
| 84 | } else if (sys::fs::is_directory(ObjectDir)) |
| 85 | // A directory name was given. Use it and the source file name. |
| 86 | sys::path::append(CoverageFileStem, sys::path::stem(SourceFile)); |
| 87 | else |
| 88 | // A file was given. Ignore the source file and look next to this file. |
| 89 | sys::path::replace_extension(CoverageFileStem, ""); |
| 90 | |
| 91 | if (InputGCNO.empty()) |
| 92 | InputGCNO = (CoverageFileStem.str() + ".gcno").str(); |
| 93 | if (InputGCDA.empty()) |
| 94 | InputGCDA = (CoverageFileStem.str() + ".gcda").str(); |
| Yuchen Wu | daaa8b7 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 95 | |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 96 | GCOVFile GF; |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 97 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 98 | std::unique_ptr<MemoryBuffer> GCNO_Buff; |
| Devang Patel | 7a50202 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 99 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) { |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 100 | errs() << InputGCNO << ": " << ec.message() << "\n"; |
| 101 | return 1; |
| 102 | } |
| Benjamin Kramer | d881c1b | 2013-11-15 09:44:17 +0000 | [diff] [blame] | 103 | GCOVBuffer GCNO_GB(GCNO_Buff.get()); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 104 | if (!GF.readGCNO(GCNO_GB)) { |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 105 | errs() << "Invalid .gcno File!\n"; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 109 | std::unique_ptr<MemoryBuffer> GCDA_Buff; |
| 110 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) { |
| 111 | if (ec != errc::no_such_file_or_directory) { |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 112 | errs() << InputGCDA << ": " << ec.message() << "\n"; |
| 113 | return 1; |
| 114 | } |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 115 | // Clear the filename to make it clear we didn't read anything. |
| 116 | InputGCDA = "-"; |
| 117 | } else { |
| Benjamin Kramer | d881c1b | 2013-11-15 09:44:17 +0000 | [diff] [blame] | 118 | GCOVBuffer GCDA_GB(GCDA_Buff.get()); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 119 | if (!GF.readGCDA(GCDA_GB)) { |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 120 | errs() << "Invalid .gcda File!\n"; |
| 121 | return 1; |
| 122 | } |
| 123 | } |
| 124 | |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 125 | if (DumpGCOV) |
| 126 | GF.dump(); |
| 127 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 128 | GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary, |
| 129 | PreservePaths, UncondBranch); |
| 130 | FileInfo FI(Options); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 131 | GF.collectLineCounts(FI); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 132 | FI.print(InputGCNO, InputGCDA); |
| Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | } |