Skip to content

Commit a3b1fb6

Browse files
committed
C#: Add missing tuple declarations to PatternExpr
`x` and `y` in `pair is var (x, y) ? x : null` are now correctly part of `PatternExpr`.
1 parent 915c0fd commit a3b1fb6

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ private predicate hasChildPattern(ControlFlowElement pm, Expr child) {
316316
child = mid.getChildExpr(0) or
317317
child = mid.getChildExpr(1)
318318
)
319+
or
320+
exists(Expr mid |
321+
hasChildPattern(pm, mid) and
322+
mid instanceof @tuple_expr and
323+
child = mid.getAChildExpr()
324+
)
319325
}
320326

321327
/**
@@ -420,13 +426,10 @@ class TypeAccessPatternExpr extends TypePatternExpr, TypeAccess {
420426
override string getAPrimaryQlClass() { result = "TypeAccessPatternExpr" }
421427
}
422428

423-
/** A pattern that may bind a variable, for example `string s` in `x is string s`. */
424-
class BindingPatternExpr extends PatternExpr {
425-
BindingPatternExpr() {
426-
this instanceof LocalVariableDeclExpr or
427-
this instanceof @recursive_pattern_expr
428-
}
429+
private class TBindingPatternExpr = @local_var_decl_expr or @recursive_pattern_expr;
429430

431+
/** A pattern that may bind a variable, for example `string s` in `x is string s`. */
432+
class BindingPatternExpr extends PatternExpr, TBindingPatternExpr {
430433
/**
431434
* Gets the local variable declaration of this pattern, if any. For example,
432435
* `string s` in `string { Length: 5 } s`.

csharp/ql/test/library-tests/csharp8/PrintAst.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,8 @@ patterns.cs:
932932
# 58| 10: [BreakStmt] break;
933933
# 59| 11: [CaseStmt] case ...:
934934
# 59| 0: [TupleExpr] (..., ...)
935-
# 59| 0: [LocalVariableDeclExpr] Int32 x
936-
# 59| 1: [LocalVariableDeclExpr] Int32 y
935+
# 59| 0: [VariablePatternExpr] Int32 x
936+
# 59| 1: [VariablePatternExpr] Int32 y
937937
# 60| 12: [BreakStmt] break;
938938
# 61| 13: [DefaultCase] default:
939939
# 62| 14: [BreakStmt] break;
@@ -1159,8 +1159,8 @@ patterns.cs:
11591159
# 130| 2: [IntLiteral] 2
11601160
# 131| 3: [SwitchCaseExpr] ... => ...
11611161
# 131| 0: [TupleExpr] (..., ...)
1162-
# 131| 0: [LocalVariableDeclExpr] Int32 x
1163-
# 131| 1: [DiscardExpr] _
1162+
# 131| 0: [VariablePatternExpr] Int32 x
1163+
# 131| 1: [DiscardPatternExpr] _
11641164
# 131| 2: [IntLiteral] 3
11651165
# 134| 2: [TryStmt] try {...} ...
11661166
# 135| 0: [BlockStmt] {...}

csharp/ql/test/library-tests/dataflow/tuples/DataFlowStep.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
| Tuples.cs:50:17:50:56 | (..., ...) | Tuples.cs:50:13:50:56 | SSA def(x) |
5858
| Tuples.cs:51:17:51:17 | access to local variable x | Tuples.cs:53:18:53:57 | SSA def(t) |
5959
| Tuples.cs:51:17:51:17 | access to local variable x | Tuples.cs:58:18:58:35 | (..., ...) |
60+
| Tuples.cs:51:17:51:17 | access to local variable x | Tuples.cs:58:18:58:35 | (..., ...) |
6061
| Tuples.cs:51:17:51:17 | access to local variable x | Tuples.cs:77:13:77:13 | access to local variable x |
6162
| Tuples.cs:53:18:53:57 | SSA def(t) | Tuples.cs:53:64:53:64 | access to local variable t |
6263
| Tuples.cs:53:18:53:57 | SSA qualifier def(t.Item1) | Tuples.cs:54:22:54:28 | access to field Item1 |
@@ -92,6 +93,7 @@
9293
| Tuples.cs:70:22:70:28 | [post] access to field Item2 | Tuples.cs:72:22:72:28 | access to field Item2 |
9394
| Tuples.cs:70:22:70:28 | access to field Item2 | Tuples.cs:72:22:72:28 | access to field Item2 |
9495
| Tuples.cs:77:13:77:13 | access to local variable x | Tuples.cs:77:18:77:35 | (..., ...) |
96+
| Tuples.cs:77:13:77:13 | access to local variable x | Tuples.cs:77:18:77:35 | (..., ...) |
9597
| Tuples.cs:77:23:77:23 | SSA def(p) | Tuples.cs:79:18:79:18 | access to local variable p |
9698
| Tuples.cs:77:27:77:27 | SSA def(q) | Tuples.cs:81:18:81:18 | access to local variable q |
9799
| Tuples.cs:77:30:77:30 | SSA def(r) | Tuples.cs:80:18:80:18 | access to local variable r |

csharp/ql/test/library-tests/dataflow/tuples/PrintAst.expected

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ Tuples.cs:
206206
# 57| 4: [BreakStmt] break;
207207
# 58| 5: [CaseStmt] case ...:
208208
# 58| 0: [TupleExpr] (..., ...)
209-
# 58| 0: [LocalVariableDeclExpr] String a
209+
# 58| 0: [VariablePatternExpr] String a
210210
# 58| 1: [TupleExpr] (..., ...)
211-
# 58| 0: [LocalVariableDeclExpr] Int32 b
212-
# 58| 1: [LocalVariableDeclExpr] String c
213-
# 58| 2: [DiscardExpr] _
211+
# 58| 0: [VariablePatternExpr] Int32 b
212+
# 58| 1: [VariablePatternExpr] String c
213+
# 58| 2: [DiscardPatternExpr] _
214214
# 59| 6: [ExprStmt] ...;
215215
# 59| 0: [MethodCall] call to method Sink<String>
216216
# 59| 0: [LocalVariableAccess] access to local variable a
@@ -238,8 +238,8 @@ Tuples.cs:
238238
# 68| 2: [PositionalPatternExpr] ( ... )
239239
# 68| 0: [ConstantPatternExpr,StringLiteral] "taint source"
240240
# 68| 1: [TupleExpr] (..., ...)
241-
# 68| 0: [LocalVariableDeclExpr] Int32 b
242-
# 68| 1: [LocalVariableDeclExpr] String c
241+
# 68| 0: [VariablePatternExpr] Int32 b
242+
# 68| 1: [VariablePatternExpr] String c
243243
# 68| 2: [DiscardPatternExpr] _
244244
# 69| 1: [ExprStmt] ...;
245245
# 69| 0: [MethodCall] call to method Sink<String>
@@ -266,11 +266,11 @@ Tuples.cs:
266266
# 77| 0: [IsExpr] ... is ...
267267
# 77| 0: [LocalVariableAccess] access to local variable x
268268
# 77| 1: [TupleExpr] (..., ...)
269-
# 77| 0: [LocalVariableDeclExpr] String p
269+
# 77| 0: [VariablePatternExpr] String p
270270
# 77| 1: [TupleExpr] (..., ...)
271-
# 77| 0: [LocalVariableDeclExpr] Int32 q
272-
# 77| 1: [LocalVariableDeclExpr] String r
273-
# 77| 2: [DiscardExpr] _
271+
# 77| 0: [VariablePatternExpr] Int32 q
272+
# 77| 1: [VariablePatternExpr] String r
273+
# 77| 2: [DiscardPatternExpr] _
274274
# 78| 1: [BlockStmt] {...}
275275
# 79| 0: [ExprStmt] ...;
276276
# 79| 0: [MethodCall] call to method Sink<String>
@@ -339,8 +339,8 @@ Tuples.cs:
339339
# 96| 0: [LocalVariableAccess] access to local variable r
340340
# 98| 0: [CaseStmt] case ...:
341341
# 98| 0: [TupleExpr] (..., ...)
342-
# 98| 0: [LocalVariableDeclExpr] String x
343-
# 98| 1: [LocalVariableDeclExpr] Int32 y
342+
# 98| 0: [VariablePatternExpr] String x
343+
# 98| 1: [VariablePatternExpr] Int32 y
344344
# 99| 1: [ExprStmt] ...;
345345
# 99| 0: [MethodCall] call to method Sink<String>
346346
# 99| 0: [LocalVariableAccess] access to local variable x

0 commit comments

Comments
 (0)