Skip to content

Commit be2eaf9

Browse files
committed
build(codegen): Pipe gen_bindings output through clang-format
Resolves the conflict between BindingCodegenDrift (which compared committed files to raw generator output) and format-check (which required clang-formatted source). The generator now formats each emitted file via clang-format before write/compare, so both checks agree on the same canonical form. Falls back to raw content if clang-format is unavailable so stripped CI images can still run --check mode.
1 parent 38f6a04 commit be2eaf9

10 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/c_api/generated/sheet_counts.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
99
// To change behaviour, edit the manifest and re-run the generator.
1010

11-
1211
#include <cstddef>
1312
#include <cstdint>
1413
#include <string>
@@ -26,8 +25,7 @@ using formulon::c_api::parts::set_binding_error;
2625
extern "C" fm_status_t fm_workbook_cell_count(const fm_workbook_t* wb, size_t sheet_index, size_t* out_count) {
2726
clear_last_error();
2827
if (wb == nullptr || out_count == nullptr) {
29-
return set_binding_error(formulon::FormulonErrorCode::kBindingNullPointer,
30-
"fm_workbook_cell_count: NULL argument");
28+
return set_binding_error(formulon::FormulonErrorCode::kBindingNullPointer, "fm_workbook_cell_count: NULL argument");
3129
}
3230
if (sheet_index >= wb->workbook().sheet_count()) {
3331
return set_binding_error(
@@ -52,4 +50,3 @@ extern "C" fm_status_t fm_workbook_pivot_count(const fm_workbook_t* wb, size_t s
5250
*out_count = wb->workbook().sheet(sheet_index).pivot_tables().size();
5351
return 0;
5452
}
55-

src/c_api/generated/styles_counts.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
99
// To change behaviour, edit the manifest and re-run the generator.
1010

11-
1211
#include <cstddef>
1312
#include <cstdint>
1413
#include <string>
@@ -62,4 +61,3 @@ extern "C" fm_status_t fm_styles_get_font_count(fm_workbook_t* wb, uint32_t* out
6261
*out_count = static_cast<uint32_t>(wb->workbook().styles().fonts.size());
6362
return 0;
6463
}
65-

src/c_api/generated/workbook_counts.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
99
// To change behaviour, edit the manifest and re-run the generator.
1010

11-
1211
#include <cstddef>
1312
#include <cstdint>
1413
#include <string>
@@ -50,4 +49,3 @@ extern "C" size_t fm_workbook_table_count(const fm_workbook_t* wb) {
5049
}
5150
return wb->workbook().tables().size();
5251
}
53-

src/node_addon/generated/sheet_counts.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

src/node_addon/generated/styles_counts.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

src/node_addon/generated/workbook_counts.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

src/wasm/generated/sheet_counts.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

src/wasm/generated/styles_counts.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

src/wasm/generated/workbook_counts.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// tools/codegen/binding_manifest.yaml. DO NOT EDIT BY HAND.
88
// To change behaviour, edit the manifest and re-run the generator.
99

10-
1110
#include <cstddef>
1211
#include <cstdint>
1312

tools/codegen/gen_bindings.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import argparse
2828
import datetime as _dt
29+
import shutil
30+
import subprocess
2931
import sys
3032
from pathlib import Path
3133
from typing import Dict, List, Tuple
@@ -431,6 +433,34 @@ def _emit_node_entry(e: dict) -> str:
431433
# ---------------------------------------------------------------------------
432434

433435

436+
def _clang_format(content: str, path: Path) -> str:
437+
"""Pipe `content` through clang-format using `path`'s filename for style detection.
438+
439+
Ensures generated files agree with the project-wide format-check (which
440+
runs `clang-format --dry-run --Werror` over `src/`). Falls back to the
441+
raw content if clang-format is unavailable, so a stripped CI image can
442+
still run the generator's --check mode against a previously-formatted
443+
snapshot.
444+
"""
445+
cf = shutil.which("clang-format")
446+
if cf is None:
447+
return content
448+
proc = subprocess.run(
449+
[cf, f"--assume-filename={path.name}"],
450+
input=content,
451+
capture_output=True,
452+
text=True,
453+
check=False,
454+
)
455+
if proc.returncode != 0:
456+
sys.stderr.write(
457+
f"clang-format failed on {path.name}: {proc.stderr}\n"
458+
"falling back to raw codegen output\n"
459+
)
460+
return content
461+
return proc.stdout
462+
463+
434464
def _write_if_changed(path: Path, content: str) -> bool:
435465
"""Write `content` to `path` only if it differs. Returns True on write."""
436466
path.parent.mkdir(parents=True, exist_ok=True)
@@ -465,6 +495,7 @@ def main(argv: List[str] | None = None) -> int:
465495
drift = False
466496
for name, content in sorted(capi_files.items()):
467497
target = args.out_c_api / name
498+
content = _clang_format(content, target)
468499
if args.check:
469500
if not target.exists() or target.read_text() != content:
470501
sys.stderr.write(f"drift: {target}\n")
@@ -475,6 +506,7 @@ def main(argv: List[str] | None = None) -> int:
475506
sys.stdout.write(f"wrote {target}\n")
476507
for name, content in sorted(embind_files.items()):
477508
target = args.out_embind / name
509+
content = _clang_format(content, target)
478510
if args.check:
479511
if not target.exists() or target.read_text() != content:
480512
sys.stderr.write(f"drift: {target}\n")
@@ -485,6 +517,7 @@ def main(argv: List[str] | None = None) -> int:
485517
sys.stdout.write(f"wrote {target}\n")
486518
for name, content in sorted(node_files.items()):
487519
target = args.out_node / name
520+
content = _clang_format(content, target)
488521
if args.check:
489522
if not target.exists() or target.read_text() != content:
490523
sys.stderr.write(f"drift: {target}\n")

0 commit comments

Comments
 (0)