1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
17 #include "llvm/Support/Endian.h"
23 error_code InstrProfReader::create(std::string Path,
24 std::unique_ptr<InstrProfReader> &Result) {
25 std::unique_ptr<MemoryBuffer> Buffer;
26 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
29 // Sanity check the file.
30 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
31 return instrprof_error::too_large;
33 // FIXME: This needs to determine which format the file is and construct the
35 Result.reset(new TextInstrProfReader(Buffer));
37 return instrprof_error::success;
40 void InstrProfIterator::Increment() {
41 if (Reader->readNextRecord(Record))
42 *this = InstrProfIterator();
45 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
47 while (!Line.is_at_end() && Line->empty())
49 // If we hit EOF while looking for a name, we're done.
51 return error(instrprof_error::eof);
53 // Read the function name.
54 Record.Name = *Line++;
56 // Read the function hash.
58 return error(instrprof_error::truncated);
59 if ((Line++)->getAsInteger(10, Record.Hash))
60 return error(instrprof_error::malformed);
62 // Read the number of counters.
65 return error(instrprof_error::truncated);
66 if ((Line++)->getAsInteger(10, NumCounters))
67 return error(instrprof_error::malformed);
69 // Read each counter and fill our internal storage with the values.
71 Counts.reserve(NumCounters);
72 for (uint64_t I = 0; I < NumCounters; ++I) {
74 return error(instrprof_error::truncated);
76 if ((Line++)->getAsInteger(10, Count))
77 return error(instrprof_error::malformed);
78 Counts.push_back(Count);
80 // Give the record a reference to our internal counter storage.
81 Record.Counts = Counts;