From: Daniel Dunbar Date: Thu, 19 Mar 2009 08:27:24 +0000 (+0000) Subject: IRgen support for alias of global variable. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9;p=clang IRgen support for alias of global variable. - PR3818. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67297 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index aed067617e..cc460c5959 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -369,7 +369,7 @@ void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, void CodeGenModule::EmitAliases() { for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { - const FunctionDecl *D = Aliases[i]; + const ValueDecl *D = Aliases[i]; const AliasAttr *AA = D->getAttr(); // This is something of a hack, if the FunctionDecl got overridden @@ -380,7 +380,7 @@ void CodeGenModule::EmitAliases() { continue; const std::string& aliaseeName = AA->getAliasee(); - llvm::Function *aliasee = getModule().getFunction(aliaseeName); + llvm::GlobalValue *aliasee = getModule().getNamedValue(aliaseeName); if (!aliasee) { // FIXME: This isn't unsupported, this is just an error, which // sema should catch, but... @@ -539,16 +539,14 @@ bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { } void CodeGenModule::EmitGlobal(const ValueDecl *Global) { - if (const FunctionDecl *FD = dyn_cast(Global)) { - // Aliases are deferred until code for everything else has been - // emitted. - if (FD->getAttr()) { - assert(!FD->isThisDeclarationADefinition() && - "Function alias cannot have a definition!"); - Aliases.push_back(FD); - return; - } + // Aliases are deferred until code for everything else has been + // emitted. + if (Global->getAttr()) { + Aliases.push_back(Global); + return; + } + if (const FunctionDecl *FD = dyn_cast(Global)) { // Forward declarations are emitted lazily on first use. if (!FD->isThisDeclarationADefinition()) return; diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index c3a84af0c5..12c103ba62 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -109,7 +109,7 @@ class CodeGenModule : public BlockModule { /// Aliases - List of aliases in module. These cannot be emitted until all the /// code has been seen, as they reference things by name instead of directly /// and may reference forward. - std::vector Aliases; + std::vector Aliases; /// DeferredDecls - List of decls for which code generation has been /// deferred. When the translation unit has been fully processed we diff --git a/test/CodeGen/alias.c b/test/CodeGen/alias.c new file mode 100644 index 0000000000..a625b6aea1 --- /dev/null +++ b/test/CodeGen/alias.c @@ -0,0 +1,13 @@ +// RUN: clang -triple i386-pc-linux-gnu -emit-llvm -o %t %s && +// RUN: grep '@g0 = common global i32 0' %t && +// RUN: grep '@f1 = alias void ()\* @f0' %t && +// RUN: grep '@g1 = alias i32\* @g0' %t && +// RUN: grep 'define void @f0() nounwind {' %t + +void f0(void) { } +extern void f1(void); +extern void f1(void) __attribute((alias("f0"))); + +int g0; +extern int g1; +extern int g1 __attribute((alias("g0")));