Skip to content

Commit 0fe5cf6

Browse files
committed
C#: WIP - Range analysis
1 parent d195bb7 commit 0fe5cf6

File tree

2 files changed

+721
-0
lines changed

2 files changed

+721
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
private import csharp
2+
private import Ssa
3+
private import semmle.code.csharp.dataflow.internal.rangeanalysis.ConstantUtils
4+
5+
private newtype TBound =
6+
TBoundZero() or
7+
TBoundSsa(Definition v) { v.getSourceVariable().getType() instanceof IntegralType }
8+
9+
/**
10+
* A bound that may be inferred for an expression plus/minus an integer delta.
11+
*/
12+
abstract class Bound extends TBound {
13+
/** Gets a textual representation of this bound. */
14+
abstract string toString();
15+
16+
/** Gets an expression that equals this bound plus `delta`. */
17+
abstract Expr getExpr(int delta);
18+
19+
/** Gets an expression that equals this bound. */
20+
Expr getExpr() { result = getExpr(0) }
21+
22+
/**
23+
* Holds if this element is at the specified location.
24+
* The location spans column `sc` of line `sl` to
25+
* column `ec` of line `el` in file `path`.
26+
* For more information, see
27+
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
28+
*/
29+
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
30+
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
31+
}
32+
}
33+
34+
/**
35+
* The bound that corresponds to the integer 0. This is used to represent all
36+
* integer bounds as bounds are always accompanied by an added integer delta.
37+
*/
38+
class ZeroBound extends Bound, TBoundZero {
39+
override string toString() { result = "0" }
40+
41+
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
42+
}
43+
44+
/**
45+
* A bound corresponding to the value of an SSA variable.
46+
*/
47+
class DefinitionBound extends Bound, TBoundSsa {
48+
/** Gets the SSA variable that equals this bound. */
49+
Definition getDefinition() { this = TBoundSsa(result) }
50+
51+
override string toString() { result = getDefinition().toString() }
52+
53+
override Expr getExpr(int delta) { result = getDefinition().getARead() and delta = 0 }
54+
55+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
56+
getDefinition().getLocation().hasLocationInfo(path, sl, sc, el, ec)
57+
}
58+
}

0 commit comments

Comments
 (0)