windows-rdl: clang parsing of constants - #4198
Merged
Kenny Kerr (kennykerr) merged 10 commits intoApr 17, 2026
Merged
Conversation
Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/d565f191-ddab-4a91-bcf3-9fb969876fcd Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
…e cast, add rfind doc Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/d565f191-ddab-4a91-bcf3-9fb969876fcd Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
… null check Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/d565f191-ddab-4a91-bcf3-9fb969876fcd Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/855d181c-ede9-4f18-b5c5-62ab2754f509 Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/855d181c-ede9-4f18-b5c5-62ab2754f509 Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
Kenny Kerr (kennykerr)
April 17, 2026 16:19
View session
Kenny Kerr (kennykerr)
marked this pull request as ready for review
April 17, 2026 16:22
windows-rdl: clang parsing of constants
Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/dff75f45-fb1e-4889-8af8-c180b415d476 Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
…ore interpolation Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/dff75f45-fb1e-4889-8af8-c180b415d476 Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Collaborator
|
Copilot - be sure to cargo |
…ead code Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/36a3c04f-e61d-46fc-896f-b9e9121e3fab Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Contributor
Author
Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Kenny Kerr (kennykerr)
deleted the
copilot/add-windows-rdl-clang-support
branch
April 17, 2026 18:20
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.
Extends the
windows-rdlclang module to evaluate complex#defineconstant expressions that the existing token-based parser cannot handle (arithmetic, bitwise shifts, cross-macro references, etc.) using the industry-standard synthetic-enum technique.What's this all about?
The existing
#defineconstant parser used token pattern-matching and only handled simple literal patterns and typed casts. Expressions like#define NAME OTHER + 123or#define SHIFTED (FLAG << 16)were silently dropped.This PR implements the industry-standard "synthetic enum" approach (also used by
bindgen) as a second-pass fallback evaluator. For each macro that the token parser cannot handle, a dedicated anonymousenumis injected into an in-memory translation unit that#includes the original header. The C++ compiler evaluates the full constant expression — handling cross-macro references, arithmetic, bitshifts, and operator precedence — and the result is read viaclang_getEnumConstantDeclValue. Using oneenumper macro name combined withCXTranslationUnit_KeepGoingensures that one bad macro (e.g. a string macro that isn't a valid integer constant expression) does not prevent the others from being evaluated.Changes
cx.rs: AddedIndex::parse_unsaved()for creating in-memory translation units from aCXUnsavedFilebuffer usingCXTranslationUnit_KeepGoing.const.rs: AddedConst::evaluate_macros()— the synthetic-enum batch evaluator. Added F64 arm towrite_const_value(). Validates macro names are safe C identifiers before interpolating into generated C++ source.mod.rs: Two-pass macro evaluation: first pass collects macros whereparse_bodyreturnedNoneinto a pending list; second pass callsConst::evaluate_macros()for those candidates.const.h/const.rdl: Added complex-expression test cases — cross-macro addition (FACILITY_DEBUGGER + 10 → 11) and bitshift (FACILITY_DEBUGGER << 16 → 65536).Testing
cargo fmt— cleancargo clippy— clean (removed unusedEvalResult/Cursor::evaluate()dead code from an earlier approach)test_clangroundtrip test passes with the new complex-expression golden valuesenum.rdlgolden restored to its original#[repr(i32)]— an unintended change introduced during development has been reverted