Skip to content

Commit

Permalink
Make default case propagate final-return status. (fixes #236)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvander committed Sep 29, 2018
1 parent 21634b6 commit ce720b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 7 additions & 4 deletions compiler/sc1.cpp
Expand Up @@ -145,7 +145,7 @@ static int doif(void);
static int dowhile(void);
static int dodo(void);
static int dofor(void);
static void doswitch(void);
static int doswitch(void);
static void doreturn(void);
static void dotypedef();
static void dotypeset();
Expand Down Expand Up @@ -5432,8 +5432,7 @@ static void statement(int *lastindent,int allow_decl)
lastst=dofor();
break;
case tSWITCH:
doswitch();
lastst=tSWITCH;
lastst=doswitch();
break;
case tCASE:
case tDEFAULT:
Expand Down Expand Up @@ -5928,7 +5927,7 @@ static int dofor(void)
* param = table offset (code segment)
*
*/
static void doswitch(void)
static int doswitch(void)
{
int lbl_table,lbl_exit,lbl_case;
int swdefault,casecount;
Expand All @@ -5938,6 +5937,7 @@ static void doswitch(void)
constvalue caselist = { NULL, "", 0, 0}; /* case list starts empty */
constvalue *cse,*csp;
char labelname[sNAMEMAX+1];
int return_statement = tSWITCH;

endtok= matchtoken('(') ? ')' : tDO;
doexpr(TRUE,FALSE,FALSE,FALSE,NULL,NULL,TRUE);/* evaluate switch expression */
Expand Down Expand Up @@ -6015,6 +6015,8 @@ static void doswitch(void)
needtoken(':');
swdefault=TRUE;
statement(NULL,FALSE);
if (lastst == tRETURN)
return_statement = tRETURN;
/* Jump to lbl_exit, even thouh this is the last clause in the
* switch, because the jump table is generated between the last
* clause of the switch and the exit label.
Expand Down Expand Up @@ -6054,6 +6056,7 @@ static void doswitch(void)

setlabel(lbl_exit);
delete_consttable(&caselist); /* clear list of case labels */
return return_statement;
}

static void doassert(void)
Expand Down
14 changes: 14 additions & 0 deletions tests/compile-only/ok-return-in-default-case.sp
@@ -0,0 +1,14 @@
// warnings_are_errors: true
int main() {
funct(2);
}
int funct(int input) {
switch (input) {
case 0:
return 1;
case 1:
return 2;
default:
return 3;
}
}

0 comments on commit ce720b4

Please sign in to comment.