Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(cobol): link CICS LINK and XCTL to their programs, following literal constants (#4436)
Raghav Chamadiya787a343bdec5
diff --git a/docs/architecture/language-support.md b/docs/architecture/language-support.mdindex 2cbc873bd..77812200c 100644--- a/docs/architecture/language-support.md+++ b/docs/architecture/language-support.md@@ -728,7 +728,7 @@ dependency surface their syntax supports.| Good | **PHP** | `.php` | `use` declarations through PSR-4 from every `composer.json`, same-namespace and fully qualified class references || Good | **Dart** | `.dart` | `import` / `export` / `part`, `package:` via every `pubspec.yaml` || Good | **Object Pascal** | `.pas` `.pp` `.dpr` `.dpk` `.lpr` | `uses` clauses via unit-name to file-stem match; project files as entry points |-| Good | **COBOL** | `.cbl` `.cob` `.cobol` `.cpy` | `COPY` members to copybooks; literal `CALL` and `PERFORM` targets; dynamic calls stay silent |+| Good | **COBOL** | `.cbl` `.cob` `.cobol` `.cpy` | `COPY` members to copybooks; literal `CALL`, `PERFORM` and CICS `LINK`/`XCTL` targets, with constants followed; dynamic targets shown as such || Good | **GDScript** | `.gd` | `preload` / `load` / `extends "res://..."` from the nearest `project.godot` || Good | **VB.NET** | `.vb` | `Imports` through the same MSBuild project index C# uses || Good | **Elixir** | `.ex` `.exs` | `alias` / `import` / `require` / `use` against a `defmodule` index, Mix path convention as fallback |@@ -1067,7 +1067,7 @@ point. A bundle two sfdx projects declare goes to the nearer one; a tie gets noboth, since props are set by the parent as markup attributes.- **Object Pascal type kinds collapse** to `kind="class"`, and its `extends`vs `implements` split follows the `I`-prefix naming convention.-- **COBOL dynamic program names stay silent**, and free-format source is read+- **COBOL dynamic `CALL` targets stay silent**, and free-format source is readwithout column handling.- **Razor has no import edges**, because every directive is blanked by theprojection.diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.mdindex c3cf6c62c..6fe20b1c9 100644--- a/docs/layers/LANGUAGE_SUPPORT.md+++ b/docs/layers/LANGUAGE_SUPPORT.md@@ -81,6 +81,9 @@ COBOL is read in fixed reference format, with `EXEC SQL/CICS/DLI` blocks and`COPY` statements masked before parsing; a copybook parses as a data orprocedure fragment. `COPY` and `EXEC SQL INCLUDE` members resolve to thenearest copybook with that name; system copybooks such as `DFHAID` stay external.+`EXEC CICS LINK` and `XCTL` link to their program when it is a literal or a+constant moved into the program variable; any other target shows as a+`cics-dynamic:` reference rather than a guess.Apex (`.cls`, `.trigger`, `.apex`) is parsed for symbols, heritage and calls,resolved case-insensitively within each sfdx project's namespace; its dead-codefindings are review-only. Apex test coverage from the Salesforce CLI is read as a@@ -276,7 +279,7 @@ Per-marker detail: [CODE_HEALTH.md](CODE_HEALTH.md).- Razor has no import edges.- Scala import resolution is partial, and the Elixir and F# import resolvershave known gaps.-- COBOL dynamic program names and free-format source produce no edges.+- COBOL dynamic `CALL` targets and free-format source produce no edges.- C has no performance risk support.The full list per language, with the mechanics behind each gap, is indiff --git a/packages/core/src/repowise/core/ingestion/cobol/cics.py b/packages/core/src/repowise/core/ingestion/cobol/cics.pynew file mode 100644index 000000000..cea1df224--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/cobol/cics.py@@ -0,0 +1,170 @@+"""CICS ``LINK`` / ``XCTL`` program transfers, with in-program constant tracking.++``PROGRAM('X')`` names its target. ``PROGRAM(VAR)`` names every value the+program moves into ``VAR`` that is a literal or a constant (a data item with a+``VALUE`` literal that nothing reassigns). Any other assignment makes the+transfer dynamic, reported as such and never guessed.++Ceiling: only the program's own text is read, so a constant defined in a+copybook, or a value arriving through a group move (a COMMAREA), is dynamic.+"""++from __future__ import annotations++import re+from dataclasses import dataclass++from .statements import exec_blocks, mask_statements++# A transfer whose program is chosen at runtime imports this, so the graph+# shows it as an unresolved reference rather than a guessed program.+DYNAMIC_PREFIX = "dynamic:"++_NAME = r"[A-Z0-9#@$][A-Z0-9#@$-]*"+_LITERAL = r"'[^']*'|\"[^\"]*\""+_TRANSFER = re.compile(r"^\s*(LINK|XCTL)\b", re.IGNORECASE)+_PROGRAM = re.compile(rf"\bPROGRAM\s*\(\s*(?:(?P<lit>{_LITERAL})|(?P<var>{_NAME}))\s*\)", re.I)+_MOVE = re.compile(+ # The receivers are a lookahead, so a MOVE inside them is matched on its own.+ rf"(?<![\w-])MOVE\s+(?P<src>{_LITERAL}|{_NAME}(?:\s*\([^)]*\))?)\s+TO\s+(?=(?P<dst>[^.]*))",+ re.IGNORECASE,+)+_OTHER_WRITE = re.compile(rf"(?<![\w-])(?:INTO|ACCEPT|INITIALIZE|COMPUTE|SET)\s+({_NAME})", re.I)+_VALUE = re.compile(+ rf"^\s*\d\d\s+(?P<name>{_NAME})\b[^.]*?\bVALUE\s+(?:IS\s+)?(?P<lit>{_LITERAL})",+ re.IGNORECASE | re.MULTILINE,+)+_WORD = re.compile(rf"{_NAME}|\([^)]*\)|,|\S+", re.IGNORECASE)+# A MOVE's receiving list ends at the next statement's verb or a scope end.+_VERBS = frozenset(+ [+ "ACCEPT",+ "ADD",+ "CALL",+ "CLOSE",+ "COMPUTE",+ "CONTINUE",+ "DELETE",+ "DISPLAY",+ "DIVIDE",+ "ELSE",+ "EVALUATE",+ "EXEC",+ "EXIT",+ "GO",+ "GOBACK",+ "IF",+ "INITIALIZE",+ "INSPECT",+ "MOVE",+ "MULTIPLY",+ "NOT",+ "OPEN",+ "PERFORM",+ "READ",+ "RETURN",+ "REWRITE",+ "SEARCH",+ "SET",+ "START",+ "STOP",+ "STRING",+ "SUBTRACT",+ "UNSTRING",+ "WHEN",+ "WRITE",+ ]+)+++@dataclass(frozen=True)+class ProgramTransfer:+ """One ``EXEC CICS LINK|XCTL``: its static targets, and the variable when dynamic."""++ verb: str+ line: int+ targets: tuple[str, ...]+ dynamic: str | None = None+++def program_transfers(code: str) -> list[ProgramTransfer]:+ """Every LINK/XCTL in *code* (a code view), its targets resolved where static."""+ statements = mask_statements(code)+ moves = _moves(statements)+ other_writes = {m.group(1).upper() for m in _OTHER_WRITE.finditer(statements)}+ rewritten = {name for _, dsts in moves for name in dsts} | other_writes+ constants = {+ m.group("name").upper(): _unquote(m.group("lit"))+ for m in _VALUE.finditer(statements)+ if m.group("name").upper() not in rewritten+ }+ out: list[ProgramTransfer] = []+ for block in exec_blocks(code):+ verb = _TRANSFER.match(block.body) if block.kind == "CICS" else None+ program = _PROGRAM.search(block.body) if verb else None+ if program is None:+ continue+ if program.group("lit"):+ out.append(+ ProgramTransfer(+ verb.group(1).upper(), block.line, (_unquote(program.group("lit")),)+ )+ )+ continue+ var = program.group("var").upper()+ targets, dynamic = _values_of(var, moves, constants)+ dynamic = dynamic or var in other_writes+ out.append(+ ProgramTransfer(verb.group(1).upper(), block.line, targets, var if dynamic else None)+ )+ return out+++def _values_of(+ var: str, moves: list[tuple[str, set[str]]], constants: dict[str, str]+) -> tuple[tuple[str, ...], bool]:+ """``(static values, any non-static assignment)`` for *var*."""+ values: list[str] = []+ dynamic = False+ sources = [src for src, dsts in moves if var in dsts]+ if not sources:+ if var in constants:+ return (constants[var],), False+ return (), True+ for src in sources:+ if src[0] in "'\"":+ value = _unquote(src)+ elif src in constants:+ value = constants[src]+ else:+ dynamic = True+ continue+ if value and value not in values:+ values.append(value)+ return tuple(values), dynamic+++def _moves(code: str) -> list[tuple[str, set[str]]]:+ """``(source, {receivers})`` for each MOVE; a literal source keeps its quotes."""+ out = []+ for m in _MOVE.finditer(code):+ src = m.group("src")+ src = src if src[0] in "'\"" else re.sub(r"\s*\(.*", "", src).upper()+ receivers: set[str] = set()+ qualifier = False+ for token in _WORD.findall(m.group("dst")):+ word = token.upper()+ if word in _VERBS or word.startswith("END-"):+ break+ if word in {"OF", "IN"}:+ qualifier = True+ elif qualifier:+ qualifier = False+ elif re.fullmatch(_NAME, word, re.IGNORECASE):+ receivers.add(word)+ out.append((src, receivers))+ return out+++def _unquote(literal: str) -> str:+ return literal[1:-1].strip().upper()diff --git a/packages/core/src/repowise/core/ingestion/cobol/copybooks.py b/packages/core/src/repowise/core/ingestion/cobol/copybooks.pydeleted file mode 100644index 82c7eec89..000000000--- a/packages/core/src/repowise/core/ingestion/cobol/copybooks.py+++ /dev/null@@ -1,34 +0,0 @@-"""``COPY`` and ``EXEC SQL INCLUDE`` statements as graph imports."""--from __future__ import annotations--from ..models import Import-from .source import code_view-from .statements import copy_statements---def copy_imports(source: bytes) -> list[Import]:- """One import per copied member, read from the unmasked source."""- imports: list[Import] = []- seen: set[str] = set()- for statement in copy_statements(code_view(source)):- # ``LIB/MEMBER`` lets the resolver prefer a library directory.- module = (- f"{statement.library}/{statement.member}" if statement.library else statement.member- )- if module in seen:- continue- seen.add(module)- verb = "EXEC SQL INCLUDE" if statement.include else "COPY"- library = f" OF {statement.library}" if statement.library else ""- imports.append(- Import(- raw_statement=f"{verb} {statement.member}{library}",- module_path=module,- # A copybook is textually included: every name in it is in scope.- imported_names=["*"],- is_relative=False,- resolved_file=None,- )- )- return importsdiff --git a/packages/core/src/repowise/core/ingestion/cobol/facts.py b/packages/core/src/repowise/core/ingestion/cobol/facts.pynew file mode 100644index 000000000..7d4991229--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/cobol/facts.py@@ -0,0 +1,62 @@+"""Graph facts read from a COBOL file's unmasked source: imports and call sites."""++from __future__ import annotations++from ..models import CallSite, Import, Symbol+from ..parser_helpers import _find_enclosing_symbol+from .cics import DYNAMIC_PREFIX, program_transfers+from .source import code_view+from .statements import copy_statements+++def cobol_imports(source: bytes) -> list[Import]:+ """One import per copied member, plus one per dynamic program transfer variable."""+ code = code_view(source)+ imports: list[Import] = []+ seen: set[str] = set()+ for statement in copy_statements(code):+ # ``LIB/MEMBER`` lets the resolver prefer a library directory.+ module = (+ f"{statement.library}/{statement.member}" if statement.library else statement.member+ )+ if module in seen:+ continue+ seen.add(module)+ verb = "EXEC SQL INCLUDE" if statement.include else "COPY"+ library = f" OF {statement.library}" if statement.library else ""+ imports.append(_import(f"{verb} {statement.member}{library}", module))+ for transfer in program_transfers(code):+ if transfer.dynamic and DYNAMIC_PREFIX + transfer.dynamic not in seen:+ seen.add(DYNAMIC_PREFIX + transfer.dynamic)+ raw = f"EXEC CICS {transfer.verb} PROGRAM({transfer.dynamic})"+ imports.append(_import(raw, DYNAMIC_PREFIX + transfer.dynamic))+ return imports+++def cobol_call_sites(source: bytes, symbols: list[Symbol]) -> list[CallSite]:+ """A call site per static LINK/XCTL target, from the paragraph holding it."""+ ranges = sorted(+ ((s.start_line, s.end_line, s.id) for s in symbols), key=lambda t: (t[0], -t[1])+ )+ return [+ CallSite(+ target_name=target,+ receiver_name=None,+ caller_symbol_id=_find_enclosing_symbol(transfer.line, ranges),+ line=transfer.line,+ argument_count=None,+ )+ for transfer in program_transfers(code_view(source))+ for target in transfer.targets+ ]+++def _import(raw: str, module: str) -> Import:+ # A copybook is textually included: every name in it is in scope.+ return Import(+ raw_statement=raw,+ module_path=module,+ imported_names=["*"],+ is_relative=False,+ resolved_file=None,+ )diff --git a/packages/core/src/repowise/core/ingestion/parser/__init__.py b/packages/core/src/repowise/core/ingestion/parser/__init__.pyindex 248c26a56..b73906a35 100644--- a/packages/core/src/repowise/core/ingestion/parser/__init__.py+++ b/packages/core/src/repowise/core/ingestion/parser/__init__.py@@ -227,6 +227,7 @@ class ASTParser:calls = _extract_calls(matches, config, file_info, src, symbols)# An SFC's markup tags (``<Foo />``) are component calls the TS buffer lost.calls.extend(component_call_sites(lang, original_source, symbols))+ calls.extend(_masked_call_sites(lang, original_source, symbols))references = (_extract_references(matches, file_info, src, symbols, imports)if lang in _REFERENCE_LANGUAGES@@ -299,9 +300,18 @@ def _parse_without_grammar(def _masked_imports(lang: str, original_source: bytes) -> list[Import]:"""Imports ``prepare_source`` masked from the grammar, read from the original bytes."""if lang == "cobol":- from ..cobol.copybooks import copy_imports+ from ..cobol.facts import cobol_imports- return copy_imports(original_source)+ return cobol_imports(original_source)+ return []+++def _masked_call_sites(lang: str, original_source: bytes, symbols: list[Symbol]) -> list:+ """Call sites ``prepare_source`` masked from the grammar (COBOL ``EXEC CICS``)."""+ if lang == "cobol":+ from ..cobol.facts import cobol_call_sites++ return cobol_call_sites(original_source, symbols)return []diff --git a/packages/core/src/repowise/core/ingestion/resolvers/cobol.py b/packages/core/src/repowise/core/ingestion/resolvers/cobol.pyindex 3cd1919ab..2bf2d527a 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/cobol.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/cobol.py@@ -11,6 +11,7 @@ from __future__ import annotationsimport posixpathfrom typing import TYPE_CHECKING+from ..cobol.cics import DYNAMIC_PREFIXfrom ..languages.registry import REGISTRYif TYPE_CHECKING:@@ -20,6 +21,9 @@ _COPYBOOK_SUFFIXES = frozenset({".cpy"})def resolve_cobol_import(module_path: str, importer_path: str, ctx: ResolverContext) -> str | None:+ if module_path.startswith(DYNAMIC_PREFIX):+ # A program chosen at runtime: shown as such, never matched to a file.+ return ctx.add_external_node(f"cics-{module_path}")library, _, member = module_path.rpartition("/")spec = REGISTRY.get("cobol")extensions = spec.extensions if spec else _COPYBOOK_SUFFIXESdiff --git a/scripts/cobol_acceptance.py b/scripts/cobol_acceptance.pyindex de5ce7824..6da816384 100644--- a/scripts/cobol_acceptance.py+++ b/scripts/cobol_acceptance.py@@ -141,14 +141,17 @@ def truth(root: str) -> dict:def _literal_assignments(text: str) -> dict[str, set[str]]:- """``{VAR: {'LIT', ...}}`` from ``MOVE 'LIT' TO VAR`` and ``VAR ... VALUE 'LIT'``."""- out: dict[str, set[str]] = collections.defaultdict(set)- for m in re.finditer(r"\bMOVE\s+'([A-Z0-9#@$-]+)'\s+TO\s+([A-Z0-9-]+)", text, re.I):- out[m.group(2).upper()].add(m.group(1).upper())+ """``{VAR: {'LIT', ...}}`` from ``VAR ... VALUE 'LIT'``, ``MOVE 'LIT' TO VAR``, and+ ``MOVE CONST TO VAR`` where ``CONST`` has a VALUE literal."""+ values: dict[str, set[str]] = collections.defaultdict(set)for m in re.finditer(r"^\s*\d\d\s+([A-Z0-9-]+)\s+PIC[^.]*?VALUE\s+'([A-Z0-9#@$-]+)'", text, re.I | re.M):- out[m.group(1).upper()].add(m.group(2).upper())+ values[m.group(1).upper()].add(m.group(2).upper())+ out = collections.defaultdict(set, {k: set(v) for k, v in values.items()})+ for m in re.finditer(r"\bMOVE\s+('?)([A-Z0-9#@$-]+)'?\s+TO\s+([A-Z0-9-]+)", text, re.I):+ source, target = m.group(2).upper(), m.group(3).upper()+ out[target] |= {source} if m.group(1) else values.get(source, set())return outdiff --git a/tests/unit/ingestion/test_cobol_cics.py b/tests/unit/ingestion/test_cobol_cics.pynew file mode 100644index 000000000..070d431fc--- /dev/null+++ b/tests/unit/ingestion/test_cobol_cics.py@@ -0,0 +1,99 @@+"""CICS LINK/XCTL targets: literals and constants are static, anything else is dynamic."""++from __future__ import annotations++from repowise.core.ingestion.cobol import code_view+from repowise.core.ingestion.cobol.cics import ProgramTransfer, program_transfers+from repowise.core.ingestion.cobol.facts import cobol_imports+++def _program(data: str, procedure: str) -> bytes:+ return (+ " IDENTIFICATION DIVISION.\n"+ " PROGRAM-ID. COSGN00C.\n"+ " DATA DIVISION.\n"+ " WORKING-STORAGE SECTION.\n"+ f"{data}\n"+ " PROCEDURE DIVISION.\n"+ " MAIN-PARA.\n"+ f"{procedure}\n"+ ).encode()+++def _transfers(source: bytes) -> list[ProgramTransfer]:+ return program_transfers(code_view(source))+++def test_a_literal_program_is_its_target() -> None:+ source = _program("", " EXEC CICS XCTL PROGRAM('COMEN01C') END-EXEC.")++ assert _transfers(source) == [ProgramTransfer("XCTL", 8, ("COMEN01C",))]+++def test_literal_moves_into_the_variable_are_its_targets() -> None:+ source = _program(+ " 01 CDEMO-TO-PROGRAM PIC X(8).",+ " IF WS-ADMIN MOVE 'COADM01C' TO CDEMO-TO-PROGRAM\n"+ " ELSE MOVE 'COMEN01C' TO CDEMO-TO-PROGRAM END-IF\n"+ " EXEC CICS XCTL\n"+ " PROGRAM(CDEMO-TO-PROGRAM)\n"+ " END-EXEC.",+ )++ assert _transfers(source) == [ProgramTransfer("XCTL", 10, ("COADM01C", "COMEN01C"))]+++def test_a_constant_moved_into_the_variable_is_followed() -> None:+ source = _program(+ " 01 LIT-MENUPGM PIC X(8) VALUE 'COMEN01C'.\n 01 CDEMO-TO-PROGRAM PIC X(8).",+ " MOVE LIT-MENUPGM TO CDEMO-TO-PROGRAM\n"+ " EXEC CICS LINK PROGRAM(CDEMO-TO-PROGRAM) END-EXEC.",+ )++ assert _transfers(source) == [ProgramTransfer("LINK", 10, ("COMEN01C",))]+++def test_a_variable_assigned_from_another_variable_is_dynamic() -> None:+ source = _program(+ " 01 CDEMO-TO-PROGRAM PIC X(8).",+ " MOVE 'COSGN00C' TO CDEMO-TO-PROGRAM\n"+ " MOVE CDEMO-FROM-PROGRAM TO CDEMO-TO-PROGRAM\n"+ " EXEC CICS XCTL PROGRAM(CDEMO-TO-PROGRAM) END-EXEC.",+ )++ assert _transfers(source) == [+ ProgramTransfer("XCTL", 10, ("COSGN00C",), dynamic="CDEMO-TO-PROGRAM")+ ]+ dynamic = [i.module_path for i in cobol_imports(source) if i.module_path.startswith("dyn")]+ assert dynamic == ["dynamic:CDEMO-TO-PROGRAM"]+++def test_a_reassigned_value_is_not_a_constant() -> None:+ source = _program(+ " 01 WS-PGM PIC X(8) VALUE 'COMEN01C'.",+ " MOVE INPUT-PGM TO WS-PGM\n EXEC CICS XCTL PROGRAM(WS-PGM) END-EXEC.",+ )++ assert _transfers(source) == [ProgramTransfer("XCTL", 9, (), dynamic="WS-PGM")]+++def test_a_literal_later_overwritten_by_another_verb_is_dynamic() -> None:+ source = _program(+ " 01 WS-PGM PIC X(8).",+ " MOVE 'COMEN01C' TO WS-PGM\n"+ " ACCEPT WS-PGM\n"+ " EXEC CICS XCTL PROGRAM(WS-PGM) END-EXEC.",+ )++ assert _transfers(source) == [ProgramTransfer("XCTL", 10, ("COMEN01C",), dynamic="WS-PGM")]+++def test_other_cics_commands_and_comments_are_not_transfers() -> None:+ source = _program(+ "",+ " * EXEC CICS XCTL PROGRAM('NOTME') END-EXEC.\n"+ " EXEC CICS RETURN TRANSID('CC00') END-EXEC.\n"+ " EXEC CICS SEND MAP('COSGN0A') MAPSET('COSGN00') END-EXEC.",+ )++ assert _transfers(source) == []diff --git a/tests/unit/ingestion/test_cobol_resolver.py b/tests/unit/ingestion/test_cobol_resolver.pyindex 73f24d453..74a97e070 100644--- a/tests/unit/ingestion/test_cobol_resolver.py+++ b/tests/unit/ingestion/test_cobol_resolver.py@@ -4,7 +4,7 @@ from __future__ import annotationsimport networkx as nx-from repowise.core.ingestion.cobol.copybooks import copy_imports+from repowise.core.ingestion.cobol.facts import cobol_importsfrom repowise.core.ingestion.graph import build_stem_mapfrom repowise.core.ingestion.resolvers.cobol import resolve_cobol_importfrom repowise.core.ingestion.resolvers.context import ResolverContext@@ -59,7 +59,7 @@ def test_copy_and_sql_include_become_one_import_per_member() -> None:b" EXEC SQL INCLUDE DCLTRTYP END-EXEC.\n")- found = [(i.module_path, i.raw_statement, i.imported_names) for i in copy_imports(source)]+ found = [(i.module_path, i.raw_statement, i.imported_names) for i in cobol_imports(source)]assert found == [("COCOM01Y", "COPY COCOM01Y", ["*"]),