Skip to content

Commit 4901c32

Browse files
committed
fix(gen): use #:if/#:else guard for case-opt namelist — prevents dangling continuation
1 parent 999cde9 commit 4901c32

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

toolchain/mfc/params/generators/fortran_gen.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ def generate_namelist_fpp(target: str) -> str:
8787
normal = [v for v in all_vars if v not in CASE_OPT_PARAMS]
8888
opt = sorted(v for v in CASE_OPT_PARAMS if v in NAMELIST_VARS and "sim" in NAMELIST_VARS[v])
8989
nl_lines = _pack_namelist(normal, _FIRST_PREFIX, _CONT_PREFIX, _MAX_LINE)
90-
nl_lines[-1] += ", &"
91-
opt_lines = _pack_namelist(opt, _CONT_PREFIX, _CONT2_PREFIX, _MAX_LINE)
92-
parts = [_HEADER.rstrip()] + nl_lines + ["#:if not MFC_CASE_OPTIMIZATION"] + opt_lines + ["#:endif"]
90+
if opt:
91+
opt_lines = _pack_namelist(opt, _CONT_PREFIX, _CONT2_PREFIX, _MAX_LINE)
92+
nl_with_cont = nl_lines[:]
93+
nl_with_cont[-1] += ", &"
94+
parts = (
95+
[_HEADER.rstrip(), "#:if MFC_CASE_OPTIMIZATION"] +
96+
nl_lines +
97+
["#:else"] +
98+
nl_with_cont +
99+
opt_lines +
100+
["#:endif"]
101+
)
102+
else:
103+
parts = [_HEADER.rstrip()] + nl_lines
93104
return "\n".join(parts) + "\n"
94105

95106

toolchain/mfc/params_tests/test_fortran_gen.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ def test_sim_namelist_case_opt_guard():
6161
from mfc.params.generators.fortran_gen import generate_namelist_fpp
6262

6363
c = generate_namelist_fpp("sim")
64-
assert "#:if not MFC_CASE_OPTIMIZATION" in c
64+
# Case-opt guard: two complete namelist statements wrapped in #:if/#:else/#:endif
65+
assert "#:if MFC_CASE_OPTIMIZATION" in c
66+
assert "#:else" in c
67+
assert "#:endif" in c
6568
assert "weno_order" in c
6669
assert "num_fluids" in c
70+
# No dangling continuation before the #:if block or after #:else
71+
lines = c.splitlines()
72+
for i, line in enumerate(lines):
73+
if line.strip() in ("#:if MFC_CASE_OPTIMIZATION", "#:endif"):
74+
assert not lines[i - 1].rstrip().endswith("&"), f"Dangling & before {line!r}"
6775

6876

6977
def test_pre_namelist_has_patch_icpp():

0 commit comments

Comments
 (0)