diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-03-20 16:59:15 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-20 16:59:15 -0400 |
| commit | 884a9bcafc5fb9ae47245fa3ea9a6e64cb65a482 (patch) | |
| tree | 6cd0de692b9e90bbdaef645ac2349a977740952f /source | |
| parent | cc4c5b8dbbac648a7e914ff46f79293c1e122570 (diff) | |
Handling of switch with empty body (#1284)
* Added handling for empty switch body.
Added test for empty switch.
* Fix testing for case in switch.
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
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<StmtLoweringVisitor> return newCaseLabel; } + bool hasSwitchCases(Stmt* inStmt) + { + Stmt* stmt = inStmt; + // Unwrap any surrounding `{ ... }` so we can look + // at the statement inside. + while (auto blockStmt = as<BlockStmt>(stmt)) + { + stmt = blockStmt->body; + continue; + } + + if (auto seqStmt = as<SeqStmt>(stmt)) + { + // Walk through the children looking for cases + for (auto childStmt : seqStmt->stmts) + { + if (hasSwitchCases(childStmt)) + { + return true; + } + } + } + else if (auto caseStmt = as<CaseStmt>(stmt)) + { + return true; + } + else if (auto defaultStmt = as<DefaultStmt>(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<StmtLoweringVisitor> // 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(); |
