-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: detect captured variables #11902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yoff
wants to merge
3
commits into
github:main
Choose a base branch
from
yoff:python/detect-captured-variables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- we added parameters to capturing function definitions. Capturing is detected via `ScopeEntryDefinitions`. These only exist for the inner-most nested function (see tests) We should make these exist at all levels to thread the values through. Right now, we cannot reach further nested functions (see `m` in tests). - parameters currently need to be `NameNode`s, so we select the first mention inside the function body. That also only exists in the inner-most nested function (if that is the only one to mention hte captured variable) so this should be replaced with a synthetic node. - we needed to extend `ParameterNodeImpl` and implement `isParameterOf` to make the data flow library connect them. - we added arguments to calls to capturing functions (referencing the call graph) - These should be current value of the variable at the call site This is currently approximated by the latest use before the call site which is further approximated by any variable of the same name in the same basic block as the call The right thing to do is to make the call a useand let SSA handle it. - every captured variable has been given position -3 this should be based on the variable name instead. That will be easy once we rebase on the new call graph PR, as it has genralised positions.
) | ||
or | ||
// captured variable | ||
exists(string name, ScopeEntryDefinition def, NameNode var | |
Check warning
Code scanning / CodeQL
Omittable 'exists' variable
This exists variable can be omitted by using a don't-care expression [in this argument](1).
) | ||
or | ||
// captured variable | ||
exists(CallableValue callable, string name, ScopeEntryDefinition def | |
Check warning
Code scanning / CodeQL
Omittable 'exists' variable
This exists variable can be omitted by using a don't-care expression [in this argument](1).
) | ||
or | ||
// captured variable | ||
exists(CallableValue callable, string name, ScopeEntryDefinition def | |
Check warning
Code scanning / CodeQL
Omittable 'exists' variable
This exists variable can be omitted by using a don't-care expression [in this argument](1).
@@ -0,0 +1,27 @@ | |||
import python | |||
import semmle.python.essa.Essa |
Check warning
Code scanning / CodeQL
Redundant import
Redundant import, the module is already imported inside [python](1).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hacky PoC for captured parameters
Capturing is detected via
ScopeEntryDefinitions
.These only exist for the inner-most nested function (see tests)
We should make these exist at all levels to thread the values through.
Right now, we cannot reach further nested functions (see
m
in tests).NameNode
s,so we select the first mention inside the function body.
That also only exists in the inner-most nested function
(if that is the only one to mention hte captured variable)
so this should be replaced with a synthetic node.
ParameterNodeImpl
and implement
isParameterOf
to make the data flow library connect them.(referencing the call graph)
This is currently approximated by the latest use before the call site
which is further approximated by any variable of the same name in the same basic block as the call
The right thing to do is to make the call a useand let SSA handle it.
this should be based on the variable name instead.
That will be easy once we rebase on the new call graph PR,
as it has genralised positions.