[PATCH] [MS Compat] Second try to fix quoting behavior of linker directives

Michael Kuperstein michael.m.kuperstein at intel.com
Sun Feb 15 07:48:01 PST 2015


Hi rnk, aaron.ballman,

Apparently, MSVC behavior is somewhat surprising:
For #pragma comment(linker, ...) it expects the comment string to be quoted, but for #pragma comment(lib, ...) it quotes the library name.

So, these are legal:
#pragma comment(linker, "/DEFAULTLIB:\"foo bar.lib\"")
#pragma comment(lib, "foo bar.lib")
And these get rejected:
#pragma comment(linker, "/DEFAULTLIB:foo bar.lib")
#pragma comment(lib, "\"foo bar.lib\"")

http://reviews.llvm.org/D7653

Files:
  lib/CodeGen/TargetInfo.cpp
  test/CodeGen/pragma-comment.c

Index: test/CodeGen/pragma-comment.c
===================================================================
--- test/CodeGen/pragma-comment.c
+++ test/CodeGen/pragma-comment.c
@@ -6,17 +6,21 @@
 #pragma comment(lib, "msvcrt.lib")
 #pragma comment(lib, "kernel32")
 #pragma comment(lib, "USER32.LIB")
+#pragma comment(lib, "with space")
 
 #define BAR "2"
 #pragma comment(linker," /bar=" BAR)
+#pragma comment(linker," /foo=\"foo bar\"")
 
 // CHECK: !llvm.module.flags = !{{{.*}}}
 // CHECK: !{{[0-9]+}} = !{i32 6, !"Linker Options", ![[link_opts:[0-9]+]]}
-// CHECK: ![[link_opts]] = !{![[msvcrt:[0-9]+]], ![[kernel32:[0-9]+]], ![[USER32:[0-9]+]], ![[bar:[0-9]+]]}
+// CHECK: ![[link_opts]] = !{![[msvcrt:[0-9]+]], ![[kernel32:[0-9]+]], ![[USER32:[0-9]+]], ![[space:[0-9]+]], ![[bar:[0-9]+]], ![[foo:[0-9]+]]}
 // CHECK: ![[msvcrt]] = !{!"/DEFAULTLIB:msvcrt.lib"}
 // CHECK: ![[kernel32]] = !{!"/DEFAULTLIB:kernel32.lib"}
 // CHECK: ![[USER32]] = !{!"/DEFAULTLIB:USER32.LIB"}
+// CHECK: ![[space]] = !{!"/DEFAULTLIB:\22with space.lib\22"}
 // CHECK: ![[bar]] = !{!" /bar=2"}
+// CHECK: ![[foo]] = !{!" /foo=\22foo bar\22"}
 
 // LINUX: !{!"-lmsvcrt.lib"}
 // LINUX: !{!"-lkernel32"}
Index: lib/CodeGen/TargetInfo.cpp
===================================================================
--- lib/CodeGen/TargetInfo.cpp
+++ lib/CodeGen/TargetInfo.cpp
@@ -1614,11 +1614,15 @@
 };
 
 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
-  // If the argument does not end in .lib, automatically add the suffix. This
-  // matches the behavior of MSVC.
-  std::string ArgStr = Lib;
+  // If the argument does not end in .lib, automatically add the suffix.
+  // If the argument contains a space, enclose it in quotes.
+  // This matches the behavior of MSVC.
+  bool Quote = (Lib.find(" ") != StringRef::npos);
+  std::string ArgStr = Quote ? "\"" : "";
+  ArgStr += Lib;
   if (!Lib.endswith_lower(".lib"))
     ArgStr += ".lib";
+  ArgStr += Quote ? "\"" : "";
   return ArgStr;
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7653.19983.patch
Type: text/x-patch
Size: 2029 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150215/faf10b0f/attachment.bin>


More information about the cfe-commits mailing list