Implement partial include support#305
Conversation
There was a problem hiding this comment.
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
#includedirectives in the preprocessor layer and expose them viaIncludeDirective. - 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.
|
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. |
00be2ff to
feda940
Compare
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. Anyway I rewrote this PR and added tests. |
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.
|
Found bug. Updated PR with fix and a test. |
Currently
#includeheaders are dropped and not converted to D import statements.This PR implements partial support so that include headers will be converted from:
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.