@@ -9,60 +9,24 @@ import semmle.code.cpp.controlflow.Dominance
9
9
private import semmle.code.cpp.valuenumbering.GlobalValueNumbering
10
10
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
11
11
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
12
+ import semmle.code.cpp.controlflow.Guards
12
13
13
14
/**
14
15
* Holds if the value of `use` is guarded using `abs`.
15
16
*/
16
17
predicate guardedAbs ( Operation e , Expr use ) {
17
18
exists ( FunctionCall fc | fc .getTarget ( ) .getName ( ) = [ "abs" , "labs" , "llabs" , "imaxabs" ] |
18
19
fc .getArgument ( 0 ) .getAChild * ( ) = use and
19
- guardedLesser ( e , fc )
20
+ exists ( GuardCondition c | c . ensuresLt ( fc , _ , _ , e . getBasicBlock ( ) , true ) )
20
21
)
21
22
}
22
23
23
- /**
24
- * Gets the position of `stmt` in basic block `block` (this is a thin layer
25
- * over `BasicBlock.getNode`, intended to improve performance).
26
- */
27
- pragma [ noinline]
28
- private int getStmtIndexInBlock ( BasicBlock block , Stmt stmt ) { block .getNode ( result ) = stmt }
29
-
30
- pragma [ inline]
31
- private predicate stmtDominates ( Stmt dominator , Stmt dominated ) {
32
- // In same block
33
- exists ( BasicBlock block , int dominatorIndex , int dominatedIndex |
34
- dominatorIndex = getStmtIndexInBlock ( block , dominator ) and
35
- dominatedIndex = getStmtIndexInBlock ( block , dominated ) and
36
- dominatedIndex >= dominatorIndex
37
- )
38
- or
39
- // In (possibly) different blocks
40
- bbStrictlyDominates ( dominator .getBasicBlock ( ) , dominated .getBasicBlock ( ) )
41
- }
42
-
43
24
/**
44
25
* Holds if the value of `use` is guarded to be less than something, and `e`
45
26
* is in code controlled by that guard (where the guard condition held).
46
27
*/
47
- pragma [ nomagic]
48
28
predicate guardedLesser ( Operation e , Expr use ) {
49
- exists ( IfStmt c , RelationalOperation guard |
50
- use = guard .getLesserOperand ( ) .getAChild * ( ) and
51
- guard = c .getControllingExpr ( ) .getAChild * ( ) and
52
- stmtDominates ( c .getThen ( ) , e .getEnclosingStmt ( ) )
53
- )
54
- or
55
- exists ( Loop c , RelationalOperation guard |
56
- use = guard .getLesserOperand ( ) .getAChild * ( ) and
57
- guard = c .getControllingExpr ( ) .getAChild * ( ) and
58
- stmtDominates ( c .getStmt ( ) , e .getEnclosingStmt ( ) )
59
- )
60
- or
61
- exists ( ConditionalExpr c , RelationalOperation guard |
62
- use = guard .getLesserOperand ( ) .getAChild * ( ) and
63
- guard = c .getCondition ( ) .getAChild * ( ) and
64
- c .getThen ( ) .getAChild * ( ) = e
65
- )
29
+ exists ( GuardCondition c | c .ensuresLt ( use , _, _, e .getBasicBlock ( ) , true ) )
66
30
or
67
31
guardedAbs ( e , use )
68
32
}
@@ -71,25 +35,8 @@ predicate guardedLesser(Operation e, Expr use) {
71
35
* Holds if the value of `use` is guarded to be greater than something, and `e`
72
36
* is in code controlled by that guard (where the guard condition held).
73
37
*/
74
- pragma [ nomagic]
75
38
predicate guardedGreater ( Operation e , Expr use ) {
76
- exists ( IfStmt c , RelationalOperation guard |
77
- use = guard .getGreaterOperand ( ) .getAChild * ( ) and
78
- guard = c .getControllingExpr ( ) .getAChild * ( ) and
79
- stmtDominates ( c .getThen ( ) , e .getEnclosingStmt ( ) )
80
- )
81
- or
82
- exists ( Loop c , RelationalOperation guard |
83
- use = guard .getGreaterOperand ( ) .getAChild * ( ) and
84
- guard = c .getControllingExpr ( ) .getAChild * ( ) and
85
- stmtDominates ( c .getStmt ( ) , e .getEnclosingStmt ( ) )
86
- )
87
- or
88
- exists ( ConditionalExpr c , RelationalOperation guard |
89
- use = guard .getGreaterOperand ( ) .getAChild * ( ) and
90
- guard = c .getCondition ( ) .getAChild * ( ) and
91
- c .getThen ( ) .getAChild * ( ) = e
92
- )
39
+ exists ( GuardCondition c | c .ensuresLt ( use , _, _, e .getBasicBlock ( ) , false ) )
93
40
or
94
41
guardedAbs ( e , use )
95
42
}
0 commit comments