Repository navigation
Description
Summary
Remove the remaining cc -E shell-outs from the c_import macro/preprocessor helper paths so the normal header-import pipeline is fully served by the compiler-owned libclang bridge. The spec makes self-contained C parsing a hard property of c_import; any helper that silently requires a host C compiler is an implementation gap that breaks clean hosts and cross-compilation.
Spec
§16.1 c_import (Compiler-owned C parsing / Cross-compilation):
- "
c_importuses With's compiler-owned libclang bridge, not a random system C compiler." - "The normal
c_importpath parses the header with this embedded Clang resource setup. It does not probe a system LLVM install, does not depend onllvm-config, and does not invokecc -Eas the core header-import mechanism." - "Any remaining shell-out to host tools for SDK discovery or macro/preprocessor helper paths is an implementation gap, not a language requirement."
This is also an AGENTS.md invariant ("Self-Contained Toolchain") and a recorded memory rule: the compiler must never shell out for its own operations.
Current state
Three helpers in src/compiler/ClangBridge.w still build cc argv lines and run them via with_exec_argv_capture → capture_command_stdout (ClangBridge.w:620-633):
- Macro dump fallback —
with_cimport_parse_macros(ClangBridge.w:2221) first tries the libclang pathcimport_collect_macros_from_libclang(ClangBridge.w:2162, usesCXTranslationUnit_DetailedPreprocessingRecordandcollect_macro_def) and returns early on success (ClangBridge.w:2227). Only when that fails does it fall back tocc ... -E -dM(argv built at ClangBridge.w:2243-2250) and parse the#definelines textually. - Preprocessed source —
with_cimport_preprocess_text(ClangBridge.w:2374) always shells out tocc ... -E(argv at ClangBridge.w:2390-2396). Its only caller is the migrator:src/CiMigrate.w:769(g_migrate_preprocessed_source). - Shared argv plumbing —
append_cc_common_args(ClangBridge.w:635-647) feeds-isysroot(fromget_sdk_path, see Darwin c_import SDK discovery still shells out to xcrun #349) and-Ipaths into theseccinvocations.
Note with_cimport_collect_object_macro_types (ClangBridge.w:2406) already uses libclang typeof-probes, not cc. Weak fallback stubs for non-c_import builds live in rt/cimport_stubs.w:376-390.
The gap: when libclang macro collection fails, or whenever the migrator needs preprocessed text, the compiler silently depends on a host cc on PATH. On a host without cc these paths return empty strings — a silent degradation, not a diagnostic (forbidden by "No Silent Fallbacks").
Implementation plan
- Delete the
cc -E -dMfallback inwith_cimport_parse_macros(ClangBridge.w:2227 onward). Ifcimport_collect_macros_from_libclangfails, return the (empty) session and let the caller surface a parse diagnostic — a failed libclang parse already means the header import itself will fail with a real error. Do not keep a second engine that can disagree with the primary one. - Harden the libclang macro path so it is the only engine: verify
collect_macro_defrecords function-like macro params and bodies with parity to what the-dMtext parser produced (names, fn_like flag, params, value text). ExtendMacroSessionpopulation from libclang token streams (clang_tokenizeover the macro definition extent) if any field is currently only available via-dMtext. - Reimplement
with_cimport_preprocess_texton libclang: parse the text with the existing session setup (same-isysroot/-resource-dir/-Iargument construction used at ClangBridge.w:2180-2210) and reconstruct expanded text viaclang_tokenizeon the translation unit, or — preferable if CiMigrate only needs macro-expansion of specific constructs — changesrc/CiMigrate.w:769to query the macro session instead of a fully preprocessed dump. Decide based on whatg_migrate_preprocessed_sourceis actually used for in CiMigrate (grep its uses before choosing). - Fail loudly: where a helper genuinely cannot produce output (libclang parse error), emit a diagnostic naming the header/source and the failing phase ("c_import: macro collection failed for : "), and propagate non-zero. No empty-string returns that callers treat as "no macros".
- Remove
append_cc_common_argsonce noccargv remains; keep the-isysroot/include logic only in the libclang session-argument builder. - Keep the
rt/cimport_stubs.wweak stubs in sync (they only matter for binaries without the bridge linked; signatures unchanged, so no Link.w change → no bootstrap hazard). Pure compiler-source change; no rt/Link/seed ordering concerns beyond the normal fixpoint gate.
Test plan
test/behavior/behav_c_import_macros_no_cc.w:use c_importof a local header (pattern:test/behavior/behav_c_import_offsetof.h) defining object-like constants (#define A 7, arithmetic#define B (A*2)) and a function-like macro#define MAX(a,b) ...; assert constants are imported and usable. Run the focused regression withPATHstripped ofcc(driver-level test or an internals test invoking the bridge with a sanitized environment) to prove no host-compiler dependency.//! expect-stdout: ok.test/compile_errors/err_c_import_macro_parse_failure.w: header text that fails to parse;//! expect-error:naming the header and the c_import failure (not a missing-ccsymptom).- Migrator coverage: a
test/migrate/case exercising the path behindwith_cimport_preprocess_text(macro-expanded C source) to prove the libclang reimplementation feeds CiMigrate equivalently. - Negative guard: grep-style internals check (or code review gate) that
src/compiler/ClangBridge.wcontains no"cc"argv construction; at minimum the acceptance checklist below.
Acceptance criteria
- No
cc/-Eargv construction remains insrc/compiler/ClangBridge.w(helpers at old lines 2243-2250 and 2390-2396 removed). -
with_cimport_parse_macrosis libclang-only; failure is a loud diagnostic, not an empty fallback result. -
with_cimport_preprocess_textno longer shells out (reimplemented or its CiMigrate caller redesigned). - Macro import behavior unchanged for the covered cases (constants, evaluated expressions per §16.2, function-like macro warnings).
- Diagnostics distinguish missing target headers/SDK inputs from toolchain-internal failures.
-
with build && with build :fixpoint && with build :testpass.
Dependencies
- Darwin c_import SDK discovery still shells out to xcrun #349 (same file; SDK discovery shell-out — shares
append_cc_common_args/get_sdk_pathplumbing; land Darwin c_import SDK discovery still shells out to xcrun #349 first or together so the-isysrootsource is the With-owned config). - --target flag and cross-compilation path (§18.5) #425 (--target cross-compilation) consumes the same "target inputs, not host tools" framing.
Traceability
Per the BDFL ruling for problematic requirement #5 (spec §16.1 cross-compilation paragraph): remaining shell-outs for macro/preprocessor helper paths are implementation gaps, not language requirements.
Activity
Requirements triage campaign: checkbox, verdict, and issue-link every…
Make c_import macro collection libclang-only (toward #348)
ehartford commentedon Jun 17, 2026
Partial progress (commit c630d94): removed the
cc -E -dMmacro-collection shell-out inwith_cimport_parse_macros— the normalc_importmacro path is now libclang-only (testbehav_c_import_macros_no_cc.w).Still open: the migrator-only
with_cimport_preprocess_textstill shells outcc -E. It feedsg_migrate_preprocessed_source, whichCImport.wreads (via#linemarkers) to reconstruct macro-expanded text at source locations for string-macro sequences and variable initializers. libclang's C API has no-Eequivalent, so replacing this needs either a C++ bridge preprocessor function (over the statically-linked clang frontend libs) or a migrator redesign that reconstructs text from libclang cursor source-ranges + constant evaluation instead of preprocessed text. That is a larger separate effort.Update Phase 8 docs: drop/move cluster + #357 first increment shipped
ehartford commentedon Jun 22, 2026
Scope finding: this is not a "finish-the-list" swap — it's an onion
Investigated the
cc -Eshell-out for replacement. The shell-out lives inwith_cimport_preprocess_text(src/compiler/ClangBridge.w): it writes the source to a temp file, runscc -E -x c <tmp>, and captures stdout. The output is fully preprocessed C with# <line> "<file>"line markers, consumed byci_preprocessed_text_at_location(src/CImport.w) to recover the macro-expanded text of a declaration at a given source location (for value/initializer extraction).Why it doesn't transplant onto libclang the way
-dMdid. The-dMmacro path could move in-process because libclang exposes macro definitions directly (with_cimport_parse_macros→cimport_collect_macros_from_libclang). The-Epath has no equivalent: libclang's C API has no "give me the preprocessed source text" call. Producing it in-process means reconstructing fully-expanded text fromclang_tokenize+ cursor annotations — re-implementing macro expansion, include splicing, and# line filefidelity well enough that the existing location-based line scanner still finds declarations. That is a substantial sub-project, not adding a curated entry.No runtime fallback to our own clang. The self-contained-toolchain invariant means the release binary embeds libclang statically and ships no clang/cc binary; we cannot shell out to our own clang at runtime either. So the only sound replacement is genuine in-process libclang reconstruction.
Current behavior is safe, not silently wrong. When libclang is present the strong impl shells out to system
cc; when absent, the weak stub returns""and consumers degrade gracefully (return""). So this is a self-contained-toolchain dependency to remove (systemcc), not a correctness bug.Recommendation: keep #348 open as its own scoped task (token-reconstruction of preprocessed text from libclang), not bundled into the Phase-8 finish-the-list. Deferred this run rather than grinding; no code changed.
ehartford commentedon Jul 2, 2026
Route investigation update (2026-07-02): the plan to add
wcb_preprocess_textto "the existing clang_bridge C++ shim" rests on a wrong premise —clang_bridge.ois compiled fromsrc/compiler/ClangBridge.w: the bridge is pure With over libclang's C API (89extern fn clang_*decls). There is no C++ source anywhere in the toolchain, and clang'sPreprocessor/-Eprinting is C++-only — libclang's C API does not expose it.So the real route choice is:
-Efidelity, but new build/link plumbing on the sacred link path and a new language in the compiler build.CXTranslationUnit_DetailedPreprocessingRecord: the four consumers need only two shapes — string-literal macro sequences ("A" NAME "B") and variable-initializer text — not arbitrary C. Object-like recursive expansion covers the observed uses; function-like/##cases fail loudly as untranslatable (consistent with existing migrator behavior).Verified context: the consumers are migrate-only (
g_migrate_preprocessed_sourceis set inci_migrate_file), golden shapes confirmed working today viawith migrate(string-macro sequence → merged literal; initializer recovery). (c') is smaller, stays pure-With, and honors the no-silent-fallback rule; (a') is the fidelity play. Needs a route ruling before implementation.ehartford commentedon Jul 2, 2026
Route ruling: (c') — the narrow With-side expander over the DetailedPreprocessingRecord, by the compass: it keeps the toolchain pure With (introducing the first C++ source is a structural change nothing currently demands), it covers the complete consumer inventory (string-literal macro sequences + initializer text — verified migrate-only), and unsupported shapes (function-like in string position,
##) fail loudly as untranslatable, which is the migrator's existing honest posture. (a') stays available later if a real consumer ever needs arbitrary-C fidelity; nothing in (c') forecloses it.Implementation order (next session): golden .c fixtures in test/migrate first (string-macro sequence, nested object-like, initializer shapes), then the expander feeding
g_migrate_preprocessed_source's consumers directly (dropping the line-marker text intermediary where possible), then deletecapture_command_stdout+ thecc -Eexec — no fallback.ehartford commentedon Jul 2, 2026
Closed by the commit above, on route (c') as ruled:
ci_expand_macros_in_text— recursive object-like expansion from the compiler-owned libclang macro session — replaces slicing acc -Edump. Every consumer already held the raw text it needed; the dump was only ever used to expand it.with_cimport_preprocess_text,capture_command_stdout,append_cc_common_args, and the line-marker parsers are deleted; no shell-out remains anywhere in the c_import/migrate path (the earlier-dMhalf was already libclang-only). Function-like invocations outside the dedicated stringify path fail loudly as before, per the no-silent-fallback rule. Golden migrations (string sequences, nested arithmetic initializers, stringify chains) verified byte-identical pre/post; goldens landed in test/migrate/. §16.1's "compiler-owned C parsing" is now true end to end — and the toolchain stays 100% With.docs: record #348 route ruling (pure-With expander; no C++ shim exists)
Remove the last cc -E shell-out: pure-With macro expansion (#348)
docs: record #348 completion
docs: sync implementation plan checkboxes with shipped #348/#357/#604/#…