<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 17, 2015 at 9:28 AM, Rafael Espindola via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rafael<br>
Date: Wed Dec 16 16:28:34 2015<br>
New Revision: 255831<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=255831&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=255831&view=rev</a><br>
Log:<br>
Simplify memory management with std::unique_ptr.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ADT/Statistic.h<br>
    llvm/trunk/lib/Support/Statistic.cpp<br>
    llvm/trunk/lib/Support/Timer.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/Statistic.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=255831&r1=255830&r2=255831&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=255831&r1=255830&r2=255831&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/Statistic.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/Statistic.h Wed Dec 16 16:28:34 2015<br>
@@ -28,9 +28,11 @@<br>
<br>
 #include "llvm/Support/Atomic.h"<br>
 #include "llvm/Support/Valgrind.h"<br>
+#include <memory><br>
<br>
 namespace llvm {<br>
 class raw_ostream;<br>
+class raw_fd_ostream;<br>
<br>
 class Statistic {<br>
 public:<br>
@@ -170,6 +172,9 @@ void EnableStatistics();<br>
 /// \brief Check if statistics are enabled.<br>
 bool AreStatisticsEnabled();<br>
<br>
+/// \brief Return a file stream to print our output on.<br>
+std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();<br>
+<br>
 /// \brief Print statistics to the file returned by CreateInfoOutputFile().<br>
 void PrintStatistics();<br>
<br>
<br>
Modified: llvm/trunk/lib/Support/Statistic.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Statistic.cpp?rev=255831&r1=255830&r2=255831&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Statistic.cpp?rev=255831&r1=255830&r2=255831&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/Statistic.cpp (original)<br>
+++ llvm/trunk/lib/Support/Statistic.cpp Wed Dec 16 16:28:34 2015<br>
@@ -34,9 +34,6 @@<br>
 #include <cstring><br>
 using namespace llvm;<br>
<br>
-// CreateInfoOutputFile - Return a file stream to print our output on.<br>
-namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }<br>
-<br>
 /// -stats - Command line option to cause transformations to emit stats about<br>
 /// what they did.<br>
 ///<br>
@@ -145,20 +142,18 @@ void llvm::PrintStatistics() {<br>
   if (Stats.Stats.empty()) return;<br>
<br>
   // Get the stream to write to.<br>
-  raw_ostream &OutStream = *CreateInfoOutputFile();<br>
-  PrintStatistics(OutStream);<br>
-  delete &OutStream;   // Close the file.<br>
+  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();<br>
+  PrintStatistics(*OutStream);<br></blockquote><div><br></div><div>Looks like now that this doesn't need explicit cleanup, you could remove the variable, but that might be a bit unclear:<br><br>PrintStatistics(*CreateInfoOutputFile());<br><br>Can (or is it already) raw_ostream be made movable? In which case CretaeInfoInputFile could return by value? (but I guess then you couldn't take a non-const ref to it to pass it to something else anyway...)<br><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
 #else<br>
   // Check if the -stats option is set instead of checking<br>
   // !Stats.Stats.empty().  In release builds, Statistics operators<br>
   // do nothing, so stats are never Registered.<br>
   if (Enabled) {<br>
     // Get the stream to write to.<br>
-    raw_ostream &OutStream = *CreateInfoOutputFile();<br>
-    OutStream << "Statistics are disabled.  "<br>
-            << "Build with asserts or with -DLLVM_ENABLE_STATS\n";<br>
-    OutStream.flush();<br>
-    delete &OutStream;   // Close the file.<br>
+    std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();<br>
+    (*OutStream) << "Statistics are disabled.  "<br>
+                 << "Build with asserts or with -DLLVM_ENABLE_STATS\n";<br>
   }<br>
 #endif<br>
 }<br>
<br>
Modified: llvm/trunk/lib/Support/Timer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Timer.cpp?rev=255831&r1=255830&r2=255831&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Timer.cpp?rev=255831&r1=255830&r2=255831&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/Timer.cpp (original)<br>
+++ llvm/trunk/lib/Support/Timer.cpp Wed Dec 16 16:28:34 2015<br>
@@ -12,6 +12,7 @@<br>
 //===----------------------------------------------------------------------===//<br>
<br>
 #include "llvm/Support/Timer.h"<br>
+#include "llvm/ADT/Statistic.h"<br>
 #include "llvm/ADT/StringMap.h"<br>
 #include "llvm/Support/CommandLine.h"<br>
 #include "llvm/Support/FileSystem.h"<br>
@@ -22,9 +23,6 @@<br>
 #include "llvm/Support/raw_ostream.h"<br>
 using namespace llvm;<br>
<br>
-// CreateInfoOutputFile - Return a file stream to print our output on.<br>
-namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }<br>
-<br>
 // getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy<br>
 // of constructor/destructor ordering being unspecified by C++.  Basically the<br>
 // problem is that a Statistic object gets destroyed, which ends up calling<br>
@@ -52,28 +50,27 @@ namespace {<br>
                    cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));<br>
 }<br>
<br>
-// CreateInfoOutputFile - Return a file stream to print our output on.<br>
-raw_ostream *llvm::CreateInfoOutputFile() {<br>
+// Return a file stream to print our output on.<br>
+std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {<br>
   const std::string &OutputFilename = getLibSupportInfoOutputFilename();<br>
   if (OutputFilename.empty())<br>
-    return new raw_fd_ostream(2, false); // stderr.<br>
+    return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.<br>
   if (OutputFilename == "-")<br>
-    return new raw_fd_ostream(1, false); // stdout.<br>
-<br>
+    return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.<br>
+<br>
   // Append mode is used because the info output file is opened and closed<br>
   // each time -stats or -time-passes wants to print output to it. To<br>
   // compensate for this, the test-suite Makefiles have code to delete the<br>
   // info output file before running commands which write to it.<br>
   std::error_code EC;<br>
-  raw_ostream *Result = new raw_fd_ostream(OutputFilename, EC,<br>
-                                           sys::fs::F_Append | sys::fs::F_Text);<br>
+  auto Result = llvm::make_unique<raw_fd_ostream>(<br>
+      OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);<br>
   if (!EC)<br>
     return Result;<br>
-<br>
+<br>
   errs() << "Error opening info-output-file '"<br>
     << OutputFilename << " for appending!\n";<br>
-  delete Result;<br>
-  return new raw_fd_ostream(2, false); // stderr.<br>
+  return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.<br>
 }<br>
<br>
<br>
@@ -292,10 +289,9 @@ void TimerGroup::removeTimer(Timer &T) {<br>
   // them were started.<br>
   if (FirstTimer || TimersToPrint.empty())<br>
     return;<br>
-<br>
-  raw_ostream *OutStream = CreateInfoOutputFile();<br>
+<br>
+  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();<br>
   PrintQueuedTimers(*OutStream);<br>
-  delete OutStream;   // Close the file.<br>
 }<br>
<br>
 void TimerGroup::addTimer(Timer &T) {<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>