2017-06-22 07:19:47 +08:00
|
|
|
//===- CoverageMapping.cpp - Code coverage mapping support ----------------===//
|
2014-07-25 07:57:54 +08:00
|
|
|
//
|
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
|
2014-07-25 07:57:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for clang's and llvm's instrumentation based
|
|
|
|
// code coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2014-09-20 23:31:56 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2014-09-20 23:31:56 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-02-13 00:18:07 +08:00
|
|
|
#include "llvm/ADT/SmallBitVector.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-04-30 02:53:05 +08:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
|
2014-09-20 23:31:56 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2014-09-25 08:34:18 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-06-14 01:23:04 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2014-09-09 05:04:00 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-05-07 07:19:35 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
2017-10-25 06:35:29 +08:00
|
|
|
#include <map>
|
2017-03-03 09:07:34 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-07-25 07:57:54 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace coverage;
|
|
|
|
|
2014-09-25 08:34:18 +08:00
|
|
|
#define DEBUG_TYPE "coverage-mapping"
|
|
|
|
|
2014-07-25 07:57:54 +08:00
|
|
|
Counter CounterExpressionBuilder::get(const CounterExpression &E) {
|
2014-10-03 01:14:18 +08:00
|
|
|
auto It = ExpressionIndices.find(E);
|
|
|
|
if (It != ExpressionIndices.end())
|
|
|
|
return Counter::getExpression(It->second);
|
|
|
|
unsigned I = Expressions.size();
|
2014-07-25 07:57:54 +08:00
|
|
|
Expressions.push_back(E);
|
2014-10-03 01:14:18 +08:00
|
|
|
ExpressionIndices[E] = I;
|
|
|
|
return Counter::getExpression(I);
|
2014-07-25 07:57:54 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 06:33:06 +08:00
|
|
|
void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
|
|
|
|
SmallVectorImpl<Term> &Terms) {
|
2014-07-25 07:57:54 +08:00
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
break;
|
|
|
|
case Counter::CounterValueReference:
|
2017-06-27 06:33:06 +08:00
|
|
|
Terms.emplace_back(C.getCounterID(), Factor);
|
2014-07-25 07:57:54 +08:00
|
|
|
break;
|
|
|
|
case Counter::Expression:
|
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
2017-06-27 06:33:06 +08:00
|
|
|
extractTerms(E.LHS, Factor, Terms);
|
|
|
|
extractTerms(
|
|
|
|
E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
|
2014-07-25 07:57:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
|
|
|
|
// Gather constant terms.
|
2017-06-27 06:33:06 +08:00
|
|
|
SmallVector<Term, 32> Terms;
|
2014-10-03 00:43:31 +08:00
|
|
|
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.
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
|
2017-06-27 06:33:06 +08:00
|
|
|
return LHS.CounterID < RHS.CounterID;
|
2014-10-03 00:43:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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) {
|
2017-06-27 06:33:06 +08:00
|
|
|
if (I->CounterID == Prev->CounterID) {
|
|
|
|
Prev->Factor += I->Factor;
|
2014-10-03 00:43:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++Prev;
|
|
|
|
*Prev = *I;
|
|
|
|
}
|
|
|
|
Terms.erase(++Prev, Terms.end());
|
2014-07-25 07:57:54 +08:00
|
|
|
|
|
|
|
Counter C;
|
2014-10-03 00:43:31 +08:00
|
|
|
// Create additions. We do this before subtractions to avoid constructs like
|
|
|
|
// ((0 - X) + Y), as opposed to (Y - X).
|
2017-06-27 06:33:06 +08:00
|
|
|
for (auto T : Terms) {
|
|
|
|
if (T.Factor <= 0)
|
2014-07-25 07:57:54 +08:00
|
|
|
continue;
|
2017-06-27 06:33:06 +08:00
|
|
|
for (int I = 0; I < T.Factor; ++I)
|
2014-07-25 07:57:54 +08:00
|
|
|
if (C.isZero())
|
2017-06-27 06:33:06 +08:00
|
|
|
C = Counter::getCounter(T.CounterID);
|
2014-07-25 07:57:54 +08:00
|
|
|
else
|
|
|
|
C = get(CounterExpression(CounterExpression::Add, C,
|
2017-06-27 06:33:06 +08:00
|
|
|
Counter::getCounter(T.CounterID)));
|
2014-07-25 07:57:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create subtractions.
|
2017-06-27 06:33:06 +08:00
|
|
|
for (auto T : Terms) {
|
|
|
|
if (T.Factor >= 0)
|
2014-07-25 07:57:54 +08:00
|
|
|
continue;
|
2017-06-27 06:33:06 +08:00
|
|
|
for (int I = 0; I < -T.Factor; ++I)
|
2014-07-25 07:57:54 +08:00
|
|
|
C = get(CounterExpression(CounterExpression::Subtract, C,
|
2017-06-27 06:33:06 +08:00
|
|
|
Counter::getCounter(T.CounterID)));
|
2014-07-25 07:57:54 +08: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)));
|
|
|
|
}
|
|
|
|
|
2017-03-03 09:07:34 +08:00
|
|
|
void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
|
2014-07-25 07:57:54 +08:00
|
|
|
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-30 03:58:16 +08:00
|
|
|
dump(E.LHS, OS);
|
2014-07-25 07:57:54 +08:00
|
|
|
OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
|
2014-07-30 03:58:16 +08:00
|
|
|
dump(E.RHS, OS);
|
2014-07-25 07:57:54 +08:00
|
|
|
OS << ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CounterValues.empty())
|
|
|
|
return;
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<int64_t> Value = evaluate(C);
|
|
|
|
if (auto E = Value.takeError()) {
|
2017-03-03 09:07:34 +08:00
|
|
|
consumeError(std::move(E));
|
2014-07-25 07:57:54 +08:00
|
|
|
return;
|
2016-05-19 11:54:45 +08:00
|
|
|
}
|
2014-09-09 05:04:00 +08:00
|
|
|
OS << '[' << *Value << ']';
|
2014-07-25 07:57:54 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
|
2014-07-25 07:57:54 +08:00
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
return 0;
|
|
|
|
case Counter::CounterValueReference:
|
2014-09-09 05:04:00 +08:00
|
|
|
if (C.getCounterID() >= CounterValues.size())
|
2016-05-19 11:54:45 +08:00
|
|
|
return errorCodeToError(errc::argument_out_of_domain);
|
2014-07-25 07:57:54 +08:00
|
|
|
return CounterValues[C.getCounterID()];
|
|
|
|
case Counter::Expression: {
|
2014-09-09 05:04:00 +08:00
|
|
|
if (C.getExpressionID() >= Expressions.size())
|
2016-05-19 11:54:45 +08:00
|
|
|
return errorCodeToError(errc::argument_out_of_domain);
|
2014-07-25 07:57:54 +08:00
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<int64_t> LHS = evaluate(E.LHS);
|
2014-09-09 05:04:00 +08:00
|
|
|
if (!LHS)
|
|
|
|
return LHS;
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<int64_t> RHS = evaluate(E.RHS);
|
2014-09-09 05:04:00 +08:00
|
|
|
if (!RHS)
|
|
|
|
return RHS;
|
|
|
|
return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
|
2014-07-25 07:57:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-09 05:04:00 +08:00
|
|
|
llvm_unreachable("Unhandled CounterKind");
|
2014-07-25 07:57:54 +08:00
|
|
|
}
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2021-05-04 02:57:14 +08:00
|
|
|
unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
|
|
|
|
switch (C.getKind()) {
|
|
|
|
case Counter::Zero:
|
|
|
|
return 0;
|
|
|
|
case Counter::CounterValueReference:
|
|
|
|
return C.getCounterID();
|
|
|
|
case Counter::Expression: {
|
|
|
|
if (C.getExpressionID() >= Expressions.size())
|
|
|
|
return 0;
|
|
|
|
const auto &E = Expressions[C.getExpressionID()];
|
|
|
|
return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unhandled CounterKind");
|
|
|
|
}
|
|
|
|
|
2014-11-14 09:50:32 +08:00
|
|
|
void FunctionRecordIterator::skipOtherFiles() {
|
|
|
|
while (Current != Records.end() && !Filename.empty() &&
|
|
|
|
Filename != Current->Filenames[0])
|
|
|
|
++Current;
|
|
|
|
if (Current == Records.end())
|
|
|
|
*this = FunctionRecordIterator();
|
|
|
|
}
|
|
|
|
|
2019-09-17 03:08:44 +08:00
|
|
|
ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
|
|
|
|
StringRef Filename) const {
|
|
|
|
size_t FilenameHash = hash_value(Filename);
|
|
|
|
auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
|
|
|
|
if (RecordIt == FilenameHash2RecordIndices.end())
|
|
|
|
return {};
|
|
|
|
return RecordIt->second;
|
|
|
|
}
|
|
|
|
|
2021-05-04 02:57:14 +08:00
|
|
|
static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
|
|
|
|
const CoverageMappingRecord &Record) {
|
|
|
|
unsigned MaxCounterID = 0;
|
|
|
|
for (const auto &Region : Record.MappingRegions) {
|
|
|
|
MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
|
|
|
|
}
|
|
|
|
return MaxCounterID;
|
|
|
|
}
|
|
|
|
|
2016-10-13 06:27:45 +08:00
|
|
|
Error CoverageMapping::loadFunctionRecord(
|
|
|
|
const CoverageMappingRecord &Record,
|
|
|
|
IndexedInstrProfReader &ProfileReader) {
|
2016-10-15 01:16:53 +08:00
|
|
|
StringRef OrigFuncName = Record.FunctionName;
|
2017-06-20 10:05:35 +08:00
|
|
|
if (OrigFuncName.empty())
|
|
|
|
return make_error<CoverageMapError>(coveragemap_error::malformed);
|
|
|
|
|
2016-10-15 01:16:53 +08:00
|
|
|
if (Record.Filenames.empty())
|
|
|
|
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
|
|
|
|
else
|
|
|
|
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
|
|
|
|
|
2016-10-13 06:27:45 +08:00
|
|
|
CounterMappingContext Ctx(Record.Expressions);
|
|
|
|
|
|
|
|
std::vector<uint64_t> Counts;
|
|
|
|
if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
|
|
|
|
Record.FunctionHash, Counts)) {
|
|
|
|
instrprof_error IPE = InstrProfError::take(std::move(E));
|
|
|
|
if (IPE == instrprof_error::hash_mismatch) {
|
2020-01-29 09:09:24 +08:00
|
|
|
FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
|
|
|
|
Record.FunctionHash);
|
2016-10-13 06:27:45 +08:00
|
|
|
return Error::success();
|
|
|
|
} else if (IPE != instrprof_error::unknown_function)
|
|
|
|
return make_error<InstrProfError>(IPE);
|
2021-05-04 02:57:14 +08:00
|
|
|
Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
|
2016-10-13 06:27:45 +08:00
|
|
|
}
|
|
|
|
Ctx.setCounts(Counts);
|
|
|
|
|
|
|
|
assert(!Record.MappingRegions.empty() && "Function has no regions");
|
|
|
|
|
2018-08-08 06:25:36 +08:00
|
|
|
// This coverage record is a zero region for a function that's unused in
|
|
|
|
// some TU, but used in a different TU. Ignore it. The coverage maps from the
|
|
|
|
// the other TU will either be loaded (providing full region counts) or they
|
|
|
|
// won't (in which case we don't unintuitively report functions as uncovered
|
|
|
|
// when they have non-zero counts in the profile).
|
|
|
|
if (Record.MappingRegions.size() == 1 &&
|
|
|
|
Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
|
|
|
|
return Error::success();
|
|
|
|
|
2016-10-13 06:27:45 +08:00
|
|
|
FunctionRecord Function(OrigFuncName, Record.Filenames);
|
|
|
|
for (const auto &Region : Record.MappingRegions) {
|
|
|
|
Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
|
|
|
|
if (auto E = ExecutionCount.takeError()) {
|
2017-03-03 09:07:34 +08:00
|
|
|
consumeError(std::move(E));
|
2016-10-13 06:27:45 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
2020-12-29 01:20:48 +08:00
|
|
|
Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
|
|
|
|
if (auto E = AltExecutionCount.takeError()) {
|
|
|
|
consumeError(std::move(E));
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
|
2016-10-13 06:27:45 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 06:25:36 +08:00
|
|
|
// Don't create records for (filenames, function) pairs we've already seen.
|
|
|
|
auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
|
|
|
|
Record.Filenames.end());
|
|
|
|
if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
|
|
|
|
return Error::success();
|
|
|
|
|
2016-10-13 06:27:45 +08:00
|
|
|
Functions.push_back(std::move(Function));
|
2019-09-17 03:08:44 +08:00
|
|
|
|
|
|
|
// Performance optimization: keep track of the indices of the function records
|
|
|
|
// which correspond to each filename. This can be used to substantially speed
|
|
|
|
// up queries for coverage info in a file.
|
|
|
|
unsigned RecordIndex = Functions.size() - 1;
|
|
|
|
for (StringRef Filename : Record.Filenames) {
|
|
|
|
auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
|
|
|
|
// Note that there may be duplicates in the filename set for a function
|
|
|
|
// record, because of e.g. macro expansions in the function in which both
|
|
|
|
// the macro and the function are defined in the same file.
|
|
|
|
if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
|
|
|
|
RecordIndices.push_back(RecordIndex);
|
|
|
|
}
|
|
|
|
|
2016-10-13 06:27:45 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:59:45 +08:00
|
|
|
// This function is for memory optimization by shortening the lifetimes
|
|
|
|
// of CoverageMappingReader instances.
|
|
|
|
Error CoverageMapping::loadFromReaders(
|
2016-10-15 01:16:53 +08:00
|
|
|
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
|
2021-03-24 06:59:45 +08:00
|
|
|
IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
|
2017-09-09 02:44:47 +08:00
|
|
|
for (const auto &CoverageReader : CoverageReaders) {
|
|
|
|
for (auto RecordOrErr : *CoverageReader) {
|
|
|
|
if (Error E = RecordOrErr.takeError())
|
2021-03-24 06:59:45 +08:00
|
|
|
return E;
|
2017-09-09 02:44:47 +08:00
|
|
|
const auto &Record = *RecordOrErr;
|
2021-03-24 06:59:45 +08:00
|
|
|
if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
|
|
|
|
return E;
|
2017-09-09 02:44:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-24 06:59:45 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
2016-10-15 01:16:53 +08:00
|
|
|
|
2021-03-24 06:59:45 +08:00
|
|
|
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
|
|
|
|
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
|
|
|
|
IndexedInstrProfReader &ProfileReader) {
|
|
|
|
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
|
|
|
|
if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
|
|
|
|
return std::move(E);
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Coverage);
|
2016-10-15 01:16:53 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 04:35:50 +08:00
|
|
|
// If E is a no_data_found error, returns success. Otherwise returns E.
|
|
|
|
static Error handleMaybeNoDataFoundError(Error E) {
|
|
|
|
return handleErrors(
|
|
|
|
std::move(E), [](const CoverageMapError &CME) {
|
|
|
|
if (CME.get() == coveragemap_error::no_data_found)
|
|
|
|
return static_cast<Error>(Error::success());
|
|
|
|
return make_error<CoverageMapError>(CME.get());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<CoverageMapping>>
|
2016-10-15 01:16:53 +08:00
|
|
|
CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
|
2021-04-10 02:53:59 +08:00
|
|
|
StringRef ProfileFilename, ArrayRef<StringRef> Arches,
|
|
|
|
StringRef CompilationDir) {
|
2015-02-17 05:28:58 +08:00
|
|
|
auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = ProfileReaderOrErr.takeError())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2015-02-17 05:28:58 +08:00
|
|
|
auto ProfileReader = std::move(ProfileReaderOrErr.get());
|
2021-03-24 06:59:45 +08:00
|
|
|
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
|
|
|
|
bool DataFound = false;
|
2016-10-15 01:16:53 +08:00
|
|
|
|
2017-08-02 05:23:26 +08:00
|
|
|
for (const auto &File : llvm::enumerate(ObjectFilenames)) {
|
2021-03-24 06:59:45 +08:00
|
|
|
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
|
2021-03-25 23:55:30 +08:00
|
|
|
File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
|
2016-10-15 01:16:53 +08:00
|
|
|
if (std::error_code EC = CovMappingBufOrErr.getError())
|
|
|
|
return errorCodeToError(EC);
|
2017-08-02 05:23:26 +08:00
|
|
|
StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()];
|
2019-06-14 04:48:57 +08:00
|
|
|
MemoryBufferRef CovMappingBufRef =
|
|
|
|
CovMappingBufOrErr.get()->getMemBufferRef();
|
2021-03-24 06:59:45 +08:00
|
|
|
SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
|
2021-04-10 02:53:59 +08:00
|
|
|
auto CoverageReadersOrErr = BinaryCoverageReader::create(
|
|
|
|
CovMappingBufRef, Arch, Buffers, CompilationDir);
|
2019-08-29 04:35:50 +08:00
|
|
|
if (Error E = CoverageReadersOrErr.takeError()) {
|
|
|
|
E = handleMaybeNoDataFoundError(std::move(E));
|
|
|
|
if (E)
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2019-08-29 04:35:50 +08:00
|
|
|
// E == success (originally a no_data_found error).
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-24 06:59:45 +08:00
|
|
|
|
|
|
|
SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
|
2019-06-14 04:48:57 +08:00
|
|
|
for (auto &Reader : CoverageReadersOrErr.get())
|
|
|
|
Readers.push_back(std::move(Reader));
|
2021-03-24 06:59:45 +08:00
|
|
|
DataFound |= !Readers.empty();
|
|
|
|
if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage))
|
|
|
|
return std::move(E);
|
2016-10-15 01:16:53 +08:00
|
|
|
}
|
2019-08-29 04:35:50 +08:00
|
|
|
// If no readers were created, either no objects were provided or none of them
|
|
|
|
// had coverage data. Return an error in the latter case.
|
2021-03-24 06:59:45 +08:00
|
|
|
if (!DataFound && !ObjectFilenames.empty())
|
2019-08-29 04:35:50 +08:00
|
|
|
return make_error<CoverageMapError>(coveragemap_error::no_data_found);
|
2021-03-24 06:59:45 +08:00
|
|
|
return std::move(Coverage);
|
2014-09-21 01:19:52 +08:00
|
|
|
}
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
namespace {
|
2017-03-03 09:07:34 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Distributes functions into instantiation sets.
|
2014-09-20 23:31:56 +08:00
|
|
|
///
|
|
|
|
/// An instantiation set is a collection of functions that have the same source
|
|
|
|
/// code, ie, template functions specializations.
|
|
|
|
class FunctionInstantiationSetCollector {
|
2017-10-25 06:35:29 +08:00
|
|
|
using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
|
2014-09-20 23:31:56 +08:00
|
|
|
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 {
|
2016-04-14 17:10:00 +08:00
|
|
|
std::vector<CoverageSegment> &Segments;
|
2014-09-20 23:31:56 +08:00
|
|
|
SmallVector<const CountedRegion *, 8> ActiveRegions;
|
|
|
|
|
2016-04-14 17:10:00 +08:00
|
|
|
SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
|
|
|
|
|
2017-09-09 02:44:50 +08:00
|
|
|
/// Emit a segment with the count from \p Region starting at \p StartLoc.
|
|
|
|
//
|
2017-09-19 07:37:28 +08:00
|
|
|
/// \p IsRegionEntry: The segment is at the start of a new non-gap region.
|
2017-09-09 02:44:50 +08:00
|
|
|
/// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
|
|
|
|
void startSegment(const CountedRegion &Region, LineColPair StartLoc,
|
|
|
|
bool IsRegionEntry, bool EmitSkippedRegion = false) {
|
|
|
|
bool HasCount = !EmitSkippedRegion &&
|
|
|
|
(Region.Kind != CounterMappingRegion::SkippedRegion);
|
|
|
|
|
|
|
|
// If the new segment wouldn't affect coverage rendering, skip it.
|
|
|
|
if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
|
|
|
|
const auto &Last = Segments.back();
|
|
|
|
if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
|
|
|
|
!Last.IsRegionEntry)
|
|
|
|
return;
|
|
|
|
}
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2017-09-09 02:44:50 +08:00
|
|
|
if (HasCount)
|
|
|
|
Segments.emplace_back(StartLoc.first, StartLoc.second,
|
2017-09-19 07:37:28 +08:00
|
|
|
Region.ExecutionCount, IsRegionEntry,
|
|
|
|
Region.Kind == CounterMappingRegion::GapRegion);
|
2017-09-09 02:44:50 +08:00
|
|
|
else
|
|
|
|
Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-09-09 02:44:50 +08:00
|
|
|
const auto &Last = Segments.back();
|
|
|
|
dbgs() << "Segment at " << Last.Line << ":" << Last.Col
|
|
|
|
<< " (count = " << Last.Count << ")"
|
|
|
|
<< (Last.IsRegionEntry ? ", RegionEntry" : "")
|
2017-09-19 07:37:28 +08:00
|
|
|
<< (!Last.HasCount ? ", Skipped" : "")
|
|
|
|
<< (Last.IsGapRegion ? ", Gap" : "") << "\n";
|
2017-09-09 02:44:50 +08:00
|
|
|
});
|
2014-09-20 23:31:56 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 02:44:50 +08:00
|
|
|
/// Emit segments for active regions which end before \p Loc.
|
|
|
|
///
|
|
|
|
/// \p Loc: The start location of the next region. If None, all active
|
|
|
|
/// regions are completed.
|
|
|
|
/// \p FirstCompletedRegion: Index of the first completed region.
|
|
|
|
void completeRegionsUntil(Optional<LineColPair> Loc,
|
|
|
|
unsigned FirstCompletedRegion) {
|
|
|
|
// Sort the completed regions by end location. This makes it simple to
|
|
|
|
// emit closing segments in sorted order.
|
|
|
|
auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
|
|
|
|
std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
|
|
|
|
[](const CountedRegion *L, const CountedRegion *R) {
|
|
|
|
return L->endLoc() < R->endLoc();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Emit segments for all completed regions.
|
|
|
|
for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
|
|
|
|
++I) {
|
|
|
|
const auto *CompletedRegion = ActiveRegions[I];
|
|
|
|
assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
|
|
|
|
"Completed region ends after start of new region");
|
|
|
|
|
|
|
|
const auto *PrevCompletedRegion = ActiveRegions[I - 1];
|
|
|
|
auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
|
|
|
|
|
|
|
|
// Don't emit any more segments if they start where the new region begins.
|
|
|
|
if (Loc && CompletedSegmentLoc == *Loc)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't emit a segment if the next completed region ends at the same
|
|
|
|
// location as this one.
|
|
|
|
if (CompletedSegmentLoc == CompletedRegion->endLoc())
|
|
|
|
continue;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2017-12-07 08:01:15 +08:00
|
|
|
// Use the count from the last completed region which ends at this loc.
|
|
|
|
for (unsigned J = I + 1; J < E; ++J)
|
|
|
|
if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
|
|
|
|
CompletedRegion = ActiveRegions[J];
|
2017-11-30 08:28:23 +08:00
|
|
|
|
2017-09-09 02:44:50 +08:00
|
|
|
startSegment(*CompletedRegion, CompletedSegmentLoc, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Last = ActiveRegions.back();
|
|
|
|
if (FirstCompletedRegion && Last->endLoc() != *Loc) {
|
|
|
|
// If there's a gap after the end of the last completed region and the
|
|
|
|
// start of the new region, use the last active region to fill the gap.
|
|
|
|
startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
|
|
|
|
false);
|
|
|
|
} else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
|
|
|
|
// Emit a skipped segment if there are no more active regions. This
|
|
|
|
// ensures that gaps between functions are marked correctly.
|
|
|
|
startSegment(*Last, Last->endLoc(), false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop the completed regions.
|
|
|
|
ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
|
2014-09-20 23:31:56 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:10:00 +08:00
|
|
|
void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
|
2017-09-09 02:44:50 +08:00
|
|
|
for (const auto &CR : enumerate(Regions)) {
|
|
|
|
auto CurStartLoc = CR.value().startLoc();
|
|
|
|
|
|
|
|
// Active regions which end before the current region need to be popped.
|
|
|
|
auto CompletedRegions =
|
|
|
|
std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
|
|
|
|
[&](const CountedRegion *Region) {
|
|
|
|
return !(Region->endLoc() <= CurStartLoc);
|
|
|
|
});
|
|
|
|
if (CompletedRegions != ActiveRegions.end()) {
|
|
|
|
unsigned FirstCompletedRegion =
|
|
|
|
std::distance(ActiveRegions.begin(), CompletedRegions);
|
|
|
|
completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
|
|
|
|
}
|
|
|
|
|
2017-09-19 07:37:28 +08:00
|
|
|
bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
|
|
|
|
|
2017-09-09 02:44:50 +08:00
|
|
|
// Try to emit a segment for the current region.
|
|
|
|
if (CurStartLoc == CR.value().endLoc()) {
|
|
|
|
// Avoid making zero-length regions active. If it's the last region,
|
|
|
|
// emit a skipped segment. Otherwise use its predecessor's count.
|
2020-09-03 05:50:32 +08:00
|
|
|
const bool Skipped =
|
|
|
|
(CR.index() + 1) == Regions.size() ||
|
|
|
|
CR.value().Kind == CounterMappingRegion::SkippedRegion;
|
2017-09-09 02:44:50 +08:00
|
|
|
startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
|
2017-09-19 07:37:28 +08:00
|
|
|
CurStartLoc, !GapRegion, Skipped);
|
2020-09-03 05:50:32 +08:00
|
|
|
// If it is skipped segment, create a segment with last pushed
|
|
|
|
// regions's count at CurStartLoc.
|
|
|
|
if (Skipped && !ActiveRegions.empty())
|
|
|
|
startSegment(*ActiveRegions.back(), CurStartLoc, false);
|
2017-09-09 02:44:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (CR.index() + 1 == Regions.size() ||
|
|
|
|
CurStartLoc != Regions[CR.index() + 1].startLoc()) {
|
|
|
|
// Emit a segment if the next region doesn't start at the same location
|
|
|
|
// as this one.
|
2017-09-19 07:37:28 +08:00
|
|
|
startSegment(CR.value(), CurStartLoc, !GapRegion);
|
2017-09-09 02:44:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This region is active (i.e not completed).
|
|
|
|
ActiveRegions.push_back(&CR.value());
|
2014-09-20 23:31:56 +08:00
|
|
|
}
|
2017-09-09 02:44:50 +08:00
|
|
|
|
|
|
|
// Complete any remaining active regions.
|
|
|
|
if (!ActiveRegions.empty())
|
|
|
|
completeRegionsUntil(None, 0);
|
2016-04-14 17:10:00 +08:00
|
|
|
}
|
|
|
|
|
2016-04-25 17:43:37 +08:00
|
|
|
/// Sort a nested sequence of regions from a single file.
|
|
|
|
static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
|
2016-05-05 17:39:45 +08:00
|
|
|
if (LHS.startLoc() != RHS.startLoc())
|
|
|
|
return LHS.startLoc() < RHS.startLoc();
|
|
|
|
if (LHS.endLoc() != RHS.endLoc())
|
|
|
|
// When LHS completely contains RHS, we sort LHS first.
|
|
|
|
return RHS.endLoc() < LHS.endLoc();
|
|
|
|
// If LHS and RHS cover the same area, we need to sort them according
|
|
|
|
// to their kinds so that the most suitable region will become "active"
|
|
|
|
// in combineRegions(). Because we accumulate counter values only from
|
|
|
|
// regions of the same kind as the first region of the area, prefer
|
|
|
|
// CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
|
2017-03-03 09:07:34 +08:00
|
|
|
static_assert(CounterMappingRegion::CodeRegion <
|
|
|
|
CounterMappingRegion::ExpansionRegion &&
|
|
|
|
CounterMappingRegion::ExpansionRegion <
|
|
|
|
CounterMappingRegion::SkippedRegion,
|
2016-05-05 17:39:45 +08:00
|
|
|
"Unexpected order of region kind values");
|
|
|
|
return LHS.Kind < RHS.Kind;
|
|
|
|
});
|
2016-04-25 17:43:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine counts of regions which cover the same area.
|
|
|
|
static ArrayRef<CountedRegion>
|
|
|
|
combineRegions(MutableArrayRef<CountedRegion> Regions) {
|
|
|
|
if (Regions.empty())
|
|
|
|
return Regions;
|
|
|
|
auto Active = Regions.begin();
|
|
|
|
auto End = Regions.end();
|
|
|
|
for (auto I = Regions.begin() + 1; I != End; ++I) {
|
|
|
|
if (Active->startLoc() != I->startLoc() ||
|
|
|
|
Active->endLoc() != I->endLoc()) {
|
|
|
|
// Shift to the next region.
|
|
|
|
++Active;
|
|
|
|
if (Active != I)
|
|
|
|
*Active = *I;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Merge duplicate region.
|
2016-05-05 17:39:45 +08:00
|
|
|
// If CodeRegions and ExpansionRegions cover the same area, it's probably
|
|
|
|
// a macro which is fully expanded to another macro. In that case, we need
|
|
|
|
// to accumulate counts only from CodeRegions, or else the area will be
|
|
|
|
// counted twice.
|
|
|
|
// On the other hand, a macro may have a nested macro in its body. If the
|
|
|
|
// outer macro is used several times, the ExpansionRegion for the nested
|
|
|
|
// macro will also be added several times. These ExpansionRegions cover
|
|
|
|
// the same source locations and have to be combined to reach the correct
|
|
|
|
// value for that area.
|
|
|
|
// We add counts of the regions of the same kind as the active region
|
|
|
|
// to handle the both situations.
|
|
|
|
if (I->Kind == Active->Kind)
|
2016-04-25 17:43:37 +08:00
|
|
|
Active->ExecutionCount += I->ExecutionCount;
|
|
|
|
}
|
|
|
|
return Regions.drop_back(std::distance(++Active, End));
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:10:00 +08:00
|
|
|
public:
|
2017-09-09 02:44:50 +08:00
|
|
|
/// Build a sorted list of CoverageSegments from a list of Regions.
|
2016-04-14 17:10:00 +08:00
|
|
|
static std::vector<CoverageSegment>
|
2016-04-25 17:43:37 +08:00
|
|
|
buildSegments(MutableArrayRef<CountedRegion> Regions) {
|
2016-04-14 17:10:00 +08:00
|
|
|
std::vector<CoverageSegment> Segments;
|
|
|
|
SegmentBuilder Builder(Segments);
|
2016-04-25 17:43:37 +08:00
|
|
|
|
|
|
|
sortNestedRegions(Regions);
|
|
|
|
ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2017-09-09 02:44:50 +08:00
|
|
|
dbgs() << "Combined regions:\n";
|
|
|
|
for (const auto &CR : CombinedRegions)
|
|
|
|
dbgs() << " " << CR.LineStart << ":" << CR.ColumnStart << " -> "
|
|
|
|
<< CR.LineEnd << ":" << CR.ColumnEnd
|
|
|
|
<< " (count=" << CR.ExecutionCount << ")\n";
|
|
|
|
});
|
|
|
|
|
2016-04-25 17:43:37 +08:00
|
|
|
Builder.buildSegmentsImpl(CombinedRegions);
|
2017-09-09 02:44:50 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
|
|
|
|
const auto &L = Segments[I - 1];
|
|
|
|
const auto &R = Segments[I];
|
|
|
|
if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
|
2020-09-03 05:50:32 +08:00
|
|
|
if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
|
|
|
|
continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
|
|
|
|
<< " followed by " << R.Line << ":" << R.Col << "\n");
|
2017-09-09 02:44:50 +08:00
|
|
|
assert(false && "Coverage segments not unique or sorted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
return Segments;
|
|
|
|
}
|
|
|
|
};
|
2017-03-03 09:07:34 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2014-11-14 09:50:32 +08:00
|
|
|
std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
|
2014-09-20 23:31:56 +08:00
|
|
|
std::vector<StringRef> Filenames;
|
|
|
|
for (const auto &Function : getCoveredFunctions())
|
2021-01-07 10:27:33 +08:00
|
|
|
llvm::append_range(Filenames, Function.Filenames);
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(Filenames);
|
2014-09-20 23:31:56 +08:00
|
|
|
auto Last = std::unique(Filenames.begin(), Filenames.end());
|
|
|
|
Filenames.erase(Last, Filenames.end());
|
|
|
|
return Filenames;
|
|
|
|
}
|
|
|
|
|
2015-02-13 00:18:07 +08:00
|
|
|
static SmallBitVector gatherFileIDs(StringRef SourceFile,
|
|
|
|
const FunctionRecord &Function) {
|
|
|
|
SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
|
2014-09-20 23:31:56 +08:00
|
|
|
for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
|
|
|
|
if (SourceFile == Function.Filenames[I])
|
|
|
|
FilenameEquivalence[I] = true;
|
2015-02-13 00:18:07 +08:00
|
|
|
return FilenameEquivalence;
|
|
|
|
}
|
|
|
|
|
2016-04-18 23:36:30 +08:00
|
|
|
/// Return the ID of the file where the definition of the function is located.
|
2016-04-16 01:53:48 +08:00
|
|
|
static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
|
|
|
|
SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
|
|
|
|
for (const auto &CR : Function.CountedRegions)
|
|
|
|
if (CR.Kind == CounterMappingRegion::ExpansionRegion)
|
|
|
|
IsNotExpandedFile[CR.ExpandedFileID] = false;
|
|
|
|
int I = IsNotExpandedFile.find_first();
|
|
|
|
if (I == -1)
|
|
|
|
return None;
|
|
|
|
return I;
|
2016-04-15 22:56:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 23:36:30 +08:00
|
|
|
/// Check if SourceFile is the file that contains the definition of
|
|
|
|
/// the Function. Return the ID of the file in that case or None otherwise.
|
|
|
|
static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
|
|
|
|
const FunctionRecord &Function) {
|
|
|
|
Optional<unsigned> I = findMainViewFileID(Function);
|
|
|
|
if (I && SourceFile == Function.Filenames[*I])
|
|
|
|
return I;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
static bool isExpansion(const CountedRegion &R, unsigned FileID) {
|
|
|
|
return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:12:23 +08:00
|
|
|
CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
|
2014-09-20 23:31:56 +08:00
|
|
|
CoverageData FileCoverage(Filename);
|
2017-03-03 09:07:34 +08:00
|
|
|
std::vector<CountedRegion> Regions;
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2019-09-17 03:08:44 +08:00
|
|
|
// Look up the function records in the given file. Due to hash collisions on
|
|
|
|
// the filename, we may get back some records that are not in the file.
|
|
|
|
ArrayRef<unsigned> RecordIndices =
|
|
|
|
getImpreciseRecordIndicesForFilename(Filename);
|
|
|
|
for (unsigned RecordIndex : RecordIndices) {
|
|
|
|
const FunctionRecord &Function = Functions[RecordIndex];
|
2014-09-20 23:31:56 +08:00
|
|
|
auto MainFileID = findMainViewFileID(Filename, Function);
|
|
|
|
auto FileIDs = gatherFileIDs(Filename, Function);
|
|
|
|
for (const auto &CR : Function.CountedRegions)
|
2015-02-13 00:18:07 +08:00
|
|
|
if (FileIDs.test(CR.FileID)) {
|
2014-09-20 23:31:56 +08:00
|
|
|
Regions.push_back(CR);
|
2016-04-18 23:36:30 +08:00
|
|
|
if (MainFileID && isExpansion(CR, *MainFileID))
|
2014-09-20 23:31:56 +08:00
|
|
|
FileCoverage.Expansions.emplace_back(CR, Function);
|
|
|
|
}
|
2020-12-29 01:20:48 +08:00
|
|
|
// Capture branch regions specific to the function (excluding expansions).
|
|
|
|
for (const auto &CR : Function.CountedBranchRegions)
|
|
|
|
if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
|
|
|
|
FileCoverage.BranchRegions.push_back(CR);
|
2014-09-20 23:31:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
|
2016-04-14 17:10:00 +08:00
|
|
|
FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
return FileCoverage;
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:35:25 +08:00
|
|
|
std::vector<InstantiationGroup>
|
|
|
|
CoverageMapping::getInstantiationGroups(StringRef Filename) const {
|
2014-09-20 23:31:56 +08:00
|
|
|
FunctionInstantiationSetCollector InstantiationSetCollector;
|
2019-09-17 03:08:44 +08:00
|
|
|
// Look up the function records in the given file. Due to hash collisions on
|
|
|
|
// the filename, we may get back some records that are not in the file.
|
|
|
|
ArrayRef<unsigned> RecordIndices =
|
|
|
|
getImpreciseRecordIndicesForFilename(Filename);
|
|
|
|
for (unsigned RecordIndex : RecordIndices) {
|
|
|
|
const FunctionRecord &Function = Functions[RecordIndex];
|
2014-09-20 23:31:56 +08:00
|
|
|
auto MainFileID = findMainViewFileID(Filename, Function);
|
|
|
|
if (!MainFileID)
|
|
|
|
continue;
|
|
|
|
InstantiationSetCollector.insert(Function, *MainFileID);
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:35:25 +08:00
|
|
|
std::vector<InstantiationGroup> Result;
|
2017-12-29 02:10:41 +08:00
|
|
|
for (auto &InstantiationSet : InstantiationSetCollector) {
|
2017-08-03 07:35:25 +08:00
|
|
|
InstantiationGroup IG{InstantiationSet.first.first,
|
|
|
|
InstantiationSet.first.second,
|
|
|
|
std::move(InstantiationSet.second)};
|
|
|
|
Result.emplace_back(std::move(IG));
|
2014-09-20 23:31:56 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverageData
|
2016-07-15 09:19:33 +08:00
|
|
|
CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
|
2014-09-20 23:31:56 +08:00
|
|
|
auto MainFileID = findMainViewFileID(Function);
|
|
|
|
if (!MainFileID)
|
|
|
|
return CoverageData();
|
|
|
|
|
|
|
|
CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
|
2017-03-03 09:07:34 +08:00
|
|
|
std::vector<CountedRegion> Regions;
|
2014-09-20 23:31:56 +08:00
|
|
|
for (const auto &CR : Function.CountedRegions)
|
|
|
|
if (CR.FileID == *MainFileID) {
|
|
|
|
Regions.push_back(CR);
|
|
|
|
if (isExpansion(CR, *MainFileID))
|
|
|
|
FunctionCoverage.Expansions.emplace_back(CR, Function);
|
|
|
|
}
|
2020-12-29 01:20:48 +08:00
|
|
|
// Capture branch regions specific to the function (excluding expansions).
|
|
|
|
for (const auto &CR : Function.CountedBranchRegions)
|
|
|
|
if (CR.FileID == *MainFileID)
|
|
|
|
FunctionCoverage.BranchRegions.push_back(CR);
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
|
|
|
|
<< "\n");
|
2016-04-14 17:10:00 +08:00
|
|
|
FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
return FunctionCoverage;
|
|
|
|
}
|
|
|
|
|
2016-07-15 09:19:33 +08:00
|
|
|
CoverageData CoverageMapping::getCoverageForExpansion(
|
|
|
|
const ExpansionRecord &Expansion) const {
|
2014-09-20 23:31:56 +08:00
|
|
|
CoverageData ExpansionCoverage(
|
|
|
|
Expansion.Function.Filenames[Expansion.FileID]);
|
2017-03-03 09:07:34 +08:00
|
|
|
std::vector<CountedRegion> Regions;
|
2014-09-20 23:31:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2020-12-29 01:20:48 +08:00
|
|
|
for (const auto &CR : Expansion.Function.CountedBranchRegions)
|
|
|
|
// Capture branch regions that only pertain to the corresponding expansion.
|
|
|
|
if (CR.FileID == Expansion.FileID)
|
|
|
|
ExpansionCoverage.BranchRegions.push_back(CR);
|
2014-09-20 23:31:56 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
|
|
|
|
<< Expansion.FileID << "\n");
|
2016-04-14 17:10:00 +08:00
|
|
|
ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
|
2014-09-20 23:31:56 +08:00
|
|
|
|
|
|
|
return ExpansionCoverage;
|
|
|
|
}
|
2015-05-07 07:19:35 +08:00
|
|
|
|
2017-10-19 07:58:28 +08:00
|
|
|
LineCoverageStats::LineCoverageStats(
|
2017-10-19 14:16:23 +08:00
|
|
|
ArrayRef<const CoverageSegment *> LineSegments,
|
|
|
|
const CoverageSegment *WrappedSegment, unsigned Line)
|
2017-10-19 07:58:28 +08:00
|
|
|
: ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
|
|
|
|
LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
|
|
|
|
// Find the minimum number of regions which start in this line.
|
|
|
|
unsigned MinRegionCount = 0;
|
2017-10-19 14:16:23 +08:00
|
|
|
auto isStartOfRegion = [](const CoverageSegment *S) {
|
2017-10-19 07:58:28 +08:00
|
|
|
return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
|
|
|
|
};
|
|
|
|
for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
|
|
|
|
if (isStartOfRegion(LineSegments[I]))
|
|
|
|
++MinRegionCount;
|
|
|
|
|
|
|
|
bool StartOfSkippedRegion = !LineSegments.empty() &&
|
|
|
|
!LineSegments.front()->HasCount &&
|
|
|
|
LineSegments.front()->IsRegionEntry;
|
|
|
|
|
|
|
|
HasMultipleRegions = MinRegionCount > 1;
|
|
|
|
Mapped =
|
|
|
|
!StartOfSkippedRegion &&
|
|
|
|
((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
|
|
|
|
|
|
|
|
if (!Mapped)
|
|
|
|
return;
|
|
|
|
|
2017-11-09 10:33:43 +08:00
|
|
|
// Pick the max count from the non-gap, region entry segments and the
|
|
|
|
// wrapped count.
|
|
|
|
if (WrappedSegment)
|
2017-10-19 07:58:28 +08:00
|
|
|
ExecutionCount = WrappedSegment->Count;
|
2017-11-09 10:33:43 +08:00
|
|
|
if (!MinRegionCount)
|
2017-10-19 07:58:28 +08:00
|
|
|
return;
|
|
|
|
for (const auto *LS : LineSegments)
|
|
|
|
if (isStartOfRegion(LS))
|
|
|
|
ExecutionCount = std::max(ExecutionCount, LS->Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineCoverageIterator &LineCoverageIterator::operator++() {
|
|
|
|
if (Next == CD.end()) {
|
|
|
|
Stats = LineCoverageStats();
|
|
|
|
Ended = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (Segments.size())
|
|
|
|
WrappedSegment = Segments.back();
|
|
|
|
Segments.clear();
|
|
|
|
while (Next != CD.end() && Next->Line == Line)
|
|
|
|
Segments.push_back(&*Next++);
|
|
|
|
Stats = LineCoverageStats(Segments, WrappedSegment, Line);
|
|
|
|
++Line;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-03 09:07:34 +08:00
|
|
|
static std::string getCoverageMapErrString(coveragemap_error Err) {
|
2016-05-19 11:54:45 +08:00
|
|
|
switch (Err) {
|
|
|
|
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";
|
2019-10-22 02:48:38 +08:00
|
|
|
case coveragemap_error::decompression_failed:
|
|
|
|
return "Failed to decompress coverage data (zlib)";
|
2020-10-14 07:20:40 +08:00
|
|
|
case coveragemap_error::invalid_or_missing_arch_specifier:
|
|
|
|
return "`-arch` specifier is invalid or missing for universal binary";
|
2016-05-19 11:54:45 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("A value of coveragemap_error has no message.");
|
|
|
|
}
|
|
|
|
|
2017-03-03 09:07:34 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-05-25 04:13:46 +08:00
|
|
|
// FIXME: This class is only here to support the transition to llvm::Error. It
|
|
|
|
// will be removed once this transition is complete. Clients should prefer to
|
|
|
|
// deal with the Error value directly, rather than converting to error_code.
|
2015-05-07 07:19:35 +08:00
|
|
|
class CoverageMappingErrorCategoryType : public std::error_category {
|
2016-10-20 07:52:38 +08:00
|
|
|
const char *name() const noexcept override { return "llvm.coveragemap"; }
|
2015-05-07 07:19:35 +08:00
|
|
|
std::string message(int IE) const override {
|
2016-05-19 11:54:45 +08:00
|
|
|
return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
|
2015-05-07 07:19:35 +08:00
|
|
|
}
|
|
|
|
};
|
2017-03-03 09:07:34 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
std::string CoverageMapError::message() const {
|
|
|
|
return getCoverageMapErrString(Err);
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-05-07 07:19:35 +08:00
|
|
|
|
|
|
|
static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
|
|
|
|
|
2016-01-11 05:56:33 +08:00
|
|
|
const std::error_category &llvm::coverage::coveragemap_category() {
|
2015-05-07 07:19:35 +08:00
|
|
|
return *ErrorCategory;
|
|
|
|
}
|
2016-05-19 11:54:45 +08:00
|
|
|
|
|
|
|
char CoverageMapError::ID = 0;
|