@@ -47,7 +47,7 @@ Error CodeGenDataReader::mergeFromObjectFile(
47
47
auto *EndData = Data + ContentsOrErr->size ();
48
48
49
49
if (*NameOrErr == CGOutLineName) {
50
- // In case dealing with an executable that has concatenaed cgdata,
50
+ // In case dealing with an executable that has concatenated cgdata,
51
51
// we want to merge them into a single cgdata.
52
52
// Although it's not a typical workflow, we support this scenario.
53
53
while (Data != EndData) {
@@ -74,10 +74,8 @@ Error IndexedCodeGenDataReader::read() {
74
74
reinterpret_cast <const unsigned char *>(DataBuffer->getBufferStart ());
75
75
auto *End =
76
76
reinterpret_cast <const unsigned char *>(DataBuffer->getBufferEnd ());
77
- auto HeaderOr = IndexedCGData::Header::readFromBuffer (Start);
78
- if (!HeaderOr)
79
- return HeaderOr.takeError ();
80
- Header = HeaderOr.get ();
77
+ if (auto E = IndexedCGData::Header::readFromBuffer (Start).moveInto (Header))
78
+ return std::move (E);
81
79
82
80
if (hasOutlinedHashTree ()) {
83
81
const unsigned char *Ptr = Start + Header.OutlinedHashTreeOffset ;
@@ -106,9 +104,9 @@ CodeGenDataReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
106
104
std::unique_ptr<CodeGenDataReader> Reader;
107
105
// Create the reader.
108
106
if (IndexedCodeGenDataReader::hasFormat (*Buffer))
109
- Reader. reset ( new IndexedCodeGenDataReader (std::move (Buffer) ));
107
+ Reader = std::make_unique< IndexedCodeGenDataReader> (std::move (Buffer));
110
108
else if (TextCodeGenDataReader::hasFormat (*Buffer))
111
- Reader. reset ( new TextCodeGenDataReader (std::move (Buffer) ));
109
+ Reader = std::make_unique< TextCodeGenDataReader> (std::move (Buffer));
112
110
else
113
111
return make_error<CGDataError>(cgdata_error::malformed);
114
112
@@ -132,31 +130,33 @@ bool IndexedCodeGenDataReader::hasFormat(const MemoryBuffer &DataBuffer) {
132
130
133
131
bool TextCodeGenDataReader::hasFormat (const MemoryBuffer &Buffer) {
134
132
// Verify that this really looks like plain ASCII text by checking a
135
- // 'reasonable' number of characters (up to profile magic size).
136
- size_t count = std::min (Buffer.getBufferSize (), sizeof (uint64_t ));
137
- StringRef buffer = Buffer.getBufferStart ();
138
- return count == 0 ||
139
- std::all_of (buffer.begin (), buffer.begin () + count,
140
- [](char c) { return isPrint (c) || isSpace (c); });
133
+ // 'reasonable' number of characters (up to the magic size).
134
+ StringRef Prefix = Buffer.getBuffer ().take_front (sizeof (uint64_t ));
135
+ return llvm::all_of (Prefix, [](char c) { return isPrint (c) || isSpace (c); });
141
136
}
142
137
Error TextCodeGenDataReader::read () {
143
138
using namespace support ;
144
139
145
140
// Parse the custom header line by line.
146
- while (Line->starts_with (" :" )) {
141
+ for (; !Line.is_at_eof (); ++Line) {
142
+ // Skip empty or whitespace-only lines
143
+ if (Line->trim ().empty ())
144
+ continue ;
145
+
146
+ if (!Line->starts_with (" :" ))
147
+ break ;
147
148
StringRef Str = Line->drop_front ().rtrim ();
148
149
if (Str.equals_insensitive (" outlined_hash_tree" ))
149
150
DataKind |= CGDataKind::FunctionOutlinedHashTree;
150
151
else
151
152
return error (cgdata_error::bad_header);
152
- ++Line;
153
153
}
154
154
155
155
// We treat an empty header (that is a comment # only) as a valid header.
156
156
if (Line.is_at_eof ()) {
157
- if (DataKind ! = CGDataKind::Unknown)
158
- return error (cgdata_error::bad_header );
159
- return Error::success ( );
157
+ if (DataKind = = CGDataKind::Unknown)
158
+ return Error::success ( );
159
+ return error (cgdata_error::bad_header );
160
160
}
161
161
162
162
// The YAML docs follow after the header.
0 commit comments