Skip to content

Let custom generators declare their own source includes#100

Draft
kwabenantim wants to merge 1 commit into
developfrom
custom-generator-source-includes
Draft

Let custom generators declare their own source includes#100
kwabenantim wants to merge 1 commit into
developfrom
custom-generator-source-includes

Conversation

@kwabenantim

Copy link
Copy Markdown
Member

Fixes #99

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for custom generators to declare additional #include headers needed by code they emit, addressing Issue #99 so users don’t have to duplicate long source_includes lists in YAML when the generator already “knows” what it needs.

Changes:

  • Introduces Custom.get_source_includes() (default []) for custom generators to supply wrapper source includes.
  • Extends ClassWriter.includes_block() to append generator-provided includes (with deduplication).
  • Adds unit tests and README documentation covering the new generator include mechanism.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/test_class_writer.py Adds tests verifying generator-provided source includes are emitted, deduped, and skipped in common-include mode.
README.md Documents get_source_includes() and provides an example of declaring includes from a custom generator.
cppwg/writers/class_writer.py Appends generator-provided includes into the wrapper #include block.
cppwg/templates/custom.py Adds the new get_source_includes() hook to the Custom base class.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cppwg/writers/class_writer.py Outdated
Add a get_source_includes() hook to the Custom generator base (returns [] by
default). The class writer folds a generator's returned headers into the
wrapper's deduplicated #include block (non-common branch; a no-op with
common_include_file, supporting quoted and <...> forms).

This covers the one dependency auto_includes cannot: types a custom generator
names only in its emitted code - e.g. a template-method instantiation like
AddCellWriter<CellAgesWriter> built from a hard-coded list - which never appear
in the parsed signatures. The generator can now supply those headers itself,
keeping the type list and its includes in one place instead of duplicating them
under source_includes.

Treat every custom-generator hook as optional and route them through a shared
call_generator_hook() helper, so a generator that does not subclass Custom (and
so omits a hook) degrades to the default instead of raising AttributeError. This
keeps the new get_source_includes() backward compatible and makes all five hooks
(pre/def-code, module pre/code, source-includes) consistent.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment on lines +186 to +189
for header in call_generator_hook(
self.class_info.custom_generator_instance, "get_source_includes", []
):
add(self._format_include(header))
Comment on lines +637 to +650
def test_includes_block_generator_source_includes_skipped_in_common():
"""In common-include-file mode, generator includes are moot (collection has all)."""
class_info = _FakeClassInfo(
"Foo",
object(),
{"common_include_file": True},
"Foo.hpp",
generator=_SourceIncludeGen(["Writer.hpp"]),
)
writer = _make_writer(class_info)

assert writer.includes_block() == (
'#include "wrapper_header_collection.cppwg.hpp"\n'
)
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.

Allow custom templates to insert includes

2 participants