Skip to content

Commit 3175385

Browse files
committed
C#: Simplify implementation.
1 parent e6b62f1 commit 3175385

File tree

8 files changed

+23
-34
lines changed

8 files changed

+23
-34
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ module AssignableInternal {
292292
newtype TAssignableDefinition =
293293
TAssignmentDefinition(Assignment a) {
294294
not a.getLValue() instanceof TupleExpr and
295-
not a instanceof AssignCompoundOperation and
296-
not a instanceof AssignCoalesceExpr
295+
(not a instanceof AssignOperation or a instanceof AddOrRemoveEventExpr)
297296
} or
298297
TTupleAssignmentDefinition(AssignExpr ae, Expr leaf) { tupleAssignmentDefinition(ae, leaf) } or
299298
TOutRefDefinition(AssignableAccess aa) {
@@ -318,10 +317,7 @@ module AssignableInternal {
318317
} or
319318
TAddressOfDefinition(AddressOfExpr aoe) or
320319
TPatternDefinition(TopLevelPatternDecl tlpd) or
321-
TAssignOperationDefinition(AssignOperation ao) {
322-
ao instanceof AssignCompoundOperation or
323-
ao instanceof AssignCoalesceExpr
324-
}
320+
TAssignOperationDefinition(AssignOperation ao) { not ao instanceof AddOrRemoveEventExpr }
325321

326322
/**
327323
* Gets the source expression assigned in tuple definition `def`, if any.

csharp/ql/lib/semmle/code/csharp/commons/Strings.qll

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,9 @@ class ImplicitToStringExpr extends Expr {
4949
this = add.getOtherOperand(o).stripImplicit()
5050
)
5151
or
52-
// s1 += s2 for where the left hand side is a string, call an operator +(string, object)
53-
exists(AssignAddExpr add, Operator o, Parameter p0, Parameter p1 |
54-
o = add.getTarget() and
55-
o.getName() = "+" and
56-
p0 = o.getParameter(0) and
57-
p1 = o.getParameter(1) and
58-
p0.getType() instanceof StringType and
59-
this = getAnAssignedArgumentOrParam(p1).stripImplicit()
52+
exists(AssignAddExpr add, Expr o | o = add.getLeftOperand() |
53+
o.stripImplicit().getType() instanceof StringType and
54+
this = add.getRightOperand().stripImplicit()
6055
)
6156
or
6257
this = any(InterpolatedStringExpr ise).getAnInsert().stripImplicit()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private module Internal {
202202
private predicate isPotentialEventCall(
203203
AssignArithmeticOperation aao, DynamicMemberAccess dma, string name
204204
) {
205-
aao instanceof DynamicOperatorCall and
205+
aao instanceof DynamicAssignOperation and
206206
dma = aao.getLeftOperand() and
207207
(
208208
aao instanceof AssignAddExpr and

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ class AssignExpr extends Assignment, @simple_assign_expr {
6060

6161
/**
6262
* An assignment operation. Either an arithmetic assignment operation
63-
* (`AssignArithmeticOperation`), a bitwise assignment operation or
64-
* (`AssignBitwiseOperation`).
63+
* (`AssignArithmeticOperation`), a bitwise assignment operation
64+
* (`AssignBitwiseOperation`), an event assignment (`AddOrRemoveEventExpr`)
65+
* or a null-coalescing assignment (`AssignCoalesceExpr`).
6566
*/
66-
class AssignOperation extends Assignment, @assign_op_expr {
67+
class AssignOperation extends Assignment, LateBindableExpr, @assign_op_expr {
6768
override string getOperator() { none() }
6869

6970
/**
@@ -79,11 +80,9 @@ class AssignOperation extends Assignment, @assign_op_expr {
7980
override string toString() { result = "... " + this.getOperator() + " ..." }
8081
}
8182

82-
/**
83-
* An assignment operation that corresponds to an operator call, for example `x += y` corresponds to `x = x + y`.
84-
*/
85-
class AssignCompoundOperation extends AssignOperation, OperatorCall, @assign_op_call_expr {
86-
override string toString() { result = "... " + this.getOperator() + " ..." }
83+
/** An assignment operation involving a dynamic operator call. */
84+
class DynamicAssignOperation extends DynamicExpr, AssignOperation {
85+
override string toString() { result = "... dynamic " + this.getOperator() + " ..." }
8786
}
8887

8988
/**
@@ -93,7 +92,7 @@ class AssignCompoundOperation extends AssignOperation, OperatorCall, @assign_op_
9392
* operation (`AssignDivExpr`), or a remainder assignment operation
9493
* (`AssignRemExpr`).
9594
*/
96-
class AssignArithmeticOperation extends AssignCompoundOperation, @assign_arith_expr { }
95+
class AssignArithmeticOperation extends AssignOperation, @assign_arith_expr { }
9796

9897
/**
9998
* An addition assignment operation, for example `x += y`.
@@ -149,7 +148,7 @@ class AssignRemExpr extends AssignArithmeticOperation, @assign_rem_expr {
149148
* operation (`AssignRightShiftExpr`), or an unsigned right-shift assignment
150149
* operation (`AssignUnsignedRightShiftExpr`).
151150
*/
152-
class AssignBitwiseOperation extends AssignCompoundOperation, @assign_bitwise_expr { }
151+
class AssignBitwiseOperation extends AssignOperation, @assign_bitwise_expr { }
153152

154153
/**
155154
* A bitwise-and assignment operation, for example `x &= y`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class ConstructorInitializer extends Call, @constructor_init_expr {
493493
* }
494494
* ```
495495
*/
496-
class OperatorCall extends Call, LateBindableExpr, @op_invoke_expr {
496+
class OperatorCall extends Call, LateBindableExpr, @operator_invocation_expr {
497497
override Operator getTarget() { expr_call(this, result) }
498498

499499
override Operator getARuntimeTarget() { result = Call.super.getARuntimeTarget() }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ private import semmle.code.csharp.dispatch.Dispatch
1212
* dynamic constructor call (`DynamicObjectCreation`), a dynamic method call
1313
* (`DynamicMethodCall`), a dynamic operator call (`DynamicOperatorCall`), a
1414
* dynamic member access (`DynamicMemberAccess`), a dynamic accessor call
15-
* (`DynamicAccessorCall`), or a dynamic element access (`DynamicElementAccess`).
15+
* (`DynamicAccessorCall`), a dynamic element access (`DynamicElementAccess`),
16+
* or a dynamic assignment operation (`DynamicAssignOperation`).
1617
*/
1718
class DynamicExpr extends LateBindableExpr {
1819
DynamicExpr() { this.isLateBound() }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ class Expr extends ControlFlowElement, @expr {
118118
* An expression whose target may be late bound when using `dynamic`
119119
* subexpressions. Either a method call (`MethodCall`), an operator call
120120
* (`OperatorCall`), a constructor call (`ObjectCreation`), a dynamic member
121-
* access (`DynamicMemberAccess`), or a dynamic element access
122-
* (`DynamicElementAccess`).
121+
* access (`DynamicMemberAccess`), a dynamic element access
122+
* (`DynamicElementAccess`), or an assignment operation (`AssignOperation`).
123123
*/
124124
class LateBindableExpr extends Expr, @late_bindable_expr {
125125
/** Holds if this expression is late bound. */

csharp/ql/lib/semmlecode.csharp.dbscheme

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,7 @@ case @expr.kind of
12161216
| @string_literal_expr | @null_literal_expr;
12171217

12181218
@assign_expr = @simple_assign_expr | @assign_op_expr | @local_var_decl_expr;
1219-
@assign_op_call_expr = @assign_arith_expr | @assign_bitwise_expr
1220-
@assign_op_expr = @assign_op_call_expr | @assign_event_expr | @assign_coalesce_expr;
1219+
@assign_op_expr = @assign_arith_expr | @assign_bitwise_expr | @assign_event_expr | @assign_coalesce_expr;
12211220
@assign_event_expr = @add_event_expr | @remove_event_expr;
12221221

12231222
@assign_arith_expr = @assign_add_expr | @assign_sub_expr | @assign_mul_expr | @assign_div_expr
@@ -1271,15 +1270,14 @@ case @expr.kind of
12711270

12721271
@anonymous_function_expr = @lambda_expr | @anonymous_method_expr;
12731272

1274-
@op_invoke_expr = @operator_invocation_expr | @assign_op_call_expr
1275-
@call = @method_invocation_expr | @constructor_init_expr | @op_invoke_expr
1273+
@call = @method_invocation_expr | @constructor_init_expr | @operator_invocation_expr
12761274
| @delegate_invocation_expr | @object_creation_expr | @call_access_expr
12771275
| @local_function_invocation_expr | @function_pointer_invocation_expr;
12781276

12791277
@call_access_expr = @property_access_expr | @event_access_expr | @indexer_access_expr;
12801278

12811279
@late_bindable_expr = @dynamic_element_access_expr | @dynamic_member_access_expr
1282-
| @object_creation_expr | @method_invocation_expr | @op_invoke_expr;
1280+
| @object_creation_expr | @method_invocation_expr | @operator_invocation_expr | @assign_op_expr;
12831281

12841282
@throw_element = @throw_expr | @throw_stmt;
12851283

0 commit comments

Comments
 (0)