<br><div class="gmail_quote">On Sat, Feb 18, 2012 at 8:12 AM, David Chisnall <span dir="ltr"><<a href="mailto:csdavec@swan.ac.uk">csdavec@swan.ac.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: theraven<br>
Date: Sat Feb 18 10:12:34 2012<br>
New Revision: 150881<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=150881&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=150881&view=rev</a><br>
Log:<br>
Implement #pragma redefine_extname.<br>
<br>
This fixes PR5172 and allows clang to compile C++ programs on Solaris using the system headers.<br>
<br>
<br>
Added:<br>
    cfe/trunk/test/CodeGen/redefine_extname.c<br>
Modified:<br>
    cfe/trunk/include/clang/Parse/Parser.h<br>
    cfe/trunk/include/clang/Sema/Sema.h<br>
    cfe/trunk/lib/Frontend/InitPreprocessor.cpp<br>
    cfe/trunk/lib/Parse/ParsePragma.cpp<br>
    cfe/trunk/lib/Parse/ParsePragma.h<br>
    cfe/trunk/lib/Parse/Parser.cpp<br>
    cfe/trunk/lib/Sema/SemaDecl.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Parse/Parser.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/include/clang/Parse/Parser.h (original)<br>
+++ cfe/trunk/include/clang/Parse/Parser.h Sat Feb 18 10:12:34 2012<br>
@@ -162,6 +162,7 @@<br>
   OwningPtr<PragmaHandler> MSStructHandler;<br>
   OwningPtr<PragmaHandler> UnusedHandler;<br>
   OwningPtr<PragmaHandler> WeakHandler;<br>
+  OwningPtr<PragmaHandler> RedefineExtnameHandler;<br>
   OwningPtr<PragmaHandler> FPContractHandler;<br>
   OwningPtr<PragmaHandler> OpenCLExtensionHandler;<br>
<br>
<br>
Modified: cfe/trunk/include/clang/Sema/Sema.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/include/clang/Sema/Sema.h (original)<br>
+++ cfe/trunk/include/clang/Sema/Sema.h Sat Feb 18 10:12:34 2012<br>
@@ -469,6 +469,13 @@<br>
   /// identifier, declared or undeclared<br>
   llvm::DenseMap<IdentifierInfo*,WeakInfo> WeakUndeclaredIdentifiers;<br>
<br>
+  /// ExtnameUndeclaredIdentifiers - Identifiers contained in<br>
+  /// #pragma redefine_extname before declared.  Used in Solaris system headers<br>
+  /// to define functions that occur in multiple standards to call the version<br>
+  /// in the currently selected standard.<br>
+  llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*> ExtnameUndeclaredIdentifiers;<br>
+<br>
+<br>
   /// \brief Load weak undeclared identifiers from the external source.<br>
   void LoadExternalWeakUndeclaredIdentifiers();<br>
<br>
@@ -5719,6 +5726,14 @@<br>
                          SourceLocation PragmaLoc,<br>
                          SourceLocation WeakNameLoc);<br>
<br>
+  /// ActOnPragmaRedefineExtname - Called on well formed<br>
+  /// #pragma redefine_extname oldname newname.<br>
+  void ActOnPragmaRedefineExtname(IdentifierInfo* WeakName,<br>
+                                  IdentifierInfo* AliasName,<br>
+                                  SourceLocation PragmaLoc,<br>
+                                  SourceLocation WeakNameLoc,<br>
+                                  SourceLocation AliasNameLoc);<br>
+<br>
   /// ActOnPragmaWeakAlias - Called on well formed #pragma weak ident = ident.<br>
   void ActOnPragmaWeakAlias(IdentifierInfo* WeakName,<br>
                             IdentifierInfo* AliasName,<br>
<br>
Modified: cfe/trunk/lib/Frontend/InitPreprocessor.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Frontend/InitPreprocessor.cpp (original)<br>
+++ cfe/trunk/lib/Frontend/InitPreprocessor.cpp Sat Feb 18 10:12:34 2012<br>
@@ -333,6 +333,9 @@<br>
   Builder.defineMacro("__ATOMIC_ACQ_REL", "4");<br>
   Builder.defineMacro("__ATOMIC_SEQ_CST", "5");<br>
<br>
+  // Support for #pragma redefine_extname (Sun compatibility)<br>
+  Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");<br>
+<br>
   // As sad as it is, enough software depends on the __VERSION__ for version<br>
   // checks that it is necessary to report 4.2.1 (the base GCC version we claim<br>
   // compatibility with) first.<br>
<br>
Modified: cfe/trunk/lib/Parse/ParsePragma.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.cpp?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.cpp?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Parse/ParsePragma.cpp (original)<br>
+++ cfe/trunk/lib/Parse/ParsePragma.cpp Sat Feb 18 10:12:34 2012<br>
@@ -426,6 +426,44 @@<br>
   }<br>
 }<br>
<br>
+// #pragma redefine_extname identifier identifier<br>
+void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,<br>
+                                               PragmaIntroducerKind Introducer,<br>
+                                                Token &RedefToken) {<br>
+  SourceLocation RedefLoc = RedefToken.getLocation();<br>
+<br>
+  Token Tok;<br>
+  PP.Lex(Tok);<br>
+  if (Tok.isNot(tok::identifier)) {<br>
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<<br>
+      "redefine_extname";<br>
+    return;<br>
+  }<br>
+<br>
+  IdentifierInfo *RedefName = Tok.getIdentifierInfo(), *AliasName = 0;<br>
+  SourceLocation RedefNameLoc = Tok.getLocation(), AliasNameLoc;<br>
+<br>
+  PP.Lex(Tok);<br>
+  if (Tok.isNot(tok::identifier)) {<br>
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)<br>
+        << "redefine_extname";<br>
+    return;<br>
+  }<br>
+  AliasName = Tok.getIdentifierInfo();<br>
+  AliasNameLoc = Tok.getLocation();<br>
+  PP.Lex(Tok);<br>
+<br>
+  if (Tok.isNot(tok::eod)) {<br>
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<<br>
+      "redefine_extname";<br>
+    return;<br>
+  }<br>
+<br>
+  Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,<br>
+      RedefNameLoc, AliasNameLoc);<br>
+}<br>
+<br>
+<br>
 void<br>
 PragmaFPContractHandler::HandlePragma(Preprocessor &PP,<br>
                                       PragmaIntroducerKind Introducer,<br>
<br>
Modified: cfe/trunk/lib/Parse/ParsePragma.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.h?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.h?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Parse/ParsePragma.h (original)<br>
+++ cfe/trunk/lib/Parse/ParsePragma.h Sat Feb 18 10:12:34 2012<br>
@@ -90,6 +90,16 @@<br>
                             Token &FirstToken);<br>
 };<br>
<br>
+class PragmaRedefineExtnameHandler : public PragmaHandler {<br>
+  Sema &Actions;<br>
+public:<br>
+  explicit PragmaRedefineExtnameHandler(Sema &A)<br>
+    : PragmaHandler("redefine_extname"), Actions(A) {}<br>
+<br>
+  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,<br>
+                            Token &FirstToken);<br>
+};<br>
+<br>
 class PragmaOpenCLExtensionHandler : public PragmaHandler {<br>
   Sema &Actions;<br>
   Parser &parser;<br>
<br>
Modified: cfe/trunk/lib/Parse/Parser.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Parse/Parser.cpp (original)<br>
+++ cfe/trunk/lib/Parse/Parser.cpp Sat Feb 18 10:12:34 2012<br>
@@ -64,6 +64,9 @@<br>
   WeakHandler.reset(new PragmaWeakHandler(actions));<br>
   PP.AddPragmaHandler(WeakHandler.get());<br>
<br>
+  RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler(actions));<br>
+  PP.AddPragmaHandler(RedefineExtnameHandler.get());<br>
+<br>
   FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));<br>
   PP.AddPragmaHandler("STDC", FPContractHandler.get());<br>
<br>
@@ -382,6 +385,8 @@<br>
   UnusedHandler.reset();<br>
   PP.RemovePragmaHandler(WeakHandler.get());<br>
   WeakHandler.reset();<br>
+  PP.RemovePragmaHandler(RedefineExtnameHandler.get());<br>
+  RedefineExtnameHandler.reset();<br>
<br>
   if (getLang().OpenCL) {<br>
     PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=150881&r1=150880&r2=150881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=150881&r1=150880&r2=150881&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Feb 18 10:12:34 2012<br>
@@ -4059,6 +4059,13 @@<br>
<br>
     NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0),<br>
                                                 Context, Label));<br>
+  } else if (!ExtnameUndeclaredIdentifiers.empty()) {<br>
+    llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =<br>
+      ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier());<br>
+    if (I != ExtnameUndeclaredIdentifiers.end()) {<br>
+      NewVD->addAttr(I->second);<br>
+      ExtnameUndeclaredIdentifiers.erase(I);<br>
+    }<br>
   }<br>
<br>
   // Diagnose shadowed variables before filtering for scope.<br>
@@ -5157,6 +5164,13 @@<br>
     StringLiteral *SE = cast<StringLiteral>(E);<br>
     NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context,<br>
                                                 SE->getString()));<br>
+  } else if (!ExtnameUndeclaredIdentifiers.empty()) {<br>
+    llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =<br>
+      ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier());<br>
+    if (I != ExtnameUndeclaredIdentifiers.end()) {<br>
+      NewFD->addAttr(I->second);<br>
+      ExtnameUndeclaredIdentifiers.erase(I);<br>
+    }<br>
   }<br>
<br>
   // Copy the parameter declarations from the declarator D to the function<br>
@@ -10152,6 +10166,24 @@<br>
   return Import;<br>
 }<br>
<br>
+void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name,<br>
+                                      IdentifierInfo* AliasName,<br>
+                                      SourceLocation PragmaLoc,<br>
+                                      SourceLocation NameLoc,<br>
+                                      SourceLocation AliasNameLoc) {<br>
+  Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc,<br>
+                                    LookupOrdinaryName);<br>
+  AsmLabelAttr *Attr =<br>
+     ::new (Context) AsmLabelAttr(AliasNameLoc, Context, AliasName->getName());<br>
+  fprintf(stderr, "Alias name: %s\n", AliasName->getName().str().c_str());<br></blockquote><div><br></div><div>Uh, remove debugging code? =[</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+<br>
+  if (PrevDecl)<br>
+    PrevDecl->addAttr(Attr);<br>
+  else<br>
+    (void)ExtnameUndeclaredIdentifiers.insert(<br>
+      std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr));<br>
+}<br>
+<br>
 void Sema::ActOnPragmaWeakID(IdentifierInfo* Name,<br>
                              SourceLocation PragmaLoc,<br>
                              SourceLocation NameLoc) {<br>
<br>
Added: cfe/trunk/test/CodeGen/redefine_extname.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/redefine_extname.c?rev=150881&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/redefine_extname.c?rev=150881&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/test/CodeGen/redefine_extname.c (added)<br>
+++ cfe/trunk/test/CodeGen/redefine_extname.c Sat Feb 18 10:12:34 2012<br>
@@ -0,0 +1,15 @@<br>
+// RUN: %clang_cc1 -triple=i386-pc-solaris2.11 -w -emit-llvm %s -o - | FileCheck %s<br>
+<br>
+#pragma redefine_extname fake real<br>
+#pragma redefine_extname name alias<br>
+<br>
+extern int fake(void);<br>
+<br>
+int name;<br>
+<br>
+// __PRAGMA_REDEFINE_EXTNAME should be defined.  This will fail if it isn't...<br></blockquote><div><br></div><div>Please test these independently; we have better places to check for predefined macros, this test should focus exclusively on the desired codegen changes.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+int fish() { return fake() + __PRAGMA_REDEFINE_EXTNAME + name; }<br>
+// Check that the call to fake() is emitted as a call to real()<br>
+// CHECK:   call i32 @real()<br>
+// Check that this also works with variables names<br>
+// CHECK:   load i32* @alias<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br>