Skip to content

Commit b3f29b0

Browse files
committed
Python: Add failing ESSA use-use test
I initially created this as a dataflow test, but then realized it could just be an ESSA test. I cound't find any existing ESSA tests though :| so created a new dir for it.
1 parent 3d025ea commit b3f29b0

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

python/ql/test/library-tests/essa/ssa-compute/UseUse.expected

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import python
2+
import semmle.python.essa.SsaCompute
3+
import TestUtilities.InlineExpectationsTest
4+
5+
class UseTest extends InlineExpectationsTest {
6+
UseTest() { this = "UseTest" }
7+
8+
override string getARelevantTag() { result in ["use-use", "def-use", "def"] }
9+
10+
override predicate hasActualResult(Location location, string element, string tag, string value) {
11+
exists(location.getFile().getRelativePath()) and
12+
exists(string name | name in ["x", "y"] |
13+
exists(NameNode nodeTo, Location prevLoc |
14+
(
15+
exists(NameNode nodeFrom | AdjacentUses::adjacentUseUse(nodeFrom, nodeTo) |
16+
prevLoc = nodeFrom.getLocation() and
17+
name = nodeFrom.getId() and
18+
tag = "use-use"
19+
)
20+
or
21+
exists(EssaVariable var | AdjacentUses::firstUse(var, nodeTo) |
22+
prevLoc = var.getDefinition().getLocation() and
23+
name = var.getName() and
24+
tag = "def-use"
25+
)
26+
) and
27+
value = name + ":" + prevLoc.getStartLine() and
28+
location = nodeTo.getLocation() and
29+
element = nodeTo.toString()
30+
)
31+
or
32+
exists(EssaVariable var | AdjacentUses::firstUse(var, _) |
33+
value = var.getName() and
34+
location = var.getDefinition().getLocation() and
35+
element = var.getName() and
36+
name = var.getName() and
37+
tag = "def"
38+
)
39+
)
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class X(object):
2+
def foo(self, *args):
3+
print("X.foo", args)
4+
5+
6+
def func(cond=True):
7+
x = X() # $ def=x
8+
9+
print(x) # $ def-use=x:7
10+
11+
y = x # $ def=y use-use=x:9
12+
print(y) # $ def-use=y:11
13+
14+
x.foo() # $ use-use=x:11
15+
16+
x.foo(1 if cond else 0) # $ use-use=x:14
17+
x.foo() # $ MISSING: use-use=x:16
18+
print(x) # $ use-use=x:17
19+
20+
func()

0 commit comments

Comments
 (0)