summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen/CGExpr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib/CodeGen/CGExpr.cpp')
-rw-r--r--clang/lib/CodeGen/CGExpr.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 70fa004de85..722ec868f59 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1012,6 +1012,36 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
}
}
+static llvm::Constant *getAbortFn(CodeGenFunction &CGF) {
+ // void abort();
+
+ const llvm::FunctionType *FTy =
+ llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
+
+ return CGF.CGM.CreateRuntimeFunction(FTy, "abort");
+}
+
+llvm::BasicBlock*CodeGenFunction::getAbortBB() {
+ if (AbortBB)
+ return AbortBB;
+
+ llvm::BasicBlock *Cont = 0;
+ if (HaveInsertPoint()) {
+ Cont = createBasicBlock("cont");
+ EmitBranch(Cont);
+ }
+ AbortBB = createBasicBlock("abort");
+ EmitBlock(AbortBB);
+ llvm::CallInst *AbortCall = Builder.CreateCall(getAbortFn(*this));
+ AbortCall->setDoesNotReturn();
+ AbortCall->setDoesNotThrow();
+ Builder.CreateUnreachable();
+
+ if (Cont)
+ EmitBlock(Cont);
+ return AbortBB;
+}
+
LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
// The index must always be an integer, which is not an aggregate. Emit it.
llvm::Value *Idx = EmitScalarExpr(E->getIdx());
@@ -1040,6 +1070,37 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
llvm::IntegerType::get(VMContext, LLVMPointerWidth),
IdxSigned, "idxprom");
+ if (CatchUndefined) {
+ if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())) {
+ if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
+ if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
+ if (const ConstantArrayType *CAT
+ = getContext().getAsConstantArrayType(DRE->getType())) {
+ llvm::APInt Size = CAT->getSize();
+ llvm::BasicBlock *Cont = createBasicBlock("cont");
+ if (IdxSigned) {
+ Builder.CreateCondBr(Builder.CreateICmpSGE(Idx,
+ llvm::ConstantInt::get(Idx->getType(), 0)),
+ Cont, getAbortBB());
+ EmitBlock(Cont);
+ Cont = createBasicBlock("cont");
+ Builder.CreateCondBr(Builder.CreateICmpSLT(Idx,
+ llvm::ConstantInt::get(Idx->getType(), Size)),
+ Cont, getAbortBB());
+ EmitBlock(Cont);
+ } else {
+ llvm::BasicBlock *Cont = createBasicBlock("cont");
+ Builder.CreateCondBr(Builder.CreateICmpULT(Idx,
+ llvm::ConstantInt::get(Idx->getType(), Size)),
+ Cont, getAbortBB());
+ EmitBlock(Cont);
+ }
+ }
+ }
+ }
+ }
+ }
+
// We know that the pointer points to a type of the correct size, unless the
// size is a VLA or Objective-C interface.
llvm::Value *Address = 0;
OpenPOWER on IntegriCloud