flow analysis working for elif statements (even in combination with else)

This commit is contained in:
Dave Halter
2014-08-07 12:15:25 +02:00
parent 743d064e6d
commit 0ae9e520c1
2 changed files with 86 additions and 19 deletions

View File

@@ -35,26 +35,33 @@ UNSURE = Status(None, 'unsure')
def break_check(evaluator, base_scope, element_scope):
reachable = REACHABLE
if isinstance(element_scope, Flow):
if element_scope.command == 'else':
check_scope = element_scope
invert = False
if check_scope.command == 'else':
while check_scope.previous is not None:
check_scope = check_scope.previous
invert = True
if check_scope.command == 'if' and check_scope.inputs:
types = evaluator.eval_statement(check_scope.inputs[0])
values = set(x.py__bool__() for x in types)
if len(values) == 1:
reachable = Status.lookup_table[values.pop()]
if invert:
reachable = _check_flow(evaluator, check_scope)
if reachable in (REACHABLE, UNSURE):
break
reachable = reachable.invert()
if reachable is UNREACHABLE:
return UNREACHABLE
else:
return UNSURE
elif check_scope.command in ('try', 'except', 'finally'):
reachable = UNSURE
reachable = _check_flow(evaluator, element_scope)
if base_scope != element_scope and base_scope != check_scope.parent:
# Only reachable branches need to be examined further.
if reachable in (UNREACHABLE, UNSURE):
return reachable
if base_scope != element_scope and base_scope != element_scope.parent:
return reachable & break_check(evaluator, base_scope, element_scope.parent)
return reachable
def _check_flow(evaluator, flow):
if flow.command in ('elif', 'if') and flow.inputs:
types = evaluator.eval_statement(flow.inputs[0])
values = set(x.py__bool__() for x in types)
if len(values) == 1:
return Status.lookup_table[values.pop()]
else:
return UNSURE
elif flow.command in ('try', 'except', 'finally', 'while'):
return UNSURE

View File

@@ -20,3 +20,63 @@ def try_except(x):
#? float() str()
try_except(1)
# Exceptions are not analyzed. So check both if branches
def try_except(x):
try:
if 0:
return 1
else:
return ''
except AttributeError:
return 1.0
#? float() str()
try_except(1)
# -----------------
# elif
# -----------------
def elif_flows1(x):
if False:
return 1
elif True:
return 1.0
else:
return ''
#? float()
elif_flows1(1)
def elif_flows2(x):
try:
if False:
return 1
elif 0:
return 1.0
else:
return ''
except ValueError:
return set
#? str() set
elif_flows2(1)
def elif_flows3(x):
try:
if True:
return 1
elif 0:
return 1.0
else:
return ''
except ValueError:
return set
#? int() set
elif_flows3(1)