Skip to content

Emit cfg helper macros for struct-literal #define constants - #1181

Open
youknowone wants to merge 1 commit into
mozilla:mainfrom
youknowone:cfg-define-macro
Open

Emit cfg helper macros for struct-literal #define constants #1181
youknowone wants to merge 1 commit into
mozilla:mainfrom
youknowone:cfg-define-macro

Conversation

@youknowone

@youknowone youknowone commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

another trial of #955

…a#955)

`#if` cannot appear inside a macro replacement list, so `#[cfg]` on
struct fields was silently dropped when a struct-literal constant was
emitted as a `#define`. Emit one `__CBINDGEN_CFG_<condition>(...)`
helper per condition that appears in such a literal and wrap each
gated field initializer, including its trailing comma, in it.

The macro name encodes the condition tree with node tags and lengths
so structurally different conditions never share a name. Nested
struct literals inside a `#define` in C++ also use the macro form
instead of the constexpr `#if` form.

Assisted-by: Claude
@youknowone youknowone changed the title Emit cfg helper macros for struct-literal #define constants (#955) Emit cfg helper macros for struct-literal #define constants Aug 29, 2026

@youknowone youknowone left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@emilio This is a replacement of #956

Comment thread src/bindgen/ir/cfg.rs
Comment on lines +281 to +283
out.push('D');
out.push_str(&define.len().to_string());
out.push('_');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

a little bit of mangling to avoid accidental name collision. please tell me a better approach if this is not the best

@youknowone
youknowone marked this pull request as ready for review August 29, 2026 12:29
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.

1 participant