Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 3327f8b

Browse files
committed
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340603 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2024fa2 commit 3327f8b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

include/llvm/XRay/Trace.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include <vector>
1818

1919
#include "llvm/ADT/StringRef.h"
20+
#include "llvm/Support/DataExtractor.h"
2021
#include "llvm/Support/Error.h"
21-
#include "llvm/Support/FileSystem.h"
2222
#include "llvm/XRay/XRayRecord.h"
2323

2424
namespace llvm {
@@ -50,21 +50,26 @@ class Trace {
5050

5151
typedef std::vector<XRayRecord>::const_iterator citerator;
5252

53-
friend Expected<Trace> loadTraceFile(StringRef, bool);
53+
friend Expected<Trace> loadTrace(const DataExtractor &, bool);
5454

5555
public:
5656
/// Provides access to the loaded XRay trace file header.
5757
const XRayFileHeader &getFileHeader() const { return FileHeader; }
5858

5959
citerator begin() const { return Records.begin(); }
6060
citerator end() const { return Records.end(); }
61+
bool empty() const { return Records.empty(); }
6162
size_t size() const { return Records.size(); }
6263
};
6364

6465
/// This function will attempt to load XRay trace records from the provided
6566
/// |Filename|.
6667
Expected<Trace> loadTraceFile(StringRef Filename, bool Sort = false);
6768

69+
/// This function will attempt to load XRay trace records from the provided
70+
/// DataExtractor.
71+
Expected<Trace> loadTrace(const DataExtractor &Extractor, bool Sort = false);
72+
6873
} // namespace xray
6974
} // namespace llvm
7075

lib/XRay/Trace.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,11 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
862862
Twine("Cannot read log from '") + Filename + "'", EC);
863863
}
864864
auto Data = StringRef(MappedFile.data(), MappedFile.size());
865+
DataExtractor DE(Data, true, 8);
866+
return loadTrace(DE, Sort);
867+
}
865868

869+
Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) {
866870
// Attempt to detect the file type using file magic. We have a slight bias
867871
// towards the binary format, and we do this by making sure that the first 4
868872
// 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) {
877881
//
878882
// Only if we can't load either the binary or the YAML format will we yield an
879883
// error.
880-
StringRef Magic(MappedFile.data(), 4);
881-
DataExtractor HeaderExtractor(Magic, true, 8);
884+
DataExtractor HeaderExtractor(DE.getData(), true, 8);
882885
uint32_t OffsetPtr = 0;
883886
uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
884887
uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
@@ -889,7 +892,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
889892
switch (Type) {
890893
case NAIVE_FORMAT:
891894
if (Version == 1 || Version == 2 || Version == 3) {
892-
if (auto E = loadNaiveFormatLog(Data, T.FileHeader, T.Records))
895+
if (auto E = loadNaiveFormatLog(DE.getData(), T.FileHeader, T.Records))
893896
return std::move(E);
894897
} else {
895898
return make_error<StringError>(
@@ -900,7 +903,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
900903
break;
901904
case FLIGHT_DATA_RECORDER_FORMAT:
902905
if (Version == 1 || Version == 2 || Version == 3) {
903-
if (auto E = loadFDRLog(Data, T.FileHeader, T.Records))
906+
if (auto E = loadFDRLog(DE.getData(), T.FileHeader, T.Records))
904907
return std::move(E);
905908
} else {
906909
return make_error<StringError>(
@@ -909,7 +912,7 @@ Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
909912
}
910913
break;
911914
default:
912-
if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records))
915+
if (auto E = loadYAMLLog(DE.getData(), T.FileHeader, T.Records))
913916
return std::move(E);
914917
}
915918

0 commit comments

Comments
 (0)