Merge pull request #19321 from davidungar/rdar-44458502-fix-spaces-in-files-in-resp-files-swift-4.2

Fix response file quote bug, add flag for testing, add tests.
diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td
index 5825adb..0262fda 100644
--- a/include/swift/Option/Options.td
+++ b/include/swift/Option/Options.td
@@ -120,6 +120,10 @@
   InternalDebugOpt,
   HelpText<"Force one batch repartitioning for testing">;
 
+def driver_force_response_files : Flag<["-"], "driver-force-response-files">,
+  InternalDebugOpt,
+  HelpText<"Force the use of response files for testing">;
+
 def driver_always_rebuild_dependents :
   Flag<["-"], "driver-always-rebuild-dependents">, InternalDebugOpt,
   HelpText<"Always rebuild dependents of files that have been modified">;
diff --git a/lib/Driver/Job.cpp b/lib/Driver/Job.cpp
index fd5f39d..e139d2e 100644
--- a/lib/Driver/Job.cpp
+++ b/lib/Driver/Job.cpp
@@ -425,9 +425,8 @@
     return true;
   }
   for (const char *arg : Arguments) {
-    OS << "\"";
     escapeAndPrintString(OS, arg);
-    OS << "\" ";
+    OS << " ";
   }
   OS.flush();
   return false;
diff --git a/lib/Driver/ToolChain.cpp b/lib/Driver/ToolChain.cpp
index 31e42ec..aa96992 100644
--- a/lib/Driver/ToolChain.cpp
+++ b/lib/Driver/ToolChain.cpp
@@ -21,6 +21,7 @@
 #include "swift/Driver/Compilation.h"
 #include "swift/Driver/Driver.h"
 #include "swift/Driver/Job.h"
+#include "swift/Option/Options.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/Option/ArgList.h"
@@ -120,9 +121,15 @@
 
   const char *responseFilePath = nullptr;
   const char *responseFileArg = nullptr;
-  if (invocationInfo.allowsResponseFiles &&
-      !llvm::sys::commandLineFitsWithinSystemLimits(
-          executablePath, invocationInfo.Arguments)) {
+
+  const bool forceResponseFiles =
+      C.getArgs().hasArg(options::OPT_driver_force_response_files);
+  assert((invocationInfo.allowsResponseFiles || !forceResponseFiles) &&
+         "Cannot force response file if platform does not allow it");
+
+  if (forceResponseFiles || (invocationInfo.allowsResponseFiles &&
+                             !llvm::sys::commandLineFitsWithinSystemLimits(
+                                 executablePath, invocationInfo.Arguments))) {
     responseFilePath = context.getTemporaryFilePath("arguments", "resp");
     responseFileArg = C.getArgs().MakeArgString(Twine("@") + responseFilePath);
   }
diff --git a/test/Driver/force-response-files.swift b/test/Driver/force-response-files.swift
new file mode 100644
index 0000000..2b97797
--- /dev/null
+++ b/test/Driver/force-response-files.swift
@@ -0,0 +1,6 @@
+// Ensure that -driver-force-response-files works.
+
+
+// RUN: %swiftc_driver -driver-force-response-files -typecheck %S/../Inputs/empty.swift -### 2>&1 | %FileCheck %s
+// CHECK: @
+// CHECK: .resp
diff --git a/test/Driver/response-files-with-spaces-in-filenames.swift b/test/Driver/response-files-with-spaces-in-filenames.swift
new file mode 100644
index 0000000..d12c3f0
--- /dev/null
+++ b/test/Driver/response-files-with-spaces-in-filenames.swift
@@ -0,0 +1,4 @@
+// RUN: %empty-directory(%t)
+// RUN: touch "%t/f i l e.swift"
+//
+// RUN: %target-build-swift -driver-force-response-files -parse  "%t/f i l e.swift"