Skip to content

Implement partial include support#305

Open
davispuh wants to merge 3 commits into
jacob-carlborg:masterfrom
davispuh:includes
Open

Implement partial include support#305
davispuh wants to merge 3 commits into
jacob-carlborg:masterfrom
davispuh:includes

Conversation

@davispuh

Copy link
Copy Markdown

Currently #include headers are dropped and not converted to D import statements.
This PR implements partial support so that include headers will be converted from:

#include <b.h>

to:

// FIXME: import b;

This allows to later process and fix them (for example with sed).
This is important when headers contain complicated inter-dependencies and without this information it's difficult to know which other files would need to be imported.

Copilot AI review requested due to automatic review settings March 19, 2026 21:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds partial #include preservation by converting include directives into emitted D imports (or // FIXME: import ...; placeholders) so downstream tooling can resolve dependencies later.

Changes:

  • Parse #include directives in the preprocessor layer and expose them via IncludeDirective.
  • Feed parsed include directives into IncludeHandler, and always emit placeholder // FIXME: import ...; lines for unhandled includes.
  • Update unit tests’ expected translated output to include the newly emitted imports.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
dstep/translator/Preprocessor.d Introduces IncludeDirective and parseInclude to recognize #include directives.
dstep/translator/Context.d Collects parsed include directives and forwards them to IncludeHandler.
dstep/translator/IncludeHandler.d Adjusts include ingestion and import emission; adds sys/ioctl mapping and emits // FIXME: import ...; for unknown includes.
dstep/translator/Options.d Removes keepUntranslatable option (unhandled includes now always emitted).
tests/unit/issues/Issue85.d Updates expected output to include core.stdc.stddef import.
tests/unit/issues/Issue8.d Updates expected output to include core.stdc.stdint import.
tests/unit/issues/Issue199.d Updates expected output to include core.stdc.stdint import.
tests/unit/UnitTests.d Updates expected outputs to include stdc imports (incl. wchar_) when headers are present.
tests/unit/MacroTranslTests.d Updates expected output to include core.stdc.stdint import.

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

Comment thread dstep/translator/IncludeHandler.d Outdated
Comment thread dstep/translator/Preprocessor.d
Comment thread dstep/translator/Preprocessor.d
@jacob-carlborg

jacob-carlborg commented Mar 21, 2026

Copy link
Copy Markdown
Owner

Unfortunately there's no one-to-one mapping of header files and D modules, that's why handling of includes is incomplete. I'm not sure this is the right approach. I've been thinking about supporting generating bindings for a whole directory. Then it would be possible to look up where a declaration is located and add imports based on that instead of the includes.

There's also no new test case added.

@davispuh davispuh force-pushed the includes branch 2 times, most recently from 00be2ff to feda940 Compare June 29, 2026 21:16
@davispuh

Copy link
Copy Markdown
Author

Unfortunately there's no one-to-one mapping of header files and D modules, that's why handling of includes is incomplete. I'm not sure this is the right approach. I've been thinking about supporting generating bindings for a whole directory. Then it would be possible to look up where a declaration is located and add imports based on that instead of the includes.

I looked into that and sadly it's not really possible to do it that way as show in #309. And even then how would you handle includes that are outside of specified dir so still would need something like this.
I think proper support can be added with dfix. That is after all .d files have been created, point dfix to that directory to automatically cleanup imports => remove unused ones and add missing ones by searching across files. Currently it doesn't have such feature but to me that seems like more realistic solution.

Anyway I rewrote this PR and added tests.
Note I included da7bdb9 commit from #297 here aswell.

davispuh added 3 commits July 3, 2026 23:22
Moved from IncludeGraphTests.d
Currently `#include` headers are dropped and not converted to D import statements.
This PR implements partial support so that include headers will be converted from:
```c
 #include <b.h>
```
to:
```d
// FIXME: import b;
```
This allows to later process and fix them (for example with sed).
This is important when headers contain complicated inter-dependencies and
without this information it's difficult to know which other files would need to be imported.
@davispuh

davispuh commented Jul 3, 2026

Copy link
Copy Markdown
Author

Found bug. Updated PR with fix and a test.

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.

3 participants