blob: 18cc1b110e2342803c9ab1fd73b4e40038176dab [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
Devang Pateld02c42b2011-09-28 18:50:00 +00002//
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 Pateld02c42b2011-09-28 18:50:00 +000010// llvm-cov is a command line tools to analyze and report coverage information.
11//
Devang Pateld02c42b2011-09-28 18:50:00 +000012//===----------------------------------------------------------------------===//
13
Stephen Hines36b56882014-04-23 16:57:46 -070014#include "llvm/ADT/SmallString.h"
Devang Pateld02c42b2011-09-28 18:50:00 +000015#include "llvm/Support/CommandLine.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070016#include "llvm/Support/Errc.h"
Stephen Hines36b56882014-04-23 16:57:46 -070017#include "llvm/Support/FileSystem.h"
Devang Patel58c62002011-10-04 17:24:48 +000018#include "llvm/Support/GCOV.h"
Devang Pateld02c42b2011-09-28 18:50:00 +000019#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/MemoryObject.h"
Stephen Hines36b56882014-04-23 16:57:46 -070021#include "llvm/Support/Path.h"
Devang Pateld02c42b2011-09-28 18:50:00 +000022#include "llvm/Support/PrettyStackTrace.h"
23#include "llvm/Support/Signals.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070024#include <system_error>
Devang Pateld02c42b2011-09-28 18:50:00 +000025using namespace llvm;
26
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070027static cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
28 cl::desc("SOURCEFILE"));
Stephen Hines36b56882014-04-23 16:57:46 -070029
Stephen Hinesdce4a402014-05-29 02:49:00 -070030static cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070031 cl::desc("Display all basic blocks"));
32static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
33
Stephen Hinesdce4a402014-05-29 02:49:00 -070034static cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070035 cl::desc("Display branch probabilities"));
36static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
37
Stephen Hinesdce4a402014-05-29 02:49:00 -070038static cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070039 cl::desc("Display branch counts instead "
40 "of percentages (requires -b)"));
41static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
42
Stephen Hinesdce4a402014-05-29 02:49:00 -070043static cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
44 cl::desc("Prefix filenames with the main file"));
45static cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
46
47static cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070048 cl::desc("Show coverage for each function"));
49static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
Devang Pateld02c42b2011-09-28 18:50:00 +000050
Stephen Hinesdce4a402014-05-29 02:49:00 -070051static cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
52 cl::desc("Do not output any .gcov files"));
53static cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
54
Devang Pateld02c42b2011-09-28 18:50:00 +000055static cl::opt<std::string>
Stephen Hines36b56882014-04-23 16:57:46 -070056ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""),
57 cl::desc("Find objects in DIR or based on FILE's path"));
58static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
59static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
Devang Pateld02c42b2011-09-28 18:50:00 +000060
Stephen Hinesdce4a402014-05-29 02:49:00 -070061static cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070062 cl::desc("Preserve path components"));
63static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
Devang Pateld02c42b2011-09-28 18:50:00 +000064
Stephen Hinesdce4a402014-05-29 02:49:00 -070065static cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
Stephen Hines36b56882014-04-23 16:57:46 -070066 cl::desc("Display unconditional branch info "
67 "(requires -b)"));
68static cl::alias UncondBranchA("unconditional-branches",
69 cl::aliasopt(UncondBranch));
Yuchen Wudaaa8b72013-11-02 00:09:17 +000070
Stephen Hines36b56882014-04-23 16:57:46 -070071static cl::OptionCategory DebugCat("Internal and debugging options");
72static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
73 cl::desc("Dump the gcov file to stderr"));
74static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
75 cl::desc("Override inferred gcno file"));
76static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
77 cl::desc("Override inferred gcda file"));
Devang Pateld02c42b2011-09-28 18:50:00 +000078
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070079void reportCoverage(StringRef SourceFile) {
Stephen Hines36b56882014-04-23 16:57:46 -070080 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 Hinesc6a4f5e2014-07-21 00:45:20 -070092 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 Pateld02c42b2011-09-28 18:50:00 +000098 GCOVFile GF;
Devang Pateld02c42b2011-09-28 18:50:00 +000099
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700100 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 Pateld02c42b2011-09-28 18:50:00 +0000105 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700106 GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
Stephen Hines36b56882014-04-23 16:57:46 -0700107 if (!GF.readGCNO(GCNO_GB)) {
Devang Pateld02c42b2011-09-28 18:50:00 +0000108 errs() << "Invalid .gcno File!\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700109 return;
Devang Pateld02c42b2011-09-28 18:50:00 +0000110 }
111
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700112 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 Pateld02c42b2011-09-28 18:50:00 +0000118 }
Stephen Hines36b56882014-04-23 16:57:46 -0700119 // Clear the filename to make it clear we didn't read anything.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700120 GCDA = "-";
Stephen Hines36b56882014-04-23 16:57:46 -0700121 } else {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700122 GCOVBuffer GCDA_GB(GCDA_Buff.get().get());
Stephen Hines36b56882014-04-23 16:57:46 -0700123 if (!GF.readGCDA(GCDA_GB)) {
Devang Pateld02c42b2011-09-28 18:50:00 +0000124 errs() << "Invalid .gcda File!\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700125 return;
Devang Pateld02c42b2011-09-28 18:50:00 +0000126 }
127 }
128
Devang Pateld02c42b2011-09-28 18:50:00 +0000129 if (DumpGCOV)
130 GF.dump();
131
Stephen Hines36b56882014-04-23 16:57:46 -0700132 GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700133 PreservePaths, UncondBranch, LongNames, NoOutput);
Stephen Hines36b56882014-04-23 16:57:46 -0700134 FileInfo FI(Options);
Devang Pateld02c42b2011-09-28 18:50:00 +0000135 GF.collectLineCounts(FI);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700136 FI.print(SourceFile, GCNO, GCDA);
137}
138
139int 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 Pateld02c42b2011-09-28 18:50:00 +0000149 return 0;
150}