Split out the command handling for split debug info, we're going
to want to propagate some information through the module into
the back end and so need to pass it through to codegen.

Also make the methods file static so we can use them in other places.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175916 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td
index 5e7a551..d1b21fd 100644
--- a/include/clang/Driver/CC1Options.td
+++ b/include/clang/Driver/CC1Options.td
@@ -379,6 +379,8 @@
   HelpText<"Generate weak vtables and RTTI with hidden visibility">;
 def main_file_name : Separate<["-"], "main-file-name">,
   HelpText<"Main file name to use for debug info">;
+def split_dwarf_file : Separate<["-"], "split-dwarf-file">,
+  HelpText<"File name to use for split dwarf debug info output">;
 def fno_signed_char : Flag<["-"], "fno-signed-char">,
   HelpText<"Char is unsigned">;
 def fno_wchar : Flag<["-"], "fno-wchar">,
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 6ba77c2..445ae53 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -1705,11 +1705,27 @@
   }
 }
 
-void Clang::SplitDebugInfo(Compilation &C, const JobAction &JA,
-                           const ArgList &Args,
-                           const InputInfoList &Inputs,
-                           const InputInfo &Output,
-                           const char *LinkingOutput) const {
+static const char *SplitDebugName(const ArgList &Args,
+                                  const InputInfoList &Inputs) {
+  Arg *FinalOutput = Args.getLastArg(options::OPT_o);
+  if (FinalOutput && Args.hasArg(options::OPT_c)) {
+    SmallString<128> T(FinalOutput->getValue());
+    llvm::sys::path::replace_extension(T, "dwo");
+    return Args.MakeArgString(T);
+  } else {
+    // Use the compilation dir.
+    SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
+    SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
+    llvm::sys::path::replace_extension(F, "dwo");
+    T += F;
+    return Args.MakeArgString(F);
+  }
+}
+
+static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
+                           const Tool &T, const JobAction &JA,
+                           const ArgList &Args, const InputInfo &Output,
+                           const char *OutFile) {
   ArgStringList ExtractArgs;
   ExtractArgs.push_back("--extract-dwo");
 
@@ -1719,31 +1735,16 @@
   // Grabbing the output of the earlier compile step.
   StripArgs.push_back(Output.getFilename());
   ExtractArgs.push_back(Output.getFilename());
-
-  // Add an output for the extract.
-  Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
-  const char *OutFile;
-  if (FinalOutput && C.getArgs().hasArg(options::OPT_c)) {
-    SmallString<128> T(FinalOutput->getValue());
-    llvm::sys::path::replace_extension(T, "dwo");
-    OutFile = Args.MakeArgString(T);
-  } else {
-    // Use the compilation dir.
-    SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
-    T += llvm::sys::path::stem(Inputs[0].getBaseInput());
-    llvm::sys::path::replace_extension(T, "dwo");
-    OutFile = Args.MakeArgString(T);
-  }
   ExtractArgs.push_back(OutFile);
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("objcopy"));
+    Args.MakeArgString(TC.GetProgramPath("objcopy"));
 
   // First extract the dwo sections.
-  C.addCommand(new Command(JA, *this, Exec, ExtractArgs));
+  C.addCommand(new Command(JA, T, Exec, ExtractArgs));
 
   // Then remove them from the original .o file.
-  C.addCommand(new Command(JA, *this, Exec, StripArgs));
+  C.addCommand(new Command(JA, T, Exec, StripArgs));
 }
 
 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
@@ -3264,15 +3265,25 @@
     CmdArgs.push_back(Args.MakeArgString(Flags.str()));
   }
 
-  // Finally add the command to the compilation.
+  // Add the split debug info name to the command lines here so we
+  // can propagate it to the backend.
+  bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
+    (getToolChain().getTriple().getOS() == llvm::Triple::Linux) &&
+    isa<AssembleJobAction>(JA);
+  const char *SplitDwarfOut;
+  if (SplitDwarf) {
+    CmdArgs.push_back("-split-dwarf-file");
+    SplitDwarfOut = SplitDebugName(Args, Inputs);
+    CmdArgs.push_back(SplitDwarfOut);
+  }
+
+  // Finally add the compile command to the compilation.
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 
   // Handle the debug info splitting at object creation time.
   // TODO: Currently only works on linux with newer objcopy.
-  if (Args.hasArg(options::OPT_gsplit_dwarf) &&
-      getToolChain().getTriple().getOS() == llvm::Triple::Linux &&
-      isa<AssembleJobAction>(JA))
-    SplitDebugInfo(C, JA, Args, Inputs, Output, LinkingOutput);
+  if (SplitDwarf)
+    SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
 
   if (Arg *A = Args.getLastArg(options::OPT_pg))
     if (Args.hasArg(options::OPT_fomit_frame_pointer))
diff --git a/lib/Driver/Tools.h b/lib/Driver/Tools.h
index 2d7f8b0..f4aebd8 100644
--- a/lib/Driver/Tools.h
+++ b/lib/Driver/Tools.h
@@ -54,10 +54,6 @@
     void AddSparcTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;
     void AddX86TargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;
     void AddHexagonTargetArgs (const ArgList &Args, ArgStringList &CmdArgs) const;
-    void SplitDebugInfo(Compilation &C, const JobAction &JA,
-                        const ArgList &Args, const InputInfoList &Inputs,
-                        const InputInfo &Output,
-                        const char *LinkingOutput) const;
 
     enum RewriteKind { RK_None, RK_Fragile, RK_NonFragile };
 
diff --git a/test/Driver/split-debug.c b/test/Driver/split-debug.c
index 5c77e6e..d8a9fe8 100644
--- a/test/Driver/split-debug.c
+++ b/test/Driver/split-debug.c
@@ -18,3 +18,8 @@
 // RUN: FileCheck -check-prefix=CHECK-BAD < %t %s
 //
 // CHECK-BAD-NOT: "Bad.dwo"
+
+// RUN: %clang -target x86_64-unknown-linux-gnu -gsplit-dwarf -c -### %s 2> %t
+// RUN: FileCheck -check-prefix=CHECK-OPTION < %t %s
+//
+// CHECK-OPTION: "-split-dwarf-file" "split-debug.dwo"