- std::string ErrorInfo;
- raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
- if (!ErrorInfo.empty())
- exitWithError(ErrorInfo, OutputFilename);
-
- if (Output.is_displayed())
- exitWithError("Refusing to write a binary file to stdout", OutputFilename);
-
- std::unique_ptr<MemoryBuffer> Buffer;
- if (error_code EC = MemoryBuffer::getFile(InputName, Buffer))
- exitWithError(EC.message(), InputName);
-
- ProfileDataWriter Writer;
- StringRef Name;
- uint64_t Hash, NumCounters;
- std::vector<uint64_t> Counters;
- for (line_iterator I(*Buffer, '#'); !I.is_at_end(); ++I) {
- if (I->empty())
- continue;
- Name = *I;
- if ((++I).is_at_end())
- exitWithError("Truncated file", InputName, I.line_number());
- if (I->getAsInteger(10, Hash))
- exitWithError("Failed to read hash", InputName, I.line_number());
- if ((++I).is_at_end())
- exitWithError("Truncated file", InputName, I.line_number());
- if (I->getAsInteger(10, NumCounters))
- exitWithError("Failed to read num counters", InputName, I.line_number());
- for (uint64_t CurCounter = 0; CurCounter < NumCounters; ++CurCounter) {
- uint64_t Counter;
- if ((++I).is_at_end())
- exitWithError("Truncated file", InputName, I.line_number());
- if (I->getAsInteger(10, Counter))
- exitWithError("Failed to read counter", InputName, I.line_number());
- Counters.push_back(Counter);
- }
- Writer.addFunctionCounts(Name, Hash, NumCounters, Counters.data());
- Counters.clear();
- }