Skip to content

Conversation

@fw-immunant
Copy link
Contributor

@fw-immunant fw-immunant commented Jan 24, 2026

When we integrate with Hayroll, we'll want to merge C decl maps from different runs of the transpiler in which different, overlapping subsets of code are enabled via the preprocessor. In prototyping this I ran into cases where we attribute all preprocessor-disabled code immediately preceding a function to that function's source range.

This PR adds logic to our decl map generation to avoid this misattribution.

While we're at it, fix a bug causing reordering of C decls in c_decls.json files: the decl map should always match the order in the C source.

This also improves handling of multiple declarations (e.g. int a, b;), which libmcs makes particular use of.

one transformation step used a non-order-preserving HashMap by mistake
we don't need to check this assertion again after only increasing the larger side of its comparison
this will serve as the anchor from which to search backwards for indications that we should cut short the prefix of the source range associated with this decl
@fw-immunant fw-immunant force-pushed the fw/c-decls-ifdefs branch 4 times, most recently from dd334fc to 097a37d Compare January 26, 2026 18:07
@fw-immunant fw-immunant changed the title Improve C decl map handling of #ifdefs Improve C decl map handling of #ifdefs and multiple declarations Jan 26, 2026
note, this is not all preprocessor directives but only those likely to separate this declaration from disabled code

this allows us to emit more precise source ranges when transpiling files in different ifdef configurations
maybe this additional organization makes things clearer
our Rust is not new enough for the latter
in the case of top-level multiple declarations like `int a, b;`, these declarations share the same begin loc. in this case we want to process a before b, so break ties with their end loc
this is not necessarily the case for multiple declarations like `int a, b;`, where `earliest_begin_loc` takes on the end of the first declaration. it may be the case that we should instead enforce this invariant by moving `earliest_begin_loc` backwards instead of moving `begin_loc` forwards, depending on what best suits c2rust-postprocess
@kkysen kkysen self-requested a review January 27, 2026 00:03
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