r193161 - This patch causes clang to reject alias attributes that point to undefined
Rafael Espindola
rafael.espindola at gmail.com
Tue Oct 22 06:51:07 PDT 2013
Author: rafael
Date: Tue Oct 22 08:51:06 2013
New Revision: 193161
URL: http://llvm.org/viewvc/llvm-project?rev=193161&view=rev
Log:
This patch causes clang to reject alias attributes that point to undefined
names. For example, with this patch we now reject
void f1(void) __attribute__((alias("g1")));
This patch is implemented in CodeGen. It is quiet a bit simpler and more
compatible with gcc than implementing it in Sema. The downside is that the
errors only fire during -emit-llvm.
Added:
cfe/trunk/test/Sema/attr-alias-elf.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/CodeGen/CGExprCXX.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.h
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=193161&r1=193160&r2=193161&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Oct 22 08:51:06 2013
@@ -2015,6 +2015,8 @@ def err_attribute_weakref_without_alias
"weakref declaration of '%0' must also have an alias attribute">;
def err_alias_not_supported_on_darwin : Error <
"only weak aliases are supported on darwin">;
+def err_alias_to_undefined : Error<
+ "alias must point to a defined variable or function">;
def warn_attribute_wrong_decl_type : Warning<
"%0 attribute only applies to %select{functions|unions|"
"variables and functions|functions and methods|parameters|"
Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=193161&r1=193160&r2=193161&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Tue Oct 22 08:51:06 2013
@@ -929,9 +929,8 @@ static RValue EmitNewDeleteCall(CodeGenF
/// to a replaceable global allocation function.
///
/// We model such elidable calls with the 'builtin' attribute.
- llvm::Function *Fn = dyn_cast<llvm::Function>(CalleeAddr);
if (Callee->isReplaceableGlobalAllocationFunction() &&
- Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
+ !Callee->hasAttr<AliasAttr>()) {
// FIXME: Add addAttribute to CallSite.
if (llvm::CallInst *CI = dyn_cast<llvm::CallInst>(CallOrInvoke))
CI->addAttribute(llvm::AttributeSet::FunctionIndex,
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=193161&r1=193160&r2=193161&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Oct 22 08:51:06 2013
@@ -37,6 +37,7 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/Version.h"
#include "clang/Frontend/CodeGenOptions.h"
+#include "clang/Sema/SemaDiagnostic.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/CallingConv.h"
@@ -933,6 +934,12 @@ void CodeGenModule::EmitDeferred() {
GlobalDecl D = DeferredDeclsToEmit.back();
DeferredDeclsToEmit.pop_back();
+ const ValueDecl *Global = cast<ValueDecl>(D.getDecl());
+ if (Global->hasAttr<AliasAttr>()) {
+ EmitAliasDefinition(D);
+ continue;
+ }
+
// Check to see if we've already emitted this. This is necessary
// for a couple of reasons: first, decls can end up in the
// deferred-decls queue multiple times, and second, decls can end
@@ -1098,7 +1105,7 @@ void CodeGenModule::EmitGlobal(GlobalDec
// If this is an alias definition (which otherwise looks like a declaration)
// emit it now.
if (Global->hasAttr<AliasAttr>())
- return EmitAliasDefinition(GD);
+ return scheduleAliasDefinitionEmission(GD);
// If this is CUDA, be selective about which declarations we emit.
if (LangOpts.CUDA) {
@@ -2075,6 +2082,24 @@ void CodeGenModule::EmitGlobalFunctionDe
AddGlobalAnnotations(D, Fn);
}
+void CodeGenModule::scheduleAliasDefinitionEmission(GlobalDecl GD) {
+ const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
+ const AliasAttr *AA = D->getAttr<AliasAttr>();
+ assert(AA && "Not an alias?");
+
+ // Schedule it.
+ DeferredDeclsToEmit.push_back(GD);
+
+ llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
+
+ // Cause the aliasee emission to be scheduled.
+ if (isa<llvm::FunctionType>(DeclTy))
+ GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, /*ForVTable=*/false);
+ else
+ GetOrCreateLLVMGlobal(AA->getAliasee(),
+ llvm::PointerType::getUnqual(DeclTy), 0);
+}
+
void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
const AliasAttr *AA = D->getAttr<AliasAttr>();
@@ -2100,6 +2125,18 @@ void CodeGenModule::EmitAliasDefinition(
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
llvm::PointerType::getUnqual(DeclTy), 0);
+ llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Aliasee);
+ if (!GV) {
+ llvm::ConstantExpr *CE = cast<llvm::ConstantExpr>(Aliasee);
+ assert(CE->getOpcode() == llvm::Instruction::BitCast ||
+ CE->getOpcode() == llvm::Instruction::GetElementPtr);
+ GV = cast<llvm::GlobalValue>(CE->getOperand(0));
+ }
+ if (GV->isDeclaration()) {
+ getDiags().Report(AA->getLocation(), diag::err_alias_to_undefined);
+ return;
+ }
+
// Create the new alias itself, but don't set a name yet.
llvm::GlobalValue *GA =
new llvm::GlobalAlias(Aliasee->getType(),
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=193161&r1=193160&r2=193161&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Oct 22 08:51:06 2013
@@ -1024,6 +1024,7 @@ private:
void EmitGlobalFunctionDefinition(GlobalDecl GD);
void EmitGlobalVarDefinition(const VarDecl *D);
+ void scheduleAliasDefinitionEmission(GlobalDecl GD);
void EmitAliasDefinition(GlobalDecl GD);
void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=193161&r1=193160&r2=193161&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue Oct 22 08:51:06 2013
@@ -5020,6 +5020,13 @@ void Sema::DeclApplyPragmaWeak(Scope *S,
W.setUsed(true);
if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
IdentifierInfo *NDId = ND->getIdentifier();
+
+ // FIXME: we should reject this (pr17640).
+ NamedDecl *Aliasee = LookupSingleName(TUScope, W.getAlias(),
+ W.getLocation(), LookupOrdinaryName);
+ if (Aliasee && Aliasee->hasAttr<AliasAttr>())
+ return;
+
NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
NDId->getName()));
Added: cfe/trunk/test/Sema/attr-alias-elf.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-alias-elf.c?rev=193161&view=auto
==============================================================================
--- cfe/trunk/test/Sema/attr-alias-elf.c (added)
+++ cfe/trunk/test/Sema/attr-alias-elf.c Tue Oct 22 08:51:06 2013
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux -fsyntax-only -verify -emit-llvm-only %s
+
+void f1(void) __attribute__((alias("g1")));
+void g1(void) {
+}
+
+void f2(void) __attribute__((alias("g2"))); // expected-error {{alias must point to a defined variable or function}}
+
+
+void f3(void) __attribute__((alias("g3"))); // expected-error {{alias must point to a defined variable or function}}
+void g3(void);
+
+extern int a1 __attribute__((alias("b1")));
+int b1 = 42;
+
+extern int a2 __attribute__((alias("b2"))); // expected-error {{alias must point to a defined variable or function}}
+
+extern int a3 __attribute__((alias("b3"))); // expected-error {{alias must point to a defined variable or function}}
+extern int b3;
+
+extern int a4 __attribute__((alias("b4"))); // expected-error {{alias must point to a defined variable or function}}
+typedef int b4;
More information about the cfe-commits
mailing list