blob: 3f8f76f60949975a305333cae35c596c7c96e018 [file] [log] [blame]
Alex Lorenza20a5d52014-07-24 23:57:54 +00001//=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for reading coverage mapping data for
11// instrumentation based coverage.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ProfileData/CoverageMappingReader.h"
16#include "llvm/ADT/DenseSet.h"
Justin Bogner43795352015-03-11 02:30:51 +000017#include "llvm/Object/MachOUniversal.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000018#include "llvm/Object/ObjectFile.h"
Justin Bognerf5846492014-09-20 15:31:51 +000019#include "llvm/Support/Debug.h"
Alex Lorenza20a5d52014-07-24 23:57:54 +000020#include "llvm/Support/LEB128.h"
21
22using namespace llvm;
23using namespace coverage;
24using namespace object;
25
Justin Bognerf5846492014-09-20 15:31:51 +000026#define DEBUG_TYPE "coverage-mapping"
27
Alex Lorenza20a5d52014-07-24 23:57:54 +000028void CoverageMappingIterator::increment() {
29 // Check if all the records were read or if an error occurred while reading
30 // the next record.
31 if (Reader->readNextRecord(Record))
32 *this = CoverageMappingIterator();
33}
34
35std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
36 if (Data.size() < 1)
37 return error(instrprof_error::truncated);
38 unsigned N = 0;
39 Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
40 if (N > Data.size())
41 return error(instrprof_error::malformed);
42 Data = Data.substr(N);
43 return success();
44}
45
46std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
47 uint64_t MaxPlus1) {
48 if (auto Err = readULEB128(Result))
49 return Err;
50 if (Result >= MaxPlus1)
51 return error(instrprof_error::malformed);
52 return success();
53}
54
55std::error_code RawCoverageReader::readSize(uint64_t &Result) {
56 if (auto Err = readULEB128(Result))
57 return Err;
58 // Sanity check the number.
59 if (Result > Data.size())
60 return error(instrprof_error::malformed);
61 return success();
62}
63
64std::error_code RawCoverageReader::readString(StringRef &Result) {
65 uint64_t Length;
66 if (auto Err = readSize(Length))
67 return Err;
68 Result = Data.substr(0, Length);
69 Data = Data.substr(Length);
70 return success();
71}
72
73std::error_code RawCoverageFilenamesReader::read() {
74 uint64_t NumFilenames;
75 if (auto Err = readSize(NumFilenames))
76 return Err;
77 for (size_t I = 0; I < NumFilenames; ++I) {
78 StringRef Filename;
79 if (auto Err = readString(Filename))
80 return Err;
81 Filenames.push_back(Filename);
82 }
83 return success();
84}
85
86std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
87 Counter &C) {
88 auto Tag = Value & Counter::EncodingTagMask;
89 switch (Tag) {
90 case Counter::Zero:
91 C = Counter::getZero();
92 return success();
93 case Counter::CounterValueReference:
94 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
95 return success();
96 default:
97 break;
98 }
99 Tag -= Counter::Expression;
100 switch (Tag) {
101 case CounterExpression::Subtract:
102 case CounterExpression::Add: {
103 auto ID = Value >> Counter::EncodingTagBits;
104 if (ID >= Expressions.size())
105 return error(instrprof_error::malformed);
106 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
107 C = Counter::getExpression(ID);
108 break;
109 }
110 default:
111 return error(instrprof_error::malformed);
112 }
113 return success();
114}
115
116std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
117 uint64_t EncodedCounter;
118 if (auto Err =
119 readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
120 return Err;
121 if (auto Err = decodeCounter(EncodedCounter, C))
122 return Err;
123 return success();
124}
125
126static const unsigned EncodingExpansionRegionBit = 1
127 << Counter::EncodingTagBits;
128
129/// \brief Read the sub-array of regions for the given inferred file id.
Ehsan Akhgari29b61ce2014-07-25 02:51:57 +0000130/// \param NumFileIDs the number of file ids that are defined for this
Alex Lorenza20a5d52014-07-24 23:57:54 +0000131/// function.
132std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
133 std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
134 size_t NumFileIDs) {
135 uint64_t NumRegions;
136 if (auto Err = readSize(NumRegions))
137 return Err;
138 unsigned LineStart = 0;
139 for (size_t I = 0; I < NumRegions; ++I) {
140 Counter C;
141 CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
142
143 // Read the combined counter + region kind.
144 uint64_t EncodedCounterAndRegion;
145 if (auto Err = readIntMax(EncodedCounterAndRegion,
146 std::numeric_limits<unsigned>::max()))
147 return Err;
148 unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
149 uint64_t ExpandedFileID = 0;
150 if (Tag != Counter::Zero) {
151 if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
152 return Err;
153 } else {
154 // Is it an expansion region?
155 if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
156 Kind = CounterMappingRegion::ExpansionRegion;
157 ExpandedFileID = EncodedCounterAndRegion >>
158 Counter::EncodingCounterTagAndExpansionRegionTagBits;
159 if (ExpandedFileID >= NumFileIDs)
160 return error(instrprof_error::malformed);
161 } else {
162 switch (EncodedCounterAndRegion >>
163 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
164 case CounterMappingRegion::CodeRegion:
165 // Don't do anything when we have a code region with a zero counter.
166 break;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000167 case CounterMappingRegion::SkippedRegion:
168 Kind = CounterMappingRegion::SkippedRegion;
169 break;
170 default:
171 return error(instrprof_error::malformed);
172 }
173 }
174 }
175
176 // Read the source range.
Justin Bognerde158172015-02-03 21:35:36 +0000177 uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000178 if (auto Err =
179 readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
180 return Err;
Justin Bognerde158172015-02-03 21:35:36 +0000181 if (auto Err = readULEB128(ColumnStart))
Alex Lorenza20a5d52014-07-24 23:57:54 +0000182 return Err;
Alex Lorenz1193b5e2014-08-04 18:00:51 +0000183 if (ColumnStart > std::numeric_limits<unsigned>::max())
184 return error(instrprof_error::malformed);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000185 if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
186 return Err;
187 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
188 return Err;
189 LineStart += LineStartDelta;
190 // Adjust the column locations for the empty regions that are supposed to
191 // cover whole lines. Those regions should be encoded with the
192 // column range (1 -> std::numeric_limits<unsigned>::max()), but because
193 // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
194 // we set the column range to (0 -> 0) to ensure that the column start and
195 // column end take up one byte each.
196 // The std::numeric_limits<unsigned>::max() is used to represent a column
197 // position at the end of the line without knowing the length of that line.
198 if (ColumnStart == 0 && ColumnEnd == 0) {
199 ColumnStart = 1;
200 ColumnEnd = std::numeric_limits<unsigned>::max();
201 }
Justin Bognerf5846492014-09-20 15:31:51 +0000202
203 DEBUG({
204 dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
205 << ColumnStart << " -> " << (LineStart + NumLines) << ":"
206 << ColumnEnd << ", ";
207 if (Kind == CounterMappingRegion::ExpansionRegion)
208 dbgs() << "Expands to file " << ExpandedFileID;
209 else
210 CounterMappingContext(Expressions).dump(C, dbgs());
211 dbgs() << "\n";
212 });
213
Justin Bogner26b31422015-02-03 23:59:33 +0000214 MappingRegions.push_back(CounterMappingRegion(
215 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
216 LineStart + NumLines, ColumnEnd, Kind));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000217 }
218 return success();
219}
220
Justin Bogner195a4f02015-02-03 00:20:11 +0000221std::error_code RawCoverageMappingReader::read() {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000222
223 // Read the virtual file mapping.
224 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
225 uint64_t NumFileMappings;
226 if (auto Err = readSize(NumFileMappings))
227 return Err;
228 for (size_t I = 0; I < NumFileMappings; ++I) {
229 uint64_t FilenameIndex;
230 if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
231 return Err;
232 VirtualFileMapping.push_back(FilenameIndex);
233 }
234
235 // Construct the files using unique filenames and virtual file mapping.
236 for (auto I : VirtualFileMapping) {
237 Filenames.push_back(TranslationUnitFilenames[I]);
238 }
239
240 // Read the expressions.
241 uint64_t NumExpressions;
242 if (auto Err = readSize(NumExpressions))
243 return Err;
244 // Create an array of dummy expressions that get the proper counters
245 // when the expressions are read, and the proper kinds when the counters
246 // are decoded.
247 Expressions.resize(
248 NumExpressions,
249 CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
250 for (size_t I = 0; I < NumExpressions; ++I) {
251 if (auto Err = readCounter(Expressions[I].LHS))
252 return Err;
253 if (auto Err = readCounter(Expressions[I].RHS))
254 return Err;
255 }
256
257 // Read the mapping regions sub-arrays.
258 for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
259 InferredFileID < S; ++InferredFileID) {
260 if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
261 VirtualFileMapping.size()))
262 return Err;
263 }
264
265 // Set the counters for the expansion regions.
266 // i.e. Counter of expansion region = counter of the first region
267 // from the expanded file.
268 // Perform multiple passes to correctly propagate the counters through
269 // all the nested expansion regions.
Alex Lorenz251b3e32014-07-29 21:42:24 +0000270 SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
271 FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000272 for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
Alex Lorenz251b3e32014-07-29 21:42:24 +0000273 for (auto &R : MappingRegions) {
274 if (R.Kind != CounterMappingRegion::ExpansionRegion)
275 continue;
276 assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
277 FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
278 }
279 for (auto &R : MappingRegions) {
280 if (FileIDExpansionRegionMapping[R.FileID]) {
281 FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
282 FileIDExpansionRegionMapping[R.FileID] = nullptr;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000283 }
284 }
285 }
286
Alex Lorenza20a5d52014-07-24 23:57:54 +0000287 return success();
288}
289
Alex Lorenza20a5d52014-07-24 23:57:54 +0000290namespace {
291/// \brief The coverage mapping data for a single function.
292/// It points to the function's name.
293template <typename IntPtrT> struct CoverageMappingFunctionRecord {
294 IntPtrT FunctionNamePtr;
295 uint32_t FunctionNameSize;
296 uint32_t CoverageMappingSize;
Alex Lorenz936b99c2014-08-21 19:23:25 +0000297 uint64_t FunctionHash;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000298};
299
300/// \brief The coverage mapping data for a single translation unit.
301/// It points to the array of function coverage mapping records and the encoded
302/// filenames array.
303template <typename IntPtrT> struct CoverageMappingTURecord {
304 uint32_t FunctionRecordsSize;
305 uint32_t FilenamesSize;
306 uint32_t CoverageMappingsSize;
307 uint32_t Version;
308};
309
310/// \brief A helper structure to access the data from a section
311/// in an object file.
312struct SectionData {
313 StringRef Data;
314 uint64_t Address;
315
316 std::error_code load(SectionRef &Section) {
317 if (auto Err = Section.getContents(Data))
318 return Err;
Rafael Espindola80291272014-10-08 15:28:58 +0000319 Address = Section.getAddress();
320 return instrprof_error::success;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000321 }
322
323 std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
324 if (Pointer < Address)
325 return instrprof_error::malformed;
326 auto Offset = Pointer - Address;
327 if (Offset + Size > Data.size())
328 return instrprof_error::malformed;
329 Result = Data.substr(Pointer - Address, Size);
330 return instrprof_error::success;
331 }
332};
333}
334
335template <typename T>
336std::error_code readCoverageMappingData(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000337 SectionData &ProfileNames, StringRef Data,
Justin Bognere84891a2015-02-26 20:06:24 +0000338 std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
Alex Lorenza20a5d52014-07-24 23:57:54 +0000339 std::vector<StringRef> &Filenames) {
340 llvm::DenseSet<T> UniqueFunctionMappingData;
341
Alex Lorenza20a5d52014-07-24 23:57:54 +0000342 // Read the records in the coverage data section.
343 while (!Data.empty()) {
344 if (Data.size() < sizeof(CoverageMappingTURecord<T>))
345 return instrprof_error::malformed;
346 auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
347 Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
348 switch (TU->Version) {
349 case CoverageMappingVersion1:
350 break;
351 default:
352 return instrprof_error::unsupported_version;
353 }
354 auto Version = CoverageMappingVersion(TU->Version);
355
356 // Get the function records.
357 auto FunctionRecords =
358 reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
359 if (Data.size() <
360 sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
361 return instrprof_error::malformed;
362 Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
363 TU->FunctionRecordsSize);
364
365 // Get the filenames.
366 if (Data.size() < TU->FilenamesSize)
367 return instrprof_error::malformed;
368 auto RawFilenames = Data.substr(0, TU->FilenamesSize);
369 Data = Data.substr(TU->FilenamesSize);
370 size_t FilenamesBegin = Filenames.size();
371 RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
372 if (auto Err = Reader.read())
373 return Err;
374
375 // Get the coverage mappings.
376 if (Data.size() < TU->CoverageMappingsSize)
377 return instrprof_error::malformed;
378 auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
379 Data = Data.substr(TU->CoverageMappingsSize);
380
381 for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
382 auto &MappingRecord = FunctionRecords[I];
383
384 // Get the coverage mapping.
385 if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
386 return instrprof_error::malformed;
387 auto Mapping =
388 CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
389 CoverageMappings =
390 CoverageMappings.substr(MappingRecord.CoverageMappingSize);
391
392 // Ignore this record if we already have a record that points to the same
393 // function name.
394 // This is useful to ignore the redundant records for the functions
395 // with ODR linkage.
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000396 if (!UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr)
397 .second)
Alex Lorenza20a5d52014-07-24 23:57:54 +0000398 continue;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000399 StringRef FunctionName;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000400 if (auto Err =
401 ProfileNames.get(MappingRecord.FunctionNamePtr,
402 MappingRecord.FunctionNameSize, FunctionName))
Alex Lorenza20a5d52014-07-24 23:57:54 +0000403 return Err;
Justin Bognere84891a2015-02-26 20:06:24 +0000404 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
Alex Lorenz936b99c2014-08-21 19:23:25 +0000405 Version, FunctionName, MappingRecord.FunctionHash, Mapping,
406 FilenamesBegin, Filenames.size() - FilenamesBegin));
Alex Lorenza20a5d52014-07-24 23:57:54 +0000407 }
408 }
409
410 return instrprof_error::success;
411}
412
Alex Lorenze82d89c2014-08-22 22:56:03 +0000413static const char *TestingFormatMagic = "llvmcovmtestdata";
414
Justin Bogner43e51632015-02-26 20:06:28 +0000415static std::error_code loadTestingFormat(StringRef Data,
416 SectionData &ProfileNames,
417 StringRef &CoverageMapping,
418 uint8_t &BytesInAddress) {
419 BytesInAddress = 8;
420
Alex Lorenze82d89c2014-08-22 22:56:03 +0000421 Data = Data.substr(StringRef(TestingFormatMagic).size());
422 if (Data.size() < 1)
423 return instrprof_error::truncated;
424 unsigned N = 0;
425 auto ProfileNamesSize =
426 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
427 if (N > Data.size())
428 return instrprof_error::malformed;
429 Data = Data.substr(N);
430 if (Data.size() < 1)
431 return instrprof_error::truncated;
432 N = 0;
433 ProfileNames.Address =
434 decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
435 if (N > Data.size())
436 return instrprof_error::malformed;
437 Data = Data.substr(N);
438 if (Data.size() < ProfileNamesSize)
439 return instrprof_error::malformed;
440 ProfileNames.Data = Data.substr(0, ProfileNamesSize);
441 CoverageMapping = Data.substr(ProfileNamesSize);
442 return instrprof_error::success;
443}
444
Justin Bogner43e51632015-02-26 20:06:28 +0000445static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
446 SectionData &ProfileNames,
447 StringRef &CoverageMapping,
Justin Bogner43795352015-03-11 02:30:51 +0000448 uint8_t &BytesInAddress,
449 Triple::ArchType Arch) {
450 auto BinOrErr = object::createBinary(ObjectBuffer);
451 if (std::error_code EC = BinOrErr.getError())
Justin Bogner43e51632015-02-26 20:06:28 +0000452 return EC;
Justin Bogner43795352015-03-11 02:30:51 +0000453 auto Bin = std::move(BinOrErr.get());
454 std::unique_ptr<ObjectFile> OF;
455 if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
456 // If we have a universal binary, try to look up the object for the
457 // appropriate architecture.
458 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
459 if (std::error_code EC = ObjectFileOrErr.getError())
460 return EC;
461 OF = std::move(ObjectFileOrErr.get());
462 } else if (isa<object::ObjectFile>(Bin.get())) {
463 // For any other object file, upcast and take ownership.
464 OF.reset(cast<object::ObjectFile>(Bin.release()));
465 // If we've asked for a particular arch, make sure they match.
466 if (Arch != Triple::ArchType::UnknownArch && OF->getArch() != Arch)
467 return object_error::arch_not_found;
468 } else
469 // We can only handle object files.
470 return instrprof_error::malformed;
471
472 // The coverage uses native pointer sizes for the object it's written in.
Justin Bogner43e51632015-02-26 20:06:28 +0000473 BytesInAddress = OF->getBytesInAddress();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000474
475 // Look for the sections that we are interested in.
476 int FoundSectionCount = 0;
Justin Bogner43e51632015-02-26 20:06:28 +0000477 SectionRef NamesSection, CoverageSection;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000478 for (const auto &Section : OF->sections()) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000479 StringRef Name;
480 if (auto Err = Section.getName(Name))
481 return Err;
482 if (Name == "__llvm_prf_names") {
Justin Bogner43e51632015-02-26 20:06:28 +0000483 NamesSection = Section;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000484 } else if (Name == "__llvm_covmap") {
Justin Bogner43e51632015-02-26 20:06:28 +0000485 CoverageSection = Section;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000486 } else
487 continue;
488 ++FoundSectionCount;
489 }
490 if (FoundSectionCount != 2)
Justin Bogner43e51632015-02-26 20:06:28 +0000491 return instrprof_error::bad_header;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000492
Alex Lorenze82d89c2014-08-22 22:56:03 +0000493 // Get the contents of the given sections.
Justin Bogner43e51632015-02-26 20:06:28 +0000494 if (std::error_code EC = CoverageSection.getContents(CoverageMapping))
495 return EC;
496 if (std::error_code EC = ProfileNames.load(NamesSection))
497 return EC;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000498
Justin Bogner43e51632015-02-26 20:06:28 +0000499 return std::error_code();
500}
501
502ErrorOr<std::unique_ptr<BinaryCoverageReader>>
Justin Bogner43795352015-03-11 02:30:51 +0000503BinaryCoverageReader::create(std::unique_ptr<MemoryBuffer> &ObjectBuffer,
504 Triple::ArchType Arch) {
Justin Bogner43e51632015-02-26 20:06:28 +0000505 std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader());
506
507 SectionData Profile;
508 StringRef Coverage;
509 uint8_t BytesInAddress;
510 std::error_code EC;
511 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
512 // This is a special format used for testing.
513 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
514 BytesInAddress);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000515 else
Justin Bogner43e51632015-02-26 20:06:28 +0000516 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
Justin Bogner43795352015-03-11 02:30:51 +0000517 BytesInAddress, Arch);
Justin Bogner43e51632015-02-26 20:06:28 +0000518 if (EC)
519 return EC;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000520
Justin Bogner43e51632015-02-26 20:06:28 +0000521 if (BytesInAddress == 4)
522 EC = readCoverageMappingData<uint32_t>(
523 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
524 else if (BytesInAddress == 8)
525 EC = readCoverageMappingData<uint64_t>(
526 Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
527 else
528 return instrprof_error::malformed;
529 if (EC)
530 return EC;
531 return std::move(Reader);
Alex Lorenza20a5d52014-07-24 23:57:54 +0000532}
533
534std::error_code
Justin Bognere84891a2015-02-26 20:06:24 +0000535BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
Alex Lorenza20a5d52014-07-24 23:57:54 +0000536 if (CurrentRecord >= MappingRecords.size())
Justin Bogner43e51632015-02-26 20:06:28 +0000537 return instrprof_error::eof;
Alex Lorenza20a5d52014-07-24 23:57:54 +0000538
539 FunctionsFilenames.clear();
540 Expressions.clear();
541 MappingRegions.clear();
542 auto &R = MappingRecords[CurrentRecord];
543 RawCoverageMappingReader Reader(
Justin Bogner195a4f02015-02-03 00:20:11 +0000544 R.CoverageMapping,
Justin Bogner346359d2015-02-03 00:00:00 +0000545 makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize),
Alex Lorenza20a5d52014-07-24 23:57:54 +0000546 FunctionsFilenames, Expressions, MappingRegions);
Justin Bogner195a4f02015-02-03 00:20:11 +0000547 if (auto Err = Reader.read())
Alex Lorenza20a5d52014-07-24 23:57:54 +0000548 return Err;
Justin Bogner195a4f02015-02-03 00:20:11 +0000549
550 Record.FunctionName = R.FunctionName;
Alex Lorenz936b99c2014-08-21 19:23:25 +0000551 Record.FunctionHash = R.FunctionHash;
Justin Bogner195a4f02015-02-03 00:20:11 +0000552 Record.Filenames = FunctionsFilenames;
553 Record.Expressions = Expressions;
554 Record.MappingRegions = MappingRegions;
555
Alex Lorenza20a5d52014-07-24 23:57:54 +0000556 ++CurrentRecord;
Justin Bogner43e51632015-02-26 20:06:28 +0000557 return std::error_code();
Alex Lorenza20a5d52014-07-24 23:57:54 +0000558}