Skip to content

Commit 01137b8

Browse files
Peefyclaude
andcommitted
test(ast): diff the c and c++ decoders against the golden capture
Both were listed in `bindings.rb` as unrunnable, and both reasons were wrong — as descriptions of the bindings, not just of this machine. The c one said a dumper "would have to re-encode the same traversal as the decoder, which is a second decoder to get wrong rather than a check", and the cpp one said the C++ AST was "the C header's union tree under RAII wrappers". Neither is true of the repository as it stands. `cpp` has its own 1987-line typed AST in `cpp/include/kcl_ast.hpp` with its own inline decoder and a `virtual const char* tag()` per variant, sharing nothing with the C header but the language. Neither has a serializer, so both are `:reflect`, and neither has runtime reflection, so both dumpers are a second hand-written statement of what the header declares — which is the same bargain the C++ `cmake` build already makes and the diff is what catches the two drifting. The reduction counts say the two models do land on the wire. c and cpp differ only where the C decoder's grouping is genuinely deeper than the Rust one: c fires R8 7 more times than cpp, all of it `Expr::Identifier`, `Expr::Target` and `Expr::Keyword`, whose newtype payloads the C struct holds in a named field of its own where the C++ variant holds them directly. c fires R7 3 more times, once per `MemberOrIndex` in the golden, because the C decoder unpacks that union into a `kind` plus a `member` or an `index` field and the wire's `value` key has no counterpart in it. Three naming decisions in the c dumper are worth stating here rather than only in its header, because each is a place where the dump could have been made to agree by renaming something the binding owns: * `@cls` is the C name with the `kcl_` prefix and the `_t` suffix off. That decoration is a convention, not part of the name, and stripping it is what keeps 24 C spellings out of `CLASS_TAGS` — a table whose entries only undo a prefix is not an independent claim about anything. CLASS_TAGS gains two entries instead, for the arms the C decoder names after the union field rather than the variant. * `LiteralType`'s four arms and `NumberLitValue`'s two are an enum and a `value` field inside a union, not types, so they carry `@tag` and nothing else. For `LiteralType::Int` the payload's fields are spread as siblings of `value` rather than nested one level deeper as the struct groups them, which is R15. * `MemberOrIndex` keeps its own `member` / `index` field names instead of being re-tagged into the wire's `value`. R7 (ii) is the rule that reconciles a payload the binding kept in a single wrapper field of its own, and firing it is the evidence the field really is the payload's. Neither binding introduces a real difference against the golden. Both reach 1012/1012 objects and 3657 leaves, and both probes now resolve the same three compiler names the scripts fall back through, so a runner that ships only clang or only g++ is not reported as a missing toolchain. The CI legs need no toolchain step: both decoders are self-contained, so `hack/dump/{c,cpp}.sh` compile their sources against a stock cc / c++ already on the runner image — not `cargo build`, not `libkcl`, and not either binding's Makefile or cmake, which build the protobuf and JNI layers around the parts that were never in question. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2761803 commit 01137b8

7 files changed

Lines changed: 2480 additions & 6 deletions

File tree

‎.github/workflows/ast-diff-test.yaml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ on:
4343
- "dotnet/KclLib.AST/**"
4444
- "zig/src/ast/**"
4545
- "swift/Sources/KclLibAST/**"
46+
# C and C++ have one decoder each, in a header and its implementation.
47+
# `kcl_ast_json.hpp` is the C++ header's own JSON parser and is the
48+
# other half of `kcl_ast.hpp`'s decoder; `kcl_lib_ast.h` is the whole
49+
# public C contract and `kcl_lib_ast.c` its implementation.
50+
- "c/include/kcl_lib_ast.h"
51+
- "c/lib/kcl_lib_ast.c"
52+
- "cpp/include/kcl_ast.hpp"
53+
- "cpp/include/kcl_ast_json.hpp"
4654
- ".github/workflows/ast-diff-test.yaml"
4755
workflow_dispatch:
4856

@@ -69,6 +77,15 @@ jobs:
6977
- binding: dart
7078
- binding: zig
7179
- binding: dotnet
80+
# C and C++ need no toolchain step at all: both decoders are
81+
# self-contained, and `hack/dump/{c,cpp}.sh` compile their sources
82+
# against a stock cc / c++ on the runner image. Not `cargo build`
83+
# (neither decoder touches the runtime), not `libkcl` (both are
84+
# plain C and C++ over an embedded JSON parser), and not either
85+
# binding's cmake / Makefile, which build the protobuf and JNI
86+
# layers around them.
87+
- binding: c
88+
- binding: cpp
7289
# The only binding that cannot share the default runner. The Swiftly
7390
# installer behind swift-actions/setup-swift@v3 cannot provision a
7491
# Linux runner — the same limitation that keeps swift-test.yaml off

‎hack/ast_diff/bindings.rb‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,31 @@ def bindings
271271
"payload and are written out as `@raw` (R11)."
272272
),
273273

274+
# ----------------------------------------------------------------- c
275+
Binding.new(
276+
name: "c",
277+
mode: :reflect,
278+
probe: lambda {
279+
# `hack/dump/c.sh` honours `CC` and falls back to `cc`, so the
280+
# probe asks for the same three names in the same order — probing
281+
# `cc` alone would report "toolchain missing" on a runner that
282+
# only ships clang.
283+
cc = which("cc") || which("clang") || which("gcc")
284+
return "no `cc`, `clang` or `gcc` on PATH" unless cc
285+
286+
run_probe([cc, "--version"])
287+
},
288+
argv: ->(golden, out) { ["bash", "hack/dump/c.sh", golden, out] },
289+
extra_ok: {},
290+
note: "Hand-written tagged unions in `c/include/kcl_lib_ast.h` with its " \
291+
"own embedded JSON parser, walked by hand: C has no runtime " \
292+
"reflection, so `hack/dump/c/Dump.c` is a second statement of what " \
293+
"the header declares and the diff is what catches the two " \
294+
"drifting. `@cls` is the union field name. The `Pos` wrapper nests " \
295+
"its position under `pos`; the seven enums the header never names " \
296+
"are given their Rust variant names by tables in the dumper."
297+
),
298+
274299
# -------------------------------------------------------------- swift
275300
Binding.new(
276301
name: "swift",
@@ -281,6 +306,31 @@ def bindings
281306
note: "Enum/struct decoder; position is nested under `NodeRef.position`."
282307
),
283308

309+
# ---------------------------------------------------------------- cpp
310+
Binding.new(
311+
name: "cpp",
312+
mode: :reflect,
313+
probe: lambda {
314+
# Same shape as c's probe: `hack/dump/cpp.sh` honours `CXX` and
315+
# falls back to `c++`, so ask for the same three names in the same
316+
# order rather than reporting a skip on a runner that only ships
317+
# `g++`.
318+
cxx = which("c++") || which("g++") || which("clang++")
319+
return "no `c++`, `g++` or `clang++` on PATH" unless cxx
320+
321+
run_probe([cxx, "--version"])
322+
},
323+
argv: ->(golden, out) { ["bash", "hack/dump/cpp.sh", golden, out] },
324+
extra_ok: {},
325+
note: "Header-only typed AST (`cpp/include/kcl_ast.hpp`, 1987 lines) " \
326+
"with its own decoder, so the walk dispatches on the " \
327+
"`virtual const char* tag()` each variant carries. C++ has no " \
328+
"runtime reflection, so the field list is written out per " \
329+
"class in `hack/dump/cpp/Dump.cpp` — that file is a second " \
330+
"statement of what the header declares, and the diff is what " \
331+
"catches the two drifting. Position is nested under `Node::pos`."
332+
),
333+
284334
# ---------------------------------------------------------------- zig
285335
Binding.new(
286336
name: "zig",
@@ -325,12 +375,7 @@ def self.unrunnable
325375
"loader is Jackson over com.kcl.ast.* — it needs a JVM to run at all",
326376
"kotlin" => "no JRE on this machine; same Jackson loader, compiled by Maven",
327377
"go" => "the Go binding has no typed AST package at all — go/api/client.go exposes " \
328-
"ast_json as an opaque string, so there is no decoder to run the golden through",
329-
"c" => "hand-written tagged unions in c/include/kcl_lib_ast.h with no reflection and " \
330-
"no serializer; a dumper would have to re-encode the same traversal as the " \
331-
"decoder, which is a second decoder to get wrong rather than a check",
332-
"cpp" => "same as c: the C++ AST is the C header's union tree under RAII wrappers, " \
333-
"with no reflection and no writer"
378+
"ast_json as an opaque string, so there is no decoder to run the golden through"
334379
}.freeze
335380
end
336381
end

‎hack/ast_diff/canonical.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ module Canonical
255255
"keywordexpr" => "Keyword",
256256
"argumentsexpr" => "Arguments",
257257
"compare" => "Compare",
258+
# The C binding names these two arms after the union field they sit in
259+
# (`c/include/kcl_lib_ast.h`, `kcl_expr_data.u.subscript_expr` and
260+
# `.compare_expr`) rather than after the variant, so they arrive with
261+
# the `Expr` suffix the other bindings' class names carry. Same two
262+
# tags, spelled the way that binding spells them.
263+
"subscriptexpr" => "Subscript",
264+
"compareexpr" => "Compare",
258265
"numberlit" => "NumberLit",
259266
"stringlit" => "StringLit",
260267
"nameconstantlit" => "NameConstantLit",
@@ -301,6 +308,13 @@ module Canonical
301308
"keyword" => nil,
302309
"numberlitvalue" => nil,
303310
"literaltypevalue" => nil,
311+
# The C++ header calls the same union `LiteralValue`
312+
# (`cpp/include/kcl_ast.hpp`, `struct LiteralValue`) rather than
313+
# `LiteralTypeValue`, so its `@cls` arrives under this spelling. Same
314+
# shape and same reason as the two above it: it is one struct per
315+
# *union* with a `kind`, not one per arm, so the wire's tag sits in a
316+
# field of its own and there is nothing for CLASS_TAGS to key on.
317+
"literalvalue" => nil,
304318
# `ast.rs:1909` `pub struct IntLiteralType { value, suffix }` — the
305319
# payload of `LiteralType::Int`. The tag `Int` belongs to the *enum
306320
# variant* wrapping it, so the struct itself is untagged: the wire is

‎hack/dump/c.sh‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Build and run the C binding's AST dumper.
3+
#
4+
# hack/dump/c.sh <golden.json> <out.json>
5+
#
6+
# Nothing is linked against `libkcl`. `c/lib/kcl_lib_ast.c` is standalone C
7+
# — the JSON parser is embedded in the header, the AST is plain structs and
8+
# an arena — so a dump needs a C compiler, the include directory and that
9+
# one source file. Not `cargo build --release`, not `libkcl.dylib`, not
10+
# `c/Makefile`'s protobuf sources, none of which the decoder touches.
11+
#
12+
# The *source* is compiled rather than an object reused from `c/build/`, for
13+
# the same reason `hack/dump/swift.sh` and `hack/dump/java.sh` compile
14+
# theirs: a build artifact may be older than the code, and a harness that
15+
# silently checked yesterday's decoder would report agreement for a decoder
16+
# that no longer exists.
17+
#
18+
# No `-Werror`: a compiler stricter than the one the binding is built with
19+
# would turn a warning in `kcl_lib_ast.c` itself into a harness failure,
20+
# which is not a finding about the decoder. Warnings the *dumper* earns are
21+
# still visible in the log.
22+
set -euo pipefail
23+
24+
if [ "$#" -ne 2 ]; then
25+
echo "usage: hack/dump/c.sh <golden.json> <out.json>" >&2
26+
exit 2
27+
fi
28+
29+
golden="$1"
30+
out="$2"
31+
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
32+
cd "$root"
33+
34+
cc="${CC:-cc}"
35+
if ! command -v "$cc" >/dev/null 2>&1; then
36+
echo "no \`${cc}\` on PATH (set CC to pick a different compiler)" >&2
37+
exit 3
38+
fi
39+
if ! "$cc" --version >/dev/null 2>&1; then
40+
echo "\`${cc} --version\` fails: there is a ${cc} on PATH but it cannot run" >&2
41+
"$cc" --version >&2 || true
42+
exit 3
43+
fi
44+
45+
build="$(mktemp -d "${TMPDIR:-/tmp}/kcl-ast-dump-c.XXXXXX")"
46+
trap 'rm -rf "$build"' EXIT
47+
48+
"$cc" -std=c99 -Wall -Wextra \
49+
-Ic/include \
50+
-o "$build/dump" \
51+
hack/dump/c/Dump.c c/lib/kcl_lib_ast.c
52+
53+
"$build/dump" "$golden" "$out"

0 commit comments

Comments
 (0)