Skip to content

Commit bb210f4

Browse files
committed
pythos: SSA for match
- new SSA definition `PatternCaptureDefinition` - new SSA definition `PatternAliasDefinition` - implement `hasDefiningNode`
1 parent de8ecb2 commit bb210f4

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

python/ql/lib/semmle/python/essa/Definitions.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ abstract class SsaSourceVariable extends @py_variable {
7070
SsaSource::exception_capture(this, def)
7171
or
7272
SsaSource::with_definition(this, def)
73+
or
74+
SsaSource::pattern_capture_definition(this, def)
75+
or
76+
SsaSource::pattern_alias_definition(this, def)
7377
}
7478

7579
/**

python/ql/lib/semmle/python/essa/Essa.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,24 @@ class WithDefinition extends EssaNodeDefinition {
545545
override string getRepresentation() { result = "with" }
546546
}
547547

548+
/** A definition of a variable via a capture pattern */
549+
class PatternCaptureDefinition extends EssaNodeDefinition {
550+
PatternCaptureDefinition() {
551+
SsaSource::pattern_capture_definition(this.getSourceVariable(), this.getDefiningNode())
552+
}
553+
554+
override string getRepresentation() { result = "pattern capture" }
555+
}
556+
557+
/** A definition of a variable via a pattern alias */
558+
class PatternAliasDefinition extends EssaNodeDefinition {
559+
PatternAliasDefinition() {
560+
SsaSource::pattern_alias_definition(this.getSourceVariable(), this.getDefiningNode())
561+
}
562+
563+
override string getRepresentation() { result = "pattern alias" }
564+
}
565+
548566
/** A definition of a variable by declaring it as a parameter */
549567
class ParameterDefinition extends EssaNodeDefinition {
550568
ParameterDefinition() {

python/ql/lib/semmle/python/essa/SsaDefinitions.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ module SsaSource {
4040
)
4141
}
4242

43+
/** Holds if `v` is defined by a capture pattern. */
44+
cached
45+
predicate pattern_capture_definition(Variable v, ControlFlowNode defn) {
46+
exists(MatchCapturePattern capture, Name var |
47+
capture.getVariable() = var and
48+
var.getAFlowNode() = defn
49+
|
50+
var = v.getAStore()
51+
)
52+
}
53+
54+
/** Holds if `v` is defined by as the alias of an as-pattern. */
55+
cached
56+
predicate pattern_alias_definition(Variable v, ControlFlowNode defn) {
57+
exists(MatchAsPattern pattern, Name var |
58+
pattern.getAlias() = var and
59+
var.getAFlowNode() = defn
60+
|
61+
var = v.getAStore()
62+
)
63+
}
64+
4365
/** Holds if `v` is defined by multiple assignment at `defn`. */
4466
cached
4567
predicate multi_assignment_definition(Variable v, ControlFlowNode defn, int n, SequenceNode lhs) {

0 commit comments

Comments
 (0)