Skip to content

Keep radix for integer literals in generated bindings #3237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

miikkas
Copy link

@miikkas miikkas commented Jun 18, 2025

The radices of integer literals in the input code, including both C and C++, are retained in the generated bindings.

  • The radix of a C/C++ integer literal in constant, enum, or macro definition is parsed. If it can't be determined, the code falls back to not doing anything about the radix.
  • This can be currently optionally enabled with Builder (.keep_integer_radices(true)) or CLI (--keep-integer-radices) switches. For now, the default is to have the feature off.
  • During Rust code generation, the radix of an integer literal – if it has been determined – is then used to output that integer's value represented in that number base.
  • New testing is added in the form of unit tests for parsing the radix of a Clang CXToken_Literal token and for outputting the tokens during codegen, and also as a new test header file paired with its matching expected Rust binding output.
  • Existing tests with hexadecimal or octal integers are updated to keep the corresponding radices of those values.

There are currently three commits, which I'll squash after review as per the contribution guidelines.

Resolves #3236.

@miikkas miikkas force-pushed the keep-radix branch 3 times, most recently from 8f8d206 to 7054e0f Compare June 19, 2025 17:32
@miikkas miikkas marked this pull request as ready for review June 19, 2025 17:35
@miikkas
Copy link
Author

miikkas commented Jun 19, 2025

I have now included additional testing to the PR.

Regarding the 13 failing existing tests: It seems that the differences are due to bindgen now generating, as intended, Rust literals using hexadecimal representation, while the values themselves have correctly stayed the same. Should I fix the expected code accordingly?

@miikkas
Copy link
Author

miikkas commented Jun 21, 2025

The test I added was changed to use C++14 instead of C23, as it seems that the latter is not available for clang 9.0, which IIUC is used in the CI.

I have changed the expected side of the previously failing existing tests. Those changes are currently included in a separate commit in case that makes review less cumbersome. I will squash it later as instructed in the contribution guidelines.

@miikkas miikkas marked this pull request as draft July 6, 2025 09:06
@miikkas miikkas force-pushed the keep-radix branch 4 times, most recently from 9064c00 to 2733cf6 Compare July 6, 2025 13:38
@miikkas miikkas marked this pull request as ready for review July 6, 2025 13:44
@miikkas
Copy link
Author

miikkas commented Jul 6, 2025

I have made the solution a lot more robust and included further unit testing.

r? @emilio

@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

Error: The feature assign is not enabled in this repository.
To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

miikkas

This comment was marked as resolved.

@miikkas miikkas force-pushed the keep-radix branch 2 times, most recently from fc8e1d6 to 91928a9 Compare July 12, 2025 05:36
@miikkas
Copy link
Author

miikkas commented Jul 15, 2025

To mitigate possible churn and not force the feature on all users immediately, I've added an option defaulting to off as per #3236 (comment).

@miikkas miikkas force-pushed the keep-radix branch 2 times, most recently from a370dde to df4cd69 Compare July 19, 2025 12:46
miikkas added 3 commits July 20, 2025 08:06
Use the different radix literals for integers that were used in the original C
and C++ header files, now that it is supported. The new values were confirmed
by hand to be the same as the original ones - they are just represented using
the Rust equivalent of the original form.
- Gate the integer literal radix retaining functionality behind option
  `keep_integer_radices`, defaulting to `false` for now.
- Add the new `--keep-integer-radices` flag to test headers that are
  affected by it.
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.

[Feature Request] Keep the integer literal radices of C and C++ in generated Rust
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy