Merge pull request #1292 from markshannon/python-prune-in-ql

Python: Do pruning in QL.
This commit is contained in:
Taus
2019-06-19 16:58:27 +02:00
committed by GitHub
8 changed files with 845 additions and 26 deletions

View File

@@ -0,0 +1,107 @@
| 8 | test | test | Truthy |
| 10 | test | test | Truthy |
| 14 | seq | seq | Truthy |
| 16 | seq | seq | Truthy |
| 17 | seq | seq | Truthy |
| 21 | UnaryExpr | t1 | Falsey |
| 21 | t1 | t1 | Truthy |
| 24 | t1 | t1 | Truthy |
| 25 | t1 | t1 | Truthy |
| 26 | t2 | t2 | Truthy |
| 29 | t2 | t2 | Truthy |
| 30 | t2 | t2 | Truthy |
| 31 | t3 | t3 | Truthy |
| 31 | t4 | t4 | Truthy |
| 32 | t3 | t3 | Truthy |
| 33 | t3 | t3 | Truthy |
| 34 | t3 | t3 | Truthy |
| 35 | t4 | t4 | Truthy |
| 36 | t5 | t5 | Truthy |
| 36 | t6 | t6 | Truthy |
| 37 | t5 | t5 | Truthy |
| 38 | t5 | t5 | Truthy |
| 39 | t6 | t6 | Truthy |
| 40 | t6 | t6 | Truthy |
| 43 | t1 | t1 | Truthy |
| 44 | UnaryExpr | t2 | Falsey |
| 44 | t2 | t2 | Truthy |
| 47 | t1 | t1 | Truthy |
| 48 | t2 | t2 | Truthy |
| 49 | t2 | t2 | Truthy |
| 51 | t2 | t2 | Truthy |
| 52 | t2 | t2 | Truthy |
| 55 | seq1 | seq1 | Truthy |
| 57 | UnaryExpr | seq2 | Falsey |
| 57 | seq2 | seq2 | Truthy |
| 60 | seq1 | seq1 | Truthy |
| 62 | seq1 | seq1 | Truthy |
| 63 | seq2 | seq2 | Truthy |
| 65 | seq2 | seq2 | Truthy |
| 66 | seq3 | seq3 | Truthy |
| 68 | UnaryExpr | seq4 | Falsey |
| 68 | seq4 | seq4 | Truthy |
| 71 | seq3 | seq3 | Truthy |
| 73 | var | var | Truthy |
| 74 | seq4 | seq4 | Truthy |
| 76 | var | var | Truthy |
| 78 | seq5 | seq5 | Truthy |
| 80 | seq5 | seq5 | Truthy |
| 81 | seq5 | seq5 | Truthy |
| 83 | var | var | Truthy |
| 88 | UnaryExpr | x | Falsey |
| 88 | x | x | Truthy |
| 89 | Exception | Exception | Truthy |
| 90 | y | y | Truthy |
| 91 | Exception | Exception | Truthy |
| 92 | make_a_call | make_a_call | Truthy |
| 93 | UnaryExpr | x | Falsey |
| 93 | x | x | Truthy |
| 94 | count | count | Truthy |
| 95 | y | y | Truthy |
| 96 | count | count | Truthy |
| 101 | make_a_call | make_a_call | Truthy |
| 102 | UnaryExpr | another_module | Falsey |
| 102 | another_module | another_module | Truthy |
| 103 | count | count | Truthy |
| 107 | UnaryExpr | t1 | Falsey |
| 107 | t1 | t1 | Truthy |
| 109 | t2 | t2 | Truthy |
| 111 | t1 | t1 | Truthy |
| 113 | UnaryExpr | t2 | Falsey |
| 113 | t2 | t2 | Truthy |
| 117 | UnaryExpr | test | Falsey |
| 117 | test | test | Truthy |
| 119 | UnaryExpr | test | Falsey |
| 119 | test | test | Truthy |
| 123 | m | m | Truthy |
| 125 | m | m | Truthy |
| 126 | m | m | Truthy |
| 158 | Compare | ps | Is not None |
| 158 | ps | ps | Truthy |
| 159 | ps | ps | Truthy |
| 160 | Compare | ps | Is None |
| 160 | ps | ps | Truthy |
| 171 | __name__ | __name__ | Truthy |
| 172 | None | None | Truthy |
| 174 | func | func | Truthy |
| 175 | Exception | Exception | Truthy |
| 176 | count | count | Truthy |
| 177 | Compare | escapes | Is None |
| 177 | None | None | Truthy |
| 177 | escapes | escapes | Truthy |
| 178 | count | count | Truthy |
| 180 | count | count | Truthy |
| 188 | true12 | true12 | Truthy |
| 195 | Compare | x | < 4 |
| 195 | x | x | Truthy |
| 197 | Compare | x | < 4 |
| 197 | x | x | Truthy |
| 201 | Compare | x | < 4 |
| 201 | x | x | Truthy |
| 203 | Compare | x | >= 4 |
| 203 | UnaryExpr | x | < 4 |
| 203 | x | x | Truthy |
| 207 | Compare | x | < 4 |
| 207 | x | x | Truthy |
| 209 | Compare | x | < 4 |
| 209 | x | x | Truthy |

View File

@@ -0,0 +1,10 @@
import python
import semmle.python.Pruning
from Pruner::Constraint c, SsaVariable var, Pruner::UnprunedCfgNode node, int line
where c = Pruner::constraintFromTest(var, node) and line = node.getNode().getLocation().getStartLine() and
line > 0
select line, node.getNode().toString(), var.getId(), c

View File

@@ -0,0 +1,2 @@
semmle-extractor-options: --dont-prune-graph
optimize: true

View File

@@ -43,3 +43,13 @@
| 154 | 1 |
| 161 | 1 |
| 163 | 1 |
| 176 | 1 |
| 178 | 1 |
| 180 | 1 |
| 184 | 1 |
| 186 | 1 |
| 189 | 1 |
| 192 | 1 |
| 198 | 0 |
| 204 | 0 |
| 210 | 0 |

View File

@@ -3,20 +3,20 @@
def dead():
return 0
count
def conditional_dead(test):
if test:
return
if test:
count
def made_true(seq):
if seq:
return
seq.append(1)
if seq:
count
def boolop(t1, t2, t3, t4, t5, t6):
if not t1:
return
@@ -38,7 +38,7 @@ def boolop(t1, t2, t3, t4, t5, t6):
t5 and count
t6 or count
t6 and count
def with_splitting(t1, t2):
if t1:
if not t2:
@@ -50,7 +50,7 @@ def with_splitting(t1, t2):
else:
t2 or count
t2 and count
def loops(seq1, seq2, seq3, seq4, seq5):
if seq1:
return
@@ -82,7 +82,7 @@ def loops(seq1, seq2, seq3, seq4, seq5):
count
print(var)
#Logic does not apply to global variables in calls,
#Logic does not apply to global variables in calls,
#as they may be changed from true to false externally.
from some_module import x, y
if not x:
@@ -102,7 +102,7 @@ make_a_call()
if not another_module:
count
def negated_conditional_live(t1, t2):
if not t1:
return
@@ -112,13 +112,13 @@ def negated_conditional_live(t1, t2):
count
if not t2:
count
def negated_conditional_dead(test):
if not test:
return
if not test:
count
def made_true2(m):
if m:
return
@@ -162,3 +162,50 @@ def attribute_lookup_cannot_effect_comparisons_with_immutable_constants(ps):
else:
count
def func():
global escapes
so_something()
escapes = True
#Don't prune on `escapes` as it escapes.
if __name__ == '__main__':
escapes = None # global
try:
func()
except Exception as err:
count
if escapes is None:
count
else:
count
def func2():
while 1:
count
if cond12:
count
try:
true12()
count
except IOError:
true12 = 0
count
def inequality1(x):
if x < 4:
return
if x < 4:
count
def inequality2(x):
if x < 4:
return
if not x >= 4:
count
def reversed_inequality(x):
if x < 4:
return
if 4 > x:
count