@@ -278,22 +278,18 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
278278
279279 std::ifstream FdataFile (Filename, std::ios::in);
280280 std::string FdataLine;
281+ std::getline (FdataFile, FdataLine);
281282
282283 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- }
284+ const bool KeyIsSet = FdataLine.rfind (Key, 0 ) == 0 ;
285+
286+ if (!Flag.has_value ())
287+ Flag = KeyIsSet;
288+ else if (*Flag != KeyIsSet)
289+ report_error (Filename, " cannot mix profile with and without " + Key);
290+ if (KeyIsSet)
291+ // Advance line
292+ std::getline (FdataFile, FdataLine);
297293 };
298294
299295 ProfileTy *Profile;
@@ -307,7 +303,7 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
307303 Profile = &Profiles[tid];
308304 }
309305
310- while ( std::getline (FdataFile, FdataLine)) {
306+ do {
311307 StringRef Line (FdataLine);
312308 size_t Pos = Line.rfind (" " );
313309 if (Pos == StringRef::npos)
@@ -318,7 +314,7 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
318314 report_error (Filename, " Malformed / corrupted profile counter" );
319315 Count += Profile->lookup (Signature);
320316 Profile->insert_or_assign (Signature, Count);
321- }
317+ } while ( std::getline (FdataFile, FdataLine));
322318 };
323319
324320 // The final reduction has non-trivial cost, make sure each thread has at
0 commit comments