Skip to content

Add #ifdef cfg support to XDR generator#515

Merged
leighmcculloch merged 26 commits intomainfrom
ifdef
Mar 25, 2026
Merged

Add #ifdef cfg support to XDR generator#515
leighmcculloch merged 26 commits intomainfrom
ifdef

Conversation

@leighmcculloch
Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch commented Mar 16, 2026

What:

Lexer tokenizes #ifdef, #else, and #endif preprocessor directives. Parser tracks a cfg condition stack and attaches a CfgExpr (Feature, Not, All) to each AST definition inside a preprocessor block, including #elif/#else branches and nested #ifdef blocks; extracted nested definitions inherit the enclosing cfg. Generator propagates cfg from AST definitions to all output structs and introduces TypeEnumEntry to carry per-variant cfg alongside the type name. Templates emit #[cfg(...)] before type definitions and all generated impl blocks when a cfg expression is present. VARIANTS/VARIANTS_STR and variants() in the type enum template change from fixed-size arrays to slices to accommodate a variable number of cfg-gated variants.

Why:

XDR specification files use C preprocessor directives to conditionally include types based on compile-time features. Without this support, the generator had no way to represent or emit those conditions, making it impossible to produce correct Rust code from specs that rely on #ifdef guards.

Close #497

Copilot AI review requested due to automatic review settings March 16, 2026 09:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds Rust-based XDR parsing + code generation support for C-preprocessor-style conditional blocks (#ifdef/#ifndef/#elif/#else/#endif), propagating those conditions into emitted Rust via #[cfg(...)] so specs with conditional type inclusion can be generated correctly.

Changes:

  • Implemented lexer+parser support for conditional directives and a CfgExpr model attached to AST definitions (including nested extracted types).
  • Added type-resolution utilities (TypeInfo) used by the Rust generator for typedef/builtin resolution, const size resolution, and cycle detection.
  • Updated Rust generator templates and output model to emit #[cfg(...)] on generated items and to carry per-type cfg into the type-variant enum.

Reviewed changes

Copilot reviewed 32 out of 36 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
xdr-generator-rust/xdr-parser/src/lexer.rs Tokenizes preprocessor conditionals and keeps %... directives skipped.
xdr-generator-rust/xdr-parser/src/parser.rs Tracks a cfg stack and attaches CfgExpr to definitions; includes tests for directive behavior.
xdr-generator-rust/xdr-parser/src/ast.rs Introduces CfgExpr and adds optional cfg to all definition nodes.
xdr-generator-rust/xdr-parser/src/types.rs Adds TypeInfo + helpers for builtin/array classification and cycle detection.
xdr-generator-rust/generator/src/generator.rs Propagates cfg into generated outputs and into type enum entries.
xdr-generator-rust/generator/templates/type_enum.rs.jinja Emits cfg-gated type variant enums/arrays (now as slices).
Makefile / CONTRIBUTING.md Runs both Ruby and Rust generators and diffs outputs to validate migration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread xdr-generator-rust/xdr-parser/src/parser.rs Outdated
Comment thread xdr-generator-rust/generator/templates/type_enum.rs.jinja
Comment thread xdr-generator-rust/generator/src/types.rs
@leighmcculloch leighmcculloch enabled auto-merge (squash) March 23, 2026 10:17
@leighmcculloch
Copy link
Copy Markdown
Member Author

Because this PR has been approved, I'm merging it and split off next changes into:

Merged via the queue into main with commit f5363cf Mar 25, 2026
15 of 16 checks passed
@leighmcculloch leighmcculloch deleted the ifdef branch March 25, 2026 05:08
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.

Add support for #ifdef #else #endif in .x files

3 participants