Skip to content

Commit

Permalink
Raise warning when return with void value
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Nov 2, 2023
1 parent e7b8dad commit 167f538
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cc/backend/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void gen_return(Stmt *stmt) {
if (is_prim_type(val->type)) {
int flag = is_unsigned(val->type) ? IRF_UNSIGNED : 0;
new_ir_result(fnbe->result_dst, vreg, flag);
} else {
} else if (val->type->kind != TY_VOID) {
VReg *retval = fnbe->retval;
if (retval != NULL) {
gen_memcpy(val->type, retval, vreg);
Expand Down
12 changes: 9 additions & 3 deletions src/cc/frontend/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,16 @@ static Stmt *parse_return(const Token *tok) {
if (rettype->kind != TY_VOID)
parse_error(PE_NOFATAL, tok, "`return' required a value");
} else {
if (rettype->kind == TY_VOID)
parse_error(PE_NOFATAL, val->token, "void function `return' a value");
else
if (rettype->kind == TY_VOID) {
if (val->type->kind == TY_VOID) {
// Allow `return void_fnc();`.
parse_error(PE_WARNING, val->token, "`return' with void value");
} else {
parse_error(PE_NOFATAL, val->token, "void function `return' a value");
}
} else {
val = make_cast(rettype, val->token, val, false);
}
}

return new_stmt_return(tok, val);
Expand Down

0 comments on commit 167f538

Please sign in to comment.