From 884a9bcafc5fb9ae47245fa3ea9a6e64cb65a482 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 20 Mar 2020 16:59:15 -0400 Subject: Handling of switch with empty body (#1284) * Added handling for empty switch body. Added test for empty switch. * Fix testing for case in switch. --- source/slang/slang-lower-to-ir.cpp | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'source') diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 029fe23f6..16dc14819 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -3592,6 +3592,41 @@ struct StmtLoweringVisitor : StmtVisitor return newCaseLabel; } + bool hasSwitchCases(Stmt* inStmt) + { + Stmt* stmt = inStmt; + // Unwrap any surrounding `{ ... }` so we can look + // at the statement inside. + while (auto blockStmt = as(stmt)) + { + stmt = blockStmt->body; + continue; + } + + if (auto seqStmt = as(stmt)) + { + // Walk through the children looking for cases + for (auto childStmt : seqStmt->stmts) + { + if (hasSwitchCases(childStmt)) + { + return true; + } + } + } + else if (auto caseStmt = as(stmt)) + { + return true; + } + else if (auto defaultStmt = as(stmt)) + { + // A 'default:' is a kind of case. + return true; + } + + return false; + } + // Given a statement that appears as (or in) the body // of a `switch` statement void lowerSwitchCases(Stmt* inStmt, SwitchStmtInfo* info) @@ -3734,6 +3769,14 @@ struct StmtLoweringVisitor : StmtVisitor // First emit code to compute the condition: auto conditionVal = getSimpleVal(context, lowerRValueExpr(context, stmt->condition)); + // Check for any cases or default. + if (!hasSwitchCases(stmt->body)) + { + // If we don't have any case/default then nothing inside switch can be executed (other than condition) + // so we are done. + return; + } + // Remember the initial block so that we can add to it // after we've collected all the `case`s auto initialBlock = builder->getBlock(); -- cgit v1.2.3