Skip to content

Commit 423d2b4

Browse files
committed
debug
1 parent 094a86e commit 423d2b4

File tree

3 files changed

+79
-29
lines changed

3 files changed

+79
-29
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import rust
22
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
44
private import codeql.rust.elements.internal.PathImpl::Impl as PathImpl
55
private import codeql.rust.elements.internal.PathExprBaseImpl::Impl as PathExprBaseImpl
66
private import codeql.rust.elements.internal.FormatTemplateVariableAccessImpl::Impl as FormatTemplateVariableAccessImpl
@@ -36,9 +36,30 @@ module Impl {
3636
ClosureBodyScope() { this = any(ClosureExpr ce).getBody() }
3737
}
3838

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;
4044

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+
}
4263

4364
private Pat getAPatAncestor(Pat p) {
4465
(p instanceof IdentPat or p instanceof OrPat) and
@@ -206,39 +227,52 @@ module Impl {
206227
private AstNode getElseBranch(
207228
AstNode elseParentParent, int index, AstNode elseParent, int elseIndex
208229
) {
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()
216235
}
217236

218-
private AstNode getLoopBody(LoopingExpr loop) { result = loop.getLoopBody() }
219-
237+
// private AstNode getLoopBody(LoopingExpr loop) { result = loop.getLoopBody() }
220238
pragma[nomagic]
221239
private Element getImmediateChildAdj(Element e, int preOrd, int index, int postOrd) {
222-
result = getImmediateChild(e, index) and
240+
result = ParentChild::getImmediateChild(e, index) and
223241
preOrd = 0 and
224242
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
227256
or
228257
result = getElseBranch(e, index, _, _) and
229258
preOrd = 0 and
230259
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+
// }
238272
pragma[nomagic]
239273
private Element getImmediateChildAdj(Element e, int index) {
240274
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 |
242276
res = getImmediateChildAdj(e, preOrd, i, postOrd)
243277
|
244278
res order by preOrd, i, postOrd
@@ -253,6 +287,7 @@ module Impl {
253287
n instanceof VariableAccessCand or
254288
n instanceof LetStmt or
255289
n instanceof LetExpr or
290+
n = any(LetExpr le).getScrutinee() or
256291
n instanceof VariableScope
257292
) and
258293
exists(AstNode n0 |
@@ -269,6 +304,10 @@ module Impl {
269304
/** Gets the immediately enclosing variable scope of `n`. */
270305
private VariableScope getEnclosingScope(AstNode n) { result = getAnAncestorInVariableScope(n) }
271306

307+
// private VariableScope testgetEnclosingScope(AstNode n) {
308+
// result = getAnAncestorInVariableScope(n) and
309+
// n.getLocation().getStartLine() = 91
310+
// }
272311
/**
273312
* Get all the pattern ancestors of this variable up to an including the
274313
* root of the pattern.
@@ -299,6 +338,7 @@ module Impl {
299338
this instanceof VariableAccessCand or
300339
this instanceof LetStmt or
301340
this instanceof LetExpr or
341+
this = any(LetExpr le).getScrutinee() or
302342
getImmediateChildAdj(this, _) instanceof RelevantElement
303343
}
304344

@@ -384,6 +424,10 @@ module Impl {
384424
)
385425
}
386426

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+
// }
387431
/**
388432
* Holds if `v` is named `name` and is declared inside variable scope
389433
* `scope`. The pre-order numbering of the binding site of `v`, amongst
@@ -412,10 +456,10 @@ module Impl {
412456
or
413457
exists(LetExpr let |
414458
let.getPat() = pat and
415-
scope = getEnclosingScope(let) and
459+
scope = getEnclosingScope(let.getScrutinee()) and
416460
// for `let` expressions, variables are bound _after_ the statement, i.e.
417461
// not in the RHS
418-
ord = getLastPreOrderNumbering(scope, let) + 1
462+
ord = getLastPreOrderNumbering(scope, let.getScrutinee()) + 1
419463
)
420464
or
421465
exists(ForExpr fe |
@@ -427,6 +471,12 @@ module Impl {
427471
)
428472
}
429473

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+
// }
430480
/**
431481
* Holds if `cand` may access a variable named `name` at pre-order number `ord`
432482
* in the variable scope `scope`.

rust/ql/test/library-tests/variables/Ssa.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ read
274274
| main.rs:335:20:335:20 | x | main.rs:335:20:335:20 | x | main.rs:337:19:337:19 | x |
275275
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:348:7:348:7 | x |
276276
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:353:7:353:7 | x |
277+
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:357:19:357:19 | x |
277278
| main.rs:347:16:347:16 | x | main.rs:347:16:347:16 | x | main.rs:350:19:350:19 | x |
278279
| main.rs:352:20:352:20 | x | main.rs:352:20:352:20 | x | main.rs:355:19:355:19 | x |
279280
| main.rs:362:5:362:6 | a8 | main.rs:362:5:362:6 | a8 | main.rs:368:15:368:16 | a8 |
@@ -567,6 +568,7 @@ adjacentReads
567568
| main.rs:318:14:318:14 | x | main.rs:318:14:318:14 | x | main.rs:321:5:321:5 | x | main.rs:323:19:323:19 | x |
568569
| main.rs:332:9:332:9 | x | main.rs:332:9:332:9 | x | main.rs:333:11:333:11 | x | main.rs:341:15:341:15 | x |
569570
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:348:7:348:7 | x | main.rs:353:7:353:7 | x |
571+
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:353:7:353:7 | x | main.rs:357:19:357:19 | x |
570572
| main.rs:389:9:389:10 | c2 | main.rs:382:13:382:14 | c2 | main.rs:395:9:395:10 | c2 | main.rs:399:15:399:16 | c2 |
571573
| main.rs:390:9:390:10 | b4 | main.rs:381:13:381:14 | b4 | main.rs:394:9:394:10 | b4 | main.rs:398:15:398:16 | b4 |
572574
| main.rs:390:9:390:10 | b4 | main.rs:381:13:381:14 | b4 | main.rs:398:15:398:16 | b4 | main.rs:412:15:412:16 | b4 |

rust/ql/test/library-tests/variables/variables.expected

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
testFailures
2-
| main.rs:357:19:357:19 | x | Unexpected result: read_access=x2 |
3-
| main.rs:357:32:357:50 | //... | Missing result: read_access=x1 |
42
variable
53
| main.rs:5:14:5:14 | s |
64
| main.rs:10:14:10:14 | i |
@@ -227,7 +225,7 @@ variableAccess
227225
| main.rs:350:19:350:19 | x | main.rs:347:16:347:16 | x |
228226
| main.rs:353:7:353:7 | x | main.rs:346:9:346:9 | x |
229227
| main.rs:355:19:355:19 | x | main.rs:352:20:352:20 | x |
230-
| main.rs:357:19:357:19 | x | main.rs:347:16:347:16 | x |
228+
| main.rs:357:19:357:19 | x | main.rs:346:9:346:9 | x |
231229
| main.rs:368:15:368:16 | a8 | main.rs:362:5:362:6 | a8 |
232230
| main.rs:369:15:369:16 | b3 | main.rs:364:9:364:10 | b3 |
233231
| main.rs:370:15:370:16 | c1 | main.rs:365:9:365:10 | c1 |
@@ -451,7 +449,7 @@ variableReadAccess
451449
| main.rs:350:19:350:19 | x | main.rs:347:16:347:16 | x |
452450
| main.rs:353:7:353:7 | x | main.rs:346:9:346:9 | x |
453451
| main.rs:355:19:355:19 | x | main.rs:352:20:352:20 | x |
454-
| main.rs:357:19:357:19 | x | main.rs:347:16:347:16 | x |
452+
| main.rs:357:19:357:19 | x | main.rs:346:9:346:9 | x |
455453
| main.rs:368:15:368:16 | a8 | main.rs:362:5:362:6 | a8 |
456454
| main.rs:369:15:369:16 | b3 | main.rs:364:9:364:10 | b3 |
457455
| main.rs:370:15:370:16 | c1 | main.rs:365:9:365:10 | c1 |

0 commit comments

Comments
 (0)