Skip to content

Commit ab4f170

Browse files
authored
Merge pull request #21909 from michaelnebel/csharp/refactoroperations
C#: Refactor- and rename operation expressions.
2 parents 14c72de + d0841d2 commit ab4f170

21 files changed

Lines changed: 6296 additions & 233 deletions

File tree

csharp/downgrades/d13c4c187d7318fd2b8f35c7e8d7f4dc26be68b1/old.dbscheme

Lines changed: 1511 additions & 0 deletions
Large diffs are not rendered by default.

csharp/downgrades/d13c4c187d7318fd2b8f35c7e8d7f4dc26be68b1/semmlecode.csharp.dbscheme

Lines changed: 1505 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
description: Restructure and rename types related to operations.
2+
compatibility: full
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: breaking
3+
---
4+
* Renamed types related to *operation* expressions. The QL classes `BinaryArithmeticOperation`, `BinaryBitwiseOperation`, and `BinaryLogicalOperation` now include compound assignments; for example, `BinaryArithmeticOperation` now includes `a += b`.

csharp/ql/lib/experimental/code/csharp/Cryptography/NonCryptographicHashes.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ private predicate maybeUsedInElfHashFunction(Variable v, Operation xor, Operatio
5050
|
5151
add instanceof AddOperation and
5252
e1.getAChild*() = add.getAnOperand() and
53-
e1 instanceof BinaryBitwiseOperation and
54-
e2 = e1.(BinaryBitwiseOperation).getLeftOperand() and
53+
e1 instanceof BinaryBitwiseExpr and
54+
e2 = e1.(BinaryBitwiseExpr).getLeftOperand() and
5555
v = addAssign.getTargetVariable() and
5656
addAssign.getAChild*() = add and
5757
(xor instanceof BitwiseXorExpr or xor instanceof AssignXorExpr) and
5858
addAssign.getControlFlowNode().getASuccessor*() = xor.getControlFlowNode() and
5959
xorAssign.getAChild*() = xor and
6060
v = xorAssign.getTargetVariable() and
61-
(notOp instanceof UnaryBitwiseOperation or notOp instanceof AssignBitwiseOperation) and
61+
(notOp instanceof UnaryBitwiseOperation or notOp instanceof AssignBitwiseExpr) and
6262
xor.getControlFlowNode().getASuccessor*() = notOp.getControlFlowNode() and
6363
notAssign.getAChild*() = notOp and
6464
v = notAssign.getTargetVariable() and

csharp/ql/lib/semmle/code/csharp/Assignable.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ module AssignableInternal {
290290
newtype TAssignableDefinition =
291291
TAssignmentDefinition(Assignment a) {
292292
not a.getLeftOperand() instanceof TupleExpr and
293-
not a instanceof AssignCallOperation and
293+
not a instanceof AssignCallExpr and
294294
not a instanceof AssignCoalesceExpr
295295
} or
296296
TTupleAssignmentDefinition(AssignExpr ae, Expr leaf) { tupleAssignmentDefinition(ae, leaf) } or
@@ -324,7 +324,7 @@ module AssignableInternal {
324324
TAddressOfDefinition(AddressOfExpr aoe) or
325325
TPatternDefinition(TopLevelPatternDecl tlpd) or
326326
TAssignOperationDefinition(AssignOperation ao) {
327-
ao instanceof AssignCallOperation and not ao instanceof CompoundAssignmentOperatorCall
327+
ao instanceof AssignCallExpr and not ao instanceof CompoundAssignmentOperatorCall
328328
or
329329
ao instanceof AssignCoalesceExpr
330330
}

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -912,18 +912,17 @@ module Internal {
912912
)
913913
or
914914
// In C#, `null + 1` has type `int?` with value `null`
915-
exists(BinaryOperation bo, Expr o |
916-
bo instanceof BinaryArithmeticOperation or
917-
bo instanceof AssignArithmeticOperation
918-
|
919-
result = bo and
920-
bo.getAnOperand() = e and
921-
bo.getAnOperand() = o and
922-
// The other operand must be provably non-null in order
923-
// for `only if` to hold
924-
nonNullValueImplied(o) and
925-
e != o
926-
)
915+
result =
916+
any(BinaryArithmeticOperation bao |
917+
exists(Expr o |
918+
bao.getAnOperand() = e and
919+
bao.getAnOperand() = o and
920+
// The other operand must be provably non-null in order
921+
// for `only if` to hold
922+
nonNullValueImplied(o) and
923+
e != o
924+
)
925+
)
927926
}
928927

929928
/**
@@ -934,10 +933,10 @@ module Internal {
934933
any(QualifiableExpr qe |
935934
qe.isConditional() and
936935
result = qe.getQualifier()
937-
) or
936+
)
937+
or
938938
// In C#, `null + 1` has type `int?` with value `null`
939-
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand()) or
940-
e = any(AssignArithmeticOperation aao | result = aao.getAnOperand())
939+
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand())
941940
}
942941

943942
deprecated predicate isGuard(Expr e, GuardValue val) {

csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ private module Internal {
124124
TDispatchDynamicOperatorCall(DynamicOperatorCall doc) or
125125
TDispatchDynamicMemberAccess(DynamicMemberAccess dma) or
126126
TDispatchDynamicElementAccess(DynamicElementAccess dea) or
127-
TDispatchDynamicEventAccess(
128-
AssignArithmeticOperation aao, DynamicMemberAccess dma, string name
129-
) {
127+
TDispatchDynamicEventAccess(AssignArithmeticExpr aao, DynamicMemberAccess dma, string name) {
130128
isPotentialEventCall(aao, dma, name)
131129
} or
132130
TDispatchDynamicObjectCreation(DynamicObjectCreation doc) or
@@ -230,7 +228,7 @@ private module Internal {
230228
* accessor.
231229
*/
232230
private predicate isPotentialEventCall(
233-
AssignArithmeticOperation aao, DynamicMemberAccess dma, string name
231+
AssignArithmeticExpr aao, DynamicMemberAccess dma, string name
234232
) {
235233
aao instanceof DynamicOperatorCall and
236234
dma = aao.getLeftOperand() and
@@ -1397,9 +1395,7 @@ private module Internal {
13971395
private class DispatchDynamicEventAccess extends DispatchReflectionOrDynamicCall,
13981396
TDispatchDynamicEventAccess
13991397
{
1400-
override AssignArithmeticOperation getCall() {
1401-
this = TDispatchDynamicEventAccess(result, _, _)
1402-
}
1398+
override AssignArithmeticExpr getCall() { this = TDispatchDynamicEventAccess(result, _, _) }
14031399

14041400
override string getName() { this = TDispatchDynamicEventAccess(_, _, result) }
14051401

csharp/ql/lib/semmle/code/csharp/exprs/ArithmeticOperation.qll

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,27 @@ import Expr
1111
* (`UnaryArithmeticOperation`) or a binary arithmetic operation
1212
* (`BinaryArithmeticOperation`).
1313
*/
14-
class ArithmeticOperation extends Operation, @arith_op_expr {
14+
class ArithmeticOperation extends Operation, @arith_operation {
1515
override string getOperator() { none() }
1616
}
1717

1818
/**
19-
* A unary arithmetic operation. Either a unary minus operation
20-
* (`UnaryMinusExpr`), a unary plus operation (`UnaryPlusExpr`),
19+
* A binary arithmetic operation. Either a binary arithmetic expression (`BinaryArithmeticExpr`) or
20+
* an arithmetic assignment expression (`AssignArithmeticExpr`).
21+
*/
22+
class BinaryArithmeticOperation extends ArithmeticOperation, BinaryOperation, @bin_arith_operation {
23+
override string getOperator() { none() }
24+
}
25+
26+
/**
27+
* A unary arithmetic operation. Either a unary minus expression
28+
* (`UnaryMinusExpr`), a unary plus expression (`UnaryPlusExpr`),
2129
* or a mutator operation (`MutatorOperation`).
2230
*/
23-
class UnaryArithmeticOperation extends ArithmeticOperation, UnaryOperation, @un_arith_op_expr { }
31+
class UnaryArithmeticOperation extends ArithmeticOperation, UnaryOperation, @un_arith_operation { }
2432

2533
/**
26-
* A unary minus operation, for example `-x`.
34+
* A unary minus expression, for example `-x`.
2735
*/
2836
class UnaryMinusExpr extends UnaryArithmeticOperation, @minus_expr {
2937
override string getOperator() { result = "-" }
@@ -32,7 +40,7 @@ class UnaryMinusExpr extends UnaryArithmeticOperation, @minus_expr {
3240
}
3341

3442
/**
35-
* A unary plus operation, for example `+x`.
43+
* A unary plus expression, for example `+x`.
3644
*/
3745
class UnaryPlusExpr extends UnaryArithmeticOperation, @plus_expr {
3846
override string getOperator() { result = "+" }
@@ -44,40 +52,40 @@ class UnaryPlusExpr extends UnaryArithmeticOperation, @plus_expr {
4452
* A mutator operation. Either an increment operation (`IncrementOperation`)
4553
* or a decrement operation (`DecrementOperation`).
4654
*/
47-
class MutatorOperation extends UnaryArithmeticOperation, @mut_op_expr { }
55+
class MutatorOperation extends UnaryArithmeticOperation, @mut_operation { }
4856

4957
/**
50-
* An increment operation. Either a postfix increment operation
51-
* (`PostIncrExpr`) or a prefix increment operation (`PreIncrExpr`).
58+
* An increment operation. Either a postfix increment expression
59+
* (`PostIncrExpr`) or a prefix increment expression (`PreIncrExpr`).
5260
*/
53-
class IncrementOperation extends MutatorOperation, @incr_op_expr {
61+
class IncrementOperation extends MutatorOperation, @incr_operation {
5462
override string getOperator() { result = "++" }
5563
}
5664

5765
/**
58-
* A decrement operation. Either a postfix decrement operation
59-
* (`PostDecrExpr`) or a prefix decrement operation (`PreDecrExpr`).
66+
* A decrement operation. Either a postfix decrement expression
67+
* (`PostDecrExpr`) or a prefix decrement expression (`PreDecrExpr`).
6068
*/
61-
class DecrementOperation extends MutatorOperation, @decr_op_expr {
69+
class DecrementOperation extends MutatorOperation, @decr_operation {
6270
override string getOperator() { result = "--" }
6371
}
6472

6573
/**
66-
* A prefix increment operation, for example `++x`.
74+
* A prefix increment expression, for example `++x`.
6775
*/
6876
class PreIncrExpr extends IncrementOperation, @pre_incr_expr {
6977
override string getAPrimaryQlClass() { result = "PreIncrExpr" }
7078
}
7179

7280
/**
73-
* A prefix decrement operation, for example `--x`.
81+
* A prefix decrement expression, for example `--x`.
7482
*/
7583
class PreDecrExpr extends DecrementOperation, @pre_decr_expr {
7684
override string getAPrimaryQlClass() { result = "PreDecrExpr" }
7785
}
7886

7987
/**
80-
* A postfix increment operation, for example `x++`.
88+
* A postfix increment expression, for example `x++`.
8189
*/
8290
class PostIncrExpr extends IncrementOperation, @post_incr_expr {
8391
override string toString() { result = "..." + this.getOperator() }
@@ -86,7 +94,7 @@ class PostIncrExpr extends IncrementOperation, @post_incr_expr {
8694
}
8795

8896
/**
89-
* A postfix decrement operation, for example `x--`.
97+
* A postfix decrement expression, for example `x--`.
9098
*/
9199
class PostDecrExpr extends DecrementOperation, @post_decr_expr {
92100
override string toString() { result = "..." + this.getOperator() }
@@ -95,55 +103,84 @@ class PostDecrExpr extends DecrementOperation, @post_decr_expr {
95103
}
96104

97105
/**
98-
* A binary arithmetic operation. Either an addition operation
99-
* (`AddExpr`), a subtraction operation (`SubExpr`), a multiplication
100-
* operation (`MulExpr`), a division operation (`DivExpr`), or a
101-
* remainder operation (`RemExpr`).
106+
* An addition operation, either `x + y` or `x += y`.
102107
*/
103-
class BinaryArithmeticOperation extends ArithmeticOperation, BinaryOperation, @bin_arith_op_expr {
104-
override string getOperator() { none() }
108+
class AddOperation extends BinaryArithmeticOperation, @add_operation { }
109+
110+
/**
111+
* A subtraction operation, either `x - y` or `x -= y`.
112+
*/
113+
class SubOperation extends BinaryArithmeticOperation, @sub_operation { }
114+
115+
/**
116+
* A multiplication operation, either `x * y` or `x *= y`.
117+
*/
118+
class MulOperation extends BinaryArithmeticOperation, @mul_operation { }
119+
120+
/**
121+
* A division operation, either `x / y` or `x /= y`.
122+
*/
123+
class DivOperation extends BinaryArithmeticOperation, @div_operation {
124+
/** Gets the numerator of this division operation. */
125+
Expr getNumerator() { result = this.getLeftOperand() }
126+
127+
/** Gets the denominator of this division operation. */
128+
Expr getDenominator() { result = this.getRightOperand() }
105129
}
106130

107131
/**
108-
* An addition operation, for example `x + y`.
132+
* A remainder operation, either `x % y` or `x %= y`.
133+
*/
134+
class RemOperation extends BinaryArithmeticOperation, @rem_operation { }
135+
136+
/**
137+
* A binary arithmetic expression. Either an addition expression
138+
* (`AddExpr`), a subtraction expression (`SubExpr`), a multiplication
139+
* expression (`MulExpr`), a division expression (`DivExpr`), or a
140+
* remainder expression (`RemExpr`).
141+
*/
142+
class BinaryArithmeticExpr extends BinaryArithmeticOperation, @bin_arith_expr { }
143+
144+
/**
145+
* An addition expression, for example `x + y`.
109146
*/
110-
class AddExpr extends BinaryArithmeticOperation, AddOperation, @add_expr {
147+
class AddExpr extends BinaryArithmeticExpr, AddOperation, @add_expr {
111148
override string getOperator() { result = "+" }
112149

113150
override string getAPrimaryQlClass() { result = "AddExpr" }
114151
}
115152

116153
/**
117-
* A subtraction operation, for example `x - y`.
154+
* A subtraction expression, for example `x - y`.
118155
*/
119-
class SubExpr extends BinaryArithmeticOperation, SubOperation, @sub_expr {
156+
class SubExpr extends BinaryArithmeticExpr, SubOperation, @sub_expr {
120157
override string getOperator() { result = "-" }
121158

122159
override string getAPrimaryQlClass() { result = "SubExpr" }
123160
}
124161

125162
/**
126-
* A multiplication operation, for example `x * y`.
163+
* A multiplication expression, for example `x * y`.
127164
*/
128-
class MulExpr extends BinaryArithmeticOperation, MulOperation, @mul_expr {
165+
class MulExpr extends BinaryArithmeticExpr, MulOperation, @mul_expr {
129166
override string getOperator() { result = "*" }
130167

131168
override string getAPrimaryQlClass() { result = "MulExpr" }
132169
}
133170

134171
/**
135-
* A division operation, for example `x / y`.
172+
* A division expression, for example `x / y`.
136173
*/
137-
class DivExpr extends BinaryArithmeticOperation, DivOperation, @div_expr {
174+
class DivExpr extends BinaryArithmeticExpr, DivOperation, @div_expr {
138175
override string getOperator() { result = "/" }
139176

140177
override string getAPrimaryQlClass() { result = "DivExpr" }
141178
}
142179

143180
/**
144-
* A remainder operation, for example `x % y`.
181+
* A remainder expression, for example `x % y`.
145182
*/
146-
class RemExpr extends BinaryArithmeticOperation, RemOperation, @rem_expr {
183+
class RemExpr extends BinaryArithmeticExpr, RemOperation, @rem_expr {
147184
override string getOperator() { result = "%" }
148185

149186
override string getAPrimaryQlClass() { result = "RemExpr" }

0 commit comments

Comments
 (0)