Skip to content

Commit 29acd69

Browse files
committed
C#: Add upgrade script.
1 parent d96e8cb commit 29acd69

File tree

4 files changed

+3118
-0
lines changed

4 files changed

+3118
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class Expr extends @expr {
2+
string toString() { none() }
3+
}
4+
5+
class Location extends @location {
6+
string toString() { none() }
7+
}
8+
9+
class ControlFlowElement extends @control_flow_element {
10+
string toString() { none() }
11+
}
12+
13+
class TypeOrRef extends @type_or_ref {
14+
string toString() { none() }
15+
}
16+
17+
class Callable extends @callable {
18+
string toString() { none() }
19+
}
20+
21+
class Accessible extends @accessible {
22+
string toString() { none() }
23+
}
24+
25+
predicate assignmentKind(int kind) {
26+
// | 63 = @simple_assign_expr
27+
// | 80 = @add_event_expr
28+
// | 81 = @remove_event_expr
29+
// | 83 = @local_var_decl_expr
30+
kind = [63, 80, 81, 83]
31+
}
32+
33+
predicate compoundAssignmentKind(int kind) {
34+
// | 64 = @assign_add_expr
35+
// | 65 = @assign_sub_expr
36+
// | 66 = @assign_mul_expr
37+
// | 67 = @assign_div_expr
38+
// | 68 = @assign_rem_expr
39+
// | 69 = @assign_and_expr
40+
// | 70 = @assign_xor_expr
41+
// | 71 = @assign_or_expr
42+
// | 72 = @assign_lshift_expr
43+
// | 73 = @assign_rshift_expr
44+
// | 119 = @assign_coalesce_expr
45+
// | 134 = @assign_urshift_expr
46+
kind = [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 119, 134]
47+
}
48+
49+
class CompoundAssignmentExpr extends Expr {
50+
CompoundAssignmentExpr() {
51+
exists(int kind | compoundAssignmentKind(kind) | expressions(this, kind, _))
52+
}
53+
}
54+
55+
predicate isAssignment(Expr ass) {
56+
exists(int kind | assignmentKind(kind) |
57+
expressions(ass, kind, _) and
58+
// Exclude assignments that are part of a compound assignment. These are handled seperatly.
59+
not exists(CompoundAssignmentExpr e | expr_parent(ass, 2, e))
60+
)
61+
}
62+
63+
Expr getOperatorCall(CompoundAssignmentExpr e) {
64+
exists(Expr assignment |
65+
expr_parent(assignment, 2, e) and
66+
expr_parent(result, 0, assignment)
67+
)
68+
}
69+
70+
query predicate new_expressions(Expr e, int kind, TypeOrRef t) {
71+
expressions(e, kind, t) and
72+
// Remove the unused expanded assignment expressions.
73+
not exists(CompoundAssignmentExpr parent, Expr assignment | expr_parent(assignment, 2, parent) |
74+
e = assignment or
75+
expr_parent(e, 0, assignment) or
76+
expr_parent(e, 1, assignment)
77+
)
78+
}
79+
80+
query predicate new_expr_parent(Expr e, int child, ControlFlowElement parent) {
81+
if isAssignment(parent)
82+
then
83+
// Swap children for assignments, local variable declarations and add/remove event.
84+
child = 0 and expr_parent(e, 1, parent)
85+
or
86+
child = 1 and expr_parent(e, 0, parent)
87+
else (
88+
// Case for compound assignments. The parent child relation is contracted.
89+
exists(Expr op | op = getOperatorCall(parent) | expr_parent(e, child, op))
90+
or
91+
// For other expressions (as long as they are included in the new expressions
92+
// table), the parent child relation is unchanged.
93+
expr_parent(e, child, parent) and
94+
new_expressions(e, _, _) and
95+
(not parent instanceof Expr or new_expressions(parent, _, _))
96+
)
97+
}
98+
99+
query predicate new_expr_location(Expr e, Location loc) {
100+
expr_location(e, loc) and new_expressions(e, _, _)
101+
}
102+
103+
query predicate new_expr_call(Expr e, Callable c) {
104+
exists(Expr op | op = getOperatorCall(e) | expr_call(op, c))
105+
or
106+
expr_call(e, c) and not e = getOperatorCall(_)
107+
}
108+
109+
query predicate new_dynamic_member_name(Expr e, string name) {
110+
exists(Expr op | op = getOperatorCall(e) | dynamic_member_name(op, name))
111+
or
112+
dynamic_member_name(e, name) and not e = getOperatorCall(_)
113+
}
114+
115+
query predicate new_expr_access(Expr e, Accessible a) {
116+
expr_access(e, a) and new_expressions(e, _, _)
117+
}

0 commit comments

Comments
 (0)