InstrProf: Simplify the construction of BinaryCoverageReader

Creating BinaryCoverageReader is a strange and complicated dance where
the constructor sets error codes that member functions will later
read, and the object is in an invalid state if readHeader isn't
immediately called after construction.

Instead, make the constructor private and add a static create method
to do the construction properly. This also has the benefit of removing
readHeader completely and simplifying the interface of the object.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230676 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Justin Bogner
2015-02-26 20:06:28 +00:00
parent 090b50ca27
commit c0c2133484
3 changed files with 64 additions and 84 deletions

View File

@@ -161,8 +161,6 @@ public:
};
private:
std::error_code LastError;
object::OwningBinary<object::ObjectFile> Object;
std::vector<StringRef> Filenames;
std::vector<ProfileMappingRecord> MappingRecords;
size_t CurrentRecord;
@@ -173,28 +171,13 @@ private:
BinaryCoverageReader(const BinaryCoverageReader &) = delete;
BinaryCoverageReader &operator=(const BinaryCoverageReader &) = delete;
/// \brief Set the current error_code and return same.
std::error_code error(std::error_code EC) {
LastError = EC;
return EC;
}
/// \brief Clear the current error code and return a successful one.
std::error_code success() { return error(instrprof_error::success); }
BinaryCoverageReader() : CurrentRecord(0) {}
public:
BinaryCoverageReader(std::unique_ptr<MemoryBuffer> &ObjectBuffer);
static ErrorOr<std::unique_ptr<BinaryCoverageReader>>
create(std::unique_ptr<MemoryBuffer> &ObjectBuffer);
std::error_code readHeader();
std::error_code readNextRecord(CoverageMappingRecord &Record) override;
/// \brief Return true if the reader has finished reading the profile data.
bool isEOF() { return LastError == instrprof_error::eof; }
/// \brief Return true if the reader encountered an error reading profiling
/// data.
bool hasError() { return LastError && !isEOF(); }
/// \brief Get the current error code.
std::error_code getError() { return LastError; }
};
} // end namespace coverage