1
1
private import rust
2
2
private import codeql.rust.controlflow.ControlFlowGraph
3
- private import codeql.rust.elements.internal.generated.ParentChild
3
+ private import codeql.rust.elements.internal.generated.ParentChild as ParentChild
4
4
private import codeql.rust.elements.internal.PathImpl:: Impl as PathImpl
5
5
private import codeql.rust.elements.internal.PathExprBaseImpl:: Impl as PathExprBaseImpl
6
6
private import codeql.rust.elements.internal.FormatTemplateVariableAccessImpl:: Impl as FormatTemplateVariableAccessImpl
@@ -36,9 +36,30 @@ module Impl {
36
36
ClosureBodyScope ( ) { this = any ( ClosureExpr ce ) .getBody ( ) }
37
37
}
38
38
39
- class IfExprScope extends VariableScope , IfExpr { }
39
+ // class IfExprScope extends VariableScope, IfExpr { }
40
+ // class WhileExprScope extends VariableScope, WhileExpr { }
41
+ class ConditionScope extends VariableScope {
42
+ private AstNode parent ;
43
+ private AstNode body ;
40
44
41
- class WhileExprScope extends VariableScope , WhileExpr { }
45
+ ConditionScope ( ) {
46
+ parent =
47
+ any ( IfExpr ie |
48
+ this = ie .getCondition ( ) and
49
+ body = ie .getThen ( )
50
+ )
51
+ or
52
+ parent =
53
+ any ( WhileExpr we |
54
+ this = we .getCondition ( ) and
55
+ body = we .getLoopBody ( )
56
+ )
57
+ }
58
+
59
+ AstNode getConditionParent ( ) { result = parent }
60
+
61
+ AstNode getBody ( ) { result = body }
62
+ }
42
63
43
64
private Pat getAPatAncestor ( Pat p ) {
44
65
( p instanceof IdentPat or p instanceof OrPat ) and
@@ -206,39 +227,52 @@ module Impl {
206
227
private AstNode getElseBranch (
207
228
AstNode elseParentParent , int index , AstNode elseParent , int elseIndex
208
229
) {
209
- elseParent = getImmediateChild ( elseParentParent , index ) and
210
- result = getImmediateChild ( elseParent , elseIndex ) and
211
- (
212
- result = elseParent .( LetStmt ) .getLetElse ( )
213
- or
214
- result = elseParent .( IfExpr ) .getElse ( )
215
- )
230
+ elseParent = ParentChild:: getImmediateChild ( elseParentParent , index ) and
231
+ result = ParentChild:: getImmediateChild ( elseParent , elseIndex ) and
232
+ result = elseParent .( LetStmt ) .getLetElse ( )
233
+ // or
234
+ // result = elseParent.(IfExpr).getElse()
216
235
}
217
236
218
- private AstNode getLoopBody ( LoopingExpr loop ) { result = loop .getLoopBody ( ) }
219
-
237
+ // private AstNode getLoopBody(LoopingExpr loop) { result = loop.getLoopBody() }
220
238
pragma [ nomagic]
221
239
private Element getImmediateChildAdj ( Element e , int preOrd , int index , int postOrd ) {
222
- result = getImmediateChild ( e , index ) and
240
+ result = ParentChild :: getImmediateChild ( e , index ) and
223
241
preOrd = 0 and
224
242
postOrd = 0 and
225
- not result = getElseBranch ( _, _, e , index ) and
226
- not result = getLoopBody ( e )
243
+ not exists ( ConditionScope cs |
244
+ e = cs .getParentNode ( ) and
245
+ result = cs .getBody ( )
246
+ ) and
247
+ not result = getElseBranch ( _, _, e , index )
248
+ or
249
+ // not result = getElseBranch(_, _, e, index) and
250
+ // not result = getLoopBody(e)
251
+ // e = getImmediateChild(e.(ConditionScope).getParentNode(), _) and
252
+ result = e .( ConditionScope ) .getBody ( ) and
253
+ preOrd = 1 and
254
+ index = 0 and
255
+ postOrd = 0
227
256
or
228
257
result = getElseBranch ( e , index , _, _) and
229
258
preOrd = 0 and
230
259
postOrd = - 1
231
- or
232
- result = getLoopBody ( e ) and
233
- index = 0 and
234
- preOrd = 1 and
235
- postOrd = 0
236
- }
237
-
260
+ // or
261
+ // result = getLoopBody(e) and
262
+ // index = 0 and
263
+ // preOrd = 1 and
264
+ // postOrd = 0
265
+ }
266
+
267
+ // private AstNode testgetImmediateChildAdj(AstNode e, int index) {
268
+ // result = getImmediateChildAdj(e, index) and
269
+ // e.getLocation().getStartLine() = 91 and
270
+ // e.getLocation().getFile().getBaseName() = "main.rs"
271
+ // }
238
272
pragma [ nomagic]
239
273
private Element getImmediateChildAdj ( Element e , int index ) {
240
274
result =
241
- rank [ index + 1 ] ( Element res , int i , int preOrd , int postOrd |
275
+ rank [ index + 1 ] ( Element res , int preOrd , int i , int postOrd |
242
276
res = getImmediateChildAdj ( e , preOrd , i , postOrd )
243
277
|
244
278
res order by preOrd , i , postOrd
@@ -253,6 +287,7 @@ module Impl {
253
287
n instanceof VariableAccessCand or
254
288
n instanceof LetStmt or
255
289
n instanceof LetExpr or
290
+ n = any ( LetExpr le ) .getScrutinee ( ) or
256
291
n instanceof VariableScope
257
292
) and
258
293
exists ( AstNode n0 |
@@ -269,6 +304,10 @@ module Impl {
269
304
/** Gets the immediately enclosing variable scope of `n`. */
270
305
private VariableScope getEnclosingScope ( AstNode n ) { result = getAnAncestorInVariableScope ( n ) }
271
306
307
+ // private VariableScope testgetEnclosingScope(AstNode n) {
308
+ // result = getAnAncestorInVariableScope(n) and
309
+ // n.getLocation().getStartLine() = 91
310
+ // }
272
311
/**
273
312
* Get all the pattern ancestors of this variable up to an including the
274
313
* root of the pattern.
@@ -299,6 +338,7 @@ module Impl {
299
338
this instanceof VariableAccessCand or
300
339
this instanceof LetStmt or
301
340
this instanceof LetExpr or
341
+ this = any ( LetExpr le ) .getScrutinee ( ) or
302
342
getImmediateChildAdj ( this , _) instanceof RelevantElement
303
343
}
304
344
@@ -384,6 +424,10 @@ module Impl {
384
424
)
385
425
}
386
426
427
+ // private predicate testvariableDeclInScope(Variable v, VariableScope scope, string name, int ord) {
428
+ // variableDeclInScope(v, scope, name, ord) and
429
+ // v.getLocation().getStartLine() = 91
430
+ // }
387
431
/**
388
432
* Holds if `v` is named `name` and is declared inside variable scope
389
433
* `scope`. The pre-order numbering of the binding site of `v`, amongst
@@ -412,10 +456,10 @@ module Impl {
412
456
or
413
457
exists ( LetExpr let |
414
458
let .getPat ( ) = pat and
415
- scope = getEnclosingScope ( let ) and
459
+ scope = getEnclosingScope ( let . getScrutinee ( ) ) and
416
460
// for `let` expressions, variables are bound _after_ the statement, i.e.
417
461
// not in the RHS
418
- ord = getLastPreOrderNumbering ( scope , let ) + 1
462
+ ord = getLastPreOrderNumbering ( scope , let . getScrutinee ( ) ) + 1
419
463
)
420
464
or
421
465
exists ( ForExpr fe |
@@ -427,6 +471,12 @@ module Impl {
427
471
)
428
472
}
429
473
474
+ // private predicate testvariableAccessCandInScope(
475
+ // VariableAccessCand cand, VariableScope scope, string name, int nestLevel, int ord
476
+ // ) {
477
+ // variableAccessCandInScope(cand, scope, name, nestLevel, ord) and
478
+ // cand.getLocation().getStartLine() = 93
479
+ // }
430
480
/**
431
481
* Holds if `cand` may access a variable named `name` at pre-order number `ord`
432
482
* in the variable scope `scope`.
0 commit comments