99 */
1010
1111import ql
12+ import codeql_ql.style.ConjunctionParent
13+ import codeql.GlobalValueNumbering as GVN
1214
1315/**
1416 * A variable that is set equal to (assigned) a value one or more times.
@@ -40,8 +42,6 @@ class AssignedVariable extends VarDecl {
4042 }
4143}
4244
43- import codeql.GlobalValueNumbering as GVN
44-
4545/**
4646 * Holds if `assigned1` and `assigned2` assigns the same value to `var`.
4747 * The assignments may be on different branches of a disjunction.
@@ -58,47 +58,14 @@ predicate candidateRedundantAssignment(AssignedVariable var, Expr assigned1, Exp
5858 assigned1 != assigned2
5959}
6060
61- /**
62- * Gets a (transitive) parent of `p`, where the parent is not a disjunction, and `p` is a candidate assignment from `candidateRedundantAssignment`.
63- */
64- AstNode getConjunctionParentRec ( AstNode p ) {
65- candidateRedundantAssignment ( _, p , _) and
66- result = p
67- or
68- result = getConjunctionParentRec ( p ) .getParent ( ) and
69- not result instanceof Disjunction and
70- not result instanceof IfFormula and
71- not result instanceof Implication and
72- not result instanceof Negation and
73- not result instanceof Predicate
74- }
75-
76- /**
77- * Gets which level in the AST `p` is at.
78- * E.g. the top-level is 0, the next level is 1, etc.
79- */
80- int level ( AstNode p ) {
81- p instanceof TopLevel and result = 0
82- or
83- result = level ( p .getParent ( ) ) + 1
84- }
85-
86- /**
87- * Gets the top-most parent of `p` that is not a disjunction.
88- */
89- AstNode getConjunctionParent ( AstNode p ) {
90- result =
91- min ( int level , AstNode parent |
92- parent = getConjunctionParentRec ( p ) and level = level ( parent )
93- |
94- parent order by level
95- )
96- }
61+ /** Holds if `p` is a candidate node for redundant assignment. */
62+ predicate candidateNode ( AstNode p ) { candidateRedundantAssignment ( _, p , _) }
9763
9864from AssignedVariable var , Expr assigned1 , Expr assigned2
9965where
10066 candidateRedundantAssignment ( var , assigned1 , assigned2 ) and
101- getConjunctionParent ( assigned1 ) = getConjunctionParent ( assigned2 ) and
67+ ConjunctionParent< candidateNode / 1 > :: getConjunctionParent ( assigned1 ) =
68+ ConjunctionParent< candidateNode / 1 > :: getConjunctionParent ( assigned2 ) and
10269 // de-duplcation:
10370 (
10471 assigned1 .getLocation ( ) .getStartLine ( ) < assigned2 .getLocation ( ) .getStartLine ( )
0 commit comments