Skip to content

Commit d5a92ce

Browse files
hvitvedCopilot
andcommitted
Shared: Model expression patterns in shared CFG
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent bc3b62f commit d5a92ce

17 files changed

Lines changed: 544 additions & 517 deletions

File tree

‎actions/ql/lib/codeql/actions/controlflow/internal/Cfg.qll‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,8 @@ module CfgImpl {
342342
Pattern getPattern(int index) { none() }
343343
}
344344

345-
class LiteralPattern extends Pattern {
346-
LiteralPattern() { none() }
347-
348-
AstNode getLiteral() { none() }
345+
class ExprPattern extends Expr, Pattern {
346+
ExprPattern() { none() }
349347
}
350348

351349
class BindingPattern extends Pattern {

‎csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,8 @@ module Ast implements AstSig<Location> {
297297
Pattern getPattern(int index) { none() }
298298
}
299299

300-
class LiteralPattern extends Pattern {
301-
LiteralPattern() { none() }
302-
303-
AstNode getLiteral() { none() }
300+
class ExprPattern extends Expr, Pattern {
301+
ExprPattern() { none() }
304302
}
305303

306304
class BindingPattern extends Pattern {

‎csharp/ql/test/library-tests/controlflow/graph/BasicBlock.expected‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,11 +1083,10 @@
10831083
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:24:125:29 | Boolean b | 8 |
10841084
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:123:10:123:12 | Exit | 2 |
10851085
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:124:5:127:5 | After {...} | 3 |
1086-
| Switch.cs:125:13:125:48 | After ... switch { ... } [true] | Switch.cs:126:13:126:19 | return ...; | 3 |
10871086
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:125:34:125:34 | access to local variable b | 3 |
10881087
| Switch.cs:125:24:125:29 | After Boolean b [no-match] | Switch.cs:125:42:125:46 | After false [false] | 8 |
10891088
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:34:125:34 | After access to local variable b [false] | 1 |
1090-
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:34:125:34 | After access to local variable b [true] | 1 |
1089+
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:126:13:126:19 | return ...; | 4 |
10911090
| Switch.cs:129:12:129:14 | Entry | Switch.cs:131:28:131:35 | String s | 9 |
10921091
| Switch.cs:131:16:131:66 | After call to method ToString | Switch.cs:129:12:129:14 | Exit | 4 |
10931092
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:16:131:66 | call to method ToString | 2 |

‎csharp/ql/test/library-tests/controlflow/graph/Condition.expected‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,13 +1392,10 @@ conditionFlow
13921392
| Switch.cs:118:25:118:33 | ... == ... | Switch.cs:118:25:118:33 | After ... == ... [false] | false |
13931393
| Switch.cs:118:25:118:33 | ... == ... | Switch.cs:118:25:118:33 | After ... == ... [true] | true |
13941394
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
1395-
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
1396-
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
13971395
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
13981396
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [false] | false |
13991397
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [true] | true |
14001398
| Switch.cs:125:42:125:46 | After false [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
1401-
| Switch.cs:125:42:125:46 | After false [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
14021399
| Switch.cs:125:42:125:46 | false | Switch.cs:125:42:125:46 | After false [false] | false |
14031400
| Switch.cs:157:13:157:13 | access to parameter b | Switch.cs:157:13:157:13 | After access to parameter b [false] | false |
14041401
| Switch.cs:157:13:157:13 | access to parameter b | Switch.cs:157:13:157:13 | After access to parameter b [true] | true |

‎csharp/ql/test/library-tests/controlflow/graph/Dominance.expected‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6112,6 +6112,7 @@ dominance
61126112
| Switch.cs:125:24:125:34 | ... => ... | Switch.cs:125:24:125:29 | Boolean b |
61136113
| Switch.cs:125:24:125:34 | After ... => ... [match] | Switch.cs:125:34:125:34 | access to local variable b |
61146114
| Switch.cs:125:24:125:34 | After ... => ... [no-match] | Switch.cs:125:37:125:46 | ... => ... |
6115+
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] |
61156116
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [false] |
61166117
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [true] |
61176118
| Switch.cs:125:37:125:37 | After _ [match] | Switch.cs:125:37:125:46 | After ... => ... [match] |
@@ -13851,6 +13852,9 @@ postDominance
1385113852
| Switch.cs:125:9:126:19 | if (...) ... | Switch.cs:124:5:127:5 | {...} |
1385213853
| Switch.cs:125:13:125:13 | access to parameter o | Switch.cs:125:13:125:48 | ... switch { ... } |
1385313854
| Switch.cs:125:13:125:48 | ... switch { ... } | Switch.cs:125:9:126:19 | if (...) ... |
13855+
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:34:125:34 | After access to local variable b [false] |
13856+
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:42:125:46 | After false [false] |
13857+
| Switch.cs:125:13:125:48 | After ... switch { ... } [true] | Switch.cs:125:34:125:34 | After access to local variable b [true] |
1385413858
| Switch.cs:125:24:125:29 | Boolean b | Switch.cs:125:24:125:34 | ... => ... |
1385513859
| Switch.cs:125:24:125:34 | ... => ... | Switch.cs:125:13:125:13 | access to parameter o |
1385613860
| Switch.cs:125:24:125:34 | After ... => ... [match] | Switch.cs:125:24:125:29 | After Boolean b [match] |
@@ -13876,6 +13880,10 @@ postDominance
1387613880
| Switch.cs:131:16:131:66 | call to method ToString | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] |
1387713881
| Switch.cs:131:17:131:17 | access to parameter o | Switch.cs:131:17:131:53 | ... switch { ... } |
1387813882
| Switch.cs:131:17:131:53 | ... switch { ... } | Switch.cs:131:16:131:66 | Before call to method ToString |
13883+
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:40:131:40 | After access to local variable s [non-null] |
13884+
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:48:131:51 | After null [non-null] |
13885+
| Switch.cs:131:17:131:53 | After ... switch { ... } [null] | Switch.cs:131:40:131:40 | After access to local variable s [null] |
13886+
| Switch.cs:131:17:131:53 | After ... switch { ... } [null] | Switch.cs:131:48:131:51 | After null [null] |
1387913887
| Switch.cs:131:28:131:35 | String s | Switch.cs:131:28:131:40 | ... => ... |
1388013888
| Switch.cs:131:28:131:40 | ... => ... | Switch.cs:131:17:131:17 | access to parameter o |
1388113889
| Switch.cs:131:28:131:40 | After ... => ... [match] | Switch.cs:131:28:131:35 | After String s [match] |
@@ -19075,14 +19083,12 @@ blockDominance
1907519083
| Switch.cs:123:10:123:12 | Entry | Switch.cs:123:10:123:12 | Entry |
1907619084
| Switch.cs:123:10:123:12 | Entry | Switch.cs:123:10:123:12 | Normal Exit |
1907719085
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:13:125:48 | After ... switch { ... } [false] |
19078-
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:13:125:48 | After ... switch { ... } [true] |
1907919086
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:24:125:29 | After Boolean b [match] |
1908019087
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:24:125:29 | After Boolean b [no-match] |
1908119088
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:34:125:34 | After access to local variable b [false] |
1908219089
| Switch.cs:123:10:123:12 | Entry | Switch.cs:125:34:125:34 | After access to local variable b [true] |
1908319090
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:123:10:123:12 | Normal Exit |
1908419091
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] |
19085-
| Switch.cs:125:13:125:48 | After ... switch { ... } [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] |
1908619092
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:125:24:125:29 | After Boolean b [match] |
1908719093
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:125:34:125:34 | After access to local variable b [false] |
1908819094
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:125:34:125:34 | After access to local variable b [true] |
@@ -23121,13 +23127,13 @@ postBlockDominance
2312123127
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:123:10:123:12 | Entry |
2312223128
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:123:10:123:12 | Normal Exit |
2312323129
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:13:125:48 | After ... switch { ... } [false] |
23124-
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:13:125:48 | After ... switch { ... } [true] |
2312523130
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:24:125:29 | After Boolean b [match] |
2312623131
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:24:125:29 | After Boolean b [no-match] |
2312723132
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:34:125:34 | After access to local variable b [false] |
2312823133
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:125:34:125:34 | After access to local variable b [true] |
2312923134
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] |
23130-
| Switch.cs:125:13:125:48 | After ... switch { ... } [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] |
23135+
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:24:125:29 | After Boolean b [no-match] |
23136+
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:125:34:125:34 | After access to local variable b [false] |
2313123137
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:125:24:125:29 | After Boolean b [match] |
2313223138
| Switch.cs:125:24:125:29 | After Boolean b [no-match] | Switch.cs:125:24:125:29 | After Boolean b [no-match] |
2313323139
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:34:125:34 | After access to local variable b [false] |
@@ -23144,7 +23150,11 @@ postBlockDominance
2314423150
| Switch.cs:131:16:131:66 | After call to method ToString | Switch.cs:131:48:131:51 | After null [non-null] |
2314523151
| Switch.cs:131:16:131:66 | After call to method ToString | Switch.cs:131:48:131:51 | After null [null] |
2314623152
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] |
23153+
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:40:131:40 | After access to local variable s [non-null] |
23154+
| Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | Switch.cs:131:48:131:51 | After null [non-null] |
2314723155
| Switch.cs:131:17:131:53 | After ... switch { ... } [null] | Switch.cs:131:17:131:53 | After ... switch { ... } [null] |
23156+
| Switch.cs:131:17:131:53 | After ... switch { ... } [null] | Switch.cs:131:40:131:40 | After access to local variable s [null] |
23157+
| Switch.cs:131:17:131:53 | After ... switch { ... } [null] | Switch.cs:131:48:131:51 | After null [null] |
2314823158
| Switch.cs:131:28:131:35 | After String s [match] | Switch.cs:131:28:131:35 | After String s [match] |
2314923159
| Switch.cs:131:28:131:35 | After String s [no-match] | Switch.cs:131:28:131:35 | After String s [no-match] |
2315023160
| Switch.cs:131:40:131:40 | After access to local variable s [non-null] | Switch.cs:131:40:131:40 | After access to local variable s [non-null] |

‎csharp/ql/test/library-tests/controlflow/graph/EnclosingCallable.expected‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9480,7 +9480,6 @@ blockEnclosing
94809480
| Switch.cs:123:10:123:12 | Entry | Switch.cs:123:10:123:12 | M11 |
94819481
| Switch.cs:123:10:123:12 | Normal Exit | Switch.cs:123:10:123:12 | M11 |
94829482
| Switch.cs:125:13:125:48 | After ... switch { ... } [false] | Switch.cs:123:10:123:12 | M11 |
9483-
| Switch.cs:125:13:125:48 | After ... switch { ... } [true] | Switch.cs:123:10:123:12 | M11 |
94849483
| Switch.cs:125:24:125:29 | After Boolean b [match] | Switch.cs:123:10:123:12 | M11 |
94859484
| Switch.cs:125:24:125:29 | After Boolean b [no-match] | Switch.cs:123:10:123:12 | M11 |
94869485
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:123:10:123:12 | M11 |

‎csharp/ql/test/library-tests/controlflow/graph/NodeGraph.expected‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6697,8 +6697,6 @@
66976697
| Switch.cs:125:24:125:34 | After ... => ... [match] | Switch.cs:125:34:125:34 | access to local variable b | |
66986698
| Switch.cs:125:24:125:34 | After ... => ... [no-match] | Switch.cs:125:37:125:46 | ... => ... | |
66996699
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
6700-
| Switch.cs:125:34:125:34 | After access to local variable b [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
6701-
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
67026700
| Switch.cs:125:34:125:34 | After access to local variable b [true] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
67036701
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [false] | false |
67046702
| Switch.cs:125:34:125:34 | access to local variable b | Switch.cs:125:34:125:34 | After access to local variable b [true] | true |
@@ -6707,7 +6705,6 @@
67076705
| Switch.cs:125:37:125:46 | ... => ... | Switch.cs:125:37:125:37 | _ | |
67086706
| Switch.cs:125:37:125:46 | After ... => ... [match] | Switch.cs:125:42:125:46 | false | |
67096707
| Switch.cs:125:42:125:46 | After false [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [false] | false |
6710-
| Switch.cs:125:42:125:46 | After false [false] | Switch.cs:125:13:125:48 | After ... switch { ... } [true] | true |
67116708
| Switch.cs:125:42:125:46 | false | Switch.cs:125:42:125:46 | After false [false] | false |
67126709
| Switch.cs:126:13:126:19 | Before return ...; | Switch.cs:126:13:126:19 | return ...; | |
67136710
| Switch.cs:126:13:126:19 | return ...; | Switch.cs:123:10:123:12 | Normal Exit | return |
@@ -6732,8 +6729,6 @@
67326729
| Switch.cs:131:28:131:40 | After ... => ... [match] | Switch.cs:131:40:131:40 | access to local variable s | |
67336730
| Switch.cs:131:28:131:40 | After ... => ... [no-match] | Switch.cs:131:43:131:51 | ... => ... | |
67346731
| Switch.cs:131:40:131:40 | After access to local variable s [non-null] | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | non-null |
6735-
| Switch.cs:131:40:131:40 | After access to local variable s [non-null] | Switch.cs:131:17:131:53 | After ... switch { ... } [null] | null |
6736-
| Switch.cs:131:40:131:40 | After access to local variable s [null] | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | non-null |
67376732
| Switch.cs:131:40:131:40 | After access to local variable s [null] | Switch.cs:131:17:131:53 | After ... switch { ... } [null] | null |
67386733
| Switch.cs:131:40:131:40 | access to local variable s | Switch.cs:131:40:131:40 | After access to local variable s [non-null] | non-null |
67396734
| Switch.cs:131:40:131:40 | access to local variable s | Switch.cs:131:40:131:40 | After access to local variable s [null] | null |
@@ -6742,8 +6737,6 @@
67426737
| Switch.cs:131:43:131:51 | ... => ... | Switch.cs:131:43:131:43 | _ | |
67436738
| Switch.cs:131:43:131:51 | After ... => ... [match] | Switch.cs:131:48:131:51 | null | |
67446739
| Switch.cs:131:48:131:51 | After null [non-null] | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | non-null |
6745-
| Switch.cs:131:48:131:51 | After null [non-null] | Switch.cs:131:17:131:53 | After ... switch { ... } [null] | null |
6746-
| Switch.cs:131:48:131:51 | After null [null] | Switch.cs:131:17:131:53 | After ... switch { ... } [non-null] | non-null |
67476740
| Switch.cs:131:48:131:51 | After null [null] | Switch.cs:131:17:131:53 | After ... switch { ... } [null] | null |
67486741
| Switch.cs:131:48:131:51 | null | Switch.cs:131:48:131:51 | After null [non-null] | non-null |
67496742
| Switch.cs:131:48:131:51 | null | Switch.cs:131:48:131:51 | After null [null] | null |

‎go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,8 @@ module CfgImpl {
420420
Pattern getPattern(int index) { none() }
421421
}
422422

423-
class LiteralPattern extends Pattern {
424-
LiteralPattern() { none() }
425-
426-
AstNode getLiteral() { none() }
423+
class ExprPattern extends Expr, Pattern {
424+
ExprPattern() { none() }
427425
}
428426

429427
class BindingPattern extends Pattern {

‎java/ql/lib/semmle/code/java/ControlFlowGraph.qll‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,8 @@ private module Ast implements AstSig<Location> {
243243
Pattern getPattern(int index) { none() }
244244
}
245245

246-
class LiteralPattern extends Pattern {
247-
LiteralPattern() { none() }
248-
249-
AstNode getLiteral() { none() }
246+
class ExprPattern extends Expr, Pattern {
247+
ExprPattern() { none() }
250248
}
251249

252250
class BindingPattern extends Pattern {

‎python/ql/lib/semmle/python/controlflow/internal/AstNodeImpl.qll‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,29 +1150,41 @@ module Ast implements AstSig<Py::Location> {
11501150
}
11511151
}
11521152

1153+
final class Pattern = PatternImpl;
1154+
11531155
/** A pattern handled by the shared CFG. */
1154-
class Pattern extends AstNode {
1155-
Pattern() { none() }
1156+
abstract private class PatternImpl extends AstNodeImpl {
1157+
abstract Pattern getPattern(int index);
1158+
}
1159+
1160+
private class EmptyPattern extends PatternImpl {
1161+
EmptyPattern() { none() }
1162+
1163+
override Pattern getPattern(int index) { none() }
1164+
1165+
override string toString() { none() }
1166+
1167+
override Py::Location getLocation() { none() }
11561168

1157-
Pattern getPattern(int index) { none() }
1169+
override Callable getEnclosingCallable() { none() }
11581170
}
11591171

1160-
/** A literal pattern handled by the shared CFG. */
1161-
class LiteralPattern extends Pattern {
1162-
LiteralPattern() { none() }
1172+
/** An expression pattern handled by the shared CFG. */
1173+
class ExprPattern extends Expr, PatternImpl {
1174+
ExprPattern() { none() }
11631175

1164-
AstNode getLiteral() { none() }
1176+
override Pattern getPattern(int index) { none() }
11651177
}
11661178

11671179
/** A binding pattern handled by the shared CFG. */
1168-
class BindingPattern extends Pattern {
1180+
class BindingPattern extends EmptyPattern {
11691181
BindingPattern() { none() }
11701182

11711183
AstNode getVariable() { none() }
11721184
}
11731185

11741186
/** An or-pattern handled by the shared CFG. */
1175-
class OrPattern extends Pattern {
1187+
class OrPattern extends EmptyPattern {
11761188
OrPattern() { none() }
11771189
}
11781190

0 commit comments

Comments
 (0)