1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-30 09:57:11 +00:00

Added proper error when returning value from void function. Closes #400

This commit is contained in:
jespergravgaard 2020-04-13 10:50:57 +02:00
parent ab10f2184d
commit 3dcec77fb7
3 changed files with 17 additions and 0 deletions

View File

@ -1678,6 +1678,9 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
KickCParser.CommaExprContext exprCtx = ctx.commaExpr(); KickCParser.CommaExprContext exprCtx = ctx.commaExpr();
RValue rValue; RValue rValue;
if(exprCtx != null) { if(exprCtx != null) {
if(SymbolType.VOID.equals(procedure.getReturnType())) {
throw new CompileError("Error! Return value from void function "+procedure.getFullName(), new StatementSource(ctx));
}
PrePostModifierHandler.addPreModifiers(this, exprCtx, new StatementSource(ctx)); PrePostModifierHandler.addPreModifiers(this, exprCtx, new StatementSource(ctx));
rValue = (RValue) this.visit(exprCtx); rValue = (RValue) this.visit(exprCtx);
Variable returnVar = procedure.getLocalVariable("return"); Variable returnVar = procedure.getLocalVariable("return");

View File

@ -3845,6 +3845,11 @@ public class TestPrograms {
assertError("noreturn.c", "Method must end with a return statement", false); assertError("noreturn.c", "Method must end with a return statement", false);
} }
@Test
public void testReturnFromVoid() throws IOException, URISyntaxException {
assertError("returnfromvoid.c", "Error! Return value from void function");
}
@Test @Test
public void testProcedureNotFound() throws IOException, URISyntaxException { public void testProcedureNotFound() throws IOException, URISyntaxException {
assertError("procedurenotfound.c", "Called procedure not found"); assertError("procedurenotfound.c", "Called procedure not found");

View File

@ -0,0 +1,9 @@
// Return a value from a void function
void main() {
print();
}
void print() {
return 2;
}