r182647 - Suffixing #pragma comment(lib) library names with .lib if necessary. This matches MSVC behavior, as well as allows us to properly link libraries such as the ones provided by the MSDN examples.

Aaron Ballman aaron at aaronballman.com
Fri May 24 08:06:56 PDT 2013


Author: aaronballman
Date: Fri May 24 10:06:56 2013
New Revision: 182647

URL: http://llvm.org/viewvc/llvm-project?rev=182647&view=rev
Log:
Suffixing #pragma comment(lib) library names with .lib if necessary.  This matches MSVC behavior, as well as allows us to properly link libraries such as the ones provided by the MSDN examples.

Modified:
    cfe/trunk/lib/CodeGen/TargetInfo.cpp
    cfe/trunk/lib/Sema/SemaAttr.cpp
    cfe/trunk/test/CodeGen/pragma-comment.c
    cfe/trunk/test/Modules/autolink.m

Modified: cfe/trunk/lib/CodeGen/TargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/TargetInfo.cpp?rev=182647&r1=182646&r2=182647&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/TargetInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/TargetInfo.cpp Fri May 24 10:06:56 2013
@@ -1266,6 +1266,17 @@ public:
 
 };
 
+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 (Lib.size() <= 4 ||
+      Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
+    ArgStr += ".lib";
+  }
+  return ArgStr;
+}
+
 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
 public:
   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms)
@@ -1274,7 +1285,7 @@ public:
   void getDependentLibraryOption(llvm::StringRef Lib,
                                  llvm::SmallString<24> &Opt) const {
     Opt = "/DEFAULTLIB:";
-    Opt += Lib;
+    Opt += qualifyWindowsLibrary(Lib);
   }
 };
 
@@ -1300,7 +1311,7 @@ public:
   void getDependentLibraryOption(llvm::StringRef Lib,
                                  llvm::SmallString<24> &Opt) const {
     Opt = "/DEFAULTLIB:";
-    Opt += Lib;
+    Opt += qualifyWindowsLibrary(Lib);
   }
 };
 

Modified: cfe/trunk/lib/Sema/SemaAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAttr.cpp?rev=182647&r1=182646&r2=182647&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAttr.cpp Fri May 24 10:06:56 2013
@@ -272,10 +272,9 @@ void Sema::ActOnPragmaMSComment(PragmaMS
   case PCK_Linker:
     Consumer.HandleLinkerOptionPragma(Arg);
     return;
-  case PCK_Lib: {
+  case PCK_Lib:
     Consumer.HandleDependentLibrary(Arg);
     return;
-  }
   case PCK_Compiler:
   case PCK_ExeStr:
   case PCK_User:

Modified: cfe/trunk/test/CodeGen/pragma-comment.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/pragma-comment.c?rev=182647&r1=182646&r2=182647&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/pragma-comment.c (original)
+++ cfe/trunk/test/CodeGen/pragma-comment.c Fri May 24 10:06:56 2013
@@ -3,15 +3,21 @@
 // RUN: %clang_cc1 %s -triple i686-pc-linux -fms-extensions -emit-llvm -o - | FileCheck -check-prefix LINUX %s
 
 #pragma comment(lib, "msvcrt.lib")
+#pragma comment(lib, "kernel32")
+#pragma comment(lib, "USER32.LIB")
 
 #define BAR "2"
 #pragma comment(linker," /bar=" BAR)
 
 // CHECK: !llvm.module.flags = !{!0}
 // CHECK: !0 = metadata !{i32 6, metadata !"Linker Options", metadata ![[link_opts:[0-9]+]]}
-// CHECK: ![[link_opts]] = metadata !{metadata ![[msvcrt:[0-9]+]], metadata ![[bar:[0-9]+]]}
+// CHECK: ![[link_opts]] = metadata !{metadata ![[msvcrt:[0-9]+]], metadata ![[kernel32:[0-9]+]], metadata ![[USER32:[0-9]+]], metadata ![[bar:[0-9]+]]}
 // CHECK: ![[msvcrt]] = metadata !{metadata !"/DEFAULTLIB:msvcrt.lib"}
+// CHECK: ![[kernel32]] = metadata !{metadata !"/DEFAULTLIB:kernel32.lib"}
+// CHECK: ![[USER32]] = metadata !{metadata !"/DEFAULTLIB:USER32.LIB"}
 // CHECK: ![[bar]] = metadata !{metadata !" /bar=2"}
 
 // LINUX: metadata !{metadata !"-lmsvcrt.lib"}
+// LINUX: metadata !{metadata !"-lkernel32"}
+// LINUX: metadata !{metadata !"-lUSER32.LIB"}
 // LINUX: metadata !{metadata !" /bar=2"}

Modified: cfe/trunk/test/Modules/autolink.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/autolink.m?rev=182647&r1=182646&r2=182647&view=diff
==============================================================================
--- cfe/trunk/test/Modules/autolink.m (original)
+++ cfe/trunk/test/Modules/autolink.m Fri May 24 10:06:56 2013
@@ -35,7 +35,7 @@ int use_no_umbrella() {
 // CHECK: !4 = metadata !{i32 6, metadata !"Linker Options", metadata ![[AUTOLINK_OPTIONS:[0-9]+]]}
 // CHECK: ![[AUTOLINK_OPTIONS]] = metadata !{metadata ![[AUTOLINK_FRAMEWORK:[0-9]+]], metadata ![[AUTOLINK:[0-9]+]], metadata ![[DEPENDSONMODULE:[0-9]+]], metadata ![[MODULE:[0-9]+]], metadata ![[NOUMBRELLA:[0-9]+]]}
 // CHECK: ![[AUTOLINK_FRAMEWORK]] = metadata !{metadata !"-framework", metadata !"autolink_framework"}
-// CHECK: ![[AUTOLINK]] = metadata !{metadata !"{{(-l|/DEFAULTLIB:)}}autolink"}
+// CHECK: ![[AUTOLINK]] = metadata !{metadata !"{{(-l|/DEFAULTLIB:)}}autolink{{(\.lib)?}}"}
 // CHECK: ![[DEPENDSONMODULE]] = metadata !{metadata !"-framework", metadata !"DependsOnModule"}
 // CHECK: ![[MODULE]] = metadata !{metadata !"-framework", metadata !"Module"}
 // CHECK: ![[NOUMBRELLA]] = metadata !{metadata !"-framework", metadata !"NoUmbrella"}





More information about the cfe-commits mailing list