Re-apply r249644: Handle inline stacks in gcov-encoded sample profiles.
[oota-llvm.git] / tools / llvm-profdata / llvm-profdata.cpp
index fdf22c3642fd1d076a45cd104ea4e543cf14436e..382ac908da4d09b320638eeae12c2e1bfcdb9c1e 100644 (file)
@@ -77,13 +77,19 @@ static void mergeSampleProfile(const cl::list<std::string> &Inputs,
 
   auto Writer = std::move(WriterOrErr.get());
   StringMap<FunctionSamples> ProfileMap;
+  SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
   for (const auto &Filename : Inputs) {
     auto ReaderOrErr =
         SampleProfileReader::create(Filename, getGlobalContext());
     if (std::error_code EC = ReaderOrErr.getError())
       exitWithError(EC.message(), Filename);
 
-    auto Reader = std::move(ReaderOrErr.get());
+    // We need to keep the readers around until after all the files are
+    // read so that we do not lose the function names stored in each
+    // reader's memory. The function names are needed to write out the
+    // merged profile map.
+    Readers.push_back(std::move(ReaderOrErr.get()));
+    const auto Reader = Readers.back().get();
     if (std::error_code EC = Reader->read())
       exitWithError(EC.message(), Filename);