postprocess: Preprocess the C source code passed to transforms#1846
Conversation
332513d to
c1d7cd9
Compare
19142b4 to
24d771a
Compare
24d771a to
e1ff226
Compare
8ee5007 to
9d8d679
Compare
9d8d679 to
9a5f17f
Compare
fw-immunant
left a comment
There was a problem hiding this comment.
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?
The transpiler now emits an allow for clippy::missing_safety_doc, which shifts the expected function spans by one line.
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 |
267c551 to
e0bbf48
Compare
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.
e0bbf48 to
0fa5b6c
Compare
The
CommentsTransformcan 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.