[llvm] r311287 - [dlltool] Make memory buffer ownership less weird.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 20 06:03:32 PDT 2017


Author: d0k
Date: Sun Aug 20 06:03:32 2017
New Revision: 311287

URL: http://llvm.org/viewvc/llvm-project?rev=311287&view=rev
Log:
[dlltool] Make memory buffer ownership less weird.

There's no reason to destroy them in a global destructor.

Modified:
    llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp

Modified: llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp?rev=311287&r1=311286&r2=311287&view=diff
==============================================================================
--- llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp (original)
+++ llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp Sun Aug 20 06:03:32 2017
@@ -56,21 +56,16 @@ public:
 
 } // namespace
 
-std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
-
 // Opens a file. Path has to be resolved already.
-// Newly created memory buffers are owned by this driver.
-Optional<MemoryBufferRef> openFile(StringRef Path) {
+static std::unique_ptr<MemoryBuffer> openFile(const Twine &Path) {
   ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB = MemoryBuffer::getFile(Path);
 
   if (std::error_code EC = MB.getError()) {
     llvm::errs() << "cannot open file " << Path << ": " << EC.message() << "\n";
-    return None;
+    return nullptr;
   }
 
-  MemoryBufferRef MBRef = MB.get()->getMemBufferRef();
-  OwningMBs.push_back(std::move(MB.get())); // take ownership
-  return MBRef;
+  return std::move(*MB);
 }
 
 static MachineTypes getEmulation(StringRef S) {
@@ -82,7 +77,7 @@ static MachineTypes getEmulation(StringR
       .Default(IMAGE_FILE_MACHINE_UNKNOWN);
 }
 
-static std::string getImplibPath(std::string Path) {
+static std::string getImplibPath(StringRef Path) {
   SmallString<128> Out = StringRef("lib");
   Out.append(Path);
   sys::path::replace_extension(Out, ".a");
@@ -122,7 +117,8 @@ int llvm::dlltoolDriverMain(llvm::ArrayR
     return 1;
   }
 
-  Optional<MemoryBufferRef> MB = openFile(Args.getLastArg(OPT_d)->getValue());
+  std::unique_ptr<MemoryBuffer> MB =
+      openFile(Args.getLastArg(OPT_d)->getValue());
   if (!MB)
     return 1;
 




More information about the llvm-commits mailing list