2013-12-05 22:02:29 +00:00
|
|
|
//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
|
2011-09-28 18:50:00 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// llvm-cov is a command line tools to analyze and report coverage information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-02-04 06:41:43 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-09-28 18:50:00 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-02-18 09:19:48 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-10-04 17:24:48 +00:00
|
|
|
#include "llvm/Support/GCOV.h"
|
2011-09-28 18:50:00 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryObject.h"
|
2014-02-04 06:41:43 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-09-28 18:50:00 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-29 21:31:34 +00:00
|
|
|
static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
|
|
|
|
cl::desc("SOURCEFILE"));
|
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::desc("Display all basic blocks"));
|
2014-01-29 21:31:47 +00:00
|
|
|
static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::desc("Display branch probabilities"));
|
2014-01-29 21:31:47 +00:00
|
|
|
static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::desc("Display branch counts instead "
|
|
|
|
"of percentages (requires -b)"));
|
2014-01-29 21:31:47 +00:00
|
|
|
static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2014-04-23 21:44:55 +00:00
|
|
|
static cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
|
|
|
|
cl::desc("Prefix filenames with the main file"));
|
|
|
|
static cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
|
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::desc("Show coverage for each function"));
|
2014-01-29 21:31:47 +00:00
|
|
|
static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
|
2013-12-10 01:02:07 +00:00
|
|
|
|
2014-05-07 02:11:18 +00:00
|
|
|
static cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
|
|
|
|
cl::desc("Do not output any .gcov files"));
|
|
|
|
static cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
|
|
|
|
|
2014-02-18 09:19:48 +00:00
|
|
|
static cl::opt<std::string>
|
|
|
|
ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""),
|
|
|
|
cl::desc("Find objects in DIR or based on FILE's path"));
|
2014-02-04 06:41:43 +00:00
|
|
|
static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
|
2014-02-18 09:19:48 +00:00
|
|
|
static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
|
2014-02-04 06:41:43 +00:00
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
|
2014-02-04 10:45:02 +00:00
|
|
|
cl::desc("Preserve path components"));
|
|
|
|
static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
|
|
|
|
|
2014-04-23 21:44:48 +00:00
|
|
|
static cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::desc("Display unconditional branch info "
|
|
|
|
"(requires -b)"));
|
2014-01-29 21:31:47 +00:00
|
|
|
static cl::alias UncondBranchA("unconditional-branches",
|
|
|
|
cl::aliasopt(UncondBranch));
|
2013-12-18 18:40:15 +00:00
|
|
|
|
2014-01-29 21:31:45 +00:00
|
|
|
static cl::OptionCategory DebugCat("Internal and debugging options");
|
|
|
|
static cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
|
|
|
|
cl::desc("Dump the gcov file to stderr"));
|
|
|
|
static cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
|
|
|
|
cl::desc("Override inferred gcno file"));
|
|
|
|
static cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
|
|
|
|
cl::desc("Override inferred gcda file"));
|
2013-12-16 22:14:02 +00:00
|
|
|
|
2011-09-28 18:50:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2014-01-29 21:31:45 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2014-02-04 06:41:43 +00:00
|
|
|
SmallString<128> CoverageFileStem(ObjectDir);
|
2014-02-18 09:19:48 +00:00
|
|
|
if (CoverageFileStem.empty()) {
|
|
|
|
// If no directory was specified with -o, look next to the source file.
|
2014-02-04 06:41:43 +00:00
|
|
|
CoverageFileStem = sys::path::parent_path(SourceFile);
|
2014-02-18 09:19:48 +00:00
|
|
|
sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
|
|
|
|
} else if (sys::fs::is_directory(ObjectDir))
|
|
|
|
// A directory name was given. Use it and the source file name.
|
|
|
|
sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
|
|
|
|
else
|
|
|
|
// A file was given. Ignore the source file and look next to this file.
|
|
|
|
sys::path::replace_extension(CoverageFileStem, "");
|
2014-02-04 06:41:43 +00:00
|
|
|
|
2011-09-28 18:50:00 +00:00
|
|
|
if (InputGCNO.empty())
|
2014-02-04 06:41:43 +00:00
|
|
|
InputGCNO = (CoverageFileStem.str() + ".gcno").str();
|
2014-01-29 21:31:34 +00:00
|
|
|
if (InputGCDA.empty())
|
2014-02-04 06:41:43 +00:00
|
|
|
InputGCDA = (CoverageFileStem.str() + ".gcda").str();
|
2014-01-29 21:31:34 +00:00
|
|
|
|
|
|
|
GCOVFile GF;
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> GCNO_Buff;
|
2011-09-29 16:46:47 +00:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
|
2011-09-28 18:50:00 +00:00
|
|
|
errs() << InputGCNO << ": " << ec.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-15 09:44:17 +00:00
|
|
|
GCOVBuffer GCNO_GB(GCNO_Buff.get());
|
2013-12-04 04:49:23 +00:00
|
|
|
if (!GF.readGCNO(GCNO_GB)) {
|
2011-09-28 18:50:00 +00:00
|
|
|
errs() << "Invalid .gcno File!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> GCDA_Buff;
|
2014-01-29 21:31:34 +00:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
|
2014-02-04 06:41:39 +00:00
|
|
|
if (ec != errc::no_such_file_or_directory) {
|
|
|
|
errs() << InputGCDA << ": " << ec.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Clear the filename to make it clear we didn't read anything.
|
|
|
|
InputGCDA = "-";
|
|
|
|
} else {
|
|
|
|
GCOVBuffer GCDA_GB(GCDA_Buff.get());
|
|
|
|
if (!GF.readGCDA(GCDA_GB)) {
|
|
|
|
errs() << "Invalid .gcda File!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2011-09-28 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DumpGCOV)
|
|
|
|
GF.dump();
|
|
|
|
|
2014-01-29 21:31:45 +00:00
|
|
|
GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
|
2014-05-07 02:11:18 +00:00
|
|
|
PreservePaths, UncondBranch, LongNames, NoOutput);
|
2013-12-13 01:15:07 +00:00
|
|
|
FileInfo FI(Options);
|
2011-09-28 18:50:00 +00:00
|
|
|
GF.collectLineCounts(FI);
|
2014-04-23 21:44:55 +00:00
|
|
|
FI.print(SourceFile, InputGCNO, InputGCDA);
|
2011-09-28 18:50:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|