Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30f2df8

Browse files
committed
Python: Refactor pruning to be more clearly symmetric and complete.
1 parent f29dfa5 commit 30f2df8

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

python/ql/src/semmle/python/Pruning.qll

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,8 @@ module Pruner {
182182
*/
183183
abstract predicate constrainsVariableToBe(boolean value);
184184

185-
/** Holds if this constraint implies that `other` cannot hold for the
186-
* same variable.
187-
* For example `x > 0` implies that `not bool(x)` is `False`.
188-
*/
189-
abstract predicate contradicts(Constraint other);
185+
/** Holds if this constraint implies that `other` cannot be `None` */
186+
abstract predicate cannotBeNone();
190187

191188
}
192189

@@ -275,8 +272,8 @@ module Pruner {
275272
value = this.booleanValue()
276273
}
277274

278-
override predicate contradicts(Constraint other) {
279-
other.constrainsVariableToBe(this.booleanValue().booleanNot())
275+
override predicate cannotBeNone() {
276+
this.booleanValue() = true
280277
}
281278

282279
}
@@ -303,10 +300,8 @@ module Pruner {
303300
value = false and this.isNone() = true
304301
}
305302

306-
override predicate contradicts(Constraint other) {
307-
other = TIsNone(this.isNone().booleanNot())
308-
or
309-
this.isNone() = true and other = TTruthy(true)
303+
override predicate cannotBeNone() {
304+
this = TIsNone(false)
310305
}
311306

312307
}
@@ -348,15 +343,16 @@ module Pruner {
348343
)
349344
}
350345

351-
override predicate contradicts(Constraint other) {
352-
exists(boolean b |
353-
this.constrainsVariableToBe(b) and other.constrainsVariableToBe(b.booleanNot())
354-
)
355-
or
356-
this.getOp() = eq() and other = TIsNone(true)
357-
or
358-
this.getOp() = ne() and other.(ConstrainedByConstant).getOp() = eq()
359-
and this.intValue() = other.(ConstrainedByConstant).intValue()
346+
predicate eq(int val) {
347+
this = TConstrainedByConstant(eq(), val)
348+
}
349+
350+
predicate ne(int val) {
351+
this = TConstrainedByConstant(ne(), val)
352+
}
353+
354+
override predicate cannotBeNone() {
355+
this.getOp() = eq()
360356
}
361357

362358
/** The minimum value that a variable fulfilling this constraint may hold
@@ -551,9 +547,23 @@ module Pruner {
551547
/* Helper for `unreachableEdge`, deal with inequalities here to avoid blow up */
552548
pragma [inline]
553549
private predicate contradicts(Constraint a, Constraint b) {
554-
a.contradicts(b) or
555-
a.(ConstrainedByConstant).minValue() > b.(ConstrainedByConstant).maxValue() or
550+
a = TIsNone(true) and b.cannotBeNone()
551+
or
552+
a.cannotBeNone() and b = TIsNone(true)
553+
or
554+
a.constrainsVariableToBe(true) and b.constrainsVariableToBe(false)
555+
or
556+
a.constrainsVariableToBe(false) and b.constrainsVariableToBe(true)
557+
or
558+
a.(ConstrainedByConstant).minValue() > b.(ConstrainedByConstant).maxValue()
559+
or
556560
a.(ConstrainedByConstant).maxValue() < b.(ConstrainedByConstant).minValue()
561+
or
562+
exists(int val |
563+
a.(ConstrainedByConstant).eq(val) and b.(ConstrainedByConstant).ne(val)
564+
or
565+
a.(ConstrainedByConstant).ne(val) and b.(ConstrainedByConstant).eq(val)
566+
)
557567
}
558568

559569
/** Holds if edge is simply dead. Stuff like `if False: ...` */

0 commit comments

Comments
 (0)