mirror of
https://github.com/github/codeql.git
synced 2026-05-02 20:25:13 +02:00
Python: Add test of PoorMansFunctionResolution
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
private import python
|
||||
private import semmle.python.dataflow.new.DataFlow
|
||||
private import semmle.python.frameworks.internal.PoorMansFunctionResolution
|
||||
import TestUtilities.InlineExpectationsTest
|
||||
|
||||
class InlinePoorMansFunctionResolutionTest extends InlineExpectationsTest {
|
||||
InlinePoorMansFunctionResolutionTest() { this = "InlinePoorMansFunctionResolutionTest" }
|
||||
|
||||
override string getARelevantTag() { result = "resolved" }
|
||||
|
||||
override predicate hasActualResult(Location location, string element, string tag, string value) {
|
||||
exists(location.getFile().getRelativePath()) and
|
||||
exists(Function func, DataFlow::Node ref |
|
||||
ref = poorMansFunctionTracker(func) and
|
||||
not ref.asExpr() instanceof FunctionExpr and
|
||||
// exclude things like `GSSA variable func`
|
||||
exists(ref.asExpr()) and
|
||||
// exclude decorator calls (which with our extractor rewrites does reference the
|
||||
// function)
|
||||
not ref.asExpr() = func.getDefinition().(FunctionExpr).getADecoratorCall()
|
||||
|
|
||||
value = func.getName() and
|
||||
tag = "resolved" and
|
||||
element = ref.toString() and
|
||||
location = ref.getLocation()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
def func():
|
||||
print("func")
|
||||
|
||||
func() # $ resolved=func
|
||||
|
||||
|
||||
class MyBase:
|
||||
def base_method(self):
|
||||
print("base_method", self)
|
||||
|
||||
|
||||
class MyClass(MyBase):
|
||||
def method1(self):
|
||||
print("method1", self)
|
||||
|
||||
@classmethod
|
||||
def cls_method(cls):
|
||||
print("cls_method", cls)
|
||||
|
||||
@staticmethod
|
||||
def static():
|
||||
print("static")
|
||||
|
||||
def method2(self):
|
||||
print("method2", self)
|
||||
self.method1()
|
||||
self.base_method()
|
||||
self.cls_method()
|
||||
self.static()
|
||||
|
||||
|
||||
|
||||
|
||||
MyClass.cls_method() # $ resolved=cls_method
|
||||
MyClass.static() # $ resolved=static
|
||||
|
||||
x = MyClass()
|
||||
x.base_method()
|
||||
x.method1()
|
||||
x.cls_method()
|
||||
x.static()
|
||||
x.method2()
|
||||
Reference in New Issue
Block a user