Skip to content

postprocess: Preprocess the C source code passed to transforms#1846

Merged
thedataking merged 4 commits into
masterfrom
perl/postprocess-json-c-gpt-5.5-2026-04-23
Jul 8, 2026
Merged

postprocess: Preprocess the C source code passed to transforms#1846
thedataking merged 4 commits into
masterfrom
perl/postprocess-json-c-gpt-5.5-2026-04-23

Conversation

@thedataking

@thedataking thedataking commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

The CommentsTransform can fail if presented with a C and Rust function pair where some comments are inside directives that are compiled out during preprocessing and thus should not be transferred to the Rust.

We can simply run the C compiler on the C snippet and it will pick up compile commands (if any) and give us the preprocessed version back.

@thedataking thedataking requested a review from Crocodoctopus June 9, 2026 04:32
@thedataking thedataking force-pushed the perl/postprocess-json-c branch from 332513d to c1d7cd9 Compare June 9, 2026 08:19
@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch 2 times, most recently from 19142b4 to 24d771a Compare June 9, 2026 09:18
@thedataking thedataking marked this pull request as draft June 9, 2026 18:43
@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch from 24d771a to e1ff226 Compare June 10, 2026 09:34
@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch 3 times, most recently from 8ee5007 to 9d8d679 Compare June 11, 2026 06:30
@thedataking thedataking marked this pull request as ready for review June 11, 2026 07:03
@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch from 9d8d679 to 9a5f17f Compare June 15, 2026 04:47
@thedataking thedataking changed the base branch from perl/postprocess-json-c to master June 15, 2026 04:48
@thedataking thedataking requested review from fw-immunant and removed request for Crocodoctopus June 15, 2026 04:48

@fw-immunant fw-immunant 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.

Generally looks good; inline comments about a couple edge-cases and formatting in tests.

One larger question I have is how this interacts with configurability--is this an improvement for only the case where we fully remove all preprocessor logic (as opposed to lowering it to #[cfg]s with Hayroll?

Comment thread c2rust-transpile/src/translator/mod.rs
Comment thread c2rust-transpile/tests/snapshots/snapshots__c_decls@nh.c.snap Outdated
Comment thread c2rust-transpile/tests/snapshots.rs
The transpiler now emits an allow for clippy::missing_safety_doc, which
shifts the expected function spans by one line.
@thedataking

Copy link
Copy Markdown
Contributor Author

One larger question I have is how this interacts with configurability--is this an improvement for only the case where we fully remove all preprocessor logic (as opposed to lowering it to #[cfg]s with Hayroll?

Yes, I think it is the right for comment transfer today since it lets us validate transferred comments in more cases.

I haven't thought much about how this would work when lowering #[cfg]s with Hayroll. Couldn't the postprocessor just run before Hayroll? It would transfer comments for the active config, and Hayroll would handle comments in the branches it reintroduces.

@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch 2 times, most recently from 267c551 to e0bbf48 Compare July 8, 2026 00:39
Restructure *.c_decls.json from a flat identifier-to-snippet map into {definitions: {ident: {definition, preprocessed_definition}}}.

The AST exporter runs an in-process preprocessor-only pass per translation unit (clang -E -fdirectives-only -C equivalent): conditional directives are resolved, dropping inactive regions and their comments, while macro invocations and comments are preserved. Line markers map the output back to each function definition's source range; the main file is recognized by the spelling in the leading line marker, since marker paths are relative to the compilation directory. Functions whose mapping fails get a null preprocessed_definition.

Preprocessed snippets are keyed by C declaration id to avoid attaching a function body to another declaration with the same C spelling. Translation units with #line directives or non-monotonic main-file line markers skip preprocessed metadata and fall back to raw definitions. Clang versions without directives-only preprocessing emit a warning before using the legacy full-expansion path.
…prompts

Read both forms from the structured *.c_decls.json instead of running a clang subprocess per snippet. The CommentsTransform prompt includes the preprocessed text only when it differs from the original, so prompts (and llm-cache keys) are unchanged for directive-free functions.

Comment gating and response validation use comments from the preprocessed text plus comments on original preprocessor directive lines, so inactive-region comments are not transferred while directive-line comments are not dropped just because clang omits them from directives-only output.
Use a slotted dataclass for CommentsTransformPrompt and centralize the preprocessed-if-changed rule on CDefinition. This keeps prompt construction behavior unchanged while removing duplicated field and invariant handling.
@thedataking thedataking force-pushed the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch from e0bbf48 to 0fa5b6c Compare July 8, 2026 01:24
@thedataking thedataking merged commit e1c2970 into master Jul 8, 2026
11 checks passed
@thedataking thedataking deleted the perl/postprocess-json-c-gpt-5.5-2026-04-23 branch July 8, 2026 01:53
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.

2 participants