diff options
-rw-r--r-- | CodeGen/CGStmt.cpp | 18 | ||||
-rw-r--r-- | test/CodeGen/switch.c | 15 |
2 files changed, 30 insertions, 3 deletions
diff --git a/CodeGen/CGStmt.cpp b/CodeGen/CGStmt.cpp index 2b9a72be53..c07929d107 100644 --- a/CodeGen/CGStmt.cpp +++ b/CodeGen/CGStmt.cpp @@ -356,9 +356,21 @@ void CodeGenFunction::EmitContinueStmt() { void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { StartBlock("sw.bb"); - llvm::Value *V = EmitScalarExpr(S.getLHS()); - SwitchInsn->addCase(cast<llvm::ConstantInt>(V), Builder.GetInsertBlock()); - assert (!S.getRHS() && "Case statement range is not yet supported"); + llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); + llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS())); + SwitchInsn->addCase(LV, CaseDest); + if (const Expr *R = S.getRHS()) { + llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R)); + llvm::APInt LHS = LV->getValue(); + llvm::APInt RHS = RV->getValue(); + LHS++; + while (LHS != RHS) { + SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); + LHS++; + } + SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); + } + EmitStmt(S.getSubStmt()); } diff --git a/test/CodeGen/switch.c b/test/CodeGen/switch.c index 690c11e9f3..25602e93c3 100644 --- a/test/CodeGen/switch.c +++ b/test/CodeGen/switch.c @@ -15,3 +15,18 @@ int foo(int i) { } +int foo2(int i) { + int j = 0; + switch (i) { + case 1 : + j = 2; break; + case 2 ... 10: + j = 3; break; + default: + j = 42; break; + } + j = j + 1; + return j; +} + + |