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