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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
#include "CoverageExporterJson.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "CoverageFilters.h"
|
|
|
|
#include "CoverageReport.h"
|
2017-02-06 04:10:58 +08:00
|
|
|
#include "CoverageSummaryInfo.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"
|
2016-07-16 06:44:57 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-03-19 08:02:23 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2016-07-16 06:44:57 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2016-10-24 18:59:17 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2016-07-14 05:38:36 +08:00
|
|
|
#include "llvm/Support/ThreadPool.h"
|
2018-01-06 00:15:07 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2016-07-16 06:44:57 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2017-09-28 18:07:30 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
#include <functional>
|
2017-09-28 18:07:30 +08:00
|
|
|
#include <map>
|
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;
|
|
|
|
|
2016-10-25 08:08:33 +08:00
|
|
|
void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
|
2017-09-09 02:44:49 +08:00
|
|
|
const CoverageViewOptions &Options,
|
2016-07-27 06:50:58 +08:00
|
|
|
raw_ostream &OS);
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
namespace {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The implementation of the coverage tool.
|
2014-08-23 06:56:03 +08:00
|
|
|
class CodeCoverageTool {
|
|
|
|
public:
|
|
|
|
enum Command {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The show command.
|
2014-08-23 06:56:03 +08:00
|
|
|
Show,
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The report command.
|
2016-07-27 06:50:58 +08:00
|
|
|
Report,
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The export command.
|
2016-07-27 06:50:58 +08:00
|
|
|
Export
|
2014-08-23 06:56:03 +08:00
|
|
|
};
|
|
|
|
|
2016-09-23 05:49:47 +08:00
|
|
|
int run(Command Cmd, int argc, const char **argv);
|
|
|
|
|
|
|
|
private:
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Print the error message to the error output stream.
|
2014-08-23 06:56:03 +08:00
|
|
|
void error(const Twine &Message, StringRef Whence = "");
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Print the warning message to the error output stream.
|
2016-07-19 01:53:12 +08:00
|
|
|
void warning(const Twine &Message, StringRef Whence = "");
|
2016-07-14 05:38:36 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Convert \p Path into an absolute path and append it to the list
|
2016-09-24 02:57:32 +08:00
|
|
|
/// of collected paths.
|
2016-06-29 00:12:18 +08:00
|
|
|
void addCollectedPath(const std::string &Path);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// If \p Path is a regular file, collect the path. If it's a
|
2016-09-23 05:49:43 +08:00
|
|
|
/// directory, recursively collect all of the paths within the directory.
|
|
|
|
void collectPaths(const std::string &Path);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Return a memory buffer for the given source file.
|
2014-08-23 06:56:03 +08:00
|
|
|
ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Create source views for the expansions of the view.
|
2014-09-20 23:31:56 +08:00
|
|
|
void attachExpansionSubViews(SourceCoverageView &View,
|
|
|
|
ArrayRef<ExpansionRecord> Expansions,
|
2016-07-15 09:19:33 +08:00
|
|
|
const CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Create the source view of a particular function.
|
2014-09-20 03:07:17 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2016-07-15 09:19:33 +08:00
|
|
|
createFunctionView(const FunctionRecord &Function,
|
|
|
|
const CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Create the main source view of a particular source file.
|
2014-09-20 03:07:17 +08:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
2016-07-15 09:19:33 +08:00
|
|
|
createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Load the coverage mapping data. Return nullptr if an error occurred.
|
2014-09-20 23:31:56 +08:00
|
|
|
std::unique_ptr<CoverageMapping> load();
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Create a mapping from files in the Coverage data to local copies
|
2017-08-14 18:20:12 +08:00
|
|
|
/// (path-equivalence).
|
|
|
|
void remapPathNames(const CoverageMapping &Coverage);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Remove input source files which aren't mapped by \p Coverage.
|
2016-09-24 04:13:41 +08:00
|
|
|
void removeUnmappedInputs(const CoverageMapping &Coverage);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// If a demangler is available, demangle all symbol names.
|
2016-07-16 06:44:57 +08:00
|
|
|
void demangleSymbols(const CoverageMapping &Coverage);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Write out a source file view to the filesystem.
|
2016-10-20 01:55:44 +08:00
|
|
|
void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
|
|
|
|
CoveragePrinter *Printer, bool ShowFilenames);
|
|
|
|
|
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
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
int doShow(int argc, const char **argv,
|
2014-08-23 06:56:03 +08:00
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
int doReport(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
|
|
|
|
|
|
|
int doExport(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser);
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2016-10-26 01:40:55 +08:00
|
|
|
std::vector<StringRef> ObjectFilenames;
|
2014-08-23 06:56:03 +08:00
|
|
|
CoverageViewOptions ViewOpts;
|
|
|
|
CoverageFiltersMatchAll Filters;
|
2018-04-09 23:20:35 +08:00
|
|
|
CoverageFilters IgnoreFilenameFilters;
|
2016-09-23 05:49:47 +08:00
|
|
|
|
|
|
|
/// The path to the indexed profile.
|
|
|
|
std::string PGOFilename;
|
|
|
|
|
|
|
|
/// A list of input source files.
|
2016-09-24 02:57:32 +08:00
|
|
|
std::vector<std::string> SourceFiles;
|
2016-09-23 05:49:47 +08:00
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
/// In -path-equivalence mode, this maps the absolute paths from the coverage
|
|
|
|
/// mapping data to the input source files.
|
2014-09-19 16:13:12 +08:00
|
|
|
StringMap<std::string> RemappedFilenames;
|
2016-09-23 05:49:47 +08:00
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
/// The coverage data path to be remapped from, and the source path to be
|
|
|
|
/// remapped to, when using -path-equivalence.
|
|
|
|
Optional<std::pair<std::string, std::string>> PathRemapping;
|
|
|
|
|
2016-09-23 05:49:47 +08:00
|
|
|
/// The architecture the coverage mapping data targets.
|
2017-08-02 05:23:26 +08:00
|
|
|
std::vector<StringRef> CoverageArches;
|
2016-06-29 00:12:18 +08:00
|
|
|
|
2017-02-06 04:10:58 +08:00
|
|
|
/// A cache for demangled symbols.
|
|
|
|
DemangleCache DC;
|
2016-07-16 06:44:57 +08:00
|
|
|
|
2017-02-06 04:10:55 +08:00
|
|
|
/// A lock which guards printing to stderr.
|
2016-07-19 01:53:12 +08:00
|
|
|
std::mutex ErrsLock;
|
2016-07-14 05:38:36 +08:00
|
|
|
|
2016-07-16 06:44:54 +08:00
|
|
|
/// A container for input source file buffers.
|
2016-07-14 05:38:36 +08:00
|
|
|
std::mutex LoadedSourceFilesLock;
|
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
|
|
|
|
LoadedSourceFiles;
|
2017-08-31 17:11:31 +08:00
|
|
|
|
|
|
|
/// Whitelist from -name-whitelist to be used for filtering.
|
|
|
|
std::unique_ptr<SpecialCaseList> NameWhitelist;
|
2014-08-23 06:56:03 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-14 05:38:36 +08:00
|
|
|
static std::string getErrorString(const Twine &Message, StringRef Whence,
|
|
|
|
bool Warning) {
|
|
|
|
std::string Str = (Warning ? "warning" : "error");
|
|
|
|
Str += ": ";
|
2014-08-23 06:56:03 +08:00
|
|
|
if (!Whence.empty())
|
2016-07-15 09:53:39 +08:00
|
|
|
Str += Whence.str() + ": ";
|
2016-07-14 05:38:36 +08:00
|
|
|
Str += Message.str() + "\n";
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
|
2016-07-19 01:53:12 +08:00
|
|
|
std::unique_lock<std::mutex> Guard{ErrsLock};
|
|
|
|
ViewOpts.colored_ostream(errs(), raw_ostream::RED)
|
|
|
|
<< getErrorString(Message, Whence, false);
|
2016-07-14 05:38:36 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 01:53:12 +08: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 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 00:12:18 +08:00
|
|
|
void CodeCoverageTool::addCollectedPath(const std::string &Path) {
|
2017-08-14 18:20:12 +08:00
|
|
|
SmallString<128> EffectivePath(Path);
|
|
|
|
if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
|
|
|
|
error(EC.message(), Path);
|
|
|
|
return;
|
2016-09-23 05:49:43 +08:00
|
|
|
}
|
2017-08-14 18:20:12 +08:00
|
|
|
sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
|
2018-04-09 23:20:35 +08:00
|
|
|
if (!IgnoreFilenameFilters.matchesFilename(EffectivePath))
|
|
|
|
SourceFiles.emplace_back(EffectivePath.str());
|
2016-06-29 00:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 05:49:43 +08: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)) {
|
2017-08-14 18:20:12 +08:00
|
|
|
if (PathRemapping)
|
2016-09-23 05:49:43 +08:00
|
|
|
addCollectedPath(Path);
|
|
|
|
else
|
2018-04-06 03:43:24 +08:00
|
|
|
warning("Source file doesn't exist, proceeded by ignoring it.", Path);
|
2016-09-23 05:49:43 +08:00
|
|
|
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;
|
2018-04-06 03:43:24 +08:00
|
|
|
F != E; F.increment(EC)) {
|
|
|
|
|
2018-10-01 20:17:05 +08:00
|
|
|
auto Status = F->status();
|
|
|
|
if (!Status) {
|
|
|
|
warning(Status.getError().message(), F->path());
|
2018-04-06 03:43:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-01 20:17:05 +08:00
|
|
|
if (Status->type() == llvm::sys::fs::file_type::regular_file)
|
2016-09-23 05:49:43 +08:00
|
|
|
addCollectedPath(F->path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
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.
|
2016-07-15 09:19:36 +08:00
|
|
|
std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
|
2014-09-19 16:13:12 +08:00
|
|
|
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()) {
|
2016-07-19 01:53:12 +08:00
|
|
|
error(EC.message(), SourceFile);
|
2014-08-23 06:56:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-15 09:19:33 +08:00
|
|
|
void CodeCoverageTool::attachExpansionSubViews(
|
|
|
|
SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
|
|
|
|
const 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,
|
2016-07-15 09:19:33 +08:00
|
|
|
const CoverageMapping &Coverage) {
|
2014-09-20 23:31:56 +08:00
|
|
|
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();
|
2017-02-06 04:10:58 +08:00
|
|
|
auto View = SourceCoverageView::create(DC.demangle(Function.Name),
|
2016-09-08 08:56:48 +08:00
|
|
|
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,
|
2016-07-15 09:19:33 +08:00
|
|
|
const 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);
|
2017-08-03 07:35:24 +08:00
|
|
|
if (!ViewOpts.ShowFunctionInstantiations)
|
|
|
|
return View;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2017-08-03 07:35:25 +08:00
|
|
|
for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) {
|
|
|
|
// Skip functions which have a single instantiation.
|
|
|
|
if (Group.size() < 2)
|
|
|
|
continue;
|
2016-09-21 05:27:48 +08:00
|
|
|
|
2017-08-03 07:35:25 +08:00
|
|
|
for (const FunctionRecord *Function : Group.getInstantiations()) {
|
|
|
|
std::unique_ptr<SourceCoverageView> SubView{nullptr};
|
|
|
|
|
|
|
|
StringRef Funcname = DC.demangle(Function->Name);
|
|
|
|
|
|
|
|
if (Function->ExecutionCount > 0) {
|
|
|
|
auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
|
|
|
|
auto SubViewExpansions = SubViewCoverage.getExpansions();
|
|
|
|
SubView = SourceCoverageView::create(
|
|
|
|
Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
|
|
|
|
attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
View->addInstantiation(Funcname, 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() {
|
2016-10-26 01:40:55 +08:00
|
|
|
for (StringRef ObjectFilename : ObjectFilenames)
|
|
|
|
if (modifiedTimeGT(ObjectFilename, PGOFilename))
|
|
|
|
warning("profile data may be out of date - object is newer",
|
|
|
|
ObjectFilename);
|
2016-07-19 01:53:12 +08:00
|
|
|
auto CoverageOrErr =
|
2017-08-02 05:23:26 +08:00
|
|
|
CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = CoverageOrErr.takeError()) {
|
2016-10-26 01:40:55 +08:00
|
|
|
error("Failed to load coverage: " + toString(std::move(E)),
|
|
|
|
join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
|
2014-09-20 23:31:56 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto Coverage = std::move(CoverageOrErr.get());
|
|
|
|
unsigned Mismatched = Coverage->getMismatchedCount();
|
2017-09-21 09:11:30 +08:00
|
|
|
if (Mismatched) {
|
2017-12-29 00:58:54 +08:00
|
|
|
warning(Twine(Mismatched) + " functions have mismatched data");
|
2014-09-19 16:13:12 +08:00
|
|
|
|
2017-09-21 09:11:30 +08:00
|
|
|
if (ViewOpts.Debug) {
|
|
|
|
for (const auto &HashMismatch : Coverage->getHashMismatches())
|
|
|
|
errs() << "hash-mismatch: "
|
|
|
|
<< "No profile record found for '" << HashMismatch.first << "'"
|
2017-12-29 00:58:54 +08:00
|
|
|
<< " with hash = 0x" << Twine::utohexstr(HashMismatch.second)
|
|
|
|
<< '\n';
|
2017-09-21 09:11:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
remapPathNames(*Coverage);
|
|
|
|
|
2016-09-24 04:13:41 +08:00
|
|
|
if (!SourceFiles.empty())
|
|
|
|
removeUnmappedInputs(*Coverage);
|
|
|
|
|
|
|
|
demangleSymbols(*Coverage);
|
|
|
|
|
|
|
|
return Coverage;
|
|
|
|
}
|
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
|
|
|
|
if (!PathRemapping)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Convert remapping paths to native paths with trailing seperators.
|
|
|
|
auto nativeWithTrailing = [](StringRef Path) -> std::string {
|
|
|
|
if (Path.empty())
|
|
|
|
return "";
|
|
|
|
SmallString<128> NativePath;
|
|
|
|
sys::path::native(Path, NativePath);
|
|
|
|
if (!sys::path::is_separator(NativePath.back()))
|
|
|
|
NativePath += sys::path::get_separator();
|
|
|
|
return NativePath.c_str();
|
|
|
|
};
|
|
|
|
std::string RemapFrom = nativeWithTrailing(PathRemapping->first);
|
|
|
|
std::string RemapTo = nativeWithTrailing(PathRemapping->second);
|
|
|
|
|
|
|
|
// Create a mapping from coverage data file paths to local paths.
|
|
|
|
for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
|
|
|
|
SmallString<128> NativeFilename;
|
|
|
|
sys::path::native(Filename, NativeFilename);
|
|
|
|
if (NativeFilename.startswith(RemapFrom)) {
|
|
|
|
RemappedFilenames[Filename] =
|
|
|
|
RemapTo + NativeFilename.substr(RemapFrom.size()).str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert input files from local paths to coverage data file paths.
|
|
|
|
StringMap<std::string> InvRemappedFilenames;
|
|
|
|
for (const auto &RemappedFilename : RemappedFilenames)
|
|
|
|
InvRemappedFilenames[RemappedFilename.getValue()] = RemappedFilename.getKey();
|
|
|
|
|
|
|
|
for (std::string &Filename : SourceFiles) {
|
|
|
|
SmallString<128> NativeFilename;
|
|
|
|
sys::path::native(Filename, NativeFilename);
|
|
|
|
auto CovFileName = InvRemappedFilenames.find(NativeFilename);
|
|
|
|
if (CovFileName != InvRemappedFilenames.end())
|
|
|
|
Filename = CovFileName->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 04:13:41 +08:00
|
|
|
void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
|
|
|
|
std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
|
2016-09-24 02:57:35 +08:00
|
|
|
|
|
|
|
auto UncoveredFilesIt = SourceFiles.end();
|
2017-08-14 18:20:12 +08:00
|
|
|
// 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);
|
|
|
|
});
|
2014-09-19 16:13:12 +08:00
|
|
|
|
2016-09-24 02:57:35 +08:00
|
|
|
SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2016-07-16 06:44:57 +08: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;
|
|
|
|
}
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile InputTOF{InputPath, InputFD};
|
2016-07-16 06:44:57 +08:00
|
|
|
|
|
|
|
unsigned NumSymbols = 0;
|
|
|
|
for (const auto &Function : Coverage.getCoveredFunctions()) {
|
|
|
|
InputTOF.os() << Function.Name << '\n';
|
|
|
|
++NumSymbols;
|
|
|
|
}
|
2016-07-16 07:08:22 +08:00
|
|
|
InputTOF.os().close();
|
2016-07-16 06:44:57 +08: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;
|
|
|
|
}
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile OutputTOF{OutputPath, OutputFD};
|
2016-07-16 07:08:22 +08:00
|
|
|
OutputTOF.os().close();
|
2016-07-16 06:44:57 +08:00
|
|
|
|
|
|
|
// Invoke the demangler.
|
2018-06-13 01:43:52 +08:00
|
|
|
std::vector<StringRef> ArgsV;
|
|
|
|
for (StringRef Arg : ViewOpts.DemanglerOpts)
|
|
|
|
ArgsV.push_back(Arg);
|
2017-09-14 01:03:37 +08:00
|
|
|
Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}};
|
2016-07-16 06:44:57 +08:00
|
|
|
std::string ErrMsg;
|
2018-06-13 01:43:52 +08:00
|
|
|
int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV,
|
|
|
|
/*env=*/None, Redirects, /*secondsToWait=*/0,
|
2016-07-16 06:44:57 +08:00
|
|
|
/*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 22:26:52 +08: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 06:44:57 +08:00
|
|
|
}
|
|
|
|
|
2016-10-20 01:55:44 +08: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,
|
2017-09-28 18:07:30 +08:00
|
|
|
/*ShowSourceName=*/ShowFilenames,
|
|
|
|
/*ShowTitle=*/ViewOpts.hasOutputDirectory());
|
2016-10-20 01:55:44 +08:00
|
|
|
Printer->closeViewFile(std::move(OS));
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
|
2016-10-26 01:40:55 +08: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-31 04:51:24 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
cl::list<std::string> InputSourceFiles(
|
|
|
|
cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
|
|
|
|
|
2016-09-23 05:49:43 +08:00
|
|
|
cl::opt<bool> DebugDumpCollectedPaths(
|
|
|
|
"dump-collected-paths", cl::Optional, cl::Hidden,
|
|
|
|
cl::desc("Show the collected paths to source files"));
|
|
|
|
|
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"));
|
|
|
|
|
2017-08-02 05:23:26 +08:00
|
|
|
cl::list<std::string> Arches(
|
|
|
|
"arch", cl::desc("architectures of the coverage mapping binaries"));
|
2015-03-11 10:30:51 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
cl::opt<bool> DebugDump("dump", cl::Optional,
|
|
|
|
cl::desc("Show internal debug dump"));
|
|
|
|
|
2016-06-29 08:38:21 +08: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-07 05:44:05 +08:00
|
|
|
clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
|
2016-10-09 03:41:06 +08:00
|
|
|
"HTML output")),
|
2016-06-29 08:38:21 +08:00
|
|
|
cl::init(CoverageViewOptions::OutputFormat::Text));
|
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
cl::opt<std::string> PathRemap(
|
|
|
|
"path-equivalence", cl::Optional,
|
|
|
|
cl::desc("<from>,<to> Map coverage data paths to local source file "
|
|
|
|
"paths"));
|
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));
|
|
|
|
|
2017-08-31 17:11:31 +08:00
|
|
|
cl::list<std::string> NameFilterFiles(
|
|
|
|
"name-whitelist", cl::Optional,
|
|
|
|
cl::desc("Show code coverage only for functions listed in the given "
|
|
|
|
"file"),
|
|
|
|
cl::ZeroOrMore, cl::cat(FilteringCategory));
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
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));
|
|
|
|
|
2018-04-09 23:20:35 +08:00
|
|
|
cl::list<std::string> IgnoreFilenameRegexFilters(
|
|
|
|
"ignore-filename-regex", cl::Optional,
|
|
|
|
cl::desc("Skip source code files with file paths that match the given "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::cat(FilteringCategory));
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
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
|
|
|
|
2016-07-16 06:44:57 +08:00
|
|
|
cl::list<std::string> DemanglerOpts(
|
|
|
|
"Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
|
|
|
|
|
2017-09-12 06:56:20 +08:00
|
|
|
cl::opt<bool> RegionSummary(
|
|
|
|
"show-region-summary", cl::Optional,
|
|
|
|
cl::desc("Show region statistics in summary table"),
|
|
|
|
cl::init(true));
|
|
|
|
|
|
|
|
cl::opt<bool> InstantiationSummary(
|
|
|
|
"show-instantiation-summary", cl::Optional,
|
|
|
|
cl::desc("Show instantiation statistics in summary table"));
|
|
|
|
|
2017-12-12 07:17:46 +08:00
|
|
|
cl::opt<bool> SummaryOnly(
|
|
|
|
"summary-only", cl::Optional,
|
|
|
|
cl::desc("Export only summary information for each source file"));
|
|
|
|
|
2018-01-06 00:15:07 +08: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 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;
|
|
|
|
|
2016-10-26 01:40:55 +08:00
|
|
|
if (!CovFilename.empty())
|
|
|
|
ObjectFilenames.emplace_back(CovFilename);
|
|
|
|
for (const std::string &Filename : CovFilenames)
|
|
|
|
ObjectFilenames.emplace_back(Filename);
|
|
|
|
if (ObjectFilenames.empty()) {
|
2016-10-26 03:52:57 +08:00
|
|
|
errs() << "No filenames specified!\n";
|
2016-10-26 01:40:55 +08:00
|
|
|
::exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-29 08:38:21 +08: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-07 05:44:05 +08:00
|
|
|
case CoverageViewOptions::OutputFormat::HTML:
|
|
|
|
if (UseColor == cl::BOU_FALSE)
|
2016-10-26 03:52:57 +08:00
|
|
|
errs() << "Color output cannot be disabled when generating html.\n";
|
2016-07-07 05:44:05 +08:00
|
|
|
ViewOpts.Colors = true;
|
|
|
|
break;
|
2016-06-29 08:38:21 +08:00
|
|
|
}
|
2015-03-19 08:02:23 +08:00
|
|
|
|
2017-08-14 18:20:12 +08:00
|
|
|
// If path-equivalence was given and is a comma seperated pair then set
|
|
|
|
// PathRemapping.
|
|
|
|
auto EquivPair = StringRef(PathRemap).split(',');
|
|
|
|
if (!(EquivPair.first.empty() && EquivPair.second.empty()))
|
|
|
|
PathRemapping = EquivPair;
|
|
|
|
|
2016-07-16 06:44:57 +08: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);
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:11:31 +08:00
|
|
|
// Read in -name-whitelist files.
|
|
|
|
if (!NameFilterFiles.empty()) {
|
|
|
|
std::string SpecialCaseListErr;
|
|
|
|
NameWhitelist =
|
|
|
|
SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr);
|
|
|
|
if (!NameWhitelist)
|
|
|
|
error(SpecialCaseListErr);
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
// Create the function filters
|
2017-08-31 17:11:31 +08:00
|
|
|
if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) {
|
2017-08-04 08:36:24 +08:00
|
|
|
auto NameFilterer = llvm::make_unique<CoverageFilters>();
|
2014-08-23 06:56:03 +08:00
|
|
|
for (const auto &Name : NameFilters)
|
|
|
|
NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
|
2017-08-31 17:11:31 +08:00
|
|
|
if (NameWhitelist)
|
|
|
|
NameFilterer->push_back(
|
|
|
|
llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
|
2014-08-23 06:56:03 +08:00
|
|
|
for (const auto &Regex : NameRegexFilters)
|
|
|
|
NameFilterer->push_back(
|
|
|
|
llvm::make_unique<NameRegexCoverageFilter>(Regex));
|
2017-08-04 08:36:24 +08:00
|
|
|
Filters.push_back(std::move(NameFilterer));
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
2018-04-09 23:20:35 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
if (RegionCoverageLtFilter.getNumOccurrences() ||
|
|
|
|
RegionCoverageGtFilter.getNumOccurrences() ||
|
|
|
|
LineCoverageLtFilter.getNumOccurrences() ||
|
|
|
|
LineCoverageGtFilter.getNumOccurrences()) {
|
2017-08-04 08:36:24 +08:00
|
|
|
auto StatFilterer = llvm::make_unique<CoverageFilters>();
|
2014-08-23 06:56:03 +08:00
|
|
|
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));
|
2017-08-04 08:36:24 +08:00
|
|
|
Filters.push_back(std::move(StatFilterer));
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:20:35 +08:00
|
|
|
// Create the ignore filename filters.
|
|
|
|
for (const auto &RE : IgnoreFilenameRegexFilters)
|
|
|
|
IgnoreFilenameFilters.push_back(
|
|
|
|
llvm::make_unique<NameRegexCoverageFilter>(RE));
|
|
|
|
|
2017-08-02 05:23:26 +08:00
|
|
|
if (!Arches.empty()) {
|
|
|
|
for (const std::string &Arch : Arches) {
|
|
|
|
if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
|
|
|
|
error("Unknown architecture: " + Arch);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
CoverageArches.emplace_back(Arch);
|
|
|
|
}
|
|
|
|
if (CoverageArches.size() != ObjectFilenames.size()) {
|
|
|
|
error("Number of architectures doesn't match the number of objects");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-03-11 10:30:51 +08:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:20:35 +08:00
|
|
|
// IgnoreFilenameFilters are applied even when InputSourceFiles specified.
|
2016-09-23 05:49:43 +08:00
|
|
|
for (const std::string &File : InputSourceFiles)
|
|
|
|
collectPaths(File);
|
|
|
|
|
|
|
|
if (DebugDumpCollectedPaths) {
|
2016-09-24 02:57:32 +08:00
|
|
|
for (const std::string &SF : SourceFiles)
|
2016-09-23 05:49:43 +08:00
|
|
|
outs() << SF << '\n';
|
|
|
|
::exit(0);
|
2014-09-19 16:13:12 +08:00
|
|
|
}
|
2016-09-23 05:49:43 +08:00
|
|
|
|
2017-09-12 06:56:20 +08:00
|
|
|
ViewOpts.ShowRegionSummary = RegionSummary;
|
|
|
|
ViewOpts.ShowInstantiationSummary = InstantiationSummary;
|
2017-12-12 07:17:46 +08:00
|
|
|
ViewOpts.ExportSummaryOnly = SummaryOnly;
|
2018-01-06 00:15:07 +08:00
|
|
|
ViewOpts.NumThreads = NumThreads;
|
2017-09-12 06:56:20 +08:00
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (Cmd) {
|
|
|
|
case Show:
|
2018-01-05 03:33:29 +08:00
|
|
|
return doShow(argc, argv, commandLineParser);
|
2014-08-23 06:56:03 +08:00
|
|
|
case Report:
|
2018-01-05 03:33:29 +08:00
|
|
|
return doReport(argc, argv, commandLineParser);
|
2016-07-27 06:50:58 +08:00
|
|
|
case Export:
|
2018-01-05 03:33:29 +08:00
|
|
|
return doExport(argc, argv, commandLineParser);
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
int CodeCoverageTool::doShow(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
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"),
|
2017-08-03 07:35:24 +08:00
|
|
|
cl::init(true), cl::cat(ViewCategory));
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2016-06-28 10:09:39 +08: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 18:39:43 +08:00
|
|
|
cl::opt<uint32_t> TabSize(
|
2016-08-05 02:00:42 +08:00
|
|
|
"tab-size", cl::init(2),
|
|
|
|
cl::desc(
|
|
|
|
"Set tab expansion size for html coverage reports (default = 2)"));
|
2016-08-04 18:39:43 +08:00
|
|
|
|
2016-08-24 22:27:23 +08:00
|
|
|
cl::opt<std::string> ProjectTitle(
|
|
|
|
"project-title", cl::Optional,
|
|
|
|
cl::desc("Set project title for the coverage report"));
|
|
|
|
|
2014-08-23 06:56:03 +08: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.ShowExpandedRegions = ShowExpansions;
|
|
|
|
ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
|
2016-06-28 10:09:39 +08:00
|
|
|
ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
|
2016-08-04 18:39:43 +08:00
|
|
|
ViewOpts.TabSize = TabSize;
|
2016-08-24 22:27:23 +08:00
|
|
|
ViewOpts.ProjectTitle = ProjectTitle;
|
2016-06-28 10:09:39 +08:00
|
|
|
|
2016-06-29 00:12:20 +08:00
|
|
|
if (ViewOpts.hasOutputDirectory()) {
|
2016-06-28 10:09:39 +08:00
|
|
|
if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
|
|
|
|
error("Could not create output directory!", E.message());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2016-08-24 22:27:23 +08: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 18:59:17 +08:00
|
|
|
std::string ModifiedTimeStr = to_string(ModifiedTime);
|
2016-11-30 18:01:11 +08:00
|
|
|
size_t found = ModifiedTimeStr.rfind(':');
|
2016-08-24 22:27:23 +08:00
|
|
|
ViewOpts.CreatedTimeStr = (found != std::string::npos)
|
|
|
|
? "Created: " + ModifiedTimeStr.substr(0, found)
|
|
|
|
: "Created: " + ModifiedTimeStr;
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 06:56:03 +08:00
|
|
|
return 1;
|
|
|
|
|
2016-06-29 00:12:24 +08:00
|
|
|
auto Printer = CoveragePrinter::create(ViewOpts);
|
|
|
|
|
2017-09-28 18:07:30 +08:00
|
|
|
if (SourceFiles.empty())
|
|
|
|
// Get the source files from the function coverage mapping.
|
2018-04-09 23:20:35 +08:00
|
|
|
for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
|
|
|
|
if (!IgnoreFilenameFilters.matchesFilename(Filename))
|
|
|
|
SourceFiles.push_back(Filename);
|
|
|
|
}
|
2017-09-28 18:07:30 +08:00
|
|
|
|
|
|
|
// Create an index out of the source files.
|
|
|
|
if (ViewOpts.hasOutputDirectory()) {
|
|
|
|
if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
|
|
|
|
error("Could not create index file!", toString(std::move(E)));
|
2016-06-29 08:38:21 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-09-28 18:07:30 +08:00
|
|
|
}
|
2017-09-28 00:20:07 +08:00
|
|
|
|
2017-09-28 18:07:30 +08:00
|
|
|
if (!Filters.empty()) {
|
|
|
|
// Build the map of filenames to functions.
|
|
|
|
std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
|
|
|
|
FilenameFunctionMap;
|
|
|
|
for (const auto &SourceFile : SourceFiles)
|
|
|
|
for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
|
|
|
|
if (Filters.matches(*Coverage.get(), Function))
|
|
|
|
FilenameFunctionMap[SourceFile].push_back(&Function);
|
|
|
|
|
|
|
|
// Only print filter matching functions for each file.
|
|
|
|
for (const auto &FileFunc : FilenameFunctionMap) {
|
|
|
|
StringRef File = FileFunc.first;
|
|
|
|
const auto &Functions = FileFunc.second;
|
|
|
|
|
|
|
|
auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
|
|
|
|
if (Error E = OSOrErr.takeError()) {
|
|
|
|
error("Could not create view file!", toString(std::move(E)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto OS = std::move(OSOrErr.get());
|
|
|
|
|
2017-10-04 16:54:37 +08:00
|
|
|
bool ShowTitle = ViewOpts.hasOutputDirectory();
|
2017-09-28 18:07:30 +08:00
|
|
|
for (const auto *Function : Functions) {
|
|
|
|
auto FunctionView = createFunctionView(*Function, *Coverage);
|
|
|
|
if (!FunctionView) {
|
|
|
|
warning("Could not read coverage for '" + Function->Name + "'.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FunctionView->print(*OS.get(), /*WholeFile=*/false,
|
|
|
|
/*ShowSourceName=*/true, ShowTitle);
|
|
|
|
ShowTitle = false;
|
2014-09-20 03:07:17 +08:00
|
|
|
}
|
2016-06-28 10:09:39 +08:00
|
|
|
|
2017-09-28 18:07:30 +08:00
|
|
|
Printer->closeViewFile(std::move(OS));
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show files
|
2016-08-24 22:27:23 +08:00
|
|
|
bool ShowFilenames =
|
2016-09-07 05:41:38 +08:00
|
|
|
(SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
|
2016-08-24 22:27:23 +08:00
|
|
|
(ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2018-01-06 00:15:07 +08:00
|
|
|
auto NumThreads = ViewOpts.NumThreads;
|
|
|
|
|
2017-07-11 09:23:29 +08: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-20 01:55:44 +08: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 09:23:29 +08:00
|
|
|
ThreadPool Pool(NumThreads);
|
2016-10-20 01:55:44 +08:00
|
|
|
for (const std::string &SourceFile : SourceFiles)
|
|
|
|
Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
|
|
|
|
Coverage.get(), Printer.get(), ShowFilenames);
|
|
|
|
Pool.wait();
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
int CodeCoverageTool::doReport(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
2017-02-06 04:11:08 +08:00
|
|
|
cl::opt<bool> ShowFunctionSummaries(
|
|
|
|
"show-functions", cl::Optional, cl::init(false),
|
|
|
|
cl::desc("Show coverage summaries for each function"));
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
2017-03-01 00:57:28 +08:00
|
|
|
if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
|
2016-07-07 05:44:05 +08:00
|
|
|
error("HTML output for summary reports is not yet supported.");
|
2017-03-01 00:57:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-07-07 05:44:05 +08:00
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage)
|
2014-08-23 06:56:03 +08:00
|
|
|
return 1;
|
|
|
|
|
2016-09-07 06:45:57 +08:00
|
|
|
CoverageReport Report(ViewOpts, *Coverage.get());
|
2017-09-26 07:10:03 +08:00
|
|
|
if (!ShowFunctionSummaries) {
|
2017-10-13 22:44:51 +08:00
|
|
|
if (SourceFiles.empty())
|
2018-04-09 23:20:35 +08:00
|
|
|
Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters);
|
2017-10-13 22:44:51 +08:00
|
|
|
else
|
|
|
|
Report.renderFileReports(llvm::outs(), SourceFiles);
|
2017-09-26 07:10:03 +08:00
|
|
|
} else {
|
|
|
|
if (SourceFiles.empty()) {
|
|
|
|
error("Source files must be specified when -show-functions=true is "
|
|
|
|
"specified");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-06 04:11:03 +08:00
|
|
|
Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
|
2017-09-26 07:10:03 +08:00
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
int CodeCoverageTool::doExport(int argc, const char **argv,
|
|
|
|
CommandLineParserType commandLineParser) {
|
2016-07-27 06:50:58 +08:00
|
|
|
|
|
|
|
auto Err = commandLineParser(argc, argv);
|
|
|
|
if (Err)
|
|
|
|
return Err;
|
|
|
|
|
2017-03-01 00:57:28 +08:00
|
|
|
if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
|
|
|
|
error("Coverage data can only be exported as textual JSON.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:50:58 +08:00
|
|
|
auto Coverage = load();
|
|
|
|
if (!Coverage) {
|
|
|
|
error("Could not load coverage information");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
auto Exporter = CoverageExporterJson(*Coverage.get(), ViewOpts, outs());
|
|
|
|
|
|
|
|
if (SourceFiles.empty())
|
2018-04-09 23:20:35 +08:00
|
|
|
Exporter.renderRoot(IgnoreFilenameFilters);
|
2018-01-05 03:33:29 +08:00
|
|
|
else
|
|
|
|
Exporter.renderRoot(SourceFiles);
|
2016-07-27 06:50:58 +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);
|
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
|
|
|
int exportMain(int argc, const char *argv[]) {
|
|
|
|
CodeCoverageTool Tool;
|
|
|
|
return Tool.run(CodeCoverageTool::Export, argc, argv);
|
|
|
|
}
|