Skip to content

Commit a8fa672

Browse files
yoffCopilot
andcommitted
Python: add shared-CFG AstSig adapter (AstNodeImpl)
Preparatory refactor for the shared-CFG dataflow migration. Adds the adapter that mediates between the Python AST and the shared codeql.controlflow.ControlFlowGraph signature, plus the test suites that validate the new CFG directly against this adapter. The public facade is added in the following commit. Library additions: - semmle.python.controlflow.internal.AstNodeImpl — wraps Python's Stmt/Expr/Scope/Pattern and adds two synthetic kinds of node (BlockStmt for body slots, intermediate nodes for multi-operand boolean expressions) to satisfy the shared CFG signature. - lib/printCfgNew.ql — debug/visualisation query for the new CFG. - consistency-queries/CfgConsistency.ql — consistency query running the shared CFG's standard checks against Python. Test additions (all driven directly off AstNodeImpl): - ControlFlow/bindings/* — annotation-driven SSA-binding tests (annassign, compound, comprehension, decorated, except_handler, imports, match_pattern, parameters, simple, type_params, walrus_starred, with_stmt, dead_under_no_raise). - ControlFlow/evaluation-order/NewCfg*.ql — mirrors of the existing OldCfg evaluation-order self-validation suite, run against the new CFG via NewCfgImpl.qll. - Minor extensions to existing test_if.py / test_boolean.py + cosmetic .expected churn on a handful of OldCfg tests. No dataflow, SSA, or production query is migrated yet. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 53cae68 commit a8fa672

50 files changed

Lines changed: 2419 additions & 9 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import semmle.python.controlflow.internal.AstNodeImpl
2+
import ControlFlow::Consistency

python/ql/lib/printCfgNew.ql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name Print CFG (New)
3+
* @description Produces a representation of a file's Control Flow Graph
4+
* using the new shared control flow library.
5+
* This query is used by the VS Code extension.
6+
* @id python/print-cfg
7+
* @kind graph
8+
* @tags ide-contextual-queries/print-cfg
9+
*/
10+
11+
private import python as Py
12+
import semmle.python.controlflow.internal.AstNodeImpl
13+
14+
external string selectedSourceFile();
15+
16+
private predicate selectedSourceFileAlias = selectedSourceFile/0;
17+
18+
external int selectedSourceLine();
19+
20+
private predicate selectedSourceLineAlias = selectedSourceLine/0;
21+
22+
external int selectedSourceColumn();
23+
24+
private predicate selectedSourceColumnAlias = selectedSourceColumn/0;
25+
26+
module ViewCfgQueryInput implements ControlFlow::ViewCfgQueryInputSig<Py::File> {
27+
predicate selectedSourceFile = selectedSourceFileAlias/0;
28+
29+
predicate selectedSourceLine = selectedSourceLineAlias/0;
30+
31+
predicate selectedSourceColumn = selectedSourceColumnAlias/0;
32+
33+
predicate cfgScopeSpan(
34+
Ast::Callable callable, Py::File file, int startLine, int startColumn, int endLine,
35+
int endColumn
36+
) {
37+
exists(Py::Scope scope |
38+
scope = callable.asScope() and
39+
file = scope.getLocation().getFile() and
40+
scope.getLocation().hasLocationInfo(_, startLine, startColumn, endLine, endColumn)
41+
)
42+
}
43+
}
44+
45+
import ControlFlow::ViewCfgQuery<Py::File, ViewCfgQueryInput>

0 commit comments

Comments
 (0)