Skip to content

windows-rdl: clang parsing of constants - #4198

Merged
Kenny Kerr (kennykerr) merged 10 commits into
masterfrom
copilot/add-windows-rdl-clang-support
Apr 17, 2026
Merged

windows-rdl: clang parsing of constants#4198
Kenny Kerr (kennykerr) merged 10 commits into
masterfrom
copilot/add-windows-rdl-clang-support

Conversation

Copilot AI commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Extends the windows-rdl clang module to evaluate complex #define constant 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 #define constant parser used token pattern-matching and only handled simple literal patterns and typed casts. Expressions like #define NAME OTHER + 123 or #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 anonymous enum is 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 via clang_getEnumConstantDeclValue. Using one enum per macro name combined with CXTranslationUnit_KeepGoing ensures 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: Added Index::parse_unsaved() for creating in-memory translation units from a CXUnsavedFile buffer using CXTranslationUnit_KeepGoing.
  • const.rs: Added Const::evaluate_macros() — the synthetic-enum batch evaluator. Added F64 arm to write_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 where parse_body returned None into a pending list; second pass calls Const::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 — clean
  • cargo clippy — clean (removed unused EvalResult/Cursor::evaluate() dead code from an earlier approach)
  • test_clang roundtrip test passes with the new complex-expression golden values
  • enum.rdl golden restored to its original #[repr(i32)] — an unintended change introduced during development has been reverted

Copilot AI and others added 5 commits April 17, 2026 15:21
…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>
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>
@kennykerr
Kenny Kerr (kennykerr) marked this pull request as ready for review April 17, 2026 16:22
@kennykerr Kenny Kerr (kennykerr) changed the title Add windows-rdl clang support for Win32-style C constants windows-rdl: clang parsing of constants Apr 17, 2026
Copilot AI and others added 2 commits April 17, 2026 17:12
@kennykerr

Copy link
Copy Markdown
Collaborator

Copilot - be sure to cargo cargo fmt and cargo clippy.

Comment thread crates/tests/libs/clang/roundtrip/enum.rdl Outdated

Copilot AI commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

Copilot - be sure to cargo cargo fmt and cargo clippy.

Done in 401cc01. cargo fmt was clean; cargo clippy flagged the unused EvalResult struct and Cursor::evaluate() method (dead code from an earlier approach), so those have been removed.

Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
@kennykerr
Kenny Kerr (kennykerr) merged commit 6f2c5ec into master Apr 17, 2026
26 checks passed
@kennykerr
Kenny Kerr (kennykerr) deleted the copilot/add-windows-rdl-clang-support branch April 17, 2026 18:20
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.

2 participants