2014-08-23 06:56:03 +08:00
|
|
|
//===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The 'CodeCoverageTool' class implements a command line tool to analyze and
|
|
|
|
// report coverage information using the profiling instrumentation and code
|
|
|
|
// coverage mapping.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CoverageFilters.h"
|
|
|
|
#include "CoverageReport.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "CoverageViewOptions.h"
|
2016-04-30 02:53:05 +08:00
|
|
|
#include "RenderingSupport.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "SourceCoverageView.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-03-11 10:30:51 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-04-30 02:53:05 +08:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2015-03-19 08:02:23 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include <functional>
|
2014-09-09 13:32:18 +08:00
|
|
|
#include <system_error>
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// \brief The implementation of the coverage tool.
|
|
|
|
class CodeCoverageTool {
|
|
|
|
public:
|
|
|
|
enum Command {
|
|
|
|
/// \brief The show command.
|
|
|
|
Show,
|
|
|
|
/// \brief The report command.
|
|
|
|
Report
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Print the error message to the error output stream.
|
|
|
|
void error(const Twine &Message, StringRef Whence = "");
|
|
|
|
|
|
|
|
/// \brief Return a memory buffer for the given source file.
|
|
|
|
ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
/// \brief Create source views for the expansions of the view.
|
|
|
|
void attachExpansionSubViews(SourceCoverageView &View,
|
|
|
|
ArrayRef<ExpansionRecord> Expansions,
|
|
|
|
CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
/// \brief Create the source view of a particular function.
|
2014-09-20 03:07:17 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2014-09-20 23:31:56 +08:00
|
|
|
createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
/// \brief Create the main source view of a particular source file.
|
2014-09-20 03:07:17 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2014-09-20 23:31:56 +08:00
|
|
|
createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
/// \brief Load the coverage mapping data. Return true if an error occured.
|
2014-09-20 23:31:56 +08:00
|
|
|
std::unique_ptr<CoverageMapping> load();
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
int run(Command Cmd, int argc, const char **argv);
|
|
|
|
|
2016-06-09 03:09:22 +08:00
|
|
|
typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
int show(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
|
|
|
int report(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
2014-10-31 04:51:24 +08:00
|
|
|
std::string ObjectFilename;
|
2014-08-23 06:56:03 +08:00
|
|
|
CoverageViewOptions ViewOpts;
|
2014-09-20 23:31:56 +08:00
|
|
|
std::string PGOFilename;
|
2014-08-23 06:56:03 +08:00
|
|
|
CoverageFiltersMatchAll Filters;
|
|
|
|
std::vector<std::string> SourceFiles;
|
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
|
|
|
|
LoadedSourceFiles;
|
|
|
|
bool CompareFilenamesOnly;
|
2014-09-19 16:13:12 +08:00
|
|
|
StringMap<std::string> RemappedFilenames;
|
2015-06-23 05:33:24 +08:00
|
|
|
std::string CoverageArch;
|
2014-08-23 06:56:03 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
|
|
|
|
errs() << "error: ";
|
|
|
|
if (!Whence.empty())
|
|
|
|
errs() << Whence << ": ";
|
|
|
|
errs() << Message << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<const MemoryBuffer &>
|
|
|
|
CodeCoverageTool::getSourceFile(StringRef SourceFile) {
|
2014-09-19 16:13:12 +08:00
|
|
|
// If we've remapped filenames, look up the real location for this file.
|
|
|
|
if (!RemappedFilenames.empty()) {
|
|
|
|
auto Loc = RemappedFilenames.find(SourceFile);
|
|
|
|
if (Loc != RemappedFilenames.end())
|
|
|
|
SourceFile = Loc->second;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
2014-09-19 16:13:12 +08:00
|
|
|
for (const auto &Files : LoadedSourceFiles)
|
|
|
|
if (sys::fs::equivalent(SourceFile, Files.first))
|
|
|
|
return *Files.second;
|
2014-08-23 06:56:03 +08:00
|
|
|
auto Buffer = MemoryBuffer::getFile(SourceFile);
|
|
|
|
if (auto EC = Buffer.getError()) {
|
|
|
|
error(EC.message(), SourceFile);
|
|
|
|
return EC;
|
|
|
|
}
|
2015-05-30 03:43:39 +08:00
|
|
|
LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
|
2014-08-23 06:56:03 +08:00
|
|
|
return *LoadedSourceFiles.back().second;
|
|
|
|
}
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
void
|
|
|
|
CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
|
|
|
|
ArrayRef<ExpansionRecord> Expansions,
|
|
|
|
CoverageMapping &Coverage) {
|
2014-08-23 06:56:03 +08:00
|
|
|
if (!ViewOpts.ShowExpandedRegions)
|
|
|
|
return;
|
2014-09-20 23:31:56 +08:00
|
|
|
for (const auto &Expansion : Expansions) {
|
|
|
|
auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
|
|
|
|
if (ExpansionCoverage.empty())
|
2014-08-23 06:56:03 +08:00
|
|
|
continue;
|
2014-09-20 23:31:56 +08:00
|
|
|
auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
|
|
|
|
if (!SourceBuffer)
|
2014-08-23 06:56:03 +08:00
|
|
|
continue;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
auto SubViewExpansions = ExpansionCoverage.getExpansions();
|
2016-06-25 10:58:30 +08:00
|
|
|
auto SubView =
|
|
|
|
SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
|
|
|
|
ViewOpts, std::move(ExpansionCoverage));
|
2014-09-20 23:31:56 +08:00
|
|
|
attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
|
|
|
|
View.addExpansion(Expansion.Region, std::move(SubView));
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 03:07:17 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2014-09-20 23:31:56 +08:00
|
|
|
CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
|
|
|
|
CoverageMapping &Coverage) {
|
|
|
|
auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
|
|
|
|
if (FunctionCoverage.empty())
|
2014-09-20 03:07:17 +08:00
|
|
|
return nullptr;
|
2014-09-20 23:31:56 +08:00
|
|
|
auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
|
2014-09-20 03:07:17 +08:00
|
|
|
if (!SourceBuffer)
|
|
|
|
return nullptr;
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Expansions = FunctionCoverage.getExpansions();
|
2016-06-25 10:58:30 +08:00
|
|
|
auto View = SourceCoverageView::create(Function.Name, SourceBuffer.get(),
|
|
|
|
ViewOpts, std::move(FunctionCoverage));
|
2014-09-20 23:31:56 +08:00
|
|
|
attachExpansionSubViews(*View, Expansions, Coverage);
|
|
|
|
|
|
|
|
return View;
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
|
|
|
CodeCoverageTool::createSourceFileView(StringRef SourceFile,
|
|
|
|
CoverageMapping &Coverage) {
|
2014-09-20 03:07:17 +08:00
|
|
|
auto SourceBuffer = getSourceFile(SourceFile);
|
|
|
|
if (!SourceBuffer)
|
|
|
|
return nullptr;
|
2014-09-20 23:31:56 +08:00
|
|
|
auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
|
|
|
|
if (FileCoverage.empty())
|
2014-09-20 03:07:17 +08:00
|
|
|
return nullptr;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
auto Expansions = FileCoverage.getExpansions();
|
2016-06-25 10:58:30 +08:00
|
|
|
auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
|
|
|
|
ViewOpts, std::move(FileCoverage));
|
2014-09-20 23:31:56 +08:00
|
|
|
attachExpansionSubViews(*View, Expansions, Coverage);
|
|
|
|
|
|
|
|
for (auto Function : Coverage.getInstantiations(SourceFile)) {
|
|
|
|
auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
|
|
|
|
auto SubViewExpansions = SubViewCoverage.getExpansions();
|
2016-06-25 10:58:30 +08:00
|
|
|
auto SubView =
|
|
|
|
SourceCoverageView::create(Function->Name, SourceBuffer.get(), ViewOpts,
|
|
|
|
std::move(SubViewCoverage));
|
2014-09-20 23:31:56 +08:00
|
|
|
attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
|
|
|
|
|
|
|
|
if (SubView) {
|
2014-09-17 13:33:20 +08:00
|
|
|
unsigned FileID = Function->CountedRegions.front().FileID;
|
|
|
|
unsigned Line = 0;
|
|
|
|
for (const auto &CR : Function->CountedRegions)
|
|
|
|
if (CR.FileID == FileID)
|
|
|
|
Line = std::max(CR.LineEnd, Line);
|
2014-09-20 23:31:56 +08:00
|
|
|
View->addInstantiation(Function->Name, Line, std::move(SubView));
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-20 03:07:17 +08:00
|
|
|
return View;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 12:09:38 +08:00
|
|
|
static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
|
|
|
|
sys::fs::file_status Status;
|
|
|
|
if (sys::fs::status(LHS, Status))
|
|
|
|
return false;
|
|
|
|
auto LHSTime = Status.getLastModificationTime();
|
|
|
|
if (sys::fs::status(RHS, Status))
|
|
|
|
return false;
|
|
|
|
auto RHSTime = Status.getLastModificationTime();
|
|
|
|
return LHSTime > RHSTime;
|
|
|
|
}
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
|
2015-05-04 12:09:38 +08:00
|
|
|
if (modifiedTimeGT(ObjectFilename, PGOFilename))
|
|
|
|
errs() << "warning: profile data may be out of date - object is newer\n";
|
2015-03-11 10:30:51 +08:00
|
|
|
auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
|
|
|
|
CoverageArch);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = CoverageOrErr.takeError()) {
|
2014-09-20 23:31:56 +08:00
|
|
|
colored_ostream(errs(), raw_ostream::RED)
|
2016-05-19 11:54:45 +08:00
|
|
|
<< "error: Failed to load coverage: " << toString(std::move(E)) << "\n";
|
2014-09-20 23:31:56 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto Coverage = std::move(CoverageOrErr.get());
|
|
|
|
unsigned Mismatched = Coverage->getMismatchedCount();
|
|
|
|
if (Mismatched) {
|
|
|
|
colored_ostream(errs(), raw_ostream::RED)
|
|
|
|
<< "warning: " << Mismatched << " functions have mismatched data. ";
|
|
|
|
errs() << "\n";
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
2014-09-19 16:13:12 +08:00
|
|
|
|
|
|
|
if (CompareFilenamesOnly) {
|
2014-09-20 23:31:56 +08:00
|
|
|
auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
|
2014-09-19 16:13:12 +08:00
|
|
|
for (auto &SF : SourceFiles) {
|
|
|
|
StringRef SFBase = sys::path::filename(SF);
|
|
|
|
for (const auto &CF : CoveredFiles)
|
|
|
|
if (SFBase == sys::path::filename(CF)) {
|
|
|
|
RemappedFilenames[CF] = SF;
|
|
|
|
SF = CF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
return Coverage;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
|
2014-10-31 04:51:24 +08:00
|
|
|
cl::opt<std::string, true> ObjectFilename(
|
|
|
|
cl::Positional, cl::Required, cl::location(this->ObjectFilename),
|
|
|
|
cl::desc("Covered executable or object file."));
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
cl::list<std::string> InputSourceFiles(
|
|
|
|
cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
cl::opt<std::string, true> PGOFilename(
|
|
|
|
"instr-profile", cl::Required, cl::location(this->PGOFilename),
|
2014-08-23 06:56:03 +08:00
|
|
|
cl::desc(
|
|
|
|
"File with the profile data obtained after an instrumented run"));
|
|
|
|
|
2015-03-11 10:30:51 +08:00
|
|
|
cl::opt<std::string> Arch(
|
|
|
|
"arch", cl::desc("architecture of the coverage mapping binary"));
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
cl::opt<bool> DebugDump("dump", cl::Optional,
|
|
|
|
cl::desc("Show internal debug dump"));
|
|
|
|
|
|
|
|
cl::opt<bool> FilenameEquivalence(
|
|
|
|
"filename-equivalence", cl::Optional,
|
2014-09-19 16:13:12 +08:00
|
|
|
cl::desc("Treat source files as equivalent to paths in the coverage data "
|
|
|
|
"when the file names match, even if the full paths do not"));
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
cl::OptionCategory FilteringCategory("Function filtering options");
|
|
|
|
|
|
|
|
cl::list<std::string> NameFilters(
|
|
|
|
"name", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions with the given name"),
|
|
|
|
cl::ZeroOrMore, cl::cat(FilteringCategory));
|
|
|
|
|
|
|
|
cl::list<std::string> NameRegexFilters(
|
|
|
|
"name-regex", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions that match the given "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::cat(FilteringCategory));
|
|
|
|
|
|
|
|
cl::opt<double> RegionCoverageLtFilter(
|
|
|
|
"region-coverage-lt", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions with region coverage "
|
|
|
|
"less than the given threshold"),
|
|
|
|
cl::cat(FilteringCategory));
|
|
|
|
|
|
|
|
cl::opt<double> RegionCoverageGtFilter(
|
|
|
|
"region-coverage-gt", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions with region coverage "
|
|
|
|
"greater than the given threshold"),
|
|
|
|
cl::cat(FilteringCategory));
|
|
|
|
|
|
|
|
cl::opt<double> LineCoverageLtFilter(
|
|
|
|
"line-coverage-lt", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions with line coverage less "
|
|
|
|
"than the given threshold"),
|
|
|
|
cl::cat(FilteringCategory));
|
|
|
|
|
|
|
|
cl::opt<double> LineCoverageGtFilter(
|
|
|
|
"line-coverage-gt", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions with line coverage "
|
|
|
|
"greater than the given threshold"),
|
|
|
|
cl::cat(FilteringCategory));
|
|
|
|
|
2015-03-19 12:45:16 +08:00
|
|
|
cl::opt<cl::boolOrDefault> UseColor(
|
|
|
|
"use-color", cl::desc("Emit colored output (default=autodetect)"),
|
|
|
|
cl::init(cl::BOU_UNSET));
|
2015-03-19 08:02:23 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
auto commandLineParser = [&, this](int argc, const char **argv) -> int {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
|
|
|
|
ViewOpts.Debug = DebugDump;
|
|
|
|
CompareFilenamesOnly = FilenameEquivalence;
|
|
|
|
|
2015-03-19 12:45:16 +08:00
|
|
|
ViewOpts.Colors = UseColor == cl::BOU_UNSET
|
|
|
|
? sys::Process::StandardOutHasColors()
|
|
|
|
: UseColor == cl::BOU_TRUE;
|
2015-03-19 08:02:23 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
// Create the function filters
|
|
|
|
if (!NameFilters.empty() || !NameRegexFilters.empty()) {
|
|
|
|
auto NameFilterer = new CoverageFilters;
|
|
|
|
for (const auto &Name : NameFilters)
|
|
|
|
NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
|
|
|
|
for (const auto &Regex : NameRegexFilters)
|
|
|
|
NameFilterer->push_back(
|
|
|
|
llvm::make_unique<NameRegexCoverageFilter>(Regex));
|
|
|
|
Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
|
|
|
|
}
|
|
|
|
if (RegionCoverageLtFilter.getNumOccurrences() ||
|
|
|
|
RegionCoverageGtFilter.getNumOccurrences() ||
|
|
|
|
LineCoverageLtFilter.getNumOccurrences() ||
|
|
|
|
LineCoverageGtFilter.getNumOccurrences()) {
|
|
|
|
auto StatFilterer = new CoverageFilters;
|
|
|
|
if (RegionCoverageLtFilter.getNumOccurrences())
|
|
|
|
StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
|
|
|
|
RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
|
|
|
|
if (RegionCoverageGtFilter.getNumOccurrences())
|
|
|
|
StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
|
|
|
|
RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
|
|
|
|
if (LineCoverageLtFilter.getNumOccurrences())
|
|
|
|
StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
|
|
|
|
LineCoverageFilter::LessThan, LineCoverageLtFilter));
|
|
|
|
if (LineCoverageGtFilter.getNumOccurrences())
|
|
|
|
StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
|
|
|
|
RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
|
|
|
|
Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:33:24 +08:00
|
|
|
if (!Arch.empty() &&
|
|
|
|
Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
|
|
|
|
errs() << "error: Unknown architecture: " << Arch << "\n";
|
|
|
|
return 1;
|
2015-03-11 10:30:51 +08:00
|
|
|
}
|
2015-06-23 05:33:24 +08:00
|
|
|
CoverageArch = Arch;
|
2015-03-11 10:30:51 +08:00
|
|
|
|
2014-09-19 16:13:12 +08:00
|
|
|
for (const auto &File : InputSourceFiles) {
|
|
|
|
SmallString<128> Path(File);
|
2015-02-14 10:05:05 +08:00
|
|
|
if (!CompareFilenamesOnly)
|
|
|
|
if (std::error_code EC = sys::fs::make_absolute(Path)) {
|
|
|
|
errs() << "error: " << File << ": " << EC.message();
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-19 16:13:12 +08:00
|
|
|
SourceFiles.push_back(Path.str());
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (Cmd) {
|
|
|
|
case Show:
|
|
|
|
return show(argc, argv, commandLineParser);
|
|
|
|
case Report:
|
|
|
|
return report(argc, argv, commandLineParser);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CodeCoverageTool::show(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
|
|
|
|
|
|
|
cl::OptionCategory ViewCategory("Viewing options");
|
|
|
|
|
|
|
|
cl::opt<bool> ShowLineExecutionCounts(
|
|
|
|
"show-line-counts", cl::Optional,
|
|
|
|
cl::desc("Show the execution counts for each line"), cl::init(true),
|
|
|
|
cl::cat(ViewCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> ShowRegions(
|
|
|
|
"show-regions", cl::Optional,
|
|
|
|
cl::desc("Show the execution counts for each region"),
|
|
|
|
cl::cat(ViewCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> ShowBestLineRegionsCounts(
|
|
|
|
"show-line-counts-or-regions", cl::Optional,
|
|
|
|
cl::desc("Show the execution counts for each line, or the execution "
|
|
|
|
"counts for each region on lines that have multiple regions"),
|
|
|
|
cl::cat(ViewCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
|
|
|
|
cl::desc("Show expanded source regions"),
|
|
|
|
cl::cat(ViewCategory));
|
|
|
|
|
|
|
|
cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
|
|
|
|
cl::desc("Show function instantiations"),
|
|
|
|
cl::cat(ViewCategory));
|
|
|
|
|
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
|
|
|
ViewOpts.ShowLineNumbers = true;
|
|
|
|
ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
|
|
|
|
!ShowRegions || ShowBestLineRegionsCounts;
|
|
|
|
ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
|
|
|
|
ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
|
|
|
|
ViewOpts.ShowExpandedRegions = ShowExpansions;
|
|
|
|
ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 06:56:03 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!Filters.empty()) {
|
|
|
|
// Show functions
|
2014-09-20 23:31:56 +08:00
|
|
|
for (const auto &Function : Coverage->getCoveredFunctions()) {
|
|
|
|
if (!Filters.matches(Function))
|
2014-08-23 06:56:03 +08:00
|
|
|
continue;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
auto mainView = createFunctionView(Function, *Coverage);
|
2014-09-20 03:07:17 +08:00
|
|
|
if (!mainView) {
|
2016-06-24 10:33:01 +08:00
|
|
|
ViewOpts.colored_ostream(errs(), raw_ostream::RED)
|
|
|
|
<< "warning: Could not read coverage for '" << Function.Name << "'."
|
|
|
|
<< "\n";
|
2014-09-20 03:07:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-25 10:58:30 +08:00
|
|
|
mainView->print(outs(), /*WholeFile=*/false, /*ShowSourceName=*/true);
|
2014-09-20 23:31:56 +08:00
|
|
|
outs() << "\n";
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show files
|
|
|
|
bool ShowFilenames = SourceFiles.size() != 1;
|
|
|
|
|
2014-09-19 16:13:12 +08:00
|
|
|
if (SourceFiles.empty())
|
2014-08-23 06:56:03 +08:00
|
|
|
// Get the source files from the function coverage mapping
|
2014-09-20 23:31:56 +08:00
|
|
|
for (StringRef Filename : Coverage->getUniqueSourceFiles())
|
2014-08-23 06:56:03 +08:00
|
|
|
SourceFiles.push_back(Filename);
|
|
|
|
|
|
|
|
for (const auto &SourceFile : SourceFiles) {
|
2014-09-20 23:31:56 +08:00
|
|
|
auto mainView = createSourceFileView(SourceFile, *Coverage);
|
2014-09-20 03:07:17 +08:00
|
|
|
if (!mainView) {
|
2016-06-24 10:33:01 +08:00
|
|
|
ViewOpts.colored_ostream(errs(), raw_ostream::RED)
|
2014-08-23 06:56:03 +08:00
|
|
|
<< "warning: The file '" << SourceFile << "' isn't covered.";
|
2016-06-24 10:33:01 +08:00
|
|
|
errs() << "\n";
|
2014-08-23 06:56:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-25 10:58:30 +08:00
|
|
|
mainView->print(outs(), /*Wholefile=*/true, /*ShowSourceName=*/ShowFilenames);
|
2014-08-23 06:56:03 +08:00
|
|
|
if (SourceFiles.size() > 1)
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CodeCoverageTool::report(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 06:56:03 +08:00
|
|
|
return 1;
|
|
|
|
|
2015-02-14 10:01:24 +08:00
|
|
|
CoverageReport Report(ViewOpts, std::move(Coverage));
|
2015-02-14 10:05:05 +08:00
|
|
|
if (SourceFiles.empty())
|
2014-08-23 06:56:03 +08:00
|
|
|
Report.renderFileReports(llvm::outs());
|
2015-02-14 10:05:05 +08:00
|
|
|
else
|
|
|
|
Report.renderFunctionReports(SourceFiles, llvm::outs());
|
2014-08-23 06:56:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 04:57:49 +08:00
|
|
|
int showMain(int argc, const char *argv[]) {
|
2014-08-23 06:56:03 +08:00
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Show, argc, argv);
|
|
|
|
}
|
|
|
|
|
2014-10-31 04:57:49 +08:00
|
|
|
int reportMain(int argc, const char *argv[]) {
|
2014-08-23 06:56:03 +08:00
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Report, argc, argv);
|
|
|
|
}
|