Skip to content

Commit

Permalink
Support switch label clauses. Fixes #693 (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamphaus authored and elliotchance committed Jun 12, 2018
1 parent 95503a2 commit 9cd4973
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 27 deletions.
2 changes: 1 addition & 1 deletion tests/code_quality/for.expected.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package main - transpiled by c2go version: v0.22.4 Aluminium 2018-04-24
Package main - transpiled by c2go version: v0.23.0 Berkelium 2018-04-27
If you have found any issues, please raise an issue at:
https://github.com/elliotchance/c2go/
Expand Down
2 changes: 1 addition & 1 deletion tests/code_quality/if.expected.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package main - transpiled by c2go version: v0.22.4 Aluminium 2018-04-24
Package main - transpiled by c2go version: v0.23.0 Berkelium 2018-04-27
If you have found any issues, please raise an issue at:
https://github.com/elliotchance/c2go/
Expand Down
2 changes: 1 addition & 1 deletion tests/code_quality/operators.expected.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package main - transpiled by c2go version: v0.22.4 Aluminium 2018-04-24
Package main - transpiled by c2go version: v0.23.0 Berkelium 2018-04-27
If you have found any issues, please raise an issue at:
https://github.com/elliotchance/c2go/
Expand Down
6 changes: 5 additions & 1 deletion tests/code_quality/switch.expected.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package main - transpiled by c2go version: v0.22.4 Aluminium 2018-04-24
Package main - transpiled by c2go version: v0.23.0 Berkelium 2018-04-27
If you have found any issues, please raise an issue at:
https://github.com/elliotchance/c2go/
Expand Down Expand Up @@ -28,7 +28,11 @@ func switch_function() {
return
}
case int32(4):
{
}
case int32(5):
{
}
case int32(6):
fallthrough
case int32(7):
Expand Down
100 changes: 99 additions & 1 deletion tests/switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// CompoundStmt with 12 children.

#include <stdio.h>
#include <stdbool.h>
#include "tests.h"

void match_a_single_case()
Expand Down Expand Up @@ -125,6 +126,45 @@ void fallthrough_several_midway_default()
}
}

void goto_label(bool use_goto)
{
for (;;) {
switch (0)
{
case 3:
continue;
case 0:
if (use_goto) {
for (;;)
break;
goto LABEL;
fail("code should not reach here");
} else if (false) {
goto LABELX;
goto LABELY;
fail("code should not reach here");
}
/* other comment */
// some comment
/* fallthrough */
LABELY:
case 4:
LABEL:
printf("x");
case 1:
pass(__func__);
break;
case 2:
;
LABELX:
default:
fail("code should not reach here");
break;
}
break;
}
}

void scoped_match_a_single_case()
{
switch (1)
Expand Down Expand Up @@ -278,6 +318,60 @@ void scoped_fallthrough_several_midway_default()
}
}

void scoped_goto_label(bool use_goto)
{
for (;;) {
switch (0)
{
case 3:
{
continue;
}
case 0:
{
if (use_goto) {
for (;;) {
break;
}
goto LABEL;
fail("code should not reach here");
} else if (false) {
goto LABELX;
goto LABELY;
fail("code should not reach here");
}
/* other comment */
// some comment
/* fallthrough */
}
LABELY: {}
case 4: {}
LABEL:
{
printf("x");
}
case 1:
{
pass(__func__);
break;
}
case 2:
{
int x = 0;
printf("%d", x);
break;
}
LABELX: {}
default:
{
fail("code should not reach here");
break;
}
}
break;
}
}

typedef struct I67 I67;
struct I67{
int x,y;
Expand Down Expand Up @@ -376,14 +470,16 @@ void switch_without_input()

int main()
{
plan(33);
plan(37);

match_a_single_case();
fallthrough_to_next_case();
match_no_cases();
match_default();
fallthrough_several_cases_including_default();
fallthrough_several_midway_default();
goto_label(false);
goto_label(true);

// For each of the tests above there will be identical cases that use scopes
// for the case statements.
Expand All @@ -393,6 +489,8 @@ int main()
scoped_match_default();
scoped_fallthrough_several_cases_including_default();
scoped_fallthrough_several_midway_default();
scoped_goto_label(false);
scoped_goto_label(true);

switch_issue67();
empty_switch();
Expand Down
Loading

0 comments on commit 9cd4973

Please sign in to comment.