Move ownership of Action objects into Compilation.

Summary:
This makes constructing Action graphs which are DAGs much simpler.  It
also just simplifies in general the ownership semantics of Actions.

Depends on D15910.

Reviewers: echristo

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D15911

llvm-svn: 257407
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 0969a7a..14ba755 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1049,19 +1049,15 @@
           << types::getTypeName(Act->getType());
 
     ActionList Inputs;
-    for (unsigned i = 0, e = Archs.size(); i != e; ++i) {
-      Inputs.push_back(
-          new BindArchAction(std::unique_ptr<Action>(Act), Archs[i]));
-      if (i != 0)
-        Inputs.back()->setOwnsInputs(false);
-    }
+    for (unsigned i = 0, e = Archs.size(); i != e; ++i)
+      Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i]));
 
     // Lipo if necessary, we do it this way because we need to set the arch flag
     // so that -Xarch_ gets overwritten.
     if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
       Actions.append(Inputs.begin(), Inputs.end());
     else
-      Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
+      Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType()));
 
     // Handle debug info queries.
     Arg *A = Args.getLastArg(options::OPT_g_Group);
@@ -1077,15 +1073,16 @@
         ActionList Inputs;
         Inputs.push_back(Actions.back());
         Actions.pop_back();
-        Actions.push_back(new DsymutilJobAction(Inputs, types::TY_dSYM));
+        Actions.push_back(
+            C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM));
       }
 
       // Verify the debug info output.
       if (Args.hasArg(options::OPT_verify_debug_info)) {
-        std::unique_ptr<Action> VerifyInput(Actions.back());
+        Action* LastAction = Actions.back();
         Actions.pop_back();
-        Actions.push_back(new VerifyDebugInfoJobAction(std::move(VerifyInput),
-                                                       types::TY_Nothing));
+        Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>(
+            LastAction, types::TY_Nothing));
       }
     }
   }
@@ -1283,16 +1280,15 @@
 // Actions and /p Current is released. Otherwise the function creates
 // and returns a new CudaHostAction which wraps /p Current and device
 // side actions.
-static std::unique_ptr<Action>
-buildCudaActions(Compilation &C, DerivedArgList &Args, const Arg *InputArg,
-                 std::unique_ptr<Action> HostAction, ActionList &Actions) {
+static Action *buildCudaActions(Compilation &C, DerivedArgList &Args,
+                                const Arg *InputArg, Action *HostAction,
+                                ActionList &Actions) {
   Arg *PartialCompilationArg = Args.getLastArg(options::OPT_cuda_host_only,
                                                options::OPT_cuda_device_only);
   // Host-only compilation case.
   if (PartialCompilationArg &&
       PartialCompilationArg->getOption().matches(options::OPT_cuda_host_only))
-    return std::unique_ptr<Action>(
-        new CudaHostAction(std::move(HostAction), {}));
+    return C.MakeAction<CudaHostAction>(HostAction, ActionList());
 
   // Collect all cuda_gpu_arch parameters, removing duplicates.
   SmallVector<const char *, 4> GpuArchList;
@@ -1347,12 +1343,12 @@
     }
 
     for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
-      Actions.push_back(new CudaDeviceAction(
-          std::unique_ptr<Action>(CudaDeviceActions[I]), GpuArchList[I],
-          /* AtTopLevel */ true));
+      Actions.push_back(C.MakeAction<CudaDeviceAction>(CudaDeviceActions[I],
+                                                       GpuArchList[I],
+                                                       /* AtTopLevel */ true));
     // Kill host action in case of device-only compilation.
     if (DeviceOnlyCompilation)
-      HostAction.reset(nullptr);
+      return nullptr;
     return HostAction;
   }
 
@@ -1360,13 +1356,12 @@
   // with AtTopLevel=false and become inputs for the host action.
   ActionList DeviceActions;
   for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
-    DeviceActions.push_back(new CudaDeviceAction(
-        std::unique_ptr<Action>(CudaDeviceActions[I]), GpuArchList[I],
-        /* AtTopLevel */ false));
+    DeviceActions.push_back(
+        C.MakeAction<CudaDeviceAction>(CudaDeviceActions[I], GpuArchList[I],
+                                       /* AtTopLevel */ false));
   // Return a new host action that incorporates original host action and all
   // device actions.
-  return std::unique_ptr<Action>(
-      new CudaHostAction(std::move(HostAction), DeviceActions));
+  return C.MakeAction<CudaHostAction>(HostAction, DeviceActions);
 }
 
 void Driver::BuildActions(Compilation &C, const ToolChain &TC,
@@ -1474,7 +1469,7 @@
             : FinalPhase;
 
     // Build the pipeline for this file.
-    std::unique_ptr<Action> Current(new InputAction(*InputArg, InputType));
+    Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
     for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end();
          i != e; ++i) {
       phases::ID Phase = *i;
@@ -1486,7 +1481,8 @@
       // Queue linker inputs.
       if (Phase == phases::Link) {
         assert((i + 1) == e && "linking must be final compilation step.");
-        LinkerInputs.push_back(Current.release());
+        LinkerInputs.push_back(Current);
+        Current = nullptr;
         break;
       }
 
@@ -1497,11 +1493,10 @@
         continue;
 
       // Otherwise construct the appropriate action.
-      Current = ConstructPhaseAction(TC, Args, Phase, std::move(Current));
+      Current = ConstructPhaseAction(C, TC, Args, Phase, Current);
 
       if (InputType == types::TY_CUDA && Phase == CudaInjectionPhase) {
-        Current =
-            buildCudaActions(C, Args, InputArg, std::move(Current), Actions);
+        Current = buildCudaActions(C, Args, InputArg, Current, Actions);
         if (!Current)
           break;
       }
@@ -1512,12 +1507,13 @@
 
     // If we ended with something, add to the output list.
     if (Current)
-      Actions.push_back(Current.release());
+      Actions.push_back(Current);
   }
 
   // Add a link action if necessary.
   if (!LinkerInputs.empty())
-    Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
+    Actions.push_back(
+        C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image));
 
   // If we are linking, claim any options which are obviously only used for
   // compilation.
@@ -1534,10 +1530,9 @@
   Args.ClaimAllArgs(options::OPT_cuda_host_only);
 }
 
-std::unique_ptr<Action>
-Driver::ConstructPhaseAction(const ToolChain &TC, const ArgList &Args,
-                             phases::ID Phase,
-                             std::unique_ptr<Action> Input) const {
+Action *Driver::ConstructPhaseAction(Compilation &C, const ToolChain &TC,
+                                     const ArgList &Args, phases::ID Phase,
+                                     Action *Input) const {
   llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
   // Build the appropriate action.
   switch (Phase) {
@@ -1557,7 +1552,7 @@
       assert(OutputTy != types::TY_INVALID &&
              "Cannot preprocess this input type!");
     }
-    return llvm::make_unique<PreprocessJobAction>(std::move(Input), OutputTy);
+    return C.MakeAction<PreprocessJobAction>(Input, OutputTy);
   }
   case phases::Precompile: {
     types::ID OutputTy = types::TY_PCH;
@@ -1565,53 +1560,43 @@
       // Syntax checks should not emit a PCH file
       OutputTy = types::TY_Nothing;
     }
-    return llvm::make_unique<PrecompileJobAction>(std::move(Input), OutputTy);
+    return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
   }
   case phases::Compile: {
     if (Args.hasArg(options::OPT_fsyntax_only))
-      return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                                 types::TY_Nothing);
+      return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing);
     if (Args.hasArg(options::OPT_rewrite_objc))
-      return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                                 types::TY_RewrittenObjC);
+      return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC);
     if (Args.hasArg(options::OPT_rewrite_legacy_objc))
-      return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                                 types::TY_RewrittenLegacyObjC);
+      return C.MakeAction<CompileJobAction>(Input,
+                                            types::TY_RewrittenLegacyObjC);
     if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto))
-      return llvm::make_unique<AnalyzeJobAction>(std::move(Input),
-                                                 types::TY_Plist);
+      return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist);
     if (Args.hasArg(options::OPT__migrate))
-      return llvm::make_unique<MigrateJobAction>(std::move(Input),
-                                                 types::TY_Remap);
+      return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap);
     if (Args.hasArg(options::OPT_emit_ast))
-      return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                                 types::TY_AST);
+      return C.MakeAction<CompileJobAction>(Input, types::TY_AST);
     if (Args.hasArg(options::OPT_module_file_info))
-      return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                                 types::TY_ModuleFile);
+      return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile);
     if (Args.hasArg(options::OPT_verify_pch))
-      return llvm::make_unique<VerifyPCHJobAction>(std::move(Input),
-                                                   types::TY_Nothing);
-    return llvm::make_unique<CompileJobAction>(std::move(Input),
-                                               types::TY_LLVM_BC);
+      return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
+    return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
   }
   case phases::Backend: {
     if (isUsingLTO()) {
       types::ID Output =
           Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
-      return llvm::make_unique<BackendJobAction>(std::move(Input), Output);
+      return C.MakeAction<BackendJobAction>(Input, Output);
     }
     if (Args.hasArg(options::OPT_emit_llvm)) {
       types::ID Output =
           Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
-      return llvm::make_unique<BackendJobAction>(std::move(Input), Output);
+      return C.MakeAction<BackendJobAction>(Input, Output);
     }
-    return llvm::make_unique<BackendJobAction>(std::move(Input),
-                                               types::TY_PP_Asm);
+    return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
   }
   case phases::Assemble:
-    return llvm::make_unique<AssembleJobAction>(std::move(Input),
-                                                types::TY_Object);
+    return C.MakeAction<AssembleJobAction>(Input, types::TY_Object);
   }
 
   llvm_unreachable("invalid phase in ConstructPhaseAction");