]> granicus.if.org Git - clang/commitdiff
Implement #pragma redefine_extname.
authorDavid Chisnall <csdavec@swan.ac.uk>
Sat, 18 Feb 2012 16:12:34 +0000 (16:12 +0000)
committerDavid Chisnall <csdavec@swan.ac.uk>
Sat, 18 Feb 2012 16:12:34 +0000 (16:12 +0000)
This fixes PR5172 and allows clang to compile C++ programs on Solaris using the system headers.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150881 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Parse/Parser.h
include/clang/Sema/Sema.h
lib/Frontend/InitPreprocessor.cpp
lib/Parse/ParsePragma.cpp
lib/Parse/ParsePragma.h
lib/Parse/Parser.cpp
lib/Sema/SemaDecl.cpp
test/CodeGen/redefine_extname.c [new file with mode: 0644]

index 3d865383cc4e1109bdaf16ccde57499f45ff9d7e..a13b48a420ac13f567e209a717a120194468b8c8 100644 (file)
@@ -162,6 +162,7 @@ class Parser : public CodeCompletionHandler {
   OwningPtr<PragmaHandler> MSStructHandler;
   OwningPtr<PragmaHandler> UnusedHandler;
   OwningPtr<PragmaHandler> WeakHandler;
+  OwningPtr<PragmaHandler> RedefineExtnameHandler;
   OwningPtr<PragmaHandler> FPContractHandler;
   OwningPtr<PragmaHandler> OpenCLExtensionHandler;
 
index 97cd27f7ed6da781f9c0478992dc63be3feb0b69..bd285c78b951d3f58ccbb91ed10f187a456993fc 100644 (file)
@@ -469,6 +469,13 @@ public:
   /// identifier, declared or undeclared
   llvm::DenseMap<IdentifierInfo*,WeakInfo> WeakUndeclaredIdentifiers;
 
+  /// ExtnameUndeclaredIdentifiers - Identifiers contained in
+  /// #pragma redefine_extname before declared.  Used in Solaris system headers
+  /// to define functions that occur in multiple standards to call the version
+  /// in the currently selected standard.
+  llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*> ExtnameUndeclaredIdentifiers;
+
+
   /// \brief Load weak undeclared identifiers from the external source.
   void LoadExternalWeakUndeclaredIdentifiers();
 
@@ -5719,6 +5726,14 @@ public:
                          SourceLocation PragmaLoc,
                          SourceLocation WeakNameLoc);
 
+  /// ActOnPragmaRedefineExtname - Called on well formed 
+  /// #pragma redefine_extname oldname newname.
+  void ActOnPragmaRedefineExtname(IdentifierInfo* WeakName,
+                                  IdentifierInfo* AliasName,
+                                  SourceLocation PragmaLoc,
+                                  SourceLocation WeakNameLoc,
+                                  SourceLocation AliasNameLoc);
+
   /// ActOnPragmaWeakAlias - Called on well formed #pragma weak ident = ident.
   void ActOnPragmaWeakAlias(IdentifierInfo* WeakName,
                             IdentifierInfo* AliasName,
index f0cf7f491401b0a2d27988916271e195a6c34b2f..504ef0160128e2643c161438a091afcd0ac5190b 100644 (file)
@@ -333,6 +333,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
   Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
   Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
 
+  // Support for #pragma redefine_extname (Sun compatibility)
+  Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
+
   // As sad as it is, enough software depends on the __VERSION__ for version
   // checks that it is necessary to report 4.2.1 (the base GCC version we claim
   // compatibility with) first.
index f47b32f513b35f844373caa8de83d34aaa2a51c5..433c76b83bcf391c271e17de2703609975c0db37 100644 (file)
@@ -426,6 +426,44 @@ void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
   }
 }
 
+// #pragma redefine_extname identifier identifier
+void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, 
+                                               PragmaIntroducerKind Introducer,
+                                                Token &RedefToken) {
+  SourceLocation RedefLoc = RedefToken.getLocation();
+
+  Token Tok;
+  PP.Lex(Tok);
+  if (Tok.isNot(tok::identifier)) {
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
+      "redefine_extname";
+    return;
+  }
+
+  IdentifierInfo *RedefName = Tok.getIdentifierInfo(), *AliasName = 0;
+  SourceLocation RedefNameLoc = Tok.getLocation(), AliasNameLoc;
+
+  PP.Lex(Tok);
+  if (Tok.isNot(tok::identifier)) {
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
+        << "redefine_extname";
+    return;
+  }
+  AliasName = Tok.getIdentifierInfo();
+  AliasNameLoc = Tok.getLocation();
+  PP.Lex(Tok);
+
+  if (Tok.isNot(tok::eod)) {
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
+      "redefine_extname";
+    return;
+  }
+
+  Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
+      RedefNameLoc, AliasNameLoc);
+}
+
+
 void
 PragmaFPContractHandler::HandlePragma(Preprocessor &PP, 
                                       PragmaIntroducerKind Introducer,
index 1d3138fa70a8266016bc167fb838e161141333a8..ebb185ad1a98b03628bd37f766e635f18999de07 100644 (file)
@@ -90,6 +90,16 @@ public:
                             Token &FirstToken);
 };
 
+class PragmaRedefineExtnameHandler : public PragmaHandler {
+  Sema &Actions;
+public:
+  explicit PragmaRedefineExtnameHandler(Sema &A)
+    : PragmaHandler("redefine_extname"), Actions(A) {}
+
+  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
+                            Token &FirstToken);
+};
+
 class PragmaOpenCLExtensionHandler : public PragmaHandler {
   Sema &Actions;
   Parser &parser;
index c7974369484025801bb5b0f4af0bd1b66070c225..4c5df4d2fff612a6f5e2b79a4a7c8dfbee12b567 100644 (file)
@@ -64,6 +64,9 @@ Parser::Parser(Preprocessor &pp, Sema &actions)
   WeakHandler.reset(new PragmaWeakHandler(actions));
   PP.AddPragmaHandler(WeakHandler.get());
 
+  RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler(actions));
+  PP.AddPragmaHandler(RedefineExtnameHandler.get());
+
   FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
   PP.AddPragmaHandler("STDC", FPContractHandler.get());
 
@@ -382,6 +385,8 @@ Parser::~Parser() {
   UnusedHandler.reset();
   PP.RemovePragmaHandler(WeakHandler.get());
   WeakHandler.reset();
+  PP.RemovePragmaHandler(RedefineExtnameHandler.get());
+  RedefineExtnameHandler.reset();
 
   if (getLang().OpenCL) {
     PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
index 796d2a2bb3f4561aea6a93af598924108df6dbc8..89f22cb72bf59a07b0447f8d416a3e31b624c767 100644 (file)
@@ -4059,6 +4059,13 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
 
     NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0),
                                                 Context, Label));
+  } else if (!ExtnameUndeclaredIdentifiers.empty()) {
+    llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =
+      ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier());
+    if (I != ExtnameUndeclaredIdentifiers.end()) {
+      NewVD->addAttr(I->second);
+      ExtnameUndeclaredIdentifiers.erase(I);
+    }
   }
 
   // Diagnose shadowed variables before filtering for scope.
@@ -5157,6 +5164,13 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
     StringLiteral *SE = cast<StringLiteral>(E);
     NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context,
                                                 SE->getString()));
+  } else if (!ExtnameUndeclaredIdentifiers.empty()) {
+    llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =
+      ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier());
+    if (I != ExtnameUndeclaredIdentifiers.end()) {
+      NewFD->addAttr(I->second);
+      ExtnameUndeclaredIdentifiers.erase(I);
+    }
   }
 
   // Copy the parameter declarations from the declarator D to the function
@@ -10152,6 +10166,24 @@ DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc,
   return Import;
 }
 
+void Sema::ActOnPragmaRedefineExtname(IdentifierInfo* Name,
+                                      IdentifierInfo* AliasName,
+                                      SourceLocation PragmaLoc,
+                                      SourceLocation NameLoc,
+                                      SourceLocation AliasNameLoc) {
+  Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc,
+                                    LookupOrdinaryName);
+  AsmLabelAttr *Attr =
+     ::new (Context) AsmLabelAttr(AliasNameLoc, Context, AliasName->getName());
+  fprintf(stderr, "Alias name: %s\n", AliasName->getName().str().c_str());
+
+  if (PrevDecl) 
+    PrevDecl->addAttr(Attr);
+  else 
+    (void)ExtnameUndeclaredIdentifiers.insert(
+      std::pair<IdentifierInfo*,AsmLabelAttr*>(Name, Attr));
+}
+
 void Sema::ActOnPragmaWeakID(IdentifierInfo* Name,
                              SourceLocation PragmaLoc,
                              SourceLocation NameLoc) {
diff --git a/test/CodeGen/redefine_extname.c b/test/CodeGen/redefine_extname.c
new file mode 100644 (file)
index 0000000..e73a3ad
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple=i386-pc-solaris2.11 -w -emit-llvm %s -o - | FileCheck %s
+
+#pragma redefine_extname fake real
+#pragma redefine_extname name alias
+
+extern int fake(void);
+
+int name;
+
+// __PRAGMA_REDEFINE_EXTNAME should be defined.  This will fail if it isn't...
+int fish() { return fake() + __PRAGMA_REDEFINE_EXTNAME + name; }
+// Check that the call to fake() is emitted as a call to real()
+// CHECK:   call i32 @real()
+// Check that this also works with variables names
+// CHECK:   load i32* @alias