2016-07-27 06:50:58 +08:00
|
|
|
//===- CoverageExporterJson.cpp - Code coverage export --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-27 06:50:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements export of code coverage data to JSON.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The json code coverage export follows the following format
|
|
|
|
// Root: dict => Root Element containing metadata
|
|
|
|
// -- Data: array => Homogeneous array of one or more export objects
|
2018-09-13 05:59:38 +08:00
|
|
|
// -- Export: dict => Json representation of one CoverageMapping
|
|
|
|
// -- Files: array => List of objects describing coverage for files
|
|
|
|
// -- File: dict => Coverage for a single file
|
|
|
|
// -- Segments: array => List of Segments contained in the file
|
|
|
|
// -- Segment: dict => Describes a segment of the file with a counter
|
|
|
|
// -- Expansions: array => List of expansion records
|
|
|
|
// -- Expansion: dict => Object that descibes a single expansion
|
|
|
|
// -- CountedRegion: dict => The region to be expanded
|
|
|
|
// -- TargetRegions: array => List of Regions in the expansion
|
|
|
|
// -- CountedRegion: dict => Single Region in the expansion
|
|
|
|
// -- Summary: dict => Object summarizing the coverage for this file
|
|
|
|
// -- LineCoverage: dict => Object summarizing line coverage
|
|
|
|
// -- FunctionCoverage: dict => Object summarizing function coverage
|
|
|
|
// -- RegionCoverage: dict => Object summarizing region coverage
|
|
|
|
// -- Functions: array => List of objects describing coverage for functions
|
|
|
|
// -- Function: dict => Coverage info for a single function
|
|
|
|
// -- Filenames: array => List of filenames that the function relates to
|
|
|
|
// -- Summary: dict => Object summarizing the coverage for the entire binary
|
|
|
|
// -- LineCoverage: dict => Object summarizing line coverage
|
|
|
|
// -- FunctionCoverage: dict => Object summarizing function coverage
|
|
|
|
// -- InstantiationCoverage: dict => Object summarizing inst. coverage
|
|
|
|
// -- RegionCoverage: dict => Object summarizing region coverage
|
2016-07-27 06:50:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-01-05 03:33:29 +08:00
|
|
|
#include "CoverageExporterJson.h"
|
2016-09-19 08:38:29 +08:00
|
|
|
#include "CoverageReport.h"
|
2019-03-15 01:49:27 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-09-13 05:59:38 +08:00
|
|
|
#include "llvm/Support/JSON.h"
|
2019-03-15 01:49:27 +08:00
|
|
|
#include "llvm/Support/ThreadPool.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <mutex>
|
|
|
|
#include <utility>
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The semantic version combined as a string.
|
2016-10-25 08:08:33 +08:00
|
|
|
#define LLVM_COVERAGE_EXPORT_JSON_STR "2.0.0"
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Unique type identifier for JSON coverage export.
|
2016-07-27 06:50:58 +08:00
|
|
|
#define LLVM_COVERAGE_EXPORT_JSON_TYPE_STR "llvm.coverage.json.export"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
namespace {
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderSegment(const coverage::CoverageSegment &Segment) {
|
|
|
|
return json::Array({Segment.Line, Segment.Col, int64_t(Segment.Count),
|
|
|
|
Segment.HasCount, Segment.IsRegionEntry});
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderRegion(const coverage::CountedRegion &Region) {
|
|
|
|
return json::Array({Region.LineStart, Region.ColumnStart, Region.LineEnd,
|
|
|
|
Region.ColumnEnd, int64_t(Region.ExecutionCount),
|
|
|
|
Region.FileID, Region.ExpandedFileID,
|
|
|
|
int64_t(Region.Kind)});
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderRegions(ArrayRef<coverage::CountedRegion> Regions) {
|
|
|
|
json::Array RegionArray;
|
|
|
|
for (const auto &Region : Regions)
|
|
|
|
RegionArray.push_back(renderRegion(Region));
|
|
|
|
return RegionArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Object renderExpansion(const coverage::ExpansionRecord &Expansion) {
|
|
|
|
return json::Object(
|
|
|
|
{{"filenames", json::Array(Expansion.Function.Filenames)},
|
|
|
|
// Mark the beginning and end of this expansion in the source file.
|
|
|
|
{"source_region", renderRegion(Expansion.Region)},
|
|
|
|
// Enumerate the coverage information for the expansion.
|
|
|
|
{"target_regions", renderRegions(Expansion.Function.CountedRegions)}});
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Object renderSummary(const FileCoverageSummary &Summary) {
|
|
|
|
return json::Object(
|
|
|
|
{{"lines",
|
|
|
|
json::Object({{"count", int64_t(Summary.LineCoverage.getNumLines())},
|
|
|
|
{"covered", int64_t(Summary.LineCoverage.getCovered())},
|
|
|
|
{"percent", Summary.LineCoverage.getPercentCovered()}})},
|
|
|
|
{"functions",
|
|
|
|
json::Object(
|
|
|
|
{{"count", int64_t(Summary.FunctionCoverage.getNumFunctions())},
|
|
|
|
{"covered", int64_t(Summary.FunctionCoverage.getExecuted())},
|
|
|
|
{"percent", Summary.FunctionCoverage.getPercentCovered()}})},
|
|
|
|
{"instantiations",
|
|
|
|
json::Object(
|
|
|
|
{{"count",
|
|
|
|
int64_t(Summary.InstantiationCoverage.getNumFunctions())},
|
|
|
|
{"covered", int64_t(Summary.InstantiationCoverage.getExecuted())},
|
|
|
|
{"percent", Summary.InstantiationCoverage.getPercentCovered()}})},
|
|
|
|
{"regions",
|
|
|
|
json::Object(
|
|
|
|
{{"count", int64_t(Summary.RegionCoverage.getNumRegions())},
|
|
|
|
{"covered", int64_t(Summary.RegionCoverage.getCovered())},
|
|
|
|
{"notcovered", int64_t(Summary.RegionCoverage.getNumRegions() -
|
|
|
|
Summary.RegionCoverage.getCovered())},
|
|
|
|
{"percent", Summary.RegionCoverage.getPercentCovered()}})}});
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Array renderFileExpansions(const coverage::CoverageData &FileCoverage,
|
|
|
|
const FileCoverageSummary &FileReport) {
|
|
|
|
json::Array ExpansionArray;
|
|
|
|
for (const auto &Expansion : FileCoverage.getExpansions())
|
|
|
|
ExpansionArray.push_back(renderExpansion(Expansion));
|
|
|
|
return ExpansionArray;
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderFileSegments(const coverage::CoverageData &FileCoverage,
|
|
|
|
const FileCoverageSummary &FileReport) {
|
|
|
|
json::Array SegmentArray;
|
|
|
|
for (const auto &Segment : FileCoverage)
|
|
|
|
SegmentArray.push_back(renderSegment(Segment));
|
|
|
|
return SegmentArray;
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Object renderFile(const coverage::CoverageMapping &Coverage,
|
|
|
|
const std::string &Filename,
|
|
|
|
const FileCoverageSummary &FileReport,
|
2019-03-15 01:49:27 +08:00
|
|
|
const CoverageViewOptions &Options) {
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Object File({{"filename", Filename}});
|
2019-03-15 01:49:27 +08:00
|
|
|
if (!Options.ExportSummaryOnly) {
|
2018-09-13 05:59:38 +08:00
|
|
|
// Calculate and render detailed coverage information for given file.
|
|
|
|
auto FileCoverage = Coverage.getCoverageForFile(Filename);
|
|
|
|
File["segments"] = renderFileSegments(FileCoverage, FileReport);
|
2019-03-15 01:49:27 +08:00
|
|
|
if (!Options.SkipExpansions) {
|
|
|
|
File["expansions"] = renderFileExpansions(FileCoverage, FileReport);
|
|
|
|
}
|
2018-09-13 05:59:38 +08:00
|
|
|
}
|
|
|
|
File["summary"] = renderSummary(FileReport);
|
|
|
|
return File;
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderFiles(const coverage::CoverageMapping &Coverage,
|
|
|
|
ArrayRef<std::string> SourceFiles,
|
|
|
|
ArrayRef<FileCoverageSummary> FileReports,
|
2019-03-15 01:49:27 +08:00
|
|
|
const CoverageViewOptions &Options) {
|
|
|
|
auto NumThreads = Options.NumThreads;
|
|
|
|
if (NumThreads == 0) {
|
|
|
|
NumThreads = std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
|
|
|
|
unsigned(SourceFiles.size())));
|
|
|
|
}
|
|
|
|
ThreadPool Pool(NumThreads);
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array FileArray;
|
2019-03-15 01:49:27 +08:00
|
|
|
std::mutex FileArrayMutex;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = SourceFiles.size(); I < E; ++I) {
|
|
|
|
auto &SourceFile = SourceFiles[I];
|
|
|
|
auto &FileReport = FileReports[I];
|
|
|
|
Pool.async([&] {
|
|
|
|
auto File = renderFile(Coverage, SourceFile, FileReport, Options);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(FileArrayMutex);
|
|
|
|
FileArray.push_back(std::move(File));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Pool.wait();
|
2018-09-13 05:59:38 +08:00
|
|
|
return FileArray;
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
json::Array renderFunctions(
|
|
|
|
const iterator_range<coverage::FunctionRecordIterator> &Functions) {
|
|
|
|
json::Array FunctionArray;
|
|
|
|
for (const auto &F : Functions)
|
|
|
|
FunctionArray.push_back(
|
|
|
|
json::Object({{"name", F.Name},
|
|
|
|
{"count", int64_t(F.ExecutionCount)},
|
|
|
|
{"regions", renderRegions(F.CountedRegions)},
|
|
|
|
{"filenames", json::Array(F.Filenames)}}));
|
|
|
|
return FunctionArray;
|
2018-01-05 03:33:29 +08:00
|
|
|
}
|
2016-07-27 06:50:58 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void CoverageExporterJson::renderRoot(const CoverageFilters &IgnoreFilters) {
|
2018-01-05 03:33:29 +08:00
|
|
|
std::vector<std::string> SourceFiles;
|
2018-04-09 23:20:35 +08:00
|
|
|
for (StringRef SF : Coverage.getUniqueSourceFiles()) {
|
2018-09-13 05:59:38 +08:00
|
|
|
if (!IgnoreFilters.matchesFilename(SF))
|
2018-04-09 23:20:35 +08:00
|
|
|
SourceFiles.emplace_back(SF);
|
|
|
|
}
|
2018-01-05 03:33:29 +08:00
|
|
|
renderRoot(SourceFiles);
|
|
|
|
}
|
2016-09-19 08:38:29 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
void CoverageExporterJson::renderRoot(ArrayRef<std::string> SourceFiles) {
|
2018-01-05 03:33:29 +08:00
|
|
|
FileCoverageSummary Totals = FileCoverageSummary("Totals");
|
|
|
|
auto FileReports = CoverageReport::prepareFileReports(Coverage, Totals,
|
|
|
|
SourceFiles, Options);
|
2019-03-15 01:49:27 +08:00
|
|
|
auto Files = renderFiles(Coverage, SourceFiles, FileReports, Options);
|
|
|
|
// Sort files in order of their names.
|
|
|
|
std::sort(Files.begin(), Files.end(),
|
|
|
|
[](const json::Value &A, const json::Value &B) {
|
|
|
|
const json::Object *ObjA = A.getAsObject();
|
|
|
|
const json::Object *ObjB = B.getAsObject();
|
|
|
|
assert(ObjA != nullptr && "Value A was not an Object");
|
|
|
|
assert(ObjB != nullptr && "Value B was not an Object");
|
|
|
|
const StringRef FilenameA = ObjA->getString("filename").getValue();
|
|
|
|
const StringRef FilenameB = ObjB->getString("filename").getValue();
|
|
|
|
return FilenameA.compare(FilenameB) < 0;
|
|
|
|
});
|
|
|
|
auto Export = json::Object(
|
|
|
|
{{"files", std::move(Files)}, {"totals", renderSummary(Totals)}});
|
|
|
|
// Skip functions-level information if necessary.
|
|
|
|
if (!Options.ExportSummaryOnly && !Options.SkipFunctions)
|
2018-09-13 05:59:38 +08:00
|
|
|
Export["functions"] = renderFunctions(Coverage.getCoveredFunctions());
|
2018-01-05 03:33:29 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
auto ExportArray = json::Array({std::move(Export)});
|
2018-01-05 03:33:29 +08:00
|
|
|
|
2018-09-13 05:59:38 +08:00
|
|
|
OS << json::Object({{"version", LLVM_COVERAGE_EXPORT_JSON_STR},
|
|
|
|
{"type", LLVM_COVERAGE_EXPORT_JSON_TYPE_STR},
|
|
|
|
{"data", std::move(ExportArray)}});
|
2016-07-27 06:50:58 +08:00
|
|
|
}
|