Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code quality #109

Merged
merged 5 commits into from
Dec 8, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Better clean up after CSE
FabioLuporini committed Dec 6, 2016
commit 2f7d7c6ce07dcad508ace7219b32f7e32d05e59b
6 changes: 1 addition & 5 deletions coffee/cse.py
Original file line number Diff line number Diff line change
@@ -505,8 +505,4 @@ def unpick(self):
self._push_temporaries(levels[i-1], trace, global_trace, ra, decls)
self._transform_temporaries(levels[i], decls)

# Clean up
for transformed_loop, nest in reversed(nests.items()):
for loop, parent in nest:
if loop == transformed_loop and not loop.body:
parent.children.remove(loop)
cleanup(self.header)
19 changes: 19 additions & 0 deletions coffee/utils.py
Original file line number Diff line number Diff line change
@@ -841,6 +841,25 @@ def remove_empty_loops(node):
parent.children.remove(loop)


def remove_unused_decls(node):
"""Remove all unused decls within node, which must be of type :class:`Block`."""

assert isinstance(node, Block)

decls = FindInstances(Decl, with_parent=True).visit(node)[Decl]
references = visit(node, info_items=['symbol_refs'])['symbol_refs']
for d, p in decls:
if len(references[d.sym.symbol]) == 1:
p.children.remove(d)


def cleanup(node):
"""Remove useless nodes in the AST rooted in node."""

remove_empty_loops(node)
remove_unused_decls(node)


def postprocess(node):
"""Rearrange the Nodes in the AST rooted in ``node`` to improve the code quality
when unparsing the tree."""