mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Fix two issues in the dead code removal module
1. When the last statement of a function is a RETURN statement which is syntactically required, it could still be deleted. 2. The child of a RETURN statement could be removed if the statement was not executed. This commit fixes both issues. Bug report and test case provided by @Tonaie. Fixes #14.
This commit is contained in:
parent
0affbf13dd
commit
d6bf0c390e
2 changed files with 22 additions and 10 deletions
|
@ -386,7 +386,7 @@ class deadcode(object):
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def CleanNode(self, curnode):
|
def CleanNode(self, curnode, isFnDef = False):
|
||||||
"""Recursively checks if the children are used, deleting those that are
|
"""Recursively checks if the children are used, deleting those that are
|
||||||
not.
|
not.
|
||||||
"""
|
"""
|
||||||
|
@ -402,14 +402,25 @@ class deadcode(object):
|
||||||
node = curnode.ch[index]
|
node = curnode.ch[index]
|
||||||
|
|
||||||
if not hasattr(node, 'X'):
|
if not hasattr(node, 'X'):
|
||||||
if curnode.ch[index].nt == 'JUMP':
|
deleted = curnode.ch[index]
|
||||||
# Decrease label reference count
|
# Don't delete the child of a RETURN statement!
|
||||||
scope = curnode.ch[index].scope
|
if curnode.nt == 'RETURN':
|
||||||
name = curnode.ch[index].name
|
pass
|
||||||
assert self.symtab[scope][name]['ref'] > 0
|
# If it's a RETURN as the last statement of a block in a
|
||||||
self.symtab[scope][name]['ref'] -= 1
|
# function definition, don't delete it (a hacky workaround for
|
||||||
del curnode.ch[index]
|
# issue #14).
|
||||||
continue
|
elif (deleted.nt == 'RETURN' and index == len(curnode.ch) - 1
|
||||||
|
and isFnDef):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
del curnode.ch[index]
|
||||||
|
if deleted.nt == 'JUMP':
|
||||||
|
# Decrease label reference count
|
||||||
|
scope = deleted.scope
|
||||||
|
name = deleted.name
|
||||||
|
assert self.symtab[scope][name]['ref'] > 0
|
||||||
|
self.symtab[scope][name]['ref'] -= 1
|
||||||
|
continue
|
||||||
|
|
||||||
nt = node.nt
|
nt = node.nt
|
||||||
|
|
||||||
|
@ -484,7 +495,7 @@ class deadcode(object):
|
||||||
child[1] = nr(nt='CONST', X=True, SEF=True, t='integer',
|
child[1] = nr(nt='CONST', X=True, SEF=True, t='integer',
|
||||||
value=0)
|
value=0)
|
||||||
|
|
||||||
self.CleanNode(node)
|
self.CleanNode(node, isFnDef = (curnode.nt == 'FNDEF'))
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
1
unit_tests/regression.suite/state-in-udf-6.skp
Normal file
1
unit_tests/regression.suite/state-in-udf-6.skp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Skipped because our fix to #14 brings back the RETURN statement.
|
Loading…
Add table
Add a link
Reference in a new issue