mirror of
https://github.com/irmen/prog8.git
synced 2025-07-24 20:24:09 +00:00
better unused variable removal
This commit is contained in:
@@ -107,9 +107,30 @@ class UnusedCodeRemover(private val program: Program,
|
||||
if(decl.type==VarDeclType.VAR) {
|
||||
val forceOutput = "force_output" in decl.definingBlock.options()
|
||||
if (!forceOutput && !decl.autogeneratedDontRemove && !decl.sharedWithAsm && !decl.definingBlock.isInLibrary) {
|
||||
if (callgraph.unused(decl)) {
|
||||
val usages = callgraph.usages(decl)
|
||||
if (usages.isEmpty()) {
|
||||
errors.warn("removing unused variable '${decl.name}'", decl.position)
|
||||
return listOf(IAstModification.Remove(decl, parent as IStatementContainer))
|
||||
} else {
|
||||
// if all usages are just an assignment to this vardecl
|
||||
// and it is in regular RAM, then remove the var as well including all assignments
|
||||
val assignTargets = usages.mapNotNull {
|
||||
if(it.parent is AssignTarget)
|
||||
it.parent as AssignTarget
|
||||
else if(it.parent.parent is AssignTarget)
|
||||
it.parent.parent as AssignTarget
|
||||
else null
|
||||
}.filter {
|
||||
it.isInRegularRAMof(compTarget.machine)
|
||||
}
|
||||
if(assignTargets.size==usages.size) {
|
||||
errors.warn("removing unused variable '${decl.name}'", decl.position)
|
||||
return assignTargets.map { it.parent to it.definingScope}.toSet().map {
|
||||
IAstModification.Remove(it.first, it.second)
|
||||
} + listOf(
|
||||
IAstModification.Remove(decl, parent as IStatementContainer)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user