Let custom generators declare their own source includes#100
Draft
kwabenantim wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
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.
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>
kwabenantim
force-pushed
the
custom-generator-source-includes
branch
from
July 21, 2026 16:54
fc818b8 to
41b80ed
Compare
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' | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #99