From 2202bce80fc72d067cbe67dc1512f7b45351fd31 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 30 Nov 2007 17:56:23 +0000 Subject: [PATCH] Fix a codegen crash on void ?: reported by Oliver git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44454 91177308-0d34-0410-b5e6-96231b3b80d8 --- CodeGen/CGExpr.cpp | 5 +++++ CodeGen/CGExprScalar.cpp | 11 +++++++---- test/CodeGen/conditional.c | 7 +++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp index f9a00ebfee..8fcc94805a 100644 --- a/CodeGen/CGExpr.cpp +++ b/CodeGen/CGExpr.cpp @@ -492,6 +492,11 @@ RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, const CallExpr *E) { else if (hasAggregateLLVMType(E->getType())) // Struct return. return RValue::getAggregate(Args[0]); + else { + // void return. + assert(E->getType()->isVoidType() && "Should only have a void expr here"); + V = 0; + } return RValue::get(V); } diff --git a/CodeGen/CGExprScalar.cpp b/CodeGen/CGExprScalar.cpp index f0a75dbf13..37389dcd44 100644 --- a/CodeGen/CGExprScalar.cpp +++ b/CodeGen/CGExprScalar.cpp @@ -912,6 +912,11 @@ VisitConditionalOperator(const ConditionalOperator *E) { CGF.EmitBlock(ContBlock); + if (!LHS) { + assert(E->getType()->isVoidType() && "Non-void value should have a value"); + return 0; + } + // Create a PHI node for the real part. llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); PN->reserveOperandSpace(2); @@ -926,16 +931,14 @@ Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS()); } -Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) -{ +Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress(); llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); return V; } -Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) -{ +Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { std::string str; CGF.getContext().getObjcEncodingForType(E->getEncodedType(), str); diff --git a/test/CodeGen/conditional.c b/test/CodeGen/conditional.c index 57a0be1b10..15359e0c8a 100644 --- a/test/CodeGen/conditional.c +++ b/test/CodeGen/conditional.c @@ -8,3 +8,10 @@ double test2(int cond, float a, double b) { return cond ? a : b; } + +void f(); + +void test3(){ + 1 ? f() : (void)0; +} +