compiler warning for unreachable code following a call to exit()

This commit is contained in:
Irmen de Jong 2020-03-13 02:21:37 +01:00
parent 85c7f8314b
commit e6be428589
3 changed files with 19 additions and 4 deletions

View File

@ -327,6 +327,8 @@ internal class AstChecker(private val program: Program,
err("Pass-by-reference types (str, array) cannot occur as a parameter type directly. Instead, use an uword for their address, or access the variable from the outer scope directly.")
}
}
visitStatements(subroutine.statements)
}
override fun visit(repeatLoop: RepeatLoop) {
@ -1040,6 +1042,20 @@ internal class AstChecker(private val program: Program,
}
}
override fun visit(scope: AnonymousScope) {
visitStatements(scope.statements)
}
private fun visitStatements(statements: List<Statement>) {
for((index, stmt) in statements.withIndex()) {
if(stmt is FunctionCallStatement && stmt.target.nameInSource.last()=="exit") {
if(index < statements.size-1) {
printWarning("unreachable code", statements[index+1].position, "exit call above never returns")
}
}
}
}
private fun checkFunctionOrLabelExists(target: IdentifierReference, statement: Statement): Statement? {
val targetStatement = target.targetStatement(program.namespace)
if(targetStatement is Label || targetStatement is Subroutine || targetStatement is BuiltinFunctionStatementPlaceholder)

View File

@ -3,7 +3,6 @@ TODO
====
- option to load library files from a directory instead of the embedded ones
- compiler warning 'unreachable code' for statement following an exit() function call
- code optimizer: remove all statements following an exit() function call
- vector inc/dec/add/sub/mul/div...?
arrayvar++ / arrayvar-- / arrayvar += 2 / arrayvar -= 2 / arrayvar *= 3 / arrayvar /= 3

View File

@ -21,9 +21,9 @@ main {
c64scr.print("sp2:")
print_stackpointer()
exit(65)
sub3() ; TODO unreachable code compiler warning + remove the code in optimizer
sub3() ; TODO unreachable code compiler warning + remove the code in optimizer
sub3() ; TODO unreachable code compiler warning + remove the code in optimizer
sub3() ; TODO remove the code in optimizer
sub3() ; TODO remove the code in optimizer
sub3() ; TODO remove the code in optimizer
}
sub sub3() {