From b1208609ee4d18b2c0adaf860b72d0b2d5fd43d4 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 15 Sep 2009 06:28:26 +0000 Subject: [PATCH] Forbid arrays of function-type and structures with function-typed fields. While I'm there, change code that does: SomeTy == Type::getFooType(Context) into: SomeTy->getTypeID() == FooTyID to decrease the amount of useless type creation which may involve locking, etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81846 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Type.cpp | 30 +++++++++++++----------------- test/Other/2009-09-14-function-elements.ll | 6 ++++++ 2 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 test/Other/2009-09-14-function-elements.ll diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp index 19c91c5f03..2ddb8c7d5a 100644 --- a/lib/VMCore/Type.cpp +++ b/lib/VMCore/Type.cpp @@ -364,11 +364,10 @@ const IntegerType *Type::getInt64Ty(LLVMContext &C) { bool FunctionType::isValidReturnType(const Type *RetTy) { if (RetTy->isFirstClassType()) { if (const PointerType *PTy = dyn_cast(RetTy)) - return PTy->getElementType() != Type::getMetadataTy(RetTy->getContext()); + return PTy->getElementType()->getTypeID() != MetadataTyID; return true; } - if (RetTy == Type::getVoidTy(RetTy->getContext()) || - RetTy == Type::getMetadataTy(RetTy->getContext()) || + if (RetTy->getTypeID() == VoidTyID || RetTy->getTypeID() == MetadataTyID || isa(RetTy)) return true; @@ -389,8 +388,7 @@ bool FunctionType::isValidReturnType(const Type *RetTy) { bool FunctionType::isValidArgumentType(const Type *ArgTy) { if ((!ArgTy->isFirstClassType() && !isa(ArgTy)) || (isa(ArgTy) && - cast(ArgTy)->getElementType() == - Type::getMetadataTy(ArgTy->getContext()))) + cast(ArgTy)->getElementType()->getTypeID() == MetadataTyID)) return false; return true; @@ -829,13 +827,12 @@ ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) { } bool ArrayType::isValidElementType(const Type *ElemTy) { - if (ElemTy == Type::getVoidTy(ElemTy->getContext()) || - ElemTy == Type::getLabelTy(ElemTy->getContext()) || - ElemTy == Type::getMetadataTy(ElemTy->getContext())) + if (ElemTy->getTypeID() == VoidTyID || ElemTy->getTypeID() == LabelTyID || + ElemTy->getTypeID() == MetadataTyID || isa(ElemTy)) return false; if (const PointerType *PTy = dyn_cast(ElemTy)) - if (PTy->getElementType() == Type::getMetadataTy(ElemTy->getContext())) + if (PTy->getElementType()->getTypeID() == MetadataTyID) return false; return true; @@ -909,13 +906,12 @@ StructType *StructType::get(LLVMContext &Context, const Type *type, ...) { } bool StructType::isValidElementType(const Type *ElemTy) { - if (ElemTy == Type::getVoidTy(ElemTy->getContext()) || - ElemTy == Type::getLabelTy(ElemTy->getContext()) || - ElemTy == Type::getMetadataTy(ElemTy->getContext())) + if (ElemTy->getTypeID() == VoidTyID || ElemTy->getTypeID() == LabelTyID || + ElemTy->getTypeID() == MetadataTyID || isa(ElemTy)) return false; if (const PointerType *PTy = dyn_cast(ElemTy)) - if (PTy->getElementType() == Type::getMetadataTy(ElemTy->getContext())) + if (PTy->getElementType()->getTypeID() == MetadataTyID) return false; return true; @@ -928,7 +924,7 @@ bool StructType::isValidElementType(const Type *ElemTy) { PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) { assert(ValueType && "Can't get a pointer to type!"); - assert(ValueType != Type::getVoidTy(ValueType->getContext()) && + assert(ValueType->getTypeID() != VoidTyID && "Pointer to void is not valid, use i8* instead!"); assert(isValidElementType(ValueType) && "Invalid type for pointer element!"); PointerValType PVT(ValueType, AddressSpace); @@ -955,12 +951,12 @@ PointerType *Type::getPointerTo(unsigned addrs) const { } bool PointerType::isValidElementType(const Type *ElemTy) { - if (ElemTy == Type::getVoidTy(ElemTy->getContext()) || - ElemTy == Type::getLabelTy(ElemTy->getContext())) + if (ElemTy->getTypeID() == VoidTyID || + ElemTy->getTypeID() == LabelTyID) return false; if (const PointerType *PTy = dyn_cast(ElemTy)) - if (PTy->getElementType() == Type::getMetadataTy(ElemTy->getContext())) + if (PTy->getElementType()->getTypeID() == MetadataTyID) return false; return true; diff --git a/test/Other/2009-09-14-function-elements.ll b/test/Other/2009-09-14-function-elements.ll new file mode 100644 index 0000000000..883d76d107 --- /dev/null +++ b/test/Other/2009-09-14-function-elements.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as %s -disable-output 2>/dev/null + +; Arrays and structures with function types (not function pointers) are illegal. + +@foo = external global [4 x i32 (i32)] +@bar = external global { i32 (i32) } -- 2.11.4.GIT