Add #ifdef cfg support to XDR generator#515
Merged
leighmcculloch merged 26 commits intomainfrom Mar 25, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
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
CfgExprmodel 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.
sisuresh
approved these changes
Mar 24, 2026
Member
Author
|
Because this PR has been approved, I'm merging it and split off next changes into: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What:
Lexer tokenizes
#ifdef,#else, and#endifpreprocessor directives. Parser tracks a cfg condition stack and attaches aCfgExpr(Feature,Not,All) to each AST definition inside a preprocessor block, including#elif/#elsebranches and nested#ifdefblocks; extracted nested definitions inherit the enclosing cfg. Generator propagates cfg from AST definitions to all output structs and introducesTypeEnumEntryto carry per-variant cfg alongside the type name. Templates emit#[cfg(...)]before type definitions and all generatedimplblocks when a cfg expression is present.VARIANTS/VARIANTS_STRandvariants()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
#ifdefguards.Close #497