Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(cobol): level-88 condition names sit under the data item they test (#4459)

Raghav Chamadiya18684e62edcf
diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.md
index faa195dbd..6bbe8b2a3 100644
--- a/docs/layers/LANGUAGE_SUPPORT.md
+++ b/docs/layers/LANGUAGE_SUPPORT.md
@@ -92,6 +92,9 @@ mapsets (`.bms`) are linked from the programs that send or receive their maps.
Health measures each paragraph and section as a function: `IF`, `WHEN` arms,
`PERFORM UNTIL/VARYING` loops and `AND`/`OR` add to its complexity, and data-only
copybooks have no functions to flag.
+A level-88 condition name is a child of the data item it tests, with an id such
+as `PROG.cbl::WS-RETURN-FLAG::APPL-AOK`, and stays findable by symbol search.
+These children do not count toward the 500-symbol warning for generated files.
Apex dead-code findings are review-only, since the platform also reaches classes
by name.
diff --git a/packages/core/src/repowise/core/ingestion/extractors/cobol.py b/packages/core/src/repowise/core/ingestion/extractors/cobol.py
index 641496975..d7077d5f7 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/cobol.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/cobol.py
@@ -31,6 +31,34 @@ def normalize_cobol_symbol_name(raw_name: str, node_type: str) -> str:
return "" if name in _UNNAMED else name
+def _level(node: Node) -> str:
+ level = node.named_children[0] if node.named_children else None
+ if level is None or level.type != "level_number" or level.text is None:
+ return ""
+ return level.text.decode("utf-8", errors="replace").strip()
+
+
+def cobol_condition_parent(def_node: Node) -> str | None:
+ """The data item a level-88 condition name belongs to, else None.
+
+ The grammar lists data entries as flat siblings, and an 88 entry follows
+ the item it tests or another 88 of that item. A FILLER item has no name
+ to hang the condition on, so its 88s stay top level.
+ """
+ if def_node.type != "data_description" or _level(def_node) != "88":
+ return None
+ item = def_node.prev_named_sibling
+ while item is not None and item.type == "data_description" and _level(item) == "88":
+ item = item.prev_named_sibling
+ if item is None or item.type != "data_description":
+ return None
+ name = next((c for c in item.named_children if c.type == "entry_name"), None)
+ if name is None or name.text is None:
+ return None
+ raw = name.text.decode("utf-8", errors="replace")
+ return normalize_cobol_symbol_name(raw, item.type) or None
+
+
def normalize_cobol_call_target(raw_name: str, _node_type: str) -> str:
"""Canonicalize static ``CALL`` literals and ``PERFORM`` labels."""
name = raw_name.strip()
diff --git a/packages/core/src/repowise/core/ingestion/parser/__init__.py b/packages/core/src/repowise/core/ingestion/parser/__init__.py
index b73906a35..1ec853171 100644
--- a/packages/core/src/repowise/core/ingestion/parser/__init__.py
+++ b/packages/core/src/repowise/core/ingestion/parser/__init__.py
@@ -316,12 +316,15 @@ def _masked_call_sites(lang: str, original_source: bytes, symbols: list[Symbol])
def _warn_symbol_bloat(file_info: FileInfo, symbols: list[Symbol]) -> None:
- if len(symbols) > _SYMBOL_COUNT_WARN_THRESHOLD:
+ # COBOL's only children are level-88 condition names: values of one data
+ # item, not declarations. Methods still count, so a generated class warns.
+ counted = sum(1 for s in symbols if not (s.language == "cobol" and s.parent_name))
+ if counted > _SYMBOL_COUNT_WARN_THRESHOLD:
log.warning(
"parser.symbol_bloat",
path=file_info.path,
language=file_info.language,
- symbol_count=len(symbols),
+ symbol_count=counted,
threshold=_SYMBOL_COUNT_WARN_THRESHOLD,
)
diff --git a/packages/core/src/repowise/core/ingestion/parser/symbols.py b/packages/core/src/repowise/core/ingestion/parser/symbols.py
index ca19b01cc..d41caea89 100644
--- a/packages/core/src/repowise/core/ingestion/parser/symbols.py
+++ b/packages/core/src/repowise/core/ingestion/parser/symbols.py
@@ -19,6 +19,7 @@ from ..extractors import (
extract_symbol_docstring,
node_text,
)
+from ..extractors.cobol import cobol_condition_parent
from ..extractors.synthetic_symbols import extract_synthetic_symbols
from ..extractors.synthetic_symbols.csharp_extension_blocks import drop_extension_block_symbols
from ..extractors.visibility import py_module_all_names, ts_deferred_export_names
@@ -528,6 +529,10 @@ def _resolve_parent_name(
# Objective-C containers name themselves with a bare first identifier.
if language == "objectivec":
return _objc_container_parent(def_node, config.parent_class_types, src)
+
+ # COBOL: a level-88 condition name is a value of the item before it.
+ if language == "cobol":
+ return cobol_condition_parent(def_node)
return None
diff --git a/tests/unit/ingestion/parser/test_cobol.py b/tests/unit/ingestion/parser/test_cobol.py
index 2c3b89187..8aad04815 100644
--- a/tests/unit/ingestion/parser/test_cobol.py
+++ b/tests/unit/ingestion/parser/test_cobol.py
@@ -71,6 +71,171 @@ class TestCobolSymbols:
assert ("CUSTOMER-NAME", "variable") in symbols
assert not result.parse_errors
+ def test_level_88_condition_names_are_children_of_their_item(
+ self, parser: ASTParser
+ ) -> None:
+ source = b""" IDENTIFICATION DIVISION.
+ PROGRAM-ID. FLAGS.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 01 WS-GROUP.
+ 05 WS-STATUS PIC X.
+ 88 STATUS-OK VALUE 'Y'.
+ * a comment between conditions
+ 88 STATUS-BAD VALUE 'N'.
+ 05 WS-OTHER PIC X.
+ 01 WS-RETURN-FLAG PIC X.
+ 88 APPL-AOK VALUE '0'.
+ 01 FILLER PIC X.
+ 88 ORPHAN-FLAG VALUE 'Z'.
+ PROCEDURE DIVISION.
+ MAIN-PARA.
+ STOP RUN.
+"""
+ result = parser.parse_file(_cobol("flags.cbl"), source)
+ by_name = {s.name: s for s in result.symbols}
+
+ assert by_name["STATUS-OK"].parent_name == "WS-STATUS"
+ assert by_name["STATUS-BAD"].parent_name == "WS-STATUS"
+ assert by_name["APPL-AOK"].parent_name == "WS-RETURN-FLAG"
+ assert by_name["APPL-AOK"].id == "flags.cbl::WS-RETURN-FLAG::APPL-AOK"
+ assert by_name["APPL-AOK"].kind == "variable"
+ # A FILLER item has no name to own its conditions.
+ assert by_name["ORPHAN-FLAG"].parent_name is None
+ # Other levels keep their top-level ids.
+ for name in ("WS-GROUP", "WS-STATUS", "WS-OTHER", "WS-RETURN-FLAG", "MAIN-PARA"):
+ assert by_name[name].parent_name is None
+ assert by_name[name].id == f"flags.cbl::{name}"
+
+ @staticmethod
+ def _parse_with_bloat_threshold(
+ parser: ASTParser, monkeypatch: pytest.MonkeyPatch, entries: str
+ ) -> tuple[list, list[dict]]:
+ import repowise.core.ingestion.parser as parser_module
+
+ source = (
+ " IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. BLOAT.\n"
+ " DATA DIVISION.\n"
+ " WORKING-STORAGE SECTION.\n" + entries
+ ).encode()
+ warnings: list[dict] = []
+ monkeypatch.setattr(parser_module, "_SYMBOL_COUNT_WARN_THRESHOLD", 5)
+ monkeypatch.setattr(
+ parser_module.log, "warning", lambda event, **kw: warnings.append(kw)
+ )
+ return parser.parse_file(_cobol("bloat.cbl"), source).symbols, warnings
+
+ def test_level_88_children_do_not_count_toward_symbol_bloat(
+ self, parser: ASTParser, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ conditions = "".join(
+ f" 88 COND-{i:03d} VALUE '{i % 10}'.\n" for i in range(12)
+ )
+ symbols, warnings = self._parse_with_bloat_threshold(
+ parser, monkeypatch, " 01 WS-CODE PIC X.\n" + conditions
+ )
+
+ assert len(symbols) == 14
+ assert warnings == []
+
+ def test_other_cobol_data_items_still_count_toward_symbol_bloat(
+ self, parser: ASTParser, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ items = "".join(f" 01 WS-ITEM-{i:03d} PIC X.\n" for i in range(12))
+ symbols, warnings = self._parse_with_bloat_threshold(parser, monkeypatch, items)
+
+ assert len(symbols) == 13
+ assert [w["symbol_count"] for w in warnings] == [13]
+
+ def test_same_condition_under_same_named_items(self, parser: ASTParser) -> None:
+ source = b""" IDENTIFICATION DIVISION.
+ PROGRAM-ID. DUP.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 01 GRP-A.
+ 05 WS-FLAG PIC X.
+ 88 IS-ON VALUE 'Y'.
+ 01 GRP-B.
+ 05 WS-FLAG PIC X.
+ 88 IS-ON VALUE 'Y'.
+"""
+ result = parser.parse_file(_cobol("dup.cbl"), source)
+ conditions = [s for s in result.symbols if s.name == "IS-ON"]
+
+ # COBOL ids carry no discriminator, so the store keeps the last twin.
+ assert [s.id for s in conditions] == ["dup.cbl::WS-FLAG::IS-ON"] * 2
+ assert [s.parent_name for s in conditions] == ["WS-FLAG", "WS-FLAG"]
+
+ def test_condition_after_redefines_and_occurs(self, parser: ASTParser) -> None:
+ source = b""" IDENTIFICATION DIVISION.
+ PROGRAM-ID. SHAPES.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 01 WS-NUM PIC 9(4).
+ 01 WS-NUM-X REDEFINES WS-NUM PIC X(4).
+ 88 NUM-BLANK VALUE SPACES.
+ 01 WS-TABLE.
+ 05 WS-ENTRY PIC X OCCURS 10 TIMES.
+ 88 ENTRY-SET VALUE 'Y'.
+"""
+ result = parser.parse_file(_cobol("shapes.cbl"), source)
+ by_name = {s.name: s for s in result.symbols}
+
+ assert by_name["NUM-BLANK"].parent_name == "WS-NUM-X"
+ assert by_name["ENTRY-SET"].parent_name == "WS-ENTRY"
+
+ def test_condition_after_copy_or_section_start_stays_top_level(
+ self, parser: ASTParser
+ ) -> None:
+ source = b""" IDENTIFICATION DIVISION.
+ PROGRAM-ID. EDGES.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 88 FIRST-COND VALUE 'A'.
+ 01 WS-A PIC X.
+ COPY CPYA.
+ 88 AFTER-COPY VALUE 'B'.
+ LINKAGE SECTION.
+ 88 LINK-COND VALUE 'C'.
+ 01 LK-A PIC X.
+"""
+ result = parser.parse_file(_cobol("edges.cbl"), source)
+ by_name = {s.name: s for s in result.symbols}
+
+ for name in ("FIRST-COND", "AFTER-COPY", "LINK-COND"):
+ assert by_name[name].parent_name is None
+ assert by_name[name].id == f"edges.cbl::{name}"
+
+ def test_conditions_in_a_two_program_file(self, parser: ASTParser) -> None:
+ source = b""" IDENTIFICATION DIVISION.
+ PROGRAM-ID. ONE.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 01 WS-ONE PIC X.
+ 88 ONE-ON VALUE 'Y'.
+ PROCEDURE DIVISION.
+ P1.
+ GOBACK.
+ END PROGRAM ONE.
+ IDENTIFICATION DIVISION.
+ PROGRAM-ID. TWO.
+ DATA DIVISION.
+ WORKING-STORAGE SECTION.
+ 01 WS-TWO PIC X.
+ 88 TWO-ON VALUE 'Y'.
+ PROCEDURE DIVISION.
+ P2.
+ GOBACK.
+ END PROGRAM TWO.
+"""
+ result = parser.parse_file(_cobol("two.cbl"), source)
+ by_name = {s.name: s for s in result.symbols}
+
+ assert {"ONE", "TWO"} <= {s.name for s in result.symbols if s.kind == "module"}
+ assert by_name["ONE-ON"].id == "two.cbl::WS-ONE::ONE-ON"
+ assert by_name["TWO-ON"].id == "two.cbl::WS-TWO::TWO-ON"
+
def test_program_id_without_terminal_period_recovers_name(self, parser: ASTParser) -> None:
source = b""" IDENTIFICATION DIVISION.
PROGRAM-ID. COURSE01