Skip to content

Commit d3b237b

Browse files
committed
Python: rename synthetic lambda nodes
1 parent 5b6ea15 commit d3b237b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,19 +1520,21 @@ abstract class ParameterNodeImpl extends Node {
15201520
}
15211521

15221522
/**
1523-
* The value of a lambda itself at function entry, viewed as a node in a data
1523+
* The value of a closure itself at function entry, viewed as a node in a data
15241524
* flow graph.
15251525
*
15261526
* This is used for tracking flow through captured variables, and we use a
15271527
* separate node and parameter/argument positions in order to distinguish
15281528
* "lambda self" from "normal self", as lambdas may also access outer `self`
15291529
* variables (through variable capture).
15301530
*/
1531-
class LambdaSelfReferenceNode extends ParameterNodeImpl, TLambdaSelfReferenceNode {
1531+
class SynthCapturingClosureParameterNode extends ParameterNodeImpl,
1532+
TSynthCapturingClosureParameterNode
1533+
{
15321534
private Function callable;
15331535

1534-
LambdaSelfReferenceNode() {
1535-
this = TLambdaSelfReferenceNode(callable) and
1536+
SynthCapturingClosureParameterNode() {
1537+
this = TSynthCapturingClosureParameterNode(callable) and
15361538
// TODO: Remove this restriction when adding proper support for captured variables in the body of the function we generate for comprehensions
15371539
exists(TFunction(callable))
15381540
}
@@ -1635,11 +1637,22 @@ private class SynthCapturePostUpdateNode extends PostUpdateNodeImpl, SynthCaptur
16351637
override Node getPreUpdateNode() { result = pre }
16361638
}
16371639

1638-
class CaptureArgumentNode extends CfgNode, ArgumentNode {
1640+
/**
1641+
* The value of a closure itself being passed to the funciton, viewed as a node in a data
1642+
* flow graph.
1643+
*
1644+
* This is used for tracking flow through captured variables, and we use a
1645+
* separate node and parameter/argument positions in order to distinguish
1646+
* "lambda self" from "normal self", as lambdas may also access outer `self`
1647+
* variables (through variable capture).
1648+
*/
1649+
class SynthCaptureArgumentNode extends TSynthCapturingClosureArgumentNode, ArgumentNode {
16391650
CallNode callNode;
16401651

1641-
CaptureArgumentNode() {
1642-
this.getNode() = callNode.getFunction() and
1652+
SynthCaptureArgumentNode() {
1653+
this = TSynthCapturingClosureArgumentNode(callNode) and
1654+
// We would prefer to put this restriction in the charpred for the branch,
1655+
// but that incurs non-monotonic recursion.
16431656
exists(Function target | resolveCall(callNode, target, _) |
16441657
target = any(VariableCapture::CapturedVariable v).getACapturingScope()
16451658
)

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPrivate.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ module VariableCapture {
504504
or
505505
result.(Flow::ParameterNode).getParameter().getCfgNode() = n.(CfgNode).getNode()
506506
or
507-
result.(Flow::ThisParameterNode).getCallable() = n.(LambdaSelfReferenceNode).getCallable()
507+
result.(Flow::ThisParameterNode).getCallable() =
508+
n.(SynthCapturingClosureParameterNode).getCallable()
508509
}
509510

510511
predicate storeStep(Node nodeFrom, CapturedVariableContent c, Node nodeTo) {
@@ -1244,7 +1245,7 @@ predicate allowParameterReturnInSelf(ParameterNode p) {
12441245
or
12451246
exists(Function f |
12461247
VariableCapture::Flow::heuristicAllowInstanceParameterReturnInSelf(f) and
1247-
p = TLambdaSelfReferenceNode(f)
1248+
p = TSynthCapturingClosureParameterNode(f)
12481249
)
12491250
}
12501251

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ newtype TNode =
118118
/** A synthetic node representing a captured variable. */
119119
TSynthCaptureNode(VariableCapture::Flow::SynthesizedCaptureNode cn) or
120120
/** A synthetic node representing the heap of a function. Used for variable capture. */
121-
TLambdaSelfReferenceNode(Function f) {
121+
TSynthCapturingClosureParameterNode(Function f) {
122122
f = any(VariableCapture::CapturedVariable v).getACapturingScope()
123-
}
123+
} or
124+
/** A synthetic node representing the heap of a function. Used for variable capture. */
125+
TSynthCapturingClosureArgumentNode(CallNode callNode)
124126

125127
private import semmle.python.internal.CachedStages
126128

0 commit comments

Comments
 (0)