2014-07-24 23:57:54 +00:00
|
|
|
//=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for clang's and llvm's instrumentation based
|
|
|
|
// code coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ProfileData/CoverageMapping.h"
|
2014-09-20 15:31:56 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-02-12 16:18:07 +00:00
|
|
|
#include "llvm/ADT/SmallBitVector.h"
|
2014-09-20 15:31:56 +00:00
|
|
|
#include "llvm/ProfileData/CoverageMappingReader.h"
|
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2014-09-25 00:34:18 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-06-13 17:23:04 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2014-09-08 21:04:00 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-05-06 23:19:35 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2015-05-05 23:44:48 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-03-23 19:32:43 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
2014-09-25 00:34:18 +00:00
|
|
|
#define DEBUG_TYPE "coverage-mapping"
|
|
|
|
|
2014-07-24 23:57:54 +00:00
|
|
|
Counter CounterExpressionBuilder::get(const CounterExpression &E) {
|
2014-10-02 17:14:18 +00:00
|
|
|
auto It = ExpressionIndices.find(E);
|
|
|
|
if (It != ExpressionIndices.end())
|
|
|
|
return Counter::getExpression(It->second);
|
|
|
|
unsigned I = Expressions.size();
|
2014-07-24 23:57:54 +00:00
|
|
|
Expressions.push_back(E);
|
2014-10-02 17:14:18 +00:00
|
|
|
ExpressionIndices[E] = I;
|
|
|
|
return Counter::getExpression(I);
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:43:31 +00:00
|
|
|
void CounterExpressionBuilder::extractTerms(
|
|
|
|
Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
|
2014-07-24 23:57:54 +00:00
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
break;
|
|
|
|
case Counter::CounterValueReference:
|
2014-10-02 16:43:31 +00:00
|
|
|
Terms.push_back(std::make_pair(C.getCounterID(), Sign));
|
2014-07-24 23:57:54 +00:00
|
|
|
break;
|
|
|
|
case Counter::Expression:
|
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
2014-10-02 16:43:31 +00:00
|
|
|
extractTerms(E.LHS, Sign, Terms);
|
|
|
|
extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
|
|
|
|
Terms);
|
2014-07-24 23:57:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
|
|
|
|
// Gather constant terms.
|
2014-10-02 16:43:31 +00:00
|
|
|
llvm::SmallVector<std::pair<unsigned, int>, 32> Terms;
|
|
|
|
extractTerms(ExpressionTree, +1, Terms);
|
|
|
|
|
|
|
|
// If there are no terms, this is just a zero. The algorithm below assumes at
|
|
|
|
// least one term.
|
|
|
|
if (Terms.size() == 0)
|
|
|
|
return Counter::getZero();
|
|
|
|
|
|
|
|
// Group the terms by counter ID.
|
|
|
|
std::sort(Terms.begin(), Terms.end(),
|
|
|
|
[](const std::pair<unsigned, int> &LHS,
|
|
|
|
const std::pair<unsigned, int> &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Combine terms by counter ID to eliminate counters that sum to zero.
|
|
|
|
auto Prev = Terms.begin();
|
|
|
|
for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
|
|
|
|
if (I->first == Prev->first) {
|
|
|
|
Prev->second += I->second;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++Prev;
|
|
|
|
*Prev = *I;
|
|
|
|
}
|
|
|
|
Terms.erase(++Prev, Terms.end());
|
2014-07-24 23:57:54 +00:00
|
|
|
|
|
|
|
Counter C;
|
2014-10-02 16:43:31 +00:00
|
|
|
// Create additions. We do this before subtractions to avoid constructs like
|
|
|
|
// ((0 - X) + Y), as opposed to (Y - X).
|
|
|
|
for (auto Term : Terms) {
|
|
|
|
if (Term.second <= 0)
|
2014-07-24 23:57:54 +00:00
|
|
|
continue;
|
2014-10-02 16:43:31 +00:00
|
|
|
for (int I = 0; I < Term.second; ++I)
|
2014-07-24 23:57:54 +00:00
|
|
|
if (C.isZero())
|
2014-10-02 16:43:31 +00:00
|
|
|
C = Counter::getCounter(Term.first);
|
2014-07-24 23:57:54 +00:00
|
|
|
else
|
|
|
|
C = get(CounterExpression(CounterExpression::Add, C,
|
2014-10-02 16:43:31 +00:00
|
|
|
Counter::getCounter(Term.first)));
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create subtractions.
|
2014-10-02 16:43:31 +00:00
|
|
|
for (auto Term : Terms) {
|
|
|
|
if (Term.second >= 0)
|
2014-07-24 23:57:54 +00:00
|
|
|
continue;
|
2014-10-02 16:43:31 +00:00
|
|
|
for (int I = 0; I < -Term.second; ++I)
|
2014-07-24 23:57:54 +00:00
|
|
|
C = get(CounterExpression(CounterExpression::Subtract, C,
|
2014-10-02 16:43:31 +00:00
|
|
|
Counter::getCounter(Term.first)));
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
|
|
|
|
return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
|
|
|
|
return simplify(
|
|
|
|
get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CounterMappingContext::dump(const Counter &C,
|
|
|
|
llvm::raw_ostream &OS) const {
|
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
OS << '0';
|
|
|
|
return;
|
|
|
|
case Counter::CounterValueReference:
|
|
|
|
OS << '#' << C.getCounterID();
|
|
|
|
break;
|
|
|
|
case Counter::Expression: {
|
|
|
|
if (C.getExpressionID() >= Expressions.size())
|
|
|
|
return;
|
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
|
|
|
OS << '(';
|
2014-07-29 19:58:16 +00:00
|
|
|
dump(E.LHS, OS);
|
2014-07-24 23:57:54 +00:00
|
|
|
OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
|
2014-07-29 19:58:16 +00:00
|
|
|
dump(E.RHS, OS);
|
2014-07-24 23:57:54 +00:00
|
|
|
OS << ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CounterValues.empty())
|
|
|
|
return;
|
2014-09-08 21:04:00 +00:00
|
|
|
ErrorOr<int64_t> Value = evaluate(C);
|
|
|
|
if (!Value)
|
2014-07-24 23:57:54 +00:00
|
|
|
return;
|
2014-09-08 21:04:00 +00:00
|
|
|
OS << '[' << *Value << ']';
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:04:00 +00:00
|
|
|
ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
|
2014-07-24 23:57:54 +00:00
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
return 0;
|
|
|
|
case Counter::CounterValueReference:
|
2014-09-08 21:04:00 +00:00
|
|
|
if (C.getCounterID() >= CounterValues.size())
|
2015-06-13 17:23:04 +00:00
|
|
|
return make_error_code(errc::argument_out_of_domain);
|
2014-07-24 23:57:54 +00:00
|
|
|
return CounterValues[C.getCounterID()];
|
|
|
|
case Counter::Expression: {
|
2014-09-08 21:04:00 +00:00
|
|
|
if (C.getExpressionID() >= Expressions.size())
|
2015-06-13 17:23:04 +00:00
|
|
|
return make_error_code(errc::argument_out_of_domain);
|
2014-07-24 23:57:54 +00:00
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
2014-09-08 21:04:00 +00:00
|
|
|
ErrorOr<int64_t> LHS = evaluate(E.LHS);
|
|
|
|
if (!LHS)
|
|
|
|
return LHS;
|
|
|
|
ErrorOr<int64_t> RHS = evaluate(E.RHS);
|
|
|
|
if (!RHS)
|
|
|
|
return RHS;
|
|
|
|
return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-08 21:04:00 +00:00
|
|
|
llvm_unreachable("Unhandled CounterKind");
|
2014-07-24 23:57:54 +00:00
|
|
|
}
|
2014-09-20 15:31:56 +00:00
|
|
|
|
2014-11-14 01:50:32 +00:00
|
|
|
void FunctionRecordIterator::skipOtherFiles() {
|
|
|
|
while (Current != Records.end() && !Filename.empty() &&
|
|
|
|
Filename != Current->Filenames[0])
|
|
|
|
++Current;
|
|
|
|
if (Current == Records.end())
|
|
|
|
*this = FunctionRecordIterator();
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:44:48 +00:00
|
|
|
/// Get the function name from the record, removing the filename prefix if
|
|
|
|
/// necessary.
|
|
|
|
static StringRef getFuncNameWithoutPrefix(const CoverageMappingRecord &Record) {
|
|
|
|
StringRef FunctionName = Record.FunctionName;
|
|
|
|
if (Record.Filenames.empty())
|
|
|
|
return FunctionName;
|
|
|
|
StringRef Filename = sys::path::filename(Record.Filenames[0]);
|
|
|
|
if (FunctionName.startswith(Filename))
|
|
|
|
FunctionName = FunctionName.drop_front(Filename.size() + 1);
|
|
|
|
return FunctionName;
|
|
|
|
}
|
|
|
|
|
2014-09-20 15:31:56 +00:00
|
|
|
ErrorOr<std::unique_ptr<CoverageMapping>>
|
2015-02-18 18:01:14 +00:00
|
|
|
CoverageMapping::load(CoverageMappingReader &CoverageReader,
|
2014-09-20 15:31:56 +00:00
|
|
|
IndexedInstrProfReader &ProfileReader) {
|
|
|
|
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
|
|
|
|
|
|
|
|
std::vector<uint64_t> Counts;
|
|
|
|
for (const auto &Record : CoverageReader) {
|
2015-02-18 18:40:46 +00:00
|
|
|
CounterMappingContext Ctx(Record.Expressions);
|
|
|
|
|
2014-09-20 15:31:56 +00:00
|
|
|
Counts.clear();
|
|
|
|
if (std::error_code EC = ProfileReader.getFunctionCounts(
|
|
|
|
Record.FunctionName, Record.FunctionHash, Counts)) {
|
2015-02-18 18:40:46 +00:00
|
|
|
if (EC == instrprof_error::hash_mismatch) {
|
|
|
|
Coverage->MismatchedFunctionCount++;
|
|
|
|
continue;
|
|
|
|
} else if (EC != instrprof_error::unknown_function)
|
2014-09-20 15:31:56 +00:00
|
|
|
return EC;
|
2015-05-13 22:03:04 +00:00
|
|
|
Counts.assign(Record.MappingRegions.size(), 0);
|
|
|
|
}
|
|
|
|
Ctx.setCounts(Counts);
|
2014-09-20 15:31:56 +00:00
|
|
|
|
2015-02-18 18:40:46 +00:00
|
|
|
assert(!Record.MappingRegions.empty() && "Function has no regions");
|
2015-05-05 23:44:48 +00:00
|
|
|
|
|
|
|
FunctionRecord Function(getFuncNameWithoutPrefix(Record), Record.Filenames);
|
2014-09-20 15:31:56 +00:00
|
|
|
for (const auto &Region : Record.MappingRegions) {
|
|
|
|
ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
|
|
|
|
if (!ExecutionCount)
|
|
|
|
break;
|
2015-02-18 18:40:46 +00:00
|
|
|
Function.pushRegion(Region, *ExecutionCount);
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
|
|
|
|
Coverage->MismatchedFunctionCount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-03 18:33:16 +00:00
|
|
|
Coverage->Functions.push_back(std::move(Function));
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Coverage);
|
|
|
|
}
|
|
|
|
|
2014-09-20 17:19:52 +00:00
|
|
|
ErrorOr<std::unique_ptr<CoverageMapping>>
|
2015-03-11 02:30:51 +00:00
|
|
|
CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
|
2015-06-22 21:33:24 +00:00
|
|
|
StringRef Arch) {
|
2014-09-20 17:19:52 +00:00
|
|
|
auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
|
2015-02-26 20:06:28 +00:00
|
|
|
if (std::error_code EC = CounterMappingBuff.getError())
|
2014-09-20 17:19:52 +00:00
|
|
|
return EC;
|
2015-02-26 20:06:28 +00:00
|
|
|
auto CoverageReaderOrErr =
|
2015-03-11 02:30:51 +00:00
|
|
|
BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
|
2015-02-26 20:06:28 +00:00
|
|
|
if (std::error_code EC = CoverageReaderOrErr.getError())
|
2014-09-20 17:19:52 +00:00
|
|
|
return EC;
|
2015-02-26 20:06:28 +00:00
|
|
|
auto CoverageReader = std::move(CoverageReaderOrErr.get());
|
2015-02-16 21:28:58 +00:00
|
|
|
auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
|
|
|
|
if (auto EC = ProfileReaderOrErr.getError())
|
2014-09-20 17:19:52 +00:00
|
|
|
return EC;
|
2015-02-16 21:28:58 +00:00
|
|
|
auto ProfileReader = std::move(ProfileReaderOrErr.get());
|
2015-02-26 20:06:28 +00:00
|
|
|
return load(*CoverageReader, *ProfileReader);
|
2014-09-20 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-20 15:31:56 +00:00
|
|
|
namespace {
|
|
|
|
/// \brief Distributes functions into instantiation sets.
|
|
|
|
///
|
|
|
|
/// An instantiation set is a collection of functions that have the same source
|
|
|
|
/// code, ie, template functions specializations.
|
|
|
|
class FunctionInstantiationSetCollector {
|
|
|
|
typedef DenseMap<std::pair<unsigned, unsigned>,
|
|
|
|
std::vector<const FunctionRecord *>> MapT;
|
|
|
|
MapT InstantiatedFunctions;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void insert(const FunctionRecord &Function, unsigned FileID) {
|
|
|
|
auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
|
|
|
|
while (I != E && I->FileID != FileID)
|
|
|
|
++I;
|
|
|
|
assert(I != E && "function does not cover the given file");
|
|
|
|
auto &Functions = InstantiatedFunctions[I->startLoc()];
|
|
|
|
Functions.push_back(&Function);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapT::iterator begin() { return InstantiatedFunctions.begin(); }
|
|
|
|
|
|
|
|
MapT::iterator end() { return InstantiatedFunctions.end(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class SegmentBuilder {
|
|
|
|
std::vector<CoverageSegment> Segments;
|
|
|
|
SmallVector<const CountedRegion *, 8> ActiveRegions;
|
|
|
|
|
|
|
|
/// Start a segment with no count specified.
|
|
|
|
void startSegment(unsigned Line, unsigned Col) {
|
2014-09-25 00:34:18 +00:00
|
|
|
DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
|
2014-09-20 15:31:56 +00:00
|
|
|
Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a segment with the given Region's count.
|
|
|
|
void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
|
|
|
|
const CountedRegion &Region) {
|
|
|
|
if (Segments.empty())
|
|
|
|
Segments.emplace_back(Line, Col, IsRegionEntry);
|
|
|
|
CoverageSegment S = Segments.back();
|
|
|
|
// Avoid creating empty regions.
|
|
|
|
if (S.Line != Line || S.Col != Col) {
|
|
|
|
Segments.emplace_back(Line, Col, IsRegionEntry);
|
|
|
|
S = Segments.back();
|
|
|
|
}
|
2014-09-25 00:34:18 +00:00
|
|
|
DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
|
2014-09-20 15:31:56 +00:00
|
|
|
// Set this region's count.
|
2014-09-25 00:34:18 +00:00
|
|
|
if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
|
|
|
|
DEBUG(dbgs() << " with count " << Region.ExecutionCount);
|
2014-09-20 15:31:56 +00:00
|
|
|
Segments.back().setCount(Region.ExecutionCount);
|
2014-09-25 00:34:18 +00:00
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "\n");
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a segment for the given region.
|
|
|
|
void startSegment(const CountedRegion &Region) {
|
|
|
|
startSegment(Region.LineStart, Region.ColumnStart, true, Region);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pop the top region off of the active stack, starting a new segment with
|
|
|
|
/// the containing Region's count.
|
|
|
|
void popRegion() {
|
|
|
|
const CountedRegion *Active = ActiveRegions.back();
|
|
|
|
unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
|
|
|
|
ActiveRegions.pop_back();
|
|
|
|
if (ActiveRegions.empty())
|
|
|
|
startSegment(Line, Col);
|
|
|
|
else
|
|
|
|
startSegment(Line, Col, false, *ActiveRegions.back());
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Build a list of CoverageSegments from a sorted list of Regions.
|
|
|
|
std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
|
2015-01-24 20:58:52 +00:00
|
|
|
const CountedRegion *PrevRegion = nullptr;
|
2014-09-20 15:31:56 +00:00
|
|
|
for (const auto &Region : Regions) {
|
|
|
|
// Pop any regions that end before this one starts.
|
|
|
|
while (!ActiveRegions.empty() &&
|
|
|
|
ActiveRegions.back()->endLoc() <= Region.startLoc())
|
|
|
|
popRegion();
|
2015-01-24 20:58:52 +00:00
|
|
|
if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() &&
|
|
|
|
PrevRegion->endLoc() == Region.endLoc()) {
|
2015-02-18 19:01:06 +00:00
|
|
|
if (Region.Kind == coverage::CounterMappingRegion::CodeRegion)
|
2014-09-25 00:34:18 +00:00
|
|
|
Segments.back().addCount(Region.ExecutionCount);
|
|
|
|
} else {
|
|
|
|
// Add this region to the stack.
|
|
|
|
ActiveRegions.push_back(&Region);
|
|
|
|
startSegment(Region);
|
|
|
|
}
|
2015-01-24 20:58:52 +00:00
|
|
|
PrevRegion = &Region;
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
// Pop any regions that are left in the stack.
|
|
|
|
while (!ActiveRegions.empty())
|
|
|
|
popRegion();
|
|
|
|
return Segments;
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2014-09-20 15:31:56 +00:00
|
|
|
|
2014-11-14 01:50:32 +00:00
|
|
|
std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
|
2014-09-20 15:31:56 +00:00
|
|
|
std::vector<StringRef> Filenames;
|
|
|
|
for (const auto &Function : getCoveredFunctions())
|
2015-02-17 15:29:18 +00:00
|
|
|
Filenames.insert(Filenames.end(), Function.Filenames.begin(),
|
|
|
|
Function.Filenames.end());
|
2014-09-20 15:31:56 +00:00
|
|
|
std::sort(Filenames.begin(), Filenames.end());
|
|
|
|
auto Last = std::unique(Filenames.begin(), Filenames.end());
|
|
|
|
Filenames.erase(Last, Filenames.end());
|
|
|
|
return Filenames;
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:18:07 +00:00
|
|
|
static SmallBitVector gatherFileIDs(StringRef SourceFile,
|
|
|
|
const FunctionRecord &Function) {
|
|
|
|
SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
|
2014-09-20 15:31:56 +00:00
|
|
|
for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
|
|
|
|
if (SourceFile == Function.Filenames[I])
|
|
|
|
FilenameEquivalence[I] = true;
|
2015-02-12 16:18:07 +00:00
|
|
|
return FilenameEquivalence;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
|
|
|
|
const FunctionRecord &Function) {
|
|
|
|
SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
|
|
|
|
SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function);
|
2014-09-20 15:31:56 +00:00
|
|
|
for (const auto &CR : Function.CountedRegions)
|
|
|
|
if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
|
|
|
|
FilenameEquivalence[CR.FileID])
|
2015-02-12 16:18:07 +00:00
|
|
|
IsNotExpandedFile[CR.ExpandedFileID] = false;
|
|
|
|
IsNotExpandedFile &= FilenameEquivalence;
|
|
|
|
int I = IsNotExpandedFile.find_first();
|
2015-02-20 07:28:28 +00:00
|
|
|
if (I == -1)
|
|
|
|
return None;
|
|
|
|
return I;
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
|
2015-02-12 16:30:00 +00:00
|
|
|
SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
|
2014-09-20 15:31:56 +00:00
|
|
|
for (const auto &CR : Function.CountedRegions)
|
|
|
|
if (CR.Kind == CounterMappingRegion::ExpansionRegion)
|
2015-02-12 16:30:00 +00:00
|
|
|
IsNotExpandedFile[CR.ExpandedFileID] = false;
|
2015-02-12 16:18:07 +00:00
|
|
|
int I = IsNotExpandedFile.find_first();
|
2015-02-20 07:28:28 +00:00
|
|
|
if (I == -1)
|
|
|
|
return None;
|
|
|
|
return I;
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sort a nested sequence of regions from a single file.
|
|
|
|
template <class It> static void sortNestedRegions(It First, It Last) {
|
|
|
|
std::sort(First, Last,
|
|
|
|
[](const CountedRegion &LHS, const CountedRegion &RHS) {
|
|
|
|
if (LHS.startLoc() == RHS.startLoc())
|
|
|
|
// When LHS completely contains RHS, we sort LHS first.
|
|
|
|
return RHS.endLoc() < LHS.endLoc();
|
|
|
|
return LHS.startLoc() < RHS.startLoc();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isExpansion(const CountedRegion &R, unsigned FileID) {
|
|
|
|
return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
|
|
|
|
CoverageData FileCoverage(Filename);
|
|
|
|
std::vector<coverage::CountedRegion> Regions;
|
|
|
|
|
|
|
|
for (const auto &Function : Functions) {
|
|
|
|
auto MainFileID = findMainViewFileID(Filename, Function);
|
|
|
|
if (!MainFileID)
|
|
|
|
continue;
|
|
|
|
auto FileIDs = gatherFileIDs(Filename, Function);
|
|
|
|
for (const auto &CR : Function.CountedRegions)
|
2015-02-12 16:18:07 +00:00
|
|
|
if (FileIDs.test(CR.FileID)) {
|
2014-09-20 15:31:56 +00:00
|
|
|
Regions.push_back(CR);
|
|
|
|
if (isExpansion(CR, *MainFileID))
|
|
|
|
FileCoverage.Expansions.emplace_back(CR, Function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortNestedRegions(Regions.begin(), Regions.end());
|
2015-01-24 20:58:52 +00:00
|
|
|
DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
|
2014-09-20 15:31:56 +00:00
|
|
|
FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
|
|
|
|
|
|
|
|
return FileCoverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const FunctionRecord *>
|
|
|
|
CoverageMapping::getInstantiations(StringRef Filename) {
|
|
|
|
FunctionInstantiationSetCollector InstantiationSetCollector;
|
|
|
|
for (const auto &Function : Functions) {
|
|
|
|
auto MainFileID = findMainViewFileID(Filename, Function);
|
|
|
|
if (!MainFileID)
|
|
|
|
continue;
|
|
|
|
InstantiationSetCollector.insert(Function, *MainFileID);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const FunctionRecord *> Result;
|
|
|
|
for (const auto &InstantiationSet : InstantiationSetCollector) {
|
|
|
|
if (InstantiationSet.second.size() < 2)
|
|
|
|
continue;
|
2015-02-17 15:29:18 +00:00
|
|
|
Result.insert(Result.end(), InstantiationSet.second.begin(),
|
|
|
|
InstantiationSet.second.end());
|
2014-09-20 15:31:56 +00:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverageData
|
|
|
|
CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
|
|
|
|
auto MainFileID = findMainViewFileID(Function);
|
|
|
|
if (!MainFileID)
|
|
|
|
return CoverageData();
|
|
|
|
|
|
|
|
CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
|
|
|
|
std::vector<coverage::CountedRegion> Regions;
|
|
|
|
for (const auto &CR : Function.CountedRegions)
|
|
|
|
if (CR.FileID == *MainFileID) {
|
|
|
|
Regions.push_back(CR);
|
|
|
|
if (isExpansion(CR, *MainFileID))
|
|
|
|
FunctionCoverage.Expansions.emplace_back(CR, Function);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortNestedRegions(Regions.begin(), Regions.end());
|
2015-01-24 20:58:52 +00:00
|
|
|
DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
|
2014-09-20 15:31:56 +00:00
|
|
|
FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
|
|
|
|
|
|
|
|
return FunctionCoverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverageData
|
|
|
|
CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
|
|
|
|
CoverageData ExpansionCoverage(
|
|
|
|
Expansion.Function.Filenames[Expansion.FileID]);
|
|
|
|
std::vector<coverage::CountedRegion> Regions;
|
|
|
|
for (const auto &CR : Expansion.Function.CountedRegions)
|
|
|
|
if (CR.FileID == Expansion.FileID) {
|
|
|
|
Regions.push_back(CR);
|
|
|
|
if (isExpansion(CR, Expansion.FileID))
|
|
|
|
ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortNestedRegions(Regions.begin(), Regions.end());
|
2015-01-24 20:58:52 +00:00
|
|
|
DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
|
|
|
|
<< "\n");
|
2014-09-20 15:31:56 +00:00
|
|
|
ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
|
|
|
|
|
|
|
|
return ExpansionCoverage;
|
|
|
|
}
|
2015-05-06 23:19:35 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CoverageMappingErrorCategoryType : public std::error_category {
|
|
|
|
const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
|
|
|
|
std::string message(int IE) const override {
|
|
|
|
auto E = static_cast<coveragemap_error>(IE);
|
|
|
|
switch (E) {
|
|
|
|
case coveragemap_error::success:
|
|
|
|
return "Success";
|
|
|
|
case coveragemap_error::eof:
|
|
|
|
return "End of File";
|
|
|
|
case coveragemap_error::no_data_found:
|
|
|
|
return "No coverage data found";
|
|
|
|
case coveragemap_error::unsupported_version:
|
|
|
|
return "Unsupported coverage format version";
|
|
|
|
case coveragemap_error::truncated:
|
|
|
|
return "Truncated coverage data";
|
|
|
|
case coveragemap_error::malformed:
|
|
|
|
return "Malformed coverage data";
|
|
|
|
}
|
|
|
|
llvm_unreachable("A value of coveragemap_error has no message.");
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2015-05-06 23:19:35 +00:00
|
|
|
|
|
|
|
static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
|
|
|
|
|
|
|
|
const std::error_category &llvm::coveragemap_category() {
|
|
|
|
return *ErrorCategory;
|
|
|
|
}
|