[PATCH][Driver] Teach the clang driver how to correctly parse response files on Windows.

Andrea Di Biagio andrea.dibiagio at gmail.com
Fri Mar 14 04:14:08 PDT 2014


Hi,

This patch teaches the driver how to use function
'llvm::cl::TokenizeWindowsCommandLine' to correctly parse response
files on Windows.

Microsoft defines specific rules for parsing c/c++ command-line
arguments here: http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
.
Those rules are currently implemented by function
TokenizeWindowsCommandLine in llvm "lib/Support/CommandLine.cpp".

However, the clang driver doesn't call that method when the host is
WIN32. Instead, we always call function
llvm::cl::TokenizeGNUCommandLine.

This is problematic, for example, when we have backslashes in a
response file (see test backslash-in-response-file.c in the patch).

This patch also adds XFAIL win32 to the driver test Driver/at_file.c.
That test verifies that the driver correctly expands arguments
surrounded by single quotes. On Windows, that test would start failing
because, single quotation marks cannot be used to identify single
arguments.

The fix for Driver/at_file.c is probably not ideal. Adding XFAIL win32
means that the test would be an expected failure if the target triple
contains 'win32'. This is ok in general if both host and target are
the same (i.e. we are not testing a cross-compiler). This is a problem
however for cross compilers where the host is win32 but the target is
not  win32.

At the moment there is no way to tell lit that the test should fail
based on the information of the host platform (and not only on the
target triple). I have an idea about how to improve LIT and
consequently test Driver/at_file.c. If ok, I plan to send another
patch for improving it.

Please let me know if this patch is ok to submit.

Thanks in advance,

Andrea Di Biagio
SN Systems - Sony Computer Entertainment Group
-------------- next part --------------
Index: test/Driver/backslash-in-response-file.c
===================================================================
--- test/Driver/backslash-in-response-file.c	(revision 0)
+++ test/Driver/backslash-in-response-file.c	(working copy)
@@ -0,0 +1,9 @@
+// RUN: %clang -### -c @Inputs/response-file.txt %s 2>&1 | FileCheck %s
+
+// REQUIRES: win32
+
+// Make sure that the driver correctly expands the backslash from the response
+// file Inputs/response-file.txt.
+
+// CHECK: "-I" ".\\"
+
Index: test/Driver/at_file.c
===================================================================
--- test/Driver/at_file.c	(revision 203914)
+++ test/Driver/at_file.c	(working copy)
@@ -3,6 +3,15 @@
 // RUN: %clang -E %s @%s.args.utf16le -o %t.log
 // RUN: FileCheck --input-file=%t.log %s
 
+// On Windows, strings surrounded by single quotation marks are not interpreted
+// as a single argument. This test would fail on Windows with the follwing
+// error messages:
+//   error: no such file or directory: 'zed3''
+//   error: no such file or directory: ''-Dfoo5=bar5'
+//   error: no such file or directory: 'zed5''
+//
+// XFAIL: win32
+
 // CHECK: bar1
 // CHECK-NEXT: bar2 zed2
 // CHECK-NEXT: bar3 zed3
Index: test/Driver/Inputs/response-file.txt
===================================================================
--- test/Driver/Inputs/response-file.txt	(revision 0)
+++ test/Driver/Inputs/response-file.txt	(working copy)
@@ -0,0 +1 @@
+-I.\ 
Index: tools/driver/driver.cpp
===================================================================
--- tools/driver/driver.cpp	(revision 203914)
+++ tools/driver/driver.cpp	(working copy)
@@ -308,7 +308,14 @@
 
   std::set<std::string> SavedStrings;
   StringSetSaver Saver(SavedStrings);
+
+
+#ifdef LLVM_ON_WIN32
+  // Make sure that we correctly expand response files on windows.
+  llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeWindowsCommandLine, argv);
+#else
   llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, argv);
+#endif
 
   // Handle -cc1 integrated tools.
   if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) {


More information about the cfe-commits mailing list