Move ownership of Pass File's to the MachoLinkingContext.

In trying to fix the leaks in the MachO lld codebase, we need to have
a better model for file and atom ownership.  Having the context own
everything seems like the simplest model, so change all the passes to
allocate File's on the context instead of owning files as a member.

git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@264004 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/lld/ReaderWriter/MachOLinkingContext.h b/include/lld/ReaderWriter/MachOLinkingContext.h
index dc7ed92..4d1233f 100644
--- a/include/lld/ReaderWriter/MachOLinkingContext.h
+++ b/include/lld/ReaderWriter/MachOLinkingContext.h
@@ -13,6 +13,7 @@
 #include "lld/Core/LinkingContext.h"
 #include "lld/Core/Reader.h"
 #include "lld/Core/Writer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -93,6 +94,18 @@
 
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
 
+  /// Creates a new file which is owned by the context.  Returns a pointer to
+  /// the new file.
+  template <class T, class... Args>
+  typename std::enable_if<!std::is_array<T>::value, T *>::type
+  make_file(Args &&... args) const {
+    auto file = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+    auto *filePtr = file.get();
+    auto *ctx = const_cast<MachOLinkingContext *>(this);
+    ctx->getNodes().push_back(llvm::make_unique<FileNode>(std::move(file)));
+    return filePtr;
+  }
+
   uint32_t getCPUType() const;
   uint32_t getCPUSubType() const;
 
diff --git a/lib/ReaderWriter/MachO/CompactUnwindPass.cpp b/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
index 68cf1b9..d279303 100644
--- a/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
+++ b/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
@@ -273,7 +273,7 @@
 public:
   CompactUnwindPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o Compact Unwind Pass>"),
+        _file(*_ctx.make_file<MachOFile>("<mach-o Compact Unwind Pass>")),
         _isBig(MachOLinkingContext::isBigEndian(_ctx.arch())) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
@@ -567,7 +567,7 @@
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler &_archHandler;
-  MachOFile _file;
+  MachOFile &_file;
   bool _isBig;
 };
 
diff --git a/lib/ReaderWriter/MachO/GOTPass.cpp b/lib/ReaderWriter/MachO/GOTPass.cpp
index 7596af9..eac4864 100644
--- a/lib/ReaderWriter/MachO/GOTPass.cpp
+++ b/lib/ReaderWriter/MachO/GOTPass.cpp
@@ -91,7 +91,7 @@
 public:
   GOTPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o GOT Pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o GOT Pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -169,7 +169,7 @@
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                             &_archHandler;
-  MachOFile                                        _file;
+  MachOFile                                       &_file;
   llvm::DenseMap<const Atom*, const GOTEntryAtom*> _targetToGOT;
 };
 
diff --git a/lib/ReaderWriter/MachO/ObjCPass.cpp b/lib/ReaderWriter/MachO/ObjCPass.cpp
index e42f02a..13fa988 100644
--- a/lib/ReaderWriter/MachO/ObjCPass.cpp
+++ b/lib/ReaderWriter/MachO/ObjCPass.cpp
@@ -93,7 +93,7 @@
 public:
   ObjCPass(const MachOLinkingContext &context)
       : _ctx(context),
-        _file("<mach-o objc pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -113,7 +113,7 @@
   }
 
   const MachOLinkingContext   &_ctx;
-  MachOFile                   _file;
+  MachOFile                   &_file;
 };
 
 
diff --git a/lib/ReaderWriter/MachO/ShimPass.cpp b/lib/ReaderWriter/MachO/ShimPass.cpp
index 60b0d80..18ed8f8 100644
--- a/lib/ReaderWriter/MachO/ShimPass.cpp
+++ b/lib/ReaderWriter/MachO/ShimPass.cpp
@@ -42,7 +42,8 @@
 public:
   ShimPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _stubInfo(_archHandler.stubInfo()), _file("<mach-o shim pass>") {
+        _stubInfo(_archHandler.stubInfo()),
+        _file(*_ctx.make_file<MachOFile>("<mach-o shim pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -114,7 +115,7 @@
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                            &_archHandler;
   const ArchHandler::StubInfo                    &_stubInfo;
-  MachOFile                                       _file;
+  MachOFile                                      &_file;
   llvm::DenseMap<const Atom*, const DefinedAtom*> _targetToShim;
 };
 
diff --git a/lib/ReaderWriter/MachO/StubsPass.cpp b/lib/ReaderWriter/MachO/StubsPass.cpp
index 2e78d5e..e69d669 100644
--- a/lib/ReaderWriter/MachO/StubsPass.cpp
+++ b/lib/ReaderWriter/MachO/StubsPass.cpp
@@ -200,7 +200,8 @@
 public:
   StubsPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _stubInfo(_archHandler.stubInfo()), _file("<mach-o Stubs pass>") {
+        _stubInfo(_archHandler.stubInfo()),
+        _file(*_ctx.make_file<MachOFile>("<mach-o Stubs pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -356,7 +357,7 @@
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                            &_archHandler;
   const ArchHandler::StubInfo                    &_stubInfo;
-  MachOFile                                       _file;
+  MachOFile                                      &_file;
   TargetToUses                                    _targetToUses;
 };
 
diff --git a/lib/ReaderWriter/MachO/TLVPass.cpp b/lib/ReaderWriter/MachO/TLVPass.cpp
index fee07cb..b12f006 100644
--- a/lib/ReaderWriter/MachO/TLVPass.cpp
+++ b/lib/ReaderWriter/MachO/TLVPass.cpp
@@ -65,7 +65,7 @@
 public:
   TLVPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o TLV Pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o TLV pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -126,7 +126,7 @@
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler &_archHandler;
-  MachOFile _file;
+  MachOFile           &_file;
   llvm::DenseMap<const Atom*, const TLVPEntryAtom*> _targetToTLVP;
 };