Skip to content

Commit 7923676

Browse files
committed
fix(precheck): use ruff/ffmt --check mode; fix formatting of fortran_gen.py
1 parent 46d3584 commit 7923676

2 files changed

Lines changed: 13 additions & 46 deletions

File tree

toolchain/bootstrap/precheck.sh

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ show_help() {
1515
exit 0
1616
}
1717

18-
# Cross-platform hash function (macOS uses md5, Linux uses md5sum)
19-
compute_hash() {
20-
if command -v md5sum > /dev/null 2>&1; then
21-
md5sum | cut -d' ' -f1
22-
elif command -v md5 > /dev/null 2>&1; then
23-
md5 -q
24-
else
25-
# Fallback: use cksum if neither available
26-
cksum | cut -d' ' -f1
27-
fi
28-
}
2918

3019
JOBS=1
3120

@@ -68,17 +57,14 @@ echo ""
6857
TMPDIR_PC=$(mktemp -d)
6958
trap "rm -rf $TMPDIR_PC" EXIT
7059

71-
# --- Phase 1: Format (modifies files, must run alone) ---
72-
BEFORE_HASH=$(git diff -- '*.f90' '*.fpp' '*.py' 2>/dev/null | compute_hash)
73-
if ! ./mfc.sh format -j "$JOBS" > /dev/null 2>&1; then
74-
FORMAT_OK=1
75-
else
76-
AFTER_HASH=$(git diff -- '*.f90' '*.fpp' '*.py' 2>/dev/null | compute_hash)
77-
if [ "$BEFORE_HASH" != "$AFTER_HASH" ]; then
78-
FORMAT_OK=2
79-
else
80-
FORMAT_OK=0
81-
fi
60+
# --- Phase 1: Format check (non-mutating; mirrors CI's format+diff check) ---
61+
# Use --check mode so staged-but-unformatted code is caught even if the
62+
# working tree was already reformatted by a prior ./mfc.sh format run.
63+
FORMAT_OK=0
64+
if ! ruff format --check toolchain/ examples/ benchmarks/ > /dev/null 2>&1; then
65+
FORMAT_OK=2
66+
elif ! ffmt --check -j "$JOBS" src/ > /dev/null 2>&1; then
67+
FORMAT_OK=2
8268
fi
8369

8470
# --- Phase 2: All fast checks in parallel (read-only) ---
@@ -147,14 +133,8 @@ PID_EXAMPLES=$!
147133
FAILED=0
148134

149135
log "[$CYAN 1/$NCHECK$COLOR_RESET] Checking$MAGENTA formatting$COLOR_RESET..."
150-
if [ "$FORMAT_OK" = "1" ]; then
151-
error "Formatting check failed to run."
152-
FAILED=1
153-
elif [ "$FORMAT_OK" = "2" ]; then
154-
error "Code was not formatted. Files have been auto-formatted; review and stage the changes."
155-
echo ""
156-
git diff --stat -- '*.f90' '*.fpp' '*.py' 2>/dev/null || true
157-
echo ""
136+
if [ "$FORMAT_OK" = "2" ]; then
137+
error "Code is not formatted. Run$MAGENTA ./mfc.sh format$COLOR_RESET and re-stage the changes."
158138
FAILED=1
159139
else
160140
ok "Formatting check passed."

toolchain/mfc/params/generators/fortran_gen.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ def generate_namelist_fpp(target: str) -> str:
9999
opt_lines = _pack_namelist(opt, _CONT_PREFIX, _CONT2_PREFIX, _MAX_LINE)
100100
nl_with_cont = nl_lines[:]
101101
nl_with_cont[-1] += ", &"
102-
parts = (
103-
[_HEADER.rstrip(), "#:if MFC_CASE_OPTIMIZATION"]
104-
+ nl_lines
105-
+ ["#:else"]
106-
+ nl_with_cont
107-
+ opt_lines
108-
+ ["#:endif"]
109-
)
102+
parts = [_HEADER.rstrip(), "#:if MFC_CASE_OPTIMIZATION"] + nl_lines + ["#:else"] + nl_with_cont + opt_lines + ["#:endif"]
110103
else:
111104
parts = [_HEADER.rstrip()] + nl_lines
112105
return "\n".join(parts) + "\n"
@@ -124,10 +117,7 @@ def generate_decls_fpp(target: str) -> str:
124117
if name in FORTRAN_ARRAY_DIMS:
125118
member = REGISTRY.all_params.get(f"{name}(1)")
126119
if member is None:
127-
raise ValueError(
128-
f"FORTRAN_ARRAY_DIMS[{name!r}] has no {name}(1) in the registry. "
129-
"Register at least one indexed variant (e.g. _r(f'{name}(1)', ...))."
130-
)
120+
raise ValueError(f"FORTRAN_ARRAY_DIMS[{name!r}] has no {name}(1) in the registry. " "Register at least one indexed variant (e.g. _r(f'{name}(1)', ...)).")
131121
ftype = fortran_type_decl(member)
132122
dim = FORTRAN_ARRAY_DIMS[name]
133123
lines.append(f"{(ftype + ', dimension(' + dim + ')').ljust(_ARRAY_DECL_COL)}:: {name}")
@@ -136,10 +126,7 @@ def generate_decls_fpp(target: str) -> str:
136126
if param is None:
137127
continue
138128
if any(k.startswith(f"{name}(") for k in REGISTRY.all_params):
139-
raise ValueError(
140-
f"{name!r} has indexed variants (e.g. {name}(1)) but is missing from "
141-
"FORTRAN_ARRAY_DIMS. Add it there with its Fortran dimension expression."
142-
)
129+
raise ValueError(f"{name!r} has indexed variants (e.g. {name}(1)) but is missing from " "FORTRAN_ARRAY_DIMS. Add it there with its Fortran dimension expression.")
143130
lines.append(f"{fortran_type_decl(param).ljust(_DECL_COL)}:: {name}")
144131
return "\n".join(lines) + "\n"
145132

0 commit comments

Comments
 (0)