Skip to content

python: emit doc comments from C++ headers into generated bindings - #129

Open
tmc wants to merge 4 commits into
ml-explore:mainfrom
tmc:doc-comments
Open

tmc wants to merge 4 commits into
ml-explore:mainfrom
tmc:doc-comments

Conversation

@tmc

@tmc tmc commented Aug 24, 2026

Copy link
Copy Markdown

cxxheaderparser discards comments, so the generated C API carried no documentation even though the C++ headers document most public functions. Add a small extractor that recovers /** */ blocks from the raw header text and matches each to its declaration by qualified name and parameter arity (exact arity only — no lenient fallback, which would steal a sibling overload's doc), then emit the block above each declaration in generated headers.

This includes a basic sanitizer to prevent c++-isms from being emitted in comments.

tmc added 4 commits August 24, 2026 16:02
cxxheaderparser discards comments, so the generated C API carried no
documentation even though the C++ headers document most public
functions. Add a small extractor that recovers /** */ blocks from the
raw header text and matches each to its declaration by qualified name
and parameter arity (exact arity only — no lenient fallback, which
would steal a sibling overload's doc), then emit the block above each
declaration in generated headers.

Implementation files are unchanged; this is additive documentation for
downstream FFI consumers.
MLX's C++ headers document some functions with @code blocks written in
C++ (auto, std::vector, ->). Carried verbatim into C bindings these are
meaningless to C consumers.

Add a conservative sanitizer in docextract.py: @code fences containing
C++-only tokens are dropped and replaced by a single 'Code example
omitted: written in C++.' note; consecutive omissions merge into one
note; dangling lead-in sentences ending in ':' above an omitted block
are removed; blank runs collapse. Blocks without C++ tokens pass
through verbatim. Nothing is silently rewritten — every application of
a rule is recorded.
Regenerating a header without passing --docstring silently dropped its
defgroup banner. When the flag is missing, reuse the title from the
previously generated mlx/c/<header>.h if one exists, so regeneration
reproduces the committed form.
MLX's C++ headers open with a structural \defgroup comment directly
above the first declaration. Treating it as that declaration's doc
re-emitted a duplicate group banner mid-header. Structural commands
(\defgroup/\addtogroup/\ingroup/@{/@}) are now ignored by the
extractor; c.py already emits the file-level group from --docstring.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant