Skip to content

Commit 44c2284

Browse files
committed
Simplify bounds
1 parent 959724c commit 44c2284

File tree

4 files changed

+36
-80
lines changed

4 files changed

+36
-80
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/BoundCommon.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Provides classes for representing abstract bounds for use in, for example, range analysis.
33
*/
44

5-
private import BoundSpecific::Private
5+
private import BoundSpecific
66

77
private newtype TBound =
88
TBoundZero() or
@@ -53,10 +53,10 @@ class SsaBound extends Bound, TBoundSsa {
5353

5454
override string toString() { result = getSsa().toString() }
5555

56-
override Expr getExpr(int delta) { result = getARead(getSsa()) and delta = 0 }
56+
override Expr getExpr(int delta) { result = getSsa().getAUse() and delta = 0 }
5757

5858
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
59-
hasLocationInfo(getSsaLocation(getSsa()), path, sl, sc, el, ec)
59+
getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec)
6060
}
6161
}
6262

@@ -70,6 +70,6 @@ class ExprBound extends Bound, TBoundExpr {
7070
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
7171

7272
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
73-
hasLocationInfo(getExprLocation(getExpr()), path, sl, sc, el, ec)
73+
getExpr().getLocation().hasLocationInfo(path, sl, sc, el, ec)
7474
}
7575
}
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
11
/**
22
* Provides C#-specific definitions for bounds.
33
*/
4-
module Private {
5-
private import csharp as CS
6-
private import semmle.code.csharp.dataflow.SSA::Ssa as Ssa
7-
private import semmle.code.csharp.dataflow.internal.rangeanalysis.ConstantUtils as CU
8-
import Impl
94

10-
class SsaVariable = Ssa::Definition;
5+
private import csharp as CS
6+
private import semmle.code.csharp.dataflow.SSA::Ssa as Ssa
7+
private import semmle.code.csharp.dataflow.internal.rangeanalysis.ConstantUtils as CU
118

12-
class Expr = CS::Expr;
13-
14-
class ConstantIntegerExpr = CU::ConstantIntegerExpr;
9+
class SsaVariable extends Ssa::Definition {
10+
/** Gets a read of the source variable underlying this SSA definition. */
11+
Expr getAUse() { result = getARead() }
1512
}
1613

17-
private module Impl {
18-
private import csharp
19-
private import ConstantUtils
20-
private import semmle.code.csharp.dataflow.SSA::Ssa
21-
22-
/** Holds if this `v` is of type `IntegralType`. */
23-
predicate ssaVariableIntegralType(Definition v) {
24-
v.getSourceVariable().getType() instanceof IntegralType
25-
}
26-
27-
/** Holds if this `e` is a bound expression and it is not an SSA variable read. */
28-
predicate nonSsaVariableBoundedExpr(Expr e) { systemArrayLengthAccess(e.(PropertyRead)) }
14+
class Expr = CS::Expr;
2915

30-
/** Gets an expression where SSA variable `v` is read. */
31-
Expr getARead(Definition v) { result = v.getARead() }
16+
class ConstantIntegerExpr = CU::ConstantIntegerExpr;
3217

33-
/** Get the location of the expression. */
34-
Location getExprLocation(Expr e) { result = e.getLocation() }
35-
36-
/** Get the location of the SSA variable. */
37-
Location getSsaLocation(Definition v) { result = v.getLocation() }
38-
39-
/** Holds if location is at the specified location. */
40-
predicate hasLocationInfo(
41-
Location l, string filepath, int startline, int startcolumn, int endline, int endcolumn
42-
) {
43-
l.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
44-
}
18+
/** Holds if this `v` is of type `IntegralType`. */
19+
predicate ssaVariableIntegralType(SsaVariable v) {
20+
v.getSourceVariable().getType() instanceof CS::IntegralType
4521
}
22+
23+
/** Holds if this `e` is a bound expression and it is not an SSA variable read. */
24+
predicate nonSsaVariableBoundedExpr(Expr e) { CU::systemArrayLengthAccess(e.(CS::PropertyRead)) }

java/ql/src/semmle/code/java/dataflow/internal/rangeanalysis/BoundCommon.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Provides classes for representing abstract bounds for use in, for example, range analysis.
33
*/
44

5-
private import BoundSpecific::Private
5+
private import BoundSpecific
66

77
private newtype TBound =
88
TBoundZero() or
@@ -53,10 +53,10 @@ class SsaBound extends Bound, TBoundSsa {
5353

5454
override string toString() { result = getSsa().toString() }
5555

56-
override Expr getExpr(int delta) { result = getARead(getSsa()) and delta = 0 }
56+
override Expr getExpr(int delta) { result = getSsa().getAUse() and delta = 0 }
5757

5858
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
59-
hasLocationInfo(getSsaLocation(getSsa()), path, sl, sc, el, ec)
59+
getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec)
6060
}
6161
}
6262

@@ -70,6 +70,6 @@ class ExprBound extends Bound, TBoundExpr {
7070
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
7171

7272
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
73-
hasLocationInfo(getExprLocation(getExpr()), path, sl, sc, el, ec)
73+
getExpr().getLocation().hasLocationInfo(path, sl, sc, el, ec)
7474
}
7575
}
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
11
/**
22
* Provides Java-specific definitions for bounds.
33
*/
4-
module Private {
5-
private import semmle.code.java.dataflow.SSA as Ssa
6-
private import java as J
7-
private import semmle.code.java.dataflow.RangeUtils as RU
8-
import Impl
94

10-
class SsaVariable = Ssa::SsaVariable;
5+
private import semmle.code.java.dataflow.SSA as Ssa
6+
private import java as J
7+
private import semmle.code.java.dataflow.RangeUtils as RU
118

12-
class Expr = J::Expr;
9+
class SsaVariable = Ssa::SsaVariable;
1310

14-
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
15-
}
16-
17-
private module Impl {
18-
private import java
19-
private import semmle.code.java.dataflow.SSA
20-
21-
/** Holds if this `v` is of type `IntegralType`. */
22-
predicate ssaVariableIntegralType(SsaVariable v) {
23-
v.getSourceVariable().getType() instanceof IntegralType
24-
}
11+
class Expr = J::Expr;
2512

26-
/** Holds if this `e` is a bound expression and it is not an SSA variable read. */
27-
predicate nonSsaVariableBoundedExpr(Expr e) {
28-
e.(FieldRead).getField() instanceof ArrayLengthField and
29-
not exists(SsaVariable v | e = v.getAUse())
30-
}
13+
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
3114

32-
/** Gets an expression where SSA variable `v` is read. */
33-
Expr getARead(SsaVariable v) { result = v.getAUse() }
34-
35-
/** Get the location of the expression. */
36-
Location getExprLocation(Expr e) { result = e.getLocation() }
37-
38-
/** Get the location of the SSA variable. */
39-
Location getSsaLocation(SsaVariable v) { result = v.getLocation() }
15+
/** Holds if this `v` is of type `IntegralType`. */
16+
predicate ssaVariableIntegralType(SsaVariable v) {
17+
v.getSourceVariable().getType() instanceof J::IntegralType
18+
}
4019

41-
/** Holds if location is at the specified location. */
42-
predicate hasLocationInfo(
43-
Location l, string filepath, int startline, int startcolumn, int endline, int endcolumn
44-
) {
45-
l.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
46-
}
20+
/** Holds if this `e` is a bound expression and it is not an SSA variable read. */
21+
predicate nonSsaVariableBoundedExpr(Expr e) {
22+
e.(J::FieldRead).getField() instanceof J::ArrayLengthField and
23+
not exists(SsaVariable v | e = v.getAUse())
4724
}

0 commit comments

Comments
 (0)