[XRay] Refactor loadTraceFile(...) into two (NFC)

This patch splits the file trace loading function into two versions, one
that takes a filename and one that takes a `DataExtractor`.

This change is a precursor to larger changes to increase test coverage
for the trace loading implementation.

llvm-svn: 340603
This commit is contained in:
Dean Michael Berris 2018-08-24 10:30:37 +00:00
parent 8e44a8e7a0
commit f81b08001a
2 changed files with 15 additions and 7 deletions

View File

@ -17,8 +17,8 @@
#include <vector>
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/XRay/XRayRecord.h"
namespace llvm {
@ -50,7 +50,7 @@ class Trace {
typedef std::vector<XRayRecord>::const_iterator citerator;
friend Expected<Trace> loadTraceFile(StringRef, bool);
friend Expected<Trace> loadTrace(const DataExtractor &, bool);
public:
/// Provides access to the loaded XRay trace file header.
@ -58,6 +58,7 @@ public:
citerator begin() const { return Records.begin(); }
citerator end() const { return Records.end(); }
bool empty() const { return Records.empty(); }
size_t size() const { return Records.size(); }
};
@ -65,6 +66,10 @@ public:
/// |Filename|.
Expected<Trace> loadTraceFile(StringRef Filename, bool Sort = false);
/// This function will attempt to load XRay trace records from the provided
/// DataExtractor.
Expected<Trace> loadTrace(const DataExtractor &Extractor, bool Sort = false);
} // namespace xray
} // namespace llvm

View File

@ -862,7 +862,11 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
Twine("Cannot read log from '") + Filename + "'", EC);
}
auto Data = StringRef(MappedFile.data(), MappedFile.size());
DataExtractor DE(Data, true, 8);
return loadTrace(DE, Sort);
}
Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) {
// Attempt to detect the file type using file magic. We have a slight bias
// towards the binary format, and we do this by making sure that the first 4
// bytes of the binary file is some combination of the following byte
@ -877,8 +881,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
//
// Only if we can't load either the binary or the YAML format will we yield an
// error.
StringRef Magic(MappedFile.data(), 4);
DataExtractor HeaderExtractor(Magic, true, 8);
DataExtractor HeaderExtractor(DE.getData(), true, 8);
uint32_t OffsetPtr = 0;
uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
@ -889,7 +892,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
switch (Type) {
case NAIVE_FORMAT:
if (Version == 1 || Version == 2 || Version == 3) {
if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
if (auto E = loadNaiveFormatLog(DE.getData(), T.FileHeader, T.Records))
return std::move(E);
} else {
return make_error<StringError>(
@ -900,7 +903,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
break;
case FLIGHT_DATA_RECORDER_FORMAT:
if (Version == 1 || Version == 2 || Version == 3) {
if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
if (auto E = loadFDRLog(DE.getData(), T.FileHeader, T.Records))
return std::move(E);
} else {
return make_error<StringError>(
@ -909,7 +912,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
}
break;
default:
if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
if (auto E = loadYAMLLog(DE.getData(), T.FileHeader, T.Records))
return std::move(E);
}