[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd closed this revision.
compnerd added a comment.

SVN r324438


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd added inline comments.



Comment at: docs/LanguageExtensions.rst:2732
+
+The ``#pragma comment(lib, ...)`` directive is supported on all ELF targets.
+The second parameter is the library name (without the traditional Unix prefix 
of

erichkeane wrote:
> The newlines in this section are seemingly inconsistent with what is above.  
> Does that one violate the 80 char rule, or is this just short?
Yeah, the previous section has 80-column violations :-(.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread Erich Keane via Phabricator via cfe-commits
erichkeane accepted this revision.
erichkeane added a comment.
This revision is now accepted and ready to land.

Just 1 format question, otherwise Looks good.




Comment at: docs/LanguageExtensions.rst:2732
+
+The ``#pragma comment(lib, ...)`` directive is supported on all ELF targets.
+The second parameter is the library name (without the traditional Unix prefix 
of

The newlines in this section are seemingly inconsistent with what is above.  
Does that one violate the 80 char rule, or is this just short?


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd updated this revision to Diff 133030.
compnerd added a comment.

Add additional test, update docs


Repository:
  rC Clang

https://reviews.llvm.org/D42758

Files:
  docs/LanguageExtensions.rst
  lib/CodeGen/CodeGenModule.cpp
  lib/CodeGen/CodeGenModule.h
  lib/Parse/ParsePragma.cpp
  test/CodeGen/elf-linker-options.c
  test/CodeGen/pragma-comment.c
  test/Preprocessor/pragma-comment-linux.c
  test/Preprocessor/pragma_microsoft.c

Index: test/Preprocessor/pragma_microsoft.c
===
--- test/Preprocessor/pragma_microsoft.c
+++ test/Preprocessor/pragma_microsoft.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -fms-extensions -Wunknown-pragmas
-// RUN: not %clang_cc1 %s -fms-extensions -E | FileCheck %s
+// RUN: %clang_cc1 -triple i686-unknown-windows-msvc %s -fsyntax-only -verify -fms-extensions -Wunknown-pragmas
+// RUN: not %clang_cc1 -triple i686-unknown-windows-msvc %s -fms-extensions -E | FileCheck %s
 // REQUIRES: non-ps4-sdk

 // rdar://6495941
Index: test/Preprocessor/pragma-comment-linux.c
===
--- /dev/null
+++ test/Preprocessor/pragma-comment-linux.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -triple i686-unknown-linux-gnu -fsyntax-only -verify %s -Wunknown-pragmas
+
+#pragma comment(linker, "")
+// expected-warning@-1 {{'#pragma comment linker' ignored}}
+
Index: test/CodeGen/pragma-comment.c
===
--- test/CodeGen/pragma-comment.c
+++ test/CodeGen/pragma-comment.c
@@ -23,10 +23,9 @@
 // CHECK: ![[bar]] = !{!" /bar=2"}
 // CHECK: ![[foo]] = !{!" /foo=\22foo bar\22"}

-// LINUX: !{!"-lmsvcrt.lib"}
-// LINUX: !{!"-lkernel32"}
-// LINUX: !{!"-lUSER32.LIB"}
-// LINUX: !{!" /bar=2"}
+// LINUX: !{!"lib", !"msvcrt.lib"}
+// LINUX: !{!"lib", !"kernel32"}
+// LINUX: !{!"lib", !"USER32.LIB"}

 // PS4: !{!"\01msvcrt.lib"}
 // PS4: !{!"\01kernel32"}
Index: test/CodeGen/elf-linker-options.c
===
--- /dev/null
+++ test/CodeGen/elf-linker-options.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple i686---elf -emit-llvm %s -o - | FileCheck %s
+
+#pragma comment(lib, "alpha")
+
+// CHECK: !llvm.linker.options = !{[[NODE:![0-9]+]]}
+// CHECK: [[NODE]] = !{!"lib", !"alpha"}
+
Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
+<< II->getName();
+return;
+  }
+
   // On PS4, issue a warning about any pragma comments other than
   // #pragma comment lib.
   if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Index: lib/CodeGen/CodeGenModule.h
===
--- lib/CodeGen/CodeGenModule.h
+++ lib/CodeGen/CodeGenModule.h
@@ -1094,6 +1094,8 @@
   /// value.
   void AddDependentLib(StringRef Lib);

+  void AddELFLibDirective(StringRef Lib);
+
   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);

   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
Index: lib/CodeGen/CodeGenModule.cpp
===
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1411,6 +1411,12 @@
   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
 }

+void CodeGenModule::AddELFLibDirective(StringRef Lib) {
+  auto  = getLLVMContext();
+  LinkerOptionsMetadata.push_back(llvm::MDNode::get(
+  C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)}));
+}
+
 void CodeGenModule::AddDependentLib(StringRef Lib) {
   llvm::SmallString<24> Opt;
   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
@@ -4345,7 +4351,11 @@
   AppendLinkerOptions(PCD->getArg());
   break;
 case PCK_Lib:
-  AddDependentLib(PCD->getArg());
+  if (getTarget().getTriple().isOSBinFormatELF() &&
+  !getTarget().getTriple().isPS4())

[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd added a comment.

@jhenderson I believe that the first one is what this is implementing.  I 
believe that adding the last two as a patch following this one is preferable as 
that is specific to the needs for PS4, but, both of those should be possible to 
accommodate.  I would love to see a single unified approach here, so Im happy 
to help get that implemented.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-06 Thread James Henderson via Phabricator via cfe-commits
jhenderson added a comment.

In https://reviews.llvm.org/D42758#997880, @erichkeane wrote:

> I'd like to see @probinson s PS4 discussion bottom out, but I don't see any 
> reason to hold this up otherwise.


The PS4 compiler uses its linker options mechanism to communicate three 
different things to the linker, namely `#pragma comment(lib, ...)` directives, 
as discussed here, symbols marked with `__declspec(dllexport)`, and symbols 
marked with `__declspec(dllimport)` (the latter two requesting the linker 
explicitly to export/import the decorated symbols). I think if all three of 
these are converted to linker commands, we should be able to adopt the new 
implementation.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-05 Thread David Majnemer via Phabricator via cfe-commits
majnemer added a comment.

docs/LanguageExtensions.html should be updated to mention that we support this 
extension on all ELF targets.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-05 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd updated this revision to Diff 132853.
compnerd added a comment.

clang-format missed line, simplify some checks


Repository:
  rC Clang

https://reviews.llvm.org/D42758

Files:
  lib/CodeGen/CodeGenModule.cpp
  lib/CodeGen/CodeGenModule.h
  lib/Parse/ParsePragma.cpp
  test/CodeGen/elf-linker-options.c
  test/CodeGen/pragma-comment.c

Index: test/CodeGen/pragma-comment.c
===
--- test/CodeGen/pragma-comment.c
+++ test/CodeGen/pragma-comment.c
@@ -23,10 +23,9 @@
 // CHECK: ![[bar]] = !{!" /bar=2"}
 // CHECK: ![[foo]] = !{!" /foo=\22foo bar\22"}

-// LINUX: !{!"-lmsvcrt.lib"}
-// LINUX: !{!"-lkernel32"}
-// LINUX: !{!"-lUSER32.LIB"}
-// LINUX: !{!" /bar=2"}
+// LINUX: !{!"lib", !"msvcrt.lib"}
+// LINUX: !{!"lib", !"kernel32"}
+// LINUX: !{!"lib", !"USER32.LIB"}

 // PS4: !{!"\01msvcrt.lib"}
 // PS4: !{!"\01kernel32"}
Index: test/CodeGen/elf-linker-options.c
===
--- /dev/null
+++ test/CodeGen/elf-linker-options.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple i686---elf -emit-llvm %s -o - | FileCheck %s
+
+#pragma comment(lib, "alpha")
+
+// CHECK: !llvm.linker.options = !{[[NODE:![0-9]+]]}
+// CHECK: [[NODE]] = !{!"lib", !"alpha"}
+
Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
+<< II->getName();
+return;
+  }
+
   // On PS4, issue a warning about any pragma comments other than
   // #pragma comment lib.
   if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Index: lib/CodeGen/CodeGenModule.h
===
--- lib/CodeGen/CodeGenModule.h
+++ lib/CodeGen/CodeGenModule.h
@@ -1094,6 +1094,8 @@
   /// value.
   void AddDependentLib(StringRef Lib);

+  void AddELFLibDirective(StringRef Lib);
+
   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);

   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
Index: lib/CodeGen/CodeGenModule.cpp
===
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1411,6 +1411,12 @@
   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
 }

+void CodeGenModule::AddELFLibDirective(StringRef Lib) {
+  auto  = getLLVMContext();
+  LinkerOptionsMetadata.push_back(llvm::MDNode::get(
+  C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)}));
+}
+
 void CodeGenModule::AddDependentLib(StringRef Lib) {
   llvm::SmallString<24> Opt;
   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
@@ -4345,7 +4351,11 @@
   AppendLinkerOptions(PCD->getArg());
   break;
 case PCK_Lib:
-  AddDependentLib(PCD->getArg());
+  if (getTarget().getTriple().isOSBinFormatELF() &&
+  !getTarget().getTriple().isPS4())
+AddELFLibDirective(PCD->getArg());
+  else
+AddDependentLib(PCD->getArg());
   break;
 case PCK_Compiler:
 case PCK_ExeStr:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-05 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added a comment.

This seems pretty OK to me.  I'd like to see @probinson s PS4 discussion bottom 
out, but I don't see any reason to hold this up otherwise.

We definitely should leave the format as #pragma comment(keyword, "message"), 
since there is significant prior art here.




Comment at: test/CodeGen/elf-linker-options.c:3
+
+#pragma comment(lib, "alpha")
+

I would like a test for Sema to validate the 'pragma ignored' warning in common 
cases (such as 'Linker').


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-02 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd marked 2 inline comments as done.
compnerd added a comment.

@probinson it would be pretty cool if we could get the PS4 environment to share 
the same linker options implementation.  What other options do you guys need 
for this to be a viable approach?


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-02-01 Thread Rui Ueyama via cfe-commits
On Wed, Jan 31, 2018 at 7:23 PM, Paul Robinson via Phabricator <
revi...@reviews.llvm.org> wrote:

> probinson added a comment.
>
> In https://reviews.llvm.org/D42758#993936, @ruiu wrote:
>
> > > I also wonder which is better `#pragma comment(lib, "m")` or `#pragma
> comment(lib, "m")`.
> >
> > Sorry, I meant `#pragma comment(lib, "m")` or `#pragma comment("lib",
> "m")`.
>
>
> I can't swear to it but I don't think Microsoft invented `#pragma
> comment`.  Various IBM compilers have it, with a syntax of `#pragma comment
> (  [ , "string" ] )`.  I'm not seeing a `lib` keyword specifically
> in the IBM docs, but being a keyword would be consistent with past practice.
>

Thank you for the info. If it's a common practice, I don't see a reason to
change that.

>
> 
> Comment at: lib/Parse/ParsePragma.cpp:299
> +  getTargetInfo().getTriple().isOSBinFormatELF()) {
>  MSCommentHandler.reset(new PragmaCommentHandler(Actions));
>  PP.AddPragmaHandler(MSCommentHandler.get());
> 
> PS4's binary format is ELF, so you should be able to remove the isPS4
> predicate.
>
>
> 
> Comment at: lib/Parse/ParsePragma.cpp:382
> +  getTargetInfo().getTriple().isOSBinFormatELF()) {
>  PP.RemovePragmaHandler(MSCommentHandler.get());
>  MSCommentHandler.reset();
> 
> Don't need isPS4, as it uses ELF.
>
>
> Repository:
>   rC Clang
>
> https://reviews.llvm.org/D42758
>
>
>
>
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-01-31 Thread Paul Robinson via Phabricator via cfe-commits
probinson added a comment.

In https://reviews.llvm.org/D42758#993936, @ruiu wrote:

> > I also wonder which is better `#pragma comment(lib, "m")` or `#pragma 
> > comment(lib, "m")`.
>
> Sorry, I meant `#pragma comment(lib, "m")` or `#pragma comment("lib", "m")`.


I can't swear to it but I don't think Microsoft invented `#pragma comment`.  
Various IBM compilers have it, with a syntax of `#pragma comment (  [ 
, "string" ] )`.  I'm not seeing a `lib` keyword specifically in the IBM docs, 
but being a keyword would be consistent with past practice.




Comment at: lib/Parse/ParsePragma.cpp:299
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());

PS4's binary format is ELF, so you should be able to remove the isPS4 predicate.



Comment at: lib/Parse/ParsePragma.cpp:382
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();

Don't need isPS4, as it uses ELF.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-01-31 Thread Rui Ueyama via Phabricator via cfe-commits
ruiu added a comment.

> I also wonder which is better `#pragma comment(lib, "m")` or `#pragma 
> comment(lib, "m")`.

Sorry, I meant `#pragma comment(lib, "m")` or `#pragma comment("lib", "m")`.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-01-31 Thread Rui Ueyama via Phabricator via cfe-commits
ruiu added a comment.

`#pragma comment` is what Microsoft is using, but is everybody happy about that 
name? It isn't clear at least to me that what it actually means is linker 
directives.

I also wonder which is better `#pragma comment(lib, "m")` or `#pragma 
comment(lib, "m")`.


Repository:
  rC Clang

https://reviews.llvm.org/D42758



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-01-31 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd updated this revision to Diff 132252.
compnerd added a comment.

Add missed file


Repository:
  rC Clang

https://reviews.llvm.org/D42758

Files:
  lib/CodeGen/CodeGenModule.cpp
  lib/CodeGen/CodeGenModule.h
  lib/Parse/ParsePragma.cpp
  test/CodeGen/elf-linker-options.c


Index: test/CodeGen/elf-linker-options.c
===
--- /dev/null
+++ test/CodeGen/elf-linker-options.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple i686---elf -emit-llvm %s -o - | FileCheck %s
+
+#pragma comment(lib, "alpha")
+
+// CHECK: !llvm.linker.options = !{[[NODE:![0-9]+]]}
+// CHECK: [[NODE]] = !{!"lib", !"alpha"}
+
Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
+  << II->getName();
+return;
+  }
+
   // On PS4, issue a warning about any pragma comments other than
   // #pragma comment lib.
   if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Index: lib/CodeGen/CodeGenModule.h
===
--- lib/CodeGen/CodeGenModule.h
+++ lib/CodeGen/CodeGenModule.h
@@ -1094,6 +1094,8 @@
   /// value.
   void AddDependentLib(StringRef Lib);

+  void AddELFLibDirective(StringRef Lib);
+
   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);

   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
Index: lib/CodeGen/CodeGenModule.cpp
===
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1411,6 +1411,12 @@
   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
 }

+void CodeGenModule::AddELFLibDirective(StringRef Lib) {
+  auto  = getLLVMContext();
+  LinkerOptionsMetadata.push_back(llvm::MDNode::get(
+  C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)}));
+}
+
 void CodeGenModule::AddDependentLib(StringRef Lib) {
   llvm::SmallString<24> Opt;
   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
@@ -4345,7 +4351,10 @@
   AppendLinkerOptions(PCD->getArg());
   break;
 case PCK_Lib:
-  AddDependentLib(PCD->getArg());
+  if (getTarget().getTriple().isOSBinFormatELF())
+AddELFLibDirective(PCD->getArg());
+  else
+AddDependentLib(PCD->getArg());
   break;
 case PCK_Compiler:
 case PCK_ExeStr:


Index: test/CodeGen/elf-linker-options.c
===
--- /dev/null
+++ test/CodeGen/elf-linker-options.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple i686---elf -emit-llvm %s -o - | FileCheck %s
+
+#pragma comment(lib, "alpha")
+
+// CHECK: !llvm.linker.options = !{[[NODE:![0-9]+]]}
+// CHECK: [[NODE]] = !{!"lib", !"alpha"}
+
Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+ 

[PATCH] D42758: Support `#pragma comment(lib, "name")` in the frontend for ELF

2018-01-31 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd created this revision.
compnerd added a reviewer: rnk.

This adds the frontend support required to support the use of the comment 
pragma to enable auto linking on ELFish targets.  This is a generic ELF 
extension supported by LLVM.  We need to change the handling for the 
"dependentlib" in order to accommodate the previously discussed encoding for 
the dependent library descriptor.  Without the custom handling of the `PCK_Lib` 
directive, the `-l` prefixed option would be encoded into the resulting object 
(which is treated as a frontend error).


Repository:
  rC Clang

https://reviews.llvm.org/D42758

Files:
  lib/CodeGen/CodeGenModule.cpp
  lib/CodeGen/CodeGenModule.h
  lib/Parse/ParsePragma.cpp


Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
+  << II->getName();
+return;
+  }
+
   // On PS4, issue a warning about any pragma comments other than
   // #pragma comment lib.
   if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Index: lib/CodeGen/CodeGenModule.h
===
--- lib/CodeGen/CodeGenModule.h
+++ lib/CodeGen/CodeGenModule.h
@@ -1094,6 +1094,8 @@
   /// value.
   void AddDependentLib(StringRef Lib);

+  void AddELFLibDirective(StringRef Lib);
+
   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);

   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
Index: lib/CodeGen/CodeGenModule.cpp
===
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1411,6 +1411,12 @@
   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
 }

+void CodeGenModule::AddELFLibDirective(StringRef Lib) {
+  auto  = getLLVMContext();
+  LinkerOptionsMetadata.push_back(llvm::MDNode::get(
+  C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)}));
+}
+
 void CodeGenModule::AddDependentLib(StringRef Lib) {
   llvm::SmallString<24> Opt;
   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
@@ -4345,7 +4351,10 @@
   AppendLinkerOptions(PCD->getArg());
   break;
 case PCK_Lib:
-  AddDependentLib(PCD->getArg());
+  if (getTarget().getTriple().isOSBinFormatELF())
+AddELFLibDirective(PCD->getArg());
+  else
+AddDependentLib(PCD->getArg());
   break;
 case PCK_Compiler:
 case PCK_ExeStr:


Index: lib/Parse/ParsePragma.cpp
===
--- lib/Parse/ParsePragma.cpp
+++ lib/Parse/ParsePragma.cpp
@@ -295,7 +295,8 @@
 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
   PP.AddPragmaHandler(OpenMPHandler.get());

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
 PP.AddPragmaHandler(MSCommentHandler.get());
   }
@@ -377,7 +378,8 @@
   PP.RemovePragmaHandler(OpenMPHandler.get());
   OpenMPHandler.reset();

-  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
+  if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4() ||
+  getTargetInfo().getTriple().isOSBinFormatELF()) {
 PP.RemovePragmaHandler(MSCommentHandler.get());
 MSCommentHandler.reset();
   }
@@ -2449,6 +2451,12 @@
 return;
   }

+  if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
+PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
+  << II->getName();
+return;
+  }
+
   // On PS4, issue a warning about any pragma comments other than
   // #pragma comment lib.
   if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Index: lib/CodeGen/CodeGenModule.h