Commit 632aefa5 authored by Vitja Makarov's avatar Vitja Makarov

RemoveUnreachableCode transform and is_terminator Node flag

parent efb0837d
...@@ -129,6 +129,7 @@ class Node(object): ...@@ -129,6 +129,7 @@ class Node(object):
is_name = 0 is_name = 0
is_literal = 0 is_literal = 0
is_terminator = 0
temps = None temps = None
# All descandants should set child_attrs to a list of the attributes # All descandants should set child_attrs to a list of the attributes
...@@ -4050,6 +4051,7 @@ class PassStatNode(StatNode): ...@@ -4050,6 +4051,7 @@ class PassStatNode(StatNode):
class BreakStatNode(StatNode): class BreakStatNode(StatNode):
child_attrs = [] child_attrs = []
is_terminator = True
def analyse_expressions(self, env): def analyse_expressions(self, env):
pass pass
...@@ -4064,6 +4066,7 @@ class BreakStatNode(StatNode): ...@@ -4064,6 +4066,7 @@ class BreakStatNode(StatNode):
class ContinueStatNode(StatNode): class ContinueStatNode(StatNode):
child_attrs = [] child_attrs = []
is_terminator = True
def analyse_expressions(self, env): def analyse_expressions(self, env):
pass pass
...@@ -4084,6 +4087,7 @@ class ReturnStatNode(StatNode): ...@@ -4084,6 +4087,7 @@ class ReturnStatNode(StatNode):
# return_type PyrexType # return_type PyrexType
child_attrs = ["value"] child_attrs = ["value"]
is_terminator = True
def analyse_expressions(self, env): def analyse_expressions(self, env):
return_type = env.return_type return_type = env.return_type
...@@ -4157,6 +4161,7 @@ class RaiseStatNode(StatNode): ...@@ -4157,6 +4161,7 @@ class RaiseStatNode(StatNode):
# cause ExprNode or None # cause ExprNode or None
child_attrs = ["exc_type", "exc_value", "exc_tb", "cause"] child_attrs = ["exc_type", "exc_value", "exc_tb", "cause"]
is_terminator = True
def analyse_expressions(self, env): def analyse_expressions(self, env):
if self.exc_type: if self.exc_type:
...@@ -4251,6 +4256,7 @@ class RaiseStatNode(StatNode): ...@@ -4251,6 +4256,7 @@ class RaiseStatNode(StatNode):
class ReraiseStatNode(StatNode): class ReraiseStatNode(StatNode):
child_attrs = [] child_attrs = []
is_terminator = True
def analyse_expressions(self, env): def analyse_expressions(self, env):
env.use_utility_code(restore_exception_utility_code) env.use_utility_code(restore_exception_utility_code)
......
...@@ -1407,6 +1407,43 @@ class AlignFunctionDefinitions(CythonTransform): ...@@ -1407,6 +1407,43 @@ class AlignFunctionDefinitions(CythonTransform):
return node return node
class RemoveUnreachableCode(CythonTransform):
def visit_Node(self, node):
self.visitchildren(node)
return node
def visit_StatListNode(self, node):
if not self.current_directives['remove_unreachable']:
return node
self.visitchildren(node)
for idx, stat in enumerate(node.stats):
idx += 1
if stat.is_terminator:
if idx < len(node.stats):
if self.current_directives['warn.unreachable']:
warning(node.stats[idx].pos, "Unreachable code", 2)
node.stats = node.stats[:idx]
node.is_terminator = True
break
return node
def visit_IfClauseNode(self, node):
self.visitchildren(node)
if node.body.is_terminator:
node.is_terminator = True
return node
def visit_IfStatNode(self, node):
self.visitchildren(node)
if node.else_clause and node.else_clause.is_terminator:
for clause in node.if_clauses:
if not clause.is_terminator:
break
else:
node.is_terminator = True
return node
class YieldNodeCollector(TreeVisitor): class YieldNodeCollector(TreeVisitor):
def __init__(self): def __init__(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment