Skip to content

Commit

Permalink
Merge pull request #4531 from 9rnsr/fix14352
Browse files Browse the repository at this point in the history
Issue 14352 - Two goto cases in one case branch does not work correctly
  • Loading branch information
yebblies committed Mar 29, 2015
2 parents 55c0dd9 + e522feb commit 83a9499
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/statement.c
Expand Up @@ -3328,11 +3328,13 @@ Statement *CaseStatement::semantic(Scope *sc)
/* This is where variables are allowed as case expressions.
*/
if (exp->op == TOKvar)
{ VarExp *ve = (VarExp *)exp;
{
VarExp *ve = (VarExp *)exp;
VarDeclaration *v = ve->var->isVarDeclaration();
Type *t = exp->type->toBasetype();
if (v && (t->isintegral() || t->ty == Tclass))
{ /* Flag that we need to do special code generation
{
/* Flag that we need to do special code generation
* for this, i.e. generate a sequence of if-then-else
*/
sw->hasVars = 1;
Expand Down Expand Up @@ -3372,15 +3374,17 @@ Statement *CaseStatement::semantic(Scope *sc)
sw->cases->push(this);

// Resolve any goto case's with no exp to this case statement
for (size_t i = 0; i < sw->gotoCases.dim; i++)
for (size_t i = 0; i < sw->gotoCases.dim; )
{
GotoCaseStatement *gcs = sw->gotoCases[i];

if (!gcs->exp)
{
gcs->cs = this;
sw->gotoCases.remove(i); // remove from array
continue;
}
i++;
}

if (sc->sw->tf != sc->tf)
Expand Down
38 changes: 38 additions & 0 deletions test/runnable/testswitch.d
Expand Up @@ -621,6 +621,43 @@ void test23()
assert(bar23(0x1000_0000_8000L+1) == 28);
}

/*****************************************/
// 14352

int transmogrify14352(int input)
{
int output = 0;
switch (input)
{
case 0, 1:
if (input == 0)
goto case;
else
output++;
goto case;
case 2:
output += 5;
goto case;
case 3:
output += 5;
break;
case 4, 5, 6:
goto default;
case 7:
case 8:
output += 20;
break;
default:
return -1;
}
return output;
}

void test14352()
{
assert(transmogrify14352(0) == 10);
}

/*****************************************/

int main()
Expand Down Expand Up @@ -649,6 +686,7 @@ int main()
test21();
test22();
test23();
test14352();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 83a9499

Please sign in to comment.