get mimimal def nodes to work in python

This commit is contained in:
Erik Krogh Kristensen
2022-01-31 23:11:18 +01:00
parent 52ca0d168b
commit df9efbe778
4 changed files with 259 additions and 4 deletions

View File

@@ -0,0 +1,36 @@
import python
import semmle.python.dataflow.new.DataFlow
import TestUtilities.InlineExpectationsTest
import semmle.python.ApiGraphs
class ApiDefTest extends InlineExpectationsTest {
ApiDefTest() { this = "ApiDefTest" }
override string getARelevantTag() { result = "def" }
private predicate relevant_node(API::Node a, DataFlow::Node n, Location l) {
n = a.getARhs() and
l = n.getLocation() and
// Module variable nodes have no suitable location, so it's best to simply exclude them entirely
// from the inline tests.
not n instanceof DataFlow::ModuleVariableNode and
exists(l.getFile().getRelativePath()) and
n.getLocation().getFile().getBaseName().matches("def%.py")
}
override predicate hasActualResult(Location location, string element, string tag, string value) {
exists(API::Node a, DataFlow::Node n | relevant_node(a, n, location) |
tag = "def" and
// Only report the longest path on this line:
value =
max(API::Node a2, Location l2 |
relevant_node(a2, _, l2) and
l2.getFile() = location.getFile() and
l2.getStartLine() = location.getStartLine()
|
a2.getPath()
) and
element = n.toString()
)
}
}

View File

@@ -0,0 +1,6 @@
from mypkg import foo #$ use=moduleImport("mypkg").getMember("foo")
def callback(x): #$ use=moduleImport("mypkg").getMember("foo").getMember("bar").getParameter(0).getParameter(0)
x.baz() #$ use=moduleImport("mypkg").getMember("foo").getMember("bar").getParameter(0).getParameter(0).getMember("baz").getReturn()
foo.bar(callback) #$ def=moduleImport("mypkg").getMember("foo").getMember("bar").getParameter(0) use=moduleImport("mypkg").getMember("foo").getMember("bar").getReturn()