|
22 | 22 | #include "llvm/Support/Signals.h" |
23 | 23 | #include "llvm/Support/ThreadPool.h" |
24 | 24 | #include <algorithm> |
| 25 | +#include <fstream> |
25 | 26 | #include <mutex> |
26 | 27 | #include <unordered_map> |
27 | 28 |
|
@@ -274,52 +275,40 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) { |
274 | 275 |
|
275 | 276 | if (isYAML(Filename)) |
276 | 277 | report_error(Filename, "cannot mix YAML and legacy formats"); |
277 | | - ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
278 | | - MemoryBuffer::getFileOrSTDIN(Filename); |
279 | | - if (std::error_code EC = MB.getError()) |
280 | | - report_error(Filename, EC); |
281 | 278 |
|
282 | | - StringRef Buf = MB.get()->getBuffer(); |
| 279 | + std::ifstream FdataFile(Filename, std::ios::in); |
| 280 | + std::string FdataLine; |
| 281 | + |
| 282 | + auto checkMode = [&](const std::string &Key, std::optional<bool> &Flag) { |
| 283 | + std::string ErrorMsg = "cannot mix profile with and without " + Key; |
| 284 | + auto Pos = FdataFile.tellg(); |
| 285 | + std::getline(FdataFile, FdataLine); |
| 286 | + if (FdataLine.rfind(Key, 0) == 0) { |
| 287 | + if (!Flag.value_or(true)) |
| 288 | + report_error(Filename, ErrorMsg); |
| 289 | + Flag = true; |
| 290 | + } else { |
| 291 | + if (Flag.value_or(false)) |
| 292 | + report_error(Filename, ErrorMsg); |
| 293 | + Flag = false; |
| 294 | + // Rewind line |
| 295 | + FdataFile.seekg(Pos); |
| 296 | + } |
| 297 | + }; |
| 298 | + |
283 | 299 | ProfileTy *Profile; |
284 | 300 | { |
285 | 301 | std::lock_guard<std::mutex> Lock(BoltedCollectionMutex); |
286 | 302 | // Check if the string "boltedcollection" is in the first line |
287 | | - if (Buf.starts_with("boltedcollection\n")) { |
288 | | - if (!BoltedCollection.value_or(true)) |
289 | | - report_error( |
290 | | - Filename, |
291 | | - "cannot mix profile collected in BOLT and non-BOLT deployments"); |
292 | | - BoltedCollection = true; |
293 | | - Buf = Buf.drop_front(17); |
294 | | - } else { |
295 | | - if (BoltedCollection.value_or(false)) |
296 | | - report_error( |
297 | | - Filename, |
298 | | - "cannot mix profile collected in BOLT and non-BOLT deployments"); |
299 | | - BoltedCollection = false; |
300 | | - } |
| 303 | + checkMode("boltedcollection", BoltedCollection); |
301 | 304 | // Check if the string "no_lbr" is in the first line |
302 | 305 | // (or second line if BoltedCollection is true) |
303 | | - size_t CheckNoLBRPos = Buf.find('\n'); |
304 | | - if (CheckNoLBRPos != StringRef::npos) { |
305 | | - StringRef FirstLine = Buf.substr(0, CheckNoLBRPos); |
306 | | - if (FirstLine.contains("no_lbr")) { |
307 | | - if (!NoLBRCollection.value_or(true)) |
308 | | - report_error(Filename, "cannot mix 'no_lbr' and 'lbr' profiles"); |
309 | | - NoLBRCollection = true; |
310 | | - Buf = Buf.drop_front(CheckNoLBRPos + 1); |
311 | | - } else { |
312 | | - if (NoLBRCollection.value_or(false)) |
313 | | - report_error(Filename, "cannot mix 'no_lbr' and 'lbr' profiles"); |
314 | | - NoLBRCollection = false; |
315 | | - } |
316 | | - } |
| 306 | + checkMode("no_lbr", NoLBRCollection); |
317 | 307 | Profile = &Profiles[tid]; |
318 | 308 | } |
319 | 309 |
|
320 | | - SmallVector<StringRef> Lines; |
321 | | - SplitString(Buf, Lines, "\n"); |
322 | | - for (StringRef Line : Lines) { |
| 310 | + while (std::getline(FdataFile, FdataLine)) { |
| 311 | + StringRef Line(FdataLine); |
323 | 312 | size_t Pos = Line.rfind(" "); |
324 | 313 | if (Pos == StringRef::npos) |
325 | 314 | report_error(Filename, "Malformed / corrupted profile"); |
|
0 commit comments