|
| 1 | +################################################################################ |
| 2 | +# Copyright IBM Corporation 2025 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +################################################################################ |
| 16 | + |
| 17 | +"""Stage 4 of the level-3 dataflow ladder: PDG assembly. |
| 18 | +
|
| 19 | +Per callable, the PDG is the union of the stage-2 control-dependence edges |
| 20 | +(``CDG``) and the stage-3 def-use edges (``DDG``), over the same |
| 21 | +``(signature, node_id)`` nodes. Nothing new is computed here — this module is |
| 22 | +bookkeeping plus the intraprocedural backward slice that gates it: reverse |
| 23 | +reachability over CDG ∪ DDG from a criterion node, expected to match a |
| 24 | +hand-computed node set exactly on the fixture. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import ast |
| 30 | +from dataclasses import dataclass, field |
| 31 | +from typing import Dict, List, Optional, Set |
| 32 | + |
| 33 | +from codeanalyzer.dataflow.access_paths import ( |
| 34 | + FunctionScope, |
| 35 | + StatementFacts, |
| 36 | + build_scope, |
| 37 | + statement_facts, |
| 38 | +) |
| 39 | +from codeanalyzer.dataflow.alias import TypeBasedAliasOracle |
| 40 | +from codeanalyzer.dataflow.cfg import ControlFlowGraph, build_cfg |
| 41 | +from codeanalyzer.dataflow.defuse import ddg_edges |
| 42 | +from codeanalyzer.dataflow.dominance import control_dependence |
| 43 | + |
| 44 | + |
| 45 | +@dataclass(frozen=True) |
| 46 | +class PDGEdge: |
| 47 | + source: int |
| 48 | + target: int |
| 49 | + type: str # "CDG" | "DDG" |
| 50 | + var: Optional[str] = None # access path on DDG edges |
| 51 | + |
| 52 | + |
| 53 | +@dataclass |
| 54 | +class FunctionPDG: |
| 55 | + """One callable's intraprocedural graphs, keyed externally by signature.""" |
| 56 | + |
| 57 | + cfg: ControlFlowGraph |
| 58 | + edges: List[PDGEdge] |
| 59 | + scope: FunctionScope |
| 60 | + facts: Dict[int, StatementFacts] = field(default_factory=dict) |
| 61 | + |
| 62 | + |
| 63 | +def build_pdg( |
| 64 | + func: ast.AST, |
| 65 | + enclosing_locals: Set[str], |
| 66 | + oracle: TypeBasedAliasOracle, |
| 67 | + k: int = 3, |
| 68 | +) -> FunctionPDG: |
| 69 | + """CFG → dominance → def-use → PDG for one callable.""" |
| 70 | + cfg = build_cfg(func) |
| 71 | + scope = build_scope(func, enclosing_locals) |
| 72 | + facts = statement_facts(cfg, func, scope, k) |
| 73 | + |
| 74 | + edges: List[PDGEdge] = [ |
| 75 | + PDGEdge(source=a, target=b, type="CDG") for a, b in control_dependence(cfg) |
| 76 | + ] |
| 77 | + edges.extend( |
| 78 | + PDGEdge(source=e.source, target=e.target, type="DDG", var=e.var) |
| 79 | + for e in ddg_edges(cfg, facts, oracle) |
| 80 | + ) |
| 81 | + edges.sort(key=lambda e: (e.source, e.target, e.type, e.var or "")) |
| 82 | + return FunctionPDG(cfg=cfg, edges=edges, scope=scope, facts=facts) |
| 83 | + |
| 84 | + |
| 85 | +def intraprocedural_backward_slice(pdg: FunctionPDG, criterion: int) -> Set[int]: |
| 86 | + """Reverse reachability over CDG ∪ DDG from the criterion node (the |
| 87 | + criterion itself is in the slice). The stage-4 gate.""" |
| 88 | + reverse: Dict[int, List[int]] = {} |
| 89 | + for e in pdg.edges: |
| 90 | + reverse.setdefault(e.target, []).append(e.source) |
| 91 | + seen: Set[int] = set() |
| 92 | + stack = [criterion] |
| 93 | + while stack: |
| 94 | + n = stack.pop() |
| 95 | + if n in seen: |
| 96 | + continue |
| 97 | + seen.add(n) |
| 98 | + stack.extend(reverse.get(n, [])) |
| 99 | + return seen |
0 commit comments