Skip to content

Commit 9696ee9

Browse files
yoffCopilot
andcommitted
Python: migrate two more test queries off legacy CFG types
After the shared-CFG migration, DataFlow::Node.asCfgNode() returns Cfg::ControlFlowNode rather than the legacy Flow::ControlFlowNode, and funcValue.getACall() / dfCall.getNode() now return different CFG types (legacy vs new). Update the two remaining test queries that still cast to legacy NameNode/CallNode types to bridge through Cfg:: types or AST. * experimental/import-resolution-namespace-relative/test.ql: cast to Cfg::NameNode instead of legacy NameNode. * experimental/library-tests/CallGraph/InlineCallGraphTest.ql: change predicate signatures from CallNode to AST Call, and bridge to legacy CallNode (points-to) and Cfg::CallNode (type-tracking) via getNode() on each side. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent de77449 commit 9696ee9

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

python/ql/test/experimental/import-resolution-namespace-relative/test.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import python
22
import semmle.python.dataflow.new.DataFlow
33
import semmle.python.dataflow.new.TaintTracking
44
import utils.test.InlineExpectationsTest
5+
private import semmle.python.controlflow.internal.Cfg as Cfg
56

67
private module TestConfig implements DataFlow::ConfigSig {
78
predicate isSource(DataFlow::Node node) {
8-
node.(DataFlow::CallCfgNode).getFunction().asCfgNode().(NameNode).getId() = "source"
9+
node.(DataFlow::CallCfgNode).getFunction().asCfgNode().(Cfg::NameNode).getId() = "source"
910
}
1011

1112
predicate isSink(DataFlow::Node node) {
1213
exists(DataFlow::CallCfgNode call |
13-
call.getFunction().asCfgNode().(NameNode).getId() = "sink" and
14+
call.getFunction().asCfgNode().(Cfg::NameNode).getId() = "sink" and
1415
node = call.getArg(0)
1516
)
1617
}

python/ql/test/experimental/library-tests/CallGraph/InlineCallGraphTest.ql

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
import python
22
import utils.test.InlineExpectationsTest
33
private import semmle.python.dataflow.new.internal.DataFlowDispatch as TT
4+
private import semmle.python.controlflow.internal.Cfg as Cfg
45
private import LegacyPointsTo
56

67
/** Holds when `call` is resolved to `callable` using points-to based call-graph. */
7-
predicate pointsToCallEdge(CallNode call, Function callable) {
8+
predicate pointsToCallEdge(Call call, Function callable) {
89
exists(call.getLocation().getFile().getRelativePath()) and
910
exists(callable.getLocation().getFile().getRelativePath()) and
1011
// I did try using viableCallable from `DataFlowDispatchPointsTo` (from temporary copy
1112
// of `dataflow.new.internal` that still uses points-to) instead of direct
1213
// `getACall()` on a Value, but it only added results for `__init__` methods, not for
1314
// anything else.
14-
exists(PythonFunctionValue funcValue |
15+
exists(PythonFunctionValue funcValue, CallNode legacyCall |
1516
funcValue.getScope() = callable and
16-
call = funcValue.getACall()
17+
legacyCall = funcValue.getACall() and
18+
legacyCall.getNode() = call
1719
)
1820
}
1921

2022
/** Holds when `call` is resolved to `callable` using type-tracking based call-graph. */
21-
predicate typeTrackerCallEdge(CallNode call, Function callable) {
23+
predicate typeTrackerCallEdge(Call call, Function callable) {
2224
exists(call.getLocation().getFile().getRelativePath()) and
2325
exists(callable.getLocation().getFile().getRelativePath()) and
2426
exists(TT::DataFlowCallable dfCallable, TT::DataFlowCall dfCall |
2527
dfCallable.getScope() = callable and
26-
dfCall.getNode() = call and
28+
dfCall.getNode().getNode() = call and
2729
dfCallable = TT::viableCallable(dfCall)
2830
)
2931
}
3032

3133
/** Holds if the call edge is from a class call. */
32-
predicate typeTrackerClassCall(CallNode call, Function callable) {
34+
predicate typeTrackerClassCall(Call call, Function callable) {
3335
exists(call.getLocation().getFile().getRelativePath()) and
3436
exists(callable.getLocation().getFile().getRelativePath()) and
35-
TT::resolveCall(call, callable, any(TT::TCallType t | t instanceof TT::CallTypeClass))
37+
exists(Cfg::CallNode cfgCall |
38+
cfgCall.getNode() = call and
39+
TT::resolveCall(cfgCall, callable, any(TT::TCallType t | t instanceof TT::CallTypeClass))
40+
)
3641
}
3742

3843
module CallGraphTest implements TestSig {
3944
string getARelevantTag() { result in ["pt", "tt"] }
4045

4146
predicate hasActualResult(Location location, string element, string tag, string value) {
4247
exists(location.getFile().getRelativePath()) and
43-
exists(CallNode call, Function target |
48+
exists(Call call, Function target |
4449
tag = "tt" and
4550
typeTrackerCallEdge(call, target)
4651
or
@@ -57,7 +62,7 @@ module CallGraphTest implements TestSig {
5762
import MakeTest<CallGraphTest>
5863

5964
bindingset[call, target]
60-
string getCallEdgeValue(CallNode call, Function target) {
65+
string getCallEdgeValue(Call call, Function target) {
6166
if call.getLocation().getFile() = target.getLocation().getFile()
6267
then result = betterQualName(target)
6368
else
@@ -100,7 +105,7 @@ query predicate debug_callableNotUnique(Function callable, string message) {
100105
"' is not unique within its file. Please fix."
101106
}
102107

103-
query predicate pointsTo_found_typeTracker_notFound(CallNode call, string qualname) {
108+
query predicate pointsTo_found_typeTracker_notFound(Call call, string qualname) {
104109
exists(Function target |
105110
pointsToCallEdge(call, target) and
106111
not typeTrackerCallEdge(call, target) and
@@ -115,7 +120,7 @@ query predicate pointsTo_found_typeTracker_notFound(CallNode call, string qualna
115120
)
116121
}
117122

118-
query predicate typeTracker_found_pointsTo_notFound(CallNode call, string qualname) {
123+
query predicate typeTracker_found_pointsTo_notFound(Call call, string qualname) {
119124
exists(Function target |
120125
not pointsToCallEdge(call, target) and
121126
typeTrackerCallEdge(call, target) and

0 commit comments

Comments
 (0)