2014-08-23 00:56:03 +02: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"
|
2017-02-05 21:10:58 +01:00
|
|
|
#include "CoverageSummaryInfo.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "CoverageViewOptions.h"
|
2016-04-29 20:53:05 +02:00
|
|
|
#include "RenderingSupport.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "SourceCoverageView.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-03-11 03:30:51 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-04-29 20:53:05 +02:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2016-07-16 00:44:57 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-03-19 01:02:23 +01:00
|
|
|
#include "llvm/Support/Process.h"
|
2016-07-16 00:44:57 +02:00
|
|
|
#include "llvm/Support/Program.h"
|
2016-10-24 12:59:17 +02:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2017-07-11 03:23:29 +02:00
|
|
|
#include "llvm/Support/Threading.h"
|
2016-07-13 23:38:36 +02:00
|
|
|
#include "llvm/Support/ThreadPool.h"
|
2016-07-16 00:44:57 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include <functional>
|
2014-09-09 07:32:18 +02:00
|
|
|
#include <system_error>
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
2016-10-25 02:08:33 +02:00
|
|
|
void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
|
2016-07-27 00:50:58 +02:00
|
|
|
raw_ostream &OS);
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
namespace {
|
|
|
|
/// \brief The implementation of the coverage tool.
|
|
|
|
class CodeCoverageTool {
|
|
|
|
public:
|
|
|
|
enum Command {
|
|
|
|
/// \brief The show command.
|
|
|
|
Show,
|
|
|
|
/// \brief The report command.
|
2016-07-27 00:50:58 +02:00
|
|
|
Report,
|
|
|
|
/// \brief The export command.
|
|
|
|
Export
|
2014-08-23 00:56:03 +02:00
|
|
|
};
|
|
|
|
|
2016-09-22 23:49:47 +02:00
|
|
|
int run(Command Cmd, int argc, const char **argv);
|
|
|
|
|
|
|
|
private:
|
2014-08-23 00:56:03 +02:00
|
|
|
/// \brief Print the error message to the error output stream.
|
|
|
|
void error(const Twine &Message, StringRef Whence = "");
|
|
|
|
|
2016-07-18 19:53:12 +02:00
|
|
|
/// \brief Print the warning message to the error output stream.
|
|
|
|
void warning(const Twine &Message, StringRef Whence = "");
|
2016-07-13 23:38:36 +02:00
|
|
|
|
2016-09-23 20:57:32 +02:00
|
|
|
/// \brief Convert \p Path into an absolute path and append it to the list
|
|
|
|
/// of collected paths.
|
2016-06-28 18:12:18 +02:00
|
|
|
void addCollectedPath(const std::string &Path);
|
|
|
|
|
2016-09-22 23:49:43 +02:00
|
|
|
/// \brief If \p Path is a regular file, collect the path. If it's a
|
|
|
|
/// directory, recursively collect all of the paths within the directory.
|
|
|
|
void collectPaths(const std::string &Path);
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
/// \brief Return a memory buffer for the given source file.
|
|
|
|
ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
|
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
/// \brief Create source views for the expansions of the view.
|
|
|
|
void attachExpansionSubViews(SourceCoverageView &View,
|
|
|
|
ArrayRef<ExpansionRecord> Expansions,
|
2016-07-15 03:19:33 +02:00
|
|
|
const CoverageMapping &Coverage);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
/// \brief Create the source view of a particular function.
|
2014-09-19 21:07:17 +02:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2016-07-15 03:19:33 +02:00
|
|
|
createFunctionView(const FunctionRecord &Function,
|
|
|
|
const CoverageMapping &Coverage);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
/// \brief Create the main source view of a particular source file.
|
2014-09-19 21:07:17 +02:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2016-07-15 03:19:33 +02:00
|
|
|
createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2016-11-20 14:31:13 +01:00
|
|
|
/// \brief Load the coverage mapping data. Return nullptr if an error occurred.
|
2014-09-20 17:31:56 +02:00
|
|
|
std::unique_ptr<CoverageMapping> load();
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2016-09-23 22:13:41 +02:00
|
|
|
/// \brief Remove input source files which aren't mapped by \p Coverage.
|
|
|
|
void removeUnmappedInputs(const CoverageMapping &Coverage);
|
|
|
|
|
2016-07-16 00:44:57 +02:00
|
|
|
/// \brief If a demangler is available, demangle all symbol names.
|
|
|
|
void demangleSymbols(const CoverageMapping &Coverage);
|
|
|
|
|
2016-10-19 19:55:44 +02:00
|
|
|
/// \brief Write out a source file view to the filesystem.
|
|
|
|
void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
|
|
|
|
CoveragePrinter *Printer, bool ShowFilenames);
|
|
|
|
|
2016-06-08 21:09:22 +02:00
|
|
|
typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
int show(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
|
|
|
int report(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
2016-07-27 00:50:58 +02:00
|
|
|
int export_(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
2016-10-25 19:40:55 +02:00
|
|
|
std::vector<StringRef> ObjectFilenames;
|
2014-08-23 00:56:03 +02:00
|
|
|
CoverageViewOptions ViewOpts;
|
|
|
|
CoverageFiltersMatchAll Filters;
|
2016-09-22 23:49:47 +02:00
|
|
|
|
|
|
|
/// The path to the indexed profile.
|
|
|
|
std::string PGOFilename;
|
|
|
|
|
|
|
|
/// A list of input source files.
|
2016-09-23 20:57:32 +02:00
|
|
|
std::vector<std::string> SourceFiles;
|
2016-09-22 23:49:47 +02:00
|
|
|
|
|
|
|
/// Whether or not we're in -filename-equivalence mode.
|
2014-08-23 00:56:03 +02:00
|
|
|
bool CompareFilenamesOnly;
|
2016-09-22 23:49:47 +02:00
|
|
|
|
|
|
|
/// In -filename-equivalence mode, this maps absolute paths from the
|
|
|
|
/// coverage mapping data to input source files.
|
2014-09-19 10:13:12 +02:00
|
|
|
StringMap<std::string> RemappedFilenames;
|
2016-09-22 23:49:47 +02:00
|
|
|
|
|
|
|
/// The architecture the coverage mapping data targets.
|
2015-06-22 23:33:24 +02:00
|
|
|
std::string CoverageArch;
|
2016-06-28 18:12:18 +02:00
|
|
|
|
2017-02-05 21:10:58 +01:00
|
|
|
/// A cache for demangled symbols.
|
|
|
|
DemangleCache DC;
|
2016-07-16 00:44:57 +02:00
|
|
|
|
2017-02-05 21:10:55 +01:00
|
|
|
/// A lock which guards printing to stderr.
|
2016-07-18 19:53:12 +02:00
|
|
|
std::mutex ErrsLock;
|
2016-07-13 23:38:36 +02:00
|
|
|
|
2016-07-16 00:44:54 +02:00
|
|
|
/// A container for input source file buffers.
|
2016-07-13 23:38:36 +02:00
|
|
|
std::mutex LoadedSourceFilesLock;
|
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
|
|
|
|
LoadedSourceFiles;
|
2014-08-23 00:56:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:38:36 +02:00
|
|
|
static std::string getErrorString(const Twine &Message, StringRef Whence,
|
|
|
|
bool Warning) {
|
|
|
|
std::string Str = (Warning ? "warning" : "error");
|
|
|
|
Str += ": ";
|
2014-08-23 00:56:03 +02:00
|
|
|
if (!Whence.empty())
|
2016-07-15 03:53:39 +02:00
|
|
|
Str += Whence.str() + ": ";
|
2016-07-13 23:38:36 +02:00
|
|
|
Str += Message.str() + "\n";
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
|
2016-07-18 19:53:12 +02:00
|
|
|
std::unique_lock<std::mutex> Guard{ErrsLock};
|
|
|
|
ViewOpts.colored_ostream(errs(), raw_ostream::RED)
|
|
|
|
<< getErrorString(Message, Whence, false);
|
2016-07-13 23:38:36 +02:00
|
|
|
}
|
|
|
|
|
2016-07-18 19:53:12 +02:00
|
|
|
void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
|
|
|
|
std::unique_lock<std::mutex> Guard{ErrsLock};
|
|
|
|
ViewOpts.colored_ostream(errs(), raw_ostream::RED)
|
|
|
|
<< getErrorString(Message, Whence, true);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:18 +02:00
|
|
|
void CodeCoverageTool::addCollectedPath(const std::string &Path) {
|
2016-09-22 23:49:43 +02:00
|
|
|
if (CompareFilenamesOnly) {
|
2016-09-23 20:57:32 +02:00
|
|
|
SourceFiles.emplace_back(Path);
|
2016-09-22 23:49:43 +02:00
|
|
|
} else {
|
|
|
|
SmallString<128> EffectivePath(Path);
|
|
|
|
if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
|
|
|
|
error(EC.message(), Path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
|
2016-09-23 20:57:32 +02:00
|
|
|
SourceFiles.emplace_back(EffectivePath.str());
|
2016-09-22 23:49:43 +02:00
|
|
|
}
|
2016-06-28 18:12:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 23:49:43 +02:00
|
|
|
void CodeCoverageTool::collectPaths(const std::string &Path) {
|
|
|
|
llvm::sys::fs::file_status Status;
|
|
|
|
llvm::sys::fs::status(Path, Status);
|
|
|
|
if (!llvm::sys::fs::exists(Status)) {
|
|
|
|
if (CompareFilenamesOnly)
|
|
|
|
addCollectedPath(Path);
|
|
|
|
else
|
|
|
|
error("Missing source file", Path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (llvm::sys::fs::is_regular_file(Status)) {
|
|
|
|
addCollectedPath(Path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (llvm::sys::fs::is_directory(Status)) {
|
|
|
|
std::error_code EC;
|
|
|
|
for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
|
|
|
|
F != E && !EC; F.increment(EC)) {
|
|
|
|
if (llvm::sys::fs::is_regular_file(F->path()))
|
|
|
|
addCollectedPath(F->path());
|
|
|
|
}
|
|
|
|
if (EC)
|
|
|
|
warning(EC.message(), Path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
ErrorOr<const MemoryBuffer &>
|
|
|
|
CodeCoverageTool::getSourceFile(StringRef SourceFile) {
|
2014-09-19 10:13:12 +02:00
|
|
|
// If we've remapped filenames, look up the real location for this file.
|
2016-07-15 03:19:36 +02:00
|
|
|
std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
|
2014-09-19 10:13:12 +02:00
|
|
|
if (!RemappedFilenames.empty()) {
|
|
|
|
auto Loc = RemappedFilenames.find(SourceFile);
|
|
|
|
if (Loc != RemappedFilenames.end())
|
|
|
|
SourceFile = Loc->second;
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
2014-09-19 10:13:12 +02:00
|
|
|
for (const auto &Files : LoadedSourceFiles)
|
|
|
|
if (sys::fs::equivalent(SourceFile, Files.first))
|
|
|
|
return *Files.second;
|
2014-08-23 00:56:03 +02:00
|
|
|
auto Buffer = MemoryBuffer::getFile(SourceFile);
|
|
|
|
if (auto EC = Buffer.getError()) {
|
2016-07-18 19:53:12 +02:00
|
|
|
error(EC.message(), SourceFile);
|
2014-08-23 00:56:03 +02:00
|
|
|
return EC;
|
|
|
|
}
|
2015-05-29 21:43:39 +02:00
|
|
|
LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
|
2014-08-23 00:56:03 +02:00
|
|
|
return *LoadedSourceFiles.back().second;
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:19:33 +02:00
|
|
|
void CodeCoverageTool::attachExpansionSubViews(
|
|
|
|
SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
|
|
|
|
const CoverageMapping &Coverage) {
|
2014-08-23 00:56:03 +02:00
|
|
|
if (!ViewOpts.ShowExpandedRegions)
|
|
|
|
return;
|
2014-09-20 17:31:56 +02:00
|
|
|
for (const auto &Expansion : Expansions) {
|
|
|
|
auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
|
|
|
|
if (ExpansionCoverage.empty())
|
2014-08-23 00:56:03 +02:00
|
|
|
continue;
|
2014-09-20 17:31:56 +02:00
|
|
|
auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
|
|
|
|
if (!SourceBuffer)
|
2014-08-23 00:56:03 +02:00
|
|
|
continue;
|
2014-09-20 17:31:56 +02:00
|
|
|
|
|
|
|
auto SubViewExpansions = ExpansionCoverage.getExpansions();
|
2016-06-25 04:58:30 +02:00
|
|
|
auto SubView =
|
|
|
|
SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
|
|
|
|
ViewOpts, std::move(ExpansionCoverage));
|
2014-09-20 17:31:56 +02:00
|
|
|
attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
|
|
|
|
View.addExpansion(Expansion.Region, std::move(SubView));
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:07:17 +02:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2014-09-20 17:31:56 +02:00
|
|
|
CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
|
2016-07-15 03:19:33 +02:00
|
|
|
const CoverageMapping &Coverage) {
|
2014-09-20 17:31:56 +02:00
|
|
|
auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
|
|
|
|
if (FunctionCoverage.empty())
|
2014-09-19 21:07:17 +02:00
|
|
|
return nullptr;
|
2014-09-20 17:31:56 +02:00
|
|
|
auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
|
2014-09-19 21:07:17 +02:00
|
|
|
if (!SourceBuffer)
|
|
|
|
return nullptr;
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
auto Expansions = FunctionCoverage.getExpansions();
|
2017-02-05 21:10:58 +01:00
|
|
|
auto View = SourceCoverageView::create(DC.demangle(Function.Name),
|
2016-09-08 02:56:48 +02:00
|
|
|
SourceBuffer.get(), ViewOpts,
|
|
|
|
std::move(FunctionCoverage));
|
2014-09-20 17:31:56 +02:00
|
|
|
attachExpansionSubViews(*View, Expansions, Coverage);
|
|
|
|
|
|
|
|
return View;
|
|
|
|
}
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
|
|
|
CodeCoverageTool::createSourceFileView(StringRef SourceFile,
|
2016-07-15 03:19:33 +02:00
|
|
|
const CoverageMapping &Coverage) {
|
2014-09-19 21:07:17 +02:00
|
|
|
auto SourceBuffer = getSourceFile(SourceFile);
|
|
|
|
if (!SourceBuffer)
|
|
|
|
return nullptr;
|
2014-09-20 17:31:56 +02:00
|
|
|
auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
|
|
|
|
if (FileCoverage.empty())
|
2014-09-19 21:07:17 +02:00
|
|
|
return nullptr;
|
2014-09-20 17:31:56 +02:00
|
|
|
|
|
|
|
auto Expansions = FileCoverage.getExpansions();
|
2016-06-25 04:58:30 +02:00
|
|
|
auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
|
|
|
|
ViewOpts, std::move(FileCoverage));
|
2014-09-20 17:31:56 +02:00
|
|
|
attachExpansionSubViews(*View, Expansions, Coverage);
|
|
|
|
|
2016-07-15 03:19:33 +02:00
|
|
|
for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
|
2016-09-15 08:44:51 +02:00
|
|
|
std::unique_ptr<SourceCoverageView> SubView{nullptr};
|
|
|
|
|
2017-02-05 21:10:58 +01:00
|
|
|
StringRef Funcname = DC.demangle(Function->Name);
|
2016-09-20 23:27:48 +02:00
|
|
|
|
2016-09-15 08:44:51 +02:00
|
|
|
if (Function->ExecutionCount > 0) {
|
|
|
|
auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
|
|
|
|
auto SubViewExpansions = SubViewCoverage.getExpansions();
|
|
|
|
SubView = SourceCoverageView::create(
|
2016-09-20 23:27:48 +02:00
|
|
|
Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
|
2016-09-15 08:44:51 +02:00
|
|
|
attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
2016-09-15 08:44:51 +02: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);
|
2016-09-20 23:27:48 +02:00
|
|
|
View->addInstantiation(Funcname, Line, std::move(SubView));
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
2014-09-19 21:07:17 +02:00
|
|
|
return View;
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 06:09:38 +02: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 17:31:56 +02:00
|
|
|
std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
|
2016-10-25 19:40:55 +02:00
|
|
|
for (StringRef ObjectFilename : ObjectFilenames)
|
|
|
|
if (modifiedTimeGT(ObjectFilename, PGOFilename))
|
|
|
|
warning("profile data may be out of date - object is newer",
|
|
|
|
ObjectFilename);
|
2016-07-18 19:53:12 +02:00
|
|
|
auto CoverageOrErr =
|
2016-10-25 19:40:55 +02:00
|
|
|
CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArch);
|
2016-05-19 05:54:45 +02:00
|
|
|
if (Error E = CoverageOrErr.takeError()) {
|
2016-10-25 19:40:55 +02:00
|
|
|
error("Failed to load coverage: " + toString(std::move(E)),
|
|
|
|
join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
|
2014-09-20 17:31:56 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto Coverage = std::move(CoverageOrErr.get());
|
|
|
|
unsigned Mismatched = Coverage->getMismatchedCount();
|
2016-07-18 19:53:12 +02:00
|
|
|
if (Mismatched)
|
|
|
|
warning(utostr(Mismatched) + " functions have mismatched data");
|
2014-09-19 10:13:12 +02:00
|
|
|
|
2016-09-23 22:13:41 +02:00
|
|
|
if (!SourceFiles.empty())
|
|
|
|
removeUnmappedInputs(*Coverage);
|
|
|
|
|
|
|
|
demangleSymbols(*Coverage);
|
|
|
|
|
|
|
|
return Coverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
|
|
|
|
std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
|
2016-09-23 20:57:35 +02:00
|
|
|
|
|
|
|
auto UncoveredFilesIt = SourceFiles.end();
|
|
|
|
if (!CompareFilenamesOnly) {
|
|
|
|
// The user may have specified source files which aren't in the coverage
|
|
|
|
// mapping. Filter these files away.
|
|
|
|
UncoveredFilesIt = std::remove_if(
|
|
|
|
SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
|
|
|
|
return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
|
|
|
|
SF);
|
|
|
|
});
|
|
|
|
} else {
|
2014-09-19 10:13:12 +02:00
|
|
|
for (auto &SF : SourceFiles) {
|
|
|
|
StringRef SFBase = sys::path::filename(SF);
|
2016-09-23 20:57:35 +02:00
|
|
|
for (const auto &CF : CoveredFiles) {
|
2014-09-19 10:13:12 +02:00
|
|
|
if (SFBase == sys::path::filename(CF)) {
|
|
|
|
RemappedFilenames[CF] = SF;
|
|
|
|
SF = CF;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-23 20:57:35 +02:00
|
|
|
}
|
2014-09-19 10:13:12 +02:00
|
|
|
}
|
2016-09-23 20:57:35 +02:00
|
|
|
UncoveredFilesIt = std::remove_if(
|
|
|
|
SourceFiles.begin(), SourceFiles.end(),
|
|
|
|
[&](const std::string &SF) { return !RemappedFilenames.count(SF); });
|
2014-09-19 10:13:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 20:57:35 +02:00
|
|
|
SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 00:44:57 +02:00
|
|
|
void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
|
|
|
|
if (!ViewOpts.hasDemangler())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Pass function names to the demangler in a temporary file.
|
|
|
|
int InputFD;
|
|
|
|
SmallString<256> InputPath;
|
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
|
|
|
|
if (EC) {
|
|
|
|
error(InputPath, EC.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tool_output_file InputTOF{InputPath, InputFD};
|
|
|
|
|
|
|
|
unsigned NumSymbols = 0;
|
|
|
|
for (const auto &Function : Coverage.getCoveredFunctions()) {
|
|
|
|
InputTOF.os() << Function.Name << '\n';
|
|
|
|
++NumSymbols;
|
|
|
|
}
|
2016-07-16 01:08:22 +02:00
|
|
|
InputTOF.os().close();
|
2016-07-16 00:44:57 +02:00
|
|
|
|
|
|
|
// Use another temporary file to store the demangler's output.
|
|
|
|
int OutputFD;
|
|
|
|
SmallString<256> OutputPath;
|
|
|
|
EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
|
|
|
|
OutputPath);
|
|
|
|
if (EC) {
|
|
|
|
error(OutputPath, EC.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tool_output_file OutputTOF{OutputPath, OutputFD};
|
2016-07-16 01:08:22 +02:00
|
|
|
OutputTOF.os().close();
|
2016-07-16 00:44:57 +02:00
|
|
|
|
|
|
|
// Invoke the demangler.
|
|
|
|
std::vector<const char *> ArgsV;
|
|
|
|
for (const std::string &Arg : ViewOpts.DemanglerOpts)
|
|
|
|
ArgsV.push_back(Arg.c_str());
|
|
|
|
ArgsV.push_back(nullptr);
|
2016-07-16 01:15:35 +02:00
|
|
|
StringRef InputPathRef = InputPath.str();
|
|
|
|
StringRef OutputPathRef = OutputPath.str();
|
|
|
|
StringRef StderrRef;
|
2016-07-16 00:44:57 +02:00
|
|
|
const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
|
|
|
|
std::string ErrMsg;
|
|
|
|
int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
|
|
|
|
/*env=*/nullptr, Redirects, /*secondsToWait=*/0,
|
|
|
|
/*memoryLimit=*/0, &ErrMsg);
|
|
|
|
if (RC) {
|
|
|
|
error(ErrMsg, ViewOpts.DemanglerOpts[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the demangler's output.
|
|
|
|
auto BufOrError = MemoryBuffer::getFile(OutputPath);
|
|
|
|
if (!BufOrError) {
|
|
|
|
error(OutputPath, BufOrError.getError().message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
|
|
|
|
|
|
|
|
SmallVector<StringRef, 8> Symbols;
|
|
|
|
StringRef DemanglerData = DemanglerBuf->getBuffer();
|
|
|
|
DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
|
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
if (Symbols.size() != NumSymbols) {
|
|
|
|
error("Demangler did not provide expected number of symbols");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the demangled names.
|
|
|
|
unsigned I = 0;
|
|
|
|
for (const auto &Function : Coverage.getCoveredFunctions())
|
2017-02-19 15:26:52 +01:00
|
|
|
// On Windows, lines in the demangler's output file end with "\r\n".
|
|
|
|
// Splitting by '\n' keeps '\r's, so cut them now.
|
|
|
|
DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
|
2016-07-16 00:44:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 19:55:44 +02:00
|
|
|
void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
|
|
|
|
CoverageMapping *Coverage,
|
|
|
|
CoveragePrinter *Printer,
|
|
|
|
bool ShowFilenames) {
|
|
|
|
auto View = createSourceFileView(SourceFile, *Coverage);
|
|
|
|
if (!View) {
|
|
|
|
warning("The file '" + SourceFile + "' isn't covered.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
|
|
|
|
if (Error E = OSOrErr.takeError()) {
|
|
|
|
error("Could not create view file!", toString(std::move(E)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto OS = std::move(OSOrErr.get());
|
|
|
|
|
|
|
|
View->print(*OS.get(), /*Wholefile=*/true,
|
|
|
|
/*ShowSourceName=*/ShowFilenames);
|
|
|
|
Printer->closeViewFile(std::move(OS));
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
|
2016-10-25 19:40:55 +02:00
|
|
|
cl::opt<std::string> CovFilename(
|
|
|
|
cl::Positional, cl::desc("Covered executable or object file."));
|
|
|
|
|
|
|
|
cl::list<std::string> CovFilenames(
|
|
|
|
"object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
|
|
|
|
cl::CommaSeparated);
|
2014-10-30 21:51:24 +01:00
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
cl::list<std::string> InputSourceFiles(
|
|
|
|
cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
|
|
|
|
|
2016-09-22 23:49:43 +02:00
|
|
|
cl::opt<bool> DebugDumpCollectedPaths(
|
|
|
|
"dump-collected-paths", cl::Optional, cl::Hidden,
|
|
|
|
cl::desc("Show the collected paths to source files"));
|
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
cl::opt<std::string, true> PGOFilename(
|
|
|
|
"instr-profile", cl::Required, cl::location(this->PGOFilename),
|
2014-08-23 00:56:03 +02:00
|
|
|
cl::desc(
|
|
|
|
"File with the profile data obtained after an instrumented run"));
|
|
|
|
|
2015-03-11 03:30:51 +01:00
|
|
|
cl::opt<std::string> Arch(
|
|
|
|
"arch", cl::desc("architecture of the coverage mapping binary"));
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
cl::opt<bool> DebugDump("dump", cl::Optional,
|
|
|
|
cl::desc("Show internal debug dump"));
|
|
|
|
|
2016-06-29 02:38:21 +02:00
|
|
|
cl::opt<CoverageViewOptions::OutputFormat> Format(
|
|
|
|
"format", cl::desc("Output format for line-based coverage reports"),
|
|
|
|
cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
|
|
|
|
"Text output"),
|
2016-07-06 23:44:05 +02:00
|
|
|
clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
|
2016-10-08 21:41:06 +02:00
|
|
|
"HTML output")),
|
2016-06-29 02:38:21 +02:00
|
|
|
cl::init(CoverageViewOptions::OutputFormat::Text));
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
cl::opt<bool> FilenameEquivalence(
|
|
|
|
"filename-equivalence", cl::Optional,
|
2014-09-19 10:13:12 +02: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 00:56:03 +02: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 05:45:16 +01:00
|
|
|
cl::opt<cl::boolOrDefault> UseColor(
|
|
|
|
"use-color", cl::desc("Emit colored output (default=autodetect)"),
|
|
|
|
cl::init(cl::BOU_UNSET));
|
2015-03-19 01:02:23 +01:00
|
|
|
|
2016-07-16 00:44:57 +02:00
|
|
|
cl::list<std::string> DemanglerOpts(
|
|
|
|
"Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
auto commandLineParser = [&, this](int argc, const char **argv) -> int {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
|
|
|
|
ViewOpts.Debug = DebugDump;
|
|
|
|
CompareFilenamesOnly = FilenameEquivalence;
|
|
|
|
|
2016-10-25 19:40:55 +02:00
|
|
|
if (!CovFilename.empty())
|
|
|
|
ObjectFilenames.emplace_back(CovFilename);
|
|
|
|
for (const std::string &Filename : CovFilenames)
|
|
|
|
ObjectFilenames.emplace_back(Filename);
|
|
|
|
if (ObjectFilenames.empty()) {
|
2016-10-25 21:52:57 +02:00
|
|
|
errs() << "No filenames specified!\n";
|
2016-10-25 19:40:55 +02:00
|
|
|
::exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-29 02:38:21 +02:00
|
|
|
ViewOpts.Format = Format;
|
|
|
|
switch (ViewOpts.Format) {
|
|
|
|
case CoverageViewOptions::OutputFormat::Text:
|
|
|
|
ViewOpts.Colors = UseColor == cl::BOU_UNSET
|
|
|
|
? sys::Process::StandardOutHasColors()
|
|
|
|
: UseColor == cl::BOU_TRUE;
|
|
|
|
break;
|
2016-07-06 23:44:05 +02:00
|
|
|
case CoverageViewOptions::OutputFormat::HTML:
|
|
|
|
if (UseColor == cl::BOU_FALSE)
|
2016-10-25 21:52:57 +02:00
|
|
|
errs() << "Color output cannot be disabled when generating html.\n";
|
2016-07-06 23:44:05 +02:00
|
|
|
ViewOpts.Colors = true;
|
|
|
|
break;
|
2016-06-29 02:38:21 +02:00
|
|
|
}
|
2015-03-19 01:02:23 +01:00
|
|
|
|
2016-07-16 00:44:57 +02:00
|
|
|
// If a demangler is supplied, check if it exists and register it.
|
|
|
|
if (DemanglerOpts.size()) {
|
|
|
|
auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
|
|
|
|
if (!DemanglerPathOrErr) {
|
|
|
|
error("Could not find the demangler!",
|
|
|
|
DemanglerPathOrErr.getError().message());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
DemanglerOpts[0] = *DemanglerPathOrErr;
|
|
|
|
ViewOpts.DemanglerOpts.swap(DemanglerOpts);
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:56:03 +02: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-22 23:33:24 +02:00
|
|
|
if (!Arch.empty() &&
|
|
|
|
Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
|
2016-07-18 19:53:12 +02:00
|
|
|
error("Unknown architecture: " + Arch);
|
2015-06-22 23:33:24 +02:00
|
|
|
return 1;
|
2015-03-11 03:30:51 +01:00
|
|
|
}
|
2015-06-22 23:33:24 +02:00
|
|
|
CoverageArch = Arch;
|
2015-03-11 03:30:51 +01:00
|
|
|
|
2016-09-22 23:49:43 +02:00
|
|
|
for (const std::string &File : InputSourceFiles)
|
|
|
|
collectPaths(File);
|
|
|
|
|
|
|
|
if (DebugDumpCollectedPaths) {
|
2016-09-23 20:57:32 +02:00
|
|
|
for (const std::string &SF : SourceFiles)
|
2016-09-22 23:49:43 +02:00
|
|
|
outs() << SF << '\n';
|
|
|
|
::exit(0);
|
2014-09-19 10:13:12 +02:00
|
|
|
}
|
2016-09-22 23:49:43 +02:00
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (Cmd) {
|
|
|
|
case Show:
|
|
|
|
return show(argc, argv, commandLineParser);
|
|
|
|
case Report:
|
|
|
|
return report(argc, argv, commandLineParser);
|
2016-07-27 00:50:58 +02:00
|
|
|
case Export:
|
|
|
|
return export_(argc, argv, commandLineParser);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
|
2016-06-28 04:09:39 +02:00
|
|
|
cl::opt<std::string> ShowOutputDirectory(
|
|
|
|
"output-dir", cl::init(""),
|
|
|
|
cl::desc("Directory in which coverage information is written out"));
|
|
|
|
cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
|
|
|
|
cl::aliasopt(ShowOutputDirectory));
|
|
|
|
|
2016-08-04 12:39:43 +02:00
|
|
|
cl::opt<uint32_t> TabSize(
|
2016-08-04 20:00:42 +02:00
|
|
|
"tab-size", cl::init(2),
|
|
|
|
cl::desc(
|
|
|
|
"Set tab expansion size for html coverage reports (default = 2)"));
|
2016-08-04 12:39:43 +02:00
|
|
|
|
2016-08-24 16:27:23 +02:00
|
|
|
cl::opt<std::string> ProjectTitle(
|
|
|
|
"project-title", cl::Optional,
|
|
|
|
cl::desc("Set project title for the coverage report"));
|
|
|
|
|
2017-07-11 03:23:29 +02:00
|
|
|
cl::opt<unsigned> NumThreads(
|
|
|
|
"num-threads", cl::init(0),
|
|
|
|
cl::desc("Number of merge threads to use (default: autodetect)"));
|
|
|
|
cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
|
|
|
|
cl::aliasopt(NumThreads));
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
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;
|
2016-06-28 04:09:39 +02:00
|
|
|
ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
|
2016-08-04 12:39:43 +02:00
|
|
|
ViewOpts.TabSize = TabSize;
|
2016-08-24 16:27:23 +02:00
|
|
|
ViewOpts.ProjectTitle = ProjectTitle;
|
2016-06-28 04:09:39 +02:00
|
|
|
|
2016-06-28 18:12:20 +02:00
|
|
|
if (ViewOpts.hasOutputDirectory()) {
|
2016-06-28 04:09:39 +02:00
|
|
|
if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
|
|
|
|
error("Could not create output directory!", E.message());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2016-08-24 16:27:23 +02:00
|
|
|
sys::fs::file_status Status;
|
|
|
|
if (sys::fs::status(PGOFilename, Status)) {
|
|
|
|
error("profdata file error: can not get the file status. \n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ModifiedTime = Status.getLastModificationTime();
|
2016-10-24 12:59:17 +02:00
|
|
|
std::string ModifiedTimeStr = to_string(ModifiedTime);
|
2016-11-30 11:01:11 +01:00
|
|
|
size_t found = ModifiedTimeStr.rfind(':');
|
2016-08-24 16:27:23 +02:00
|
|
|
ViewOpts.CreatedTimeStr = (found != std::string::npos)
|
|
|
|
? "Created: " + ModifiedTimeStr.substr(0, found)
|
|
|
|
: "Created: " + ModifiedTimeStr;
|
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 00:56:03 +02:00
|
|
|
return 1;
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
auto Printer = CoveragePrinter::create(ViewOpts);
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
if (!Filters.empty()) {
|
2016-06-29 02:38:21 +02:00
|
|
|
auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
|
|
|
|
if (Error E = OSOrErr.takeError()) {
|
2016-07-15 03:53:39 +02:00
|
|
|
error("Could not create view file!", toString(std::move(E)));
|
2016-06-29 02:38:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto OS = std::move(OSOrErr.get());
|
|
|
|
|
|
|
|
// Show functions.
|
2014-09-20 17:31:56 +02:00
|
|
|
for (const auto &Function : Coverage->getCoveredFunctions()) {
|
|
|
|
if (!Filters.matches(Function))
|
2014-08-23 00:56:03 +02:00
|
|
|
continue;
|
2014-09-20 17:31:56 +02:00
|
|
|
|
|
|
|
auto mainView = createFunctionView(Function, *Coverage);
|
2014-09-19 21:07:17 +02:00
|
|
|
if (!mainView) {
|
2016-07-18 19:53:12 +02:00
|
|
|
warning("Could not read coverage for '" + Function.Name + "'.");
|
2014-09-19 21:07:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-28 04:09:39 +02:00
|
|
|
|
|
|
|
mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
2016-06-29 02:38:21 +02:00
|
|
|
|
|
|
|
Printer->closeViewFile(std::move(OS));
|
2014-08-23 00:56:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show files
|
2016-08-24 16:27:23 +02:00
|
|
|
bool ShowFilenames =
|
2016-09-06 23:41:38 +02:00
|
|
|
(SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
|
2016-08-24 16:27:23 +02:00
|
|
|
(ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2014-09-19 10:13:12 +02:00
|
|
|
if (SourceFiles.empty())
|
2016-06-28 18:12:20 +02:00
|
|
|
// Get the source files from the function coverage mapping.
|
2014-09-20 17:31:56 +02:00
|
|
|
for (StringRef Filename : Coverage->getUniqueSourceFiles())
|
2014-08-23 00:56:03 +02:00
|
|
|
SourceFiles.push_back(Filename);
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
// Create an index out of the source files.
|
|
|
|
if (ViewOpts.hasOutputDirectory()) {
|
2016-09-09 03:32:55 +02:00
|
|
|
if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
|
2016-07-15 03:53:39 +02:00
|
|
|
error("Could not create index file!", toString(std::move(E)));
|
2016-06-28 18:12:24 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 03:23:29 +02:00
|
|
|
// If NumThreads is not specified, auto-detect a good default.
|
|
|
|
if (NumThreads == 0)
|
|
|
|
NumThreads =
|
|
|
|
std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
|
|
|
|
unsigned(SourceFiles.size())));
|
|
|
|
|
|
|
|
if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
|
2016-10-19 19:55:44 +02:00
|
|
|
for (const std::string &SourceFile : SourceFiles)
|
|
|
|
writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
|
|
|
|
ShowFilenames);
|
|
|
|
} else {
|
|
|
|
// In -output-dir mode, it's safe to use multiple threads to print files.
|
2017-07-11 03:23:29 +02:00
|
|
|
ThreadPool Pool(NumThreads);
|
2016-10-19 19:55:44 +02:00
|
|
|
for (const std::string &SourceFile : SourceFiles)
|
|
|
|
Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
|
|
|
|
Coverage.get(), Printer.get(), ShowFilenames);
|
|
|
|
Pool.wait();
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CodeCoverageTool::report(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
2017-02-05 21:11:08 +01:00
|
|
|
cl::opt<bool> ShowFunctionSummaries(
|
|
|
|
"show-functions", cl::Optional, cl::init(false),
|
|
|
|
cl::desc("Show coverage summaries for each function"));
|
|
|
|
|
2014-08-23 00:56:03 +02:00
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
2017-02-28 17:57:28 +01:00
|
|
|
if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
|
2016-07-06 23:44:05 +02:00
|
|
|
error("HTML output for summary reports is not yet supported.");
|
2017-02-28 17:57:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-07-06 23:44:05 +02:00
|
|
|
|
2014-09-20 17:31:56 +02:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 00:56:03 +02:00
|
|
|
return 1;
|
|
|
|
|
2016-09-07 00:45:57 +02:00
|
|
|
CoverageReport Report(ViewOpts, *Coverage.get());
|
2017-02-05 21:11:08 +01:00
|
|
|
if (!ShowFunctionSummaries)
|
2014-08-23 00:56:03 +02:00
|
|
|
Report.renderFileReports(llvm::outs());
|
2015-02-14 03:05:05 +01:00
|
|
|
else
|
2017-02-05 21:11:03 +01:00
|
|
|
Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
|
2014-08-23 00:56:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:50:58 +02:00
|
|
|
int CodeCoverageTool::export_(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
|
|
|
|
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
2017-02-28 17:57:28 +01:00
|
|
|
if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
|
|
|
|
error("Coverage data can only be exported as textual JSON.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:50:58 +02:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage) {
|
|
|
|
error("Could not load coverage information");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-25 02:08:33 +02:00
|
|
|
exportCoverageDataToJson(*Coverage.get(), outs());
|
2016-07-27 00:50:58 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-30 21:57:49 +01:00
|
|
|
int showMain(int argc, const char *argv[]) {
|
2014-08-23 00:56:03 +02:00
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Show, argc, argv);
|
|
|
|
}
|
|
|
|
|
2014-10-30 21:57:49 +01:00
|
|
|
int reportMain(int argc, const char *argv[]) {
|
2014-08-23 00:56:03 +02:00
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Report, argc, argv);
|
|
|
|
}
|
2016-07-27 00:50:58 +02:00
|
|
|
|
|
|
|
int exportMain(int argc, const char *argv[]) {
|
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Export, argc, argv);
|
|
|
|
}
|