Skip to content

Commit eee49cd

Browse files
authored
Merge pull request github#5184 from tausbn/python-move-type-tracker-tests-to-source-nodes
Python: Use `LocalSourceNode` in type tracker tests
2 parents 8caafb3 + 04eb0c7 commit eee49cd

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

python/ql/test/experimental/dataflow/typetracking/moduleattr.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module_tracker
22
| import_as_attr.py:1:6:1:11 | ControlFlowNode for ImportExpr |
33
module_attr_tracker
4-
| import_as_attr.py:0:0:0:0 | ModuleVariableNode for Global Variable attr_ref in Module import_as_attr |
54
| import_as_attr.py:1:20:1:35 | ControlFlowNode for ImportMember |
65
| import_as_attr.py:1:28:1:35 | GSSA Variable attr_ref |
76
| import_as_attr.py:3:1:3:1 | GSSA Variable x |

python/ql/test/experimental/dataflow/typetracking/moduleattr.ql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ import python
22
import semmle.python.dataflow.new.DataFlow
33
import semmle.python.dataflow.new.TypeTracker
44

5-
DataFlow::Node module_tracker(TypeTracker t) {
5+
DataFlow::LocalSourceNode module_tracker(TypeTracker t) {
66
t.start() and
77
result = DataFlow::importNode("module")
88
or
99
exists(TypeTracker t2 | result = module_tracker(t2).track(t2, t))
1010
}
1111

12-
query DataFlow::Node module_tracker() { result = module_tracker(DataFlow::TypeTracker::end()) }
12+
query DataFlow::Node module_tracker() {
13+
module_tracker(DataFlow::TypeTracker::end()).flowsTo(result)
14+
}
1315

14-
DataFlow::Node module_attr_tracker(TypeTracker t) {
16+
DataFlow::LocalSourceNode module_attr_tracker(TypeTracker t) {
1517
t.startInAttr("attr") and
1618
result = module_tracker()
1719
or
1820
exists(TypeTracker t2 | result = module_attr_tracker(t2).track(t2, t))
1921
}
2022

2123
query DataFlow::Node module_attr_tracker() {
22-
result = module_attr_tracker(DataFlow::TypeTracker::end())
24+
module_attr_tracker(DataFlow::TypeTracker::end()).flowsTo(result)
2325
}

python/ql/test/experimental/dataflow/typetracking/tracked.ql

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TestUtilities.InlineExpectationsTest
66
// -----------------------------------------------------------------------------
77
// tracked
88
// -----------------------------------------------------------------------------
9-
DataFlow::Node tracked(TypeTracker t) {
9+
DataFlow::LocalSourceNode tracked(TypeTracker t) {
1010
t.start() and
1111
result.asCfgNode() = any(NameNode n | n.getId() = "tracked")
1212
or
@@ -20,7 +20,7 @@ class TrackedTest extends InlineExpectationsTest {
2020

2121
override predicate hasActualResult(Location location, string element, string tag, string value) {
2222
exists(DataFlow::Node e, TypeTracker t |
23-
e = tracked(t) and
23+
tracked(t).flowsTo(e) and
2424
// Module variables have no sensible location, and hence can't be annotated.
2525
not e instanceof DataFlow::ModuleVariableNode and
2626
tag = "tracked" and
@@ -34,14 +34,14 @@ class TrackedTest extends InlineExpectationsTest {
3434
// -----------------------------------------------------------------------------
3535
// int + str
3636
// -----------------------------------------------------------------------------
37-
DataFlow::Node int_type(TypeTracker t) {
37+
DataFlow::LocalSourceNode int_type(TypeTracker t) {
3838
t.start() and
3939
result.asCfgNode() = any(CallNode c | c.getFunction().(NameNode).getId() = "int")
4040
or
4141
exists(TypeTracker t2 | result = int_type(t2).track(t2, t))
4242
}
4343

44-
DataFlow::Node string_type(TypeTracker t) {
44+
DataFlow::LocalSourceNode string_type(TypeTracker t) {
4545
t.start() and
4646
result.asCfgNode() = any(CallNode c | c.getFunction().(NameNode).getId() = "str")
4747
or
@@ -55,7 +55,7 @@ class TrackedIntTest extends InlineExpectationsTest {
5555

5656
override predicate hasActualResult(Location location, string element, string tag, string value) {
5757
exists(DataFlow::Node e, TypeTracker t |
58-
e = int_type(t) and
58+
int_type(t).flowsTo(e) and
5959
tag = "int" and
6060
location = e.getLocation() and
6161
value = t.getAttr() and
@@ -71,7 +71,7 @@ class TrackedStringTest extends InlineExpectationsTest {
7171

7272
override predicate hasActualResult(Location location, string element, string tag, string value) {
7373
exists(DataFlow::Node e, TypeTracker t |
74-
e = string_type(t) and
74+
string_type(t).flowsTo(e) and
7575
tag = "str" and
7676
location = e.getLocation() and
7777
value = t.getAttr() and
@@ -83,7 +83,7 @@ class TrackedStringTest extends InlineExpectationsTest {
8383
// -----------------------------------------------------------------------------
8484
// tracked_self
8585
// -----------------------------------------------------------------------------
86-
DataFlow::Node tracked_self(TypeTracker t) {
86+
DataFlow::LocalSourceNode tracked_self(TypeTracker t) {
8787
t.start() and
8888
exists(Function f |
8989
f.isMethod() and
@@ -101,7 +101,7 @@ class TrackedSelfTest extends InlineExpectationsTest {
101101

102102
override predicate hasActualResult(Location location, string element, string tag, string value) {
103103
exists(DataFlow::Node e, TypeTracker t |
104-
e = tracked_self(t) and
104+
tracked_self(t).flowsTo(e) and
105105
// Module variables have no sensible location, and hence can't be annotated.
106106
not e instanceof DataFlow::ModuleVariableNode and
107107
tag = "tracked_self" and
@@ -117,18 +117,18 @@ class TrackedSelfTest extends InlineExpectationsTest {
117117
// -----------------------------------------------------------------------------
118118
// This modeling follows the same pattern that we currently use in our real library modeling.
119119
/** Gets a reference to `foo` (fictive module). */
120-
private DataFlow::Node foo(DataFlow::TypeTracker t) {
120+
private DataFlow::LocalSourceNode foo(DataFlow::TypeTracker t) {
121121
t.start() and
122122
result = DataFlow::importNode("foo")
123123
or
124124
exists(DataFlow::TypeTracker t2 | result = foo(t2).track(t2, t))
125125
}
126126

127127
/** Gets a reference to `foo` (fictive module). */
128-
DataFlow::Node foo() { result = foo(DataFlow::TypeTracker::end()) }
128+
DataFlow::Node foo() { foo(DataFlow::TypeTracker::end()).flowsTo(result) }
129129

130130
/** Gets a reference to `foo.bar` (fictive module). */
131-
private DataFlow::Node foo_bar(DataFlow::TypeTracker t) {
131+
private DataFlow::LocalSourceNode foo_bar(DataFlow::TypeTracker t) {
132132
t.start() and
133133
result = DataFlow::importNode("foo.bar")
134134
or
@@ -139,10 +139,10 @@ private DataFlow::Node foo_bar(DataFlow::TypeTracker t) {
139139
}
140140

141141
/** Gets a reference to `foo.bar` (fictive module). */
142-
DataFlow::Node foo_bar() { result = foo_bar(DataFlow::TypeTracker::end()) }
142+
DataFlow::Node foo_bar() { foo_bar(DataFlow::TypeTracker::end()).flowsTo(result) }
143143

144144
/** Gets a reference to `foo.bar.baz` (fictive attribute on `foo.bar` module). */
145-
private DataFlow::Node foo_bar_baz(DataFlow::TypeTracker t) {
145+
private DataFlow::LocalSourceNode foo_bar_baz(DataFlow::TypeTracker t) {
146146
t.start() and
147147
result = DataFlow::importNode("foo.bar.baz")
148148
or
@@ -153,7 +153,7 @@ private DataFlow::Node foo_bar_baz(DataFlow::TypeTracker t) {
153153
}
154154

155155
/** Gets a reference to `foo.bar.baz` (fictive attribute on `foo.bar` module). */
156-
DataFlow::Node foo_bar_baz() { result = foo_bar_baz(DataFlow::TypeTracker::end()) }
156+
DataFlow::Node foo_bar_baz() { foo_bar_baz(DataFlow::TypeTracker::end()).flowsTo(result) }
157157

158158
class TrackedFooBarBaz extends InlineExpectationsTest {
159159
TrackedFooBarBaz() { this = "TrackedFooBarBaz" }

0 commit comments

Comments
 (0)