[Coverage] Factor out logic to create FunctionRecords (NFC)

llvm-svn: 284063
This commit is contained in:
Vedant Kumar 2016-10-12 22:27:45 +00:00
parent 85d54d6bcb
commit 68216d7bd3
2 changed files with 51 additions and 41 deletions

View File

@ -75,6 +75,7 @@ class IndexedInstrProfReader;
namespace coverage {
class CoverageMappingReader;
struct CoverageMappingRecord;
class CoverageMapping;
struct CounterExpressions;
@ -424,6 +425,10 @@ class CoverageMapping {
CoverageMapping() : MismatchedFunctionCount(0) {}
/// \brief Add a function record corresponding to \p Record.
Error loadFunctionRecord(const CoverageMappingRecord &Record,
IndexedInstrProfReader &ProfileReader);
public:
/// \brief Load the coverage mapping using the given readers.
static Expected<std::unique_ptr<CoverageMapping>>

View File

@ -183,52 +183,57 @@ void FunctionRecordIterator::skipOtherFiles() {
*this = FunctionRecordIterator();
}
Error CoverageMapping::loadFunctionRecord(
const CoverageMappingRecord &Record,
IndexedInstrProfReader &ProfileReader) {
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) {
MismatchedFunctionCount++;
return Error::success();
} else if (IPE != instrprof_error::unknown_function)
return make_error<InstrProfError>(IPE);
Counts.assign(Record.MappingRegions.size(), 0);
}
Ctx.setCounts(Counts);
assert(!Record.MappingRegions.empty() && "Function has no regions");
StringRef OrigFuncName = Record.FunctionName;
if (Record.Filenames.empty())
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
else
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
FunctionRecord Function(OrigFuncName, Record.Filenames);
for (const auto &Region : Record.MappingRegions) {
Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
if (auto E = ExecutionCount.takeError()) {
llvm::consumeError(std::move(E));
return Error::success();
}
Function.pushRegion(Region, *ExecutionCount);
}
if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
MismatchedFunctionCount++;
return Error::success();
}
Functions.push_back(std::move(Function));
return Error::success();
}
Expected<std::unique_ptr<CoverageMapping>>
CoverageMapping::load(CoverageMappingReader &CoverageReader,
IndexedInstrProfReader &ProfileReader) {
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
std::vector<uint64_t> Counts;
for (const auto &Record : CoverageReader) {
CounterMappingContext Ctx(Record.Expressions);
Counts.clear();
if (Error E = ProfileReader.getFunctionCounts(
Record.FunctionName, Record.FunctionHash, Counts)) {
instrprof_error IPE = InstrProfError::take(std::move(E));
if (IPE == instrprof_error::hash_mismatch) {
Coverage->MismatchedFunctionCount++;
continue;
} else if (IPE != instrprof_error::unknown_function)
return make_error<InstrProfError>(IPE);
Counts.assign(Record.MappingRegions.size(), 0);
}
Ctx.setCounts(Counts);
assert(!Record.MappingRegions.empty() && "Function has no regions");
StringRef OrigFuncName = Record.FunctionName;
if (Record.Filenames.empty())
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
else
OrigFuncName =
getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
FunctionRecord Function(OrigFuncName, Record.Filenames);
for (const auto &Region : Record.MappingRegions) {
Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
if (auto E = ExecutionCount.takeError()) {
llvm::consumeError(std::move(E));
break;
}
Function.pushRegion(Region, *ExecutionCount);
}
if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
Coverage->MismatchedFunctionCount++;
continue;
}
Coverage->Functions.push_back(std::move(Function));
}
for (const auto &Record : CoverageReader)
if (Error E = Coverage->loadFunctionRecord(Record, ProfileReader))
return std::move(E);
return std::move(Coverage);
}