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