2013-12-06 06:02:29 +08:00
|
|
|
//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
|
2011-09-29 02:50:00 +08: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 14:41:43 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-09-29 02:50:00 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2014-02-18 17:19:48 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-10-05 01:24:48 +08:00
|
|
|
#include "llvm/Support/GCOV.h"
|
2011-09-29 02:50:00 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryObject.h"
|
2014-02-04 14:41:43 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-09-29 02:50:00 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2011-09-29 02:50:00 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-30 05:31:34 +08:00
|
|
|
static cl::opt<std::string> SourceFile(cl::Positional, cl::Required,
|
|
|
|
cl::desc("SOURCEFILE"));
|
|
|
|
|
2014-04-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
|
2014-01-30 05:31:45 +08:00
|
|
|
cl::desc("Display all basic blocks"));
|
2014-01-30 05:31:47 +08:00
|
|
|
static cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
|
2011-09-29 02:50:00 +08:00
|
|
|
|
2014-04-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
|
2014-01-30 05:31:45 +08:00
|
|
|
cl::desc("Display branch probabilities"));
|
2014-01-30 05:31:47 +08:00
|
|
|
static cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
|
2011-09-29 02:50:00 +08:00
|
|
|
|
2014-04-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
|
2014-01-30 05:31:45 +08:00
|
|
|
cl::desc("Display branch counts instead "
|
|
|
|
"of percentages (requires -b)"));
|
2014-01-30 05:31:47 +08:00
|
|
|
static cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
|
2011-09-29 02:50:00 +08:00
|
|
|
|
2014-04-24 05:44:55 +08: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-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
|
2014-01-30 05:31:45 +08:00
|
|
|
cl::desc("Show coverage for each function"));
|
2014-01-30 05:31:47 +08:00
|
|
|
static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
|
2013-12-10 09:02:07 +08:00
|
|
|
|
2014-05-07 10:11:18 +08: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 17:19:48 +08: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 14:41:43 +08:00
|
|
|
static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
|
2014-02-18 17:19:48 +08:00
|
|
|
static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
|
2014-02-04 14:41:43 +08:00
|
|
|
|
2014-04-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
|
2014-02-04 18:45:02 +08:00
|
|
|
cl::desc("Preserve path components"));
|
|
|
|
static cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
|
|
|
|
|
2014-04-24 05:44:48 +08:00
|
|
|
static cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
|
2014-01-30 05:31:45 +08:00
|
|
|
cl::desc("Display unconditional branch info "
|
|
|
|
"(requires -b)"));
|
2014-01-30 05:31:47 +08:00
|
|
|
static cl::alias UncondBranchA("unconditional-branches",
|
|
|
|
cl::aliasopt(UncondBranch));
|
2013-12-19 02:40:15 +08:00
|
|
|
|
2014-01-30 05:31:45 +08: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-17 06:14:02 +08:00
|
|
|
|
2011-09-29 02:50:00 +08: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-30 05:31:45 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
|
2011-09-29 02:50:00 +08:00
|
|
|
|
2014-02-04 14:41:43 +08:00
|
|
|
SmallString<128> CoverageFileStem(ObjectDir);
|
2014-02-18 17:19:48 +08:00
|
|
|
if (CoverageFileStem.empty()) {
|
|
|
|
// If no directory was specified with -o, look next to the source file.
|
2014-02-04 14:41:43 +08:00
|
|
|
CoverageFileStem = sys::path::parent_path(SourceFile);
|
2014-02-18 17:19:48 +08: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 14:41:43 +08:00
|
|
|
|
2011-09-29 02:50:00 +08:00
|
|
|
if (InputGCNO.empty())
|
2014-02-04 14:41:43 +08:00
|
|
|
InputGCNO = (CoverageFileStem.str() + ".gcno").str();
|
2014-01-30 05:31:34 +08:00
|
|
|
if (InputGCDA.empty())
|
2014-02-04 14:41:43 +08:00
|
|
|
InputGCDA = (CoverageFileStem.str() + ".gcda").str();
|
2014-01-30 05:31:34 +08:00
|
|
|
|
|
|
|
GCOVFile GF;
|
2011-09-29 02:50:00 +08:00
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> GCNO_Buff;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCNO, GCNO_Buff)) {
|
2011-09-29 02:50:00 +08:00
|
|
|
errs() << InputGCNO << ": " << ec.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-15 17:44:17 +08:00
|
|
|
GCOVBuffer GCNO_GB(GCNO_Buff.get());
|
2013-12-04 12:49:23 +08:00
|
|
|
if (!GF.readGCNO(GCNO_GB)) {
|
2011-09-29 02:50:00 +08:00
|
|
|
errs() << "Invalid .gcno File!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> GCDA_Buff;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputGCDA, GCDA_Buff)) {
|
2014-06-14 01:20:48 +08:00
|
|
|
if (ec != errc::no_such_file_or_directory) {
|
2014-02-04 14:41:39 +08:00
|
|
|
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-29 02:50:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DumpGCOV)
|
|
|
|
GF.dump();
|
|
|
|
|
2014-01-30 05:31:45 +08:00
|
|
|
GCOVOptions Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
|
2014-05-07 10:11:18 +08:00
|
|
|
PreservePaths, UncondBranch, LongNames, NoOutput);
|
2013-12-13 09:15:07 +08:00
|
|
|
FileInfo FI(Options);
|
2011-09-29 02:50:00 +08:00
|
|
|
GF.collectLineCounts(FI);
|
2014-04-24 05:44:55 +08:00
|
|
|
FI.print(SourceFile, InputGCNO, InputGCDA);
|
2011-09-29 02:50:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|