-
Notifications
You must be signed in to change notification settings - Fork 324
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
Add support for expressions like (0 << SHIFT) | BOOL
#425
Conversation
Moved similar patterns into a nested block, 1. The non exhaustive pattern match warnings are way more clearer and explicit now because it is on a specific smaller type than all of syn::Expr. This refactor showed that cbindgen doesn't handle byte strings and verbatim nodes. 2. Slightly shorter without sacrificing readability even though I added some newlines and comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch! This looks like the right direction, just some concerns, and we should fix the interaction with the renaming options, so that it doesn't break builds.
src/bindgen/ir/constant.rs
Outdated
// Handle only the simplest identifiers and error for anything else. | ||
// NOTE: Should this use `ir::Path` instead? | ||
if segments.len() == 1 { | ||
Ok(Literal::Expr(format!("{}", segments.last().unwrap().ident))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a different kind of Literal
, so that it can work correctly with export.prefix
and such configuration options. rename_for_config
should be called on it.
Please add a test for this to the prefix
tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a different kind of Literal
@emilio Just to be clear, did you mean I should add a variant to enum ir::constant::Literal
to handle names instead of reusing Expr? That would indeed be better. What do you think we should call it? Name/Reference/Segment/Path ?
Makes sense about renaming for config, will do that and add a test 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Literal::Path
would make sense to me, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Literal::Path(String) sounds good!
tests/expectations/constant.compat.c
Outdated
|
||
#define XFALSE (0 << SHIFT) | XBOOL | ||
|
||
#define XTRUE (1 << SHIFT) | XBOOL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty footgunny C code. We should probably parenthesize all binary ops when emmitting to a macro. That would also avoid the need for Literal::Paren
, I think, which would maybe make the patch a bit simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty footgunny C code. We should probably parenthesize all binary ops when emmitting to a macro.
I agree, this can go wrong in so many ways - happy to add a parenthesis around the whole expression if there is a binary op present. Should be able to peek into the structure inside the write function do this.
That would also avoid the need for Literal::Paren, I think, which would maybe make the patch a bit simpler.
I agree, adding that variant did make me cringe a bit. I prefer to not have it there.
Do you think I should just recursively call the value inside and avoid the wrapping all together? Something like this?
- syn::Expr::Paren(syn::ExprParen { ref expr, .. }) => Ok(Literal::Paren {
- inner: Box::new(Self::load(expr)?),
- }),
+ syn::Expr::Paren(syn::ExprParen { ref expr, .. }) => Self::load(expr),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, thtat should work, but let me know if it doesn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was slightly sceptical but this seems to have worked fine, working on a few tests now to make sure the config and renaming work correctly.
Ref: mozilla#412 This is a really naive implementation that only works in the simplest cases but is a decent starting point. Importantly, this patch doesn't handle dependencies correctly and works when `b` depends on `a` but fails the other way around because of the default lexicographical order.
To keep things simple, this patch always adds a () around binary operations in literals as suggested by @emilio here mozilla#425 (comment) Some tests obviously had to be regenerated, those are included as well.
@emilio Thanks for the initial review, v2 looks much better. The prefix tests seems to be renaming things alright, there is no Paren anymore and operator precedence around () seems to be working as expected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this looks neat, and much simpler, thanks!
Looks good to me :)
To keep things simple, this patch always adds a () around binary operations in literals as suggested by @emilio here #425 (comment) Some tests obviously had to be regenerated, those are included as well.
Thanks @emilio 🙏 |
134: Bump cbindgen from 0.10.0 to 0.11.1 r=MikailBag a=dependabot-preview[bot] Bumps [cbindgen](https://github.com/eqrion/cbindgen) from 0.10.0 to 0.11.1. <details> <summary>Changelog</summary> *Sourced from [cbindgen's changelog](https://github.com/eqrion/cbindgen/blob/master/CHANGES).* > ## 0.11.1 > > * More binary operators and expressions are supported. [mozilla/cbindgen#425](https://github-redirect.dependabot.com/eqrion/cbindgen/pull/425) > * More built-in bitflags operators. [mozilla/cbindgen#426](https://github-redirect.dependabot.com/eqrion/cbindgen/pull/426) > > ## 0.11.0 > > * Made rust char map to uint32_t. [mozilla/cbindgen#424](https://github-redirect.dependabot.com/eqrion/cbindgen/pull/424) > > ## 0.10.1 > > * Improved error message for missing config file. [mozilla/cbindgen#422](https://github-redirect.dependabot.com/eqrion/cbindgen/pull/422) > * Add missing header for char32_t. [mozilla/cbindgen#414](https://github-redirect.dependabot.com/eqrion/cbindgen/pull/414) </details> <details> <summary>Commits</summary> - [`e050442`](mozilla/cbindgen@e050442) Add CHANGES entry for v0.11.1 - [`c8f94b5`](mozilla/cbindgen@c8f94b5) v0.11.1 - [`230042b`](mozilla/cbindgen@230042b) Implement more operators for bitflags. - [`e0fe4e4`](mozilla/cbindgen@e0fe4e4) Add tests for expressions like `(0 << SHIFT) | BOOL` - [`df347d9`](mozilla/cbindgen@df347d9) Handle parenthesized literals like `(1 << 5)` - [`9a1f4b7`](mozilla/cbindgen@9a1f4b7) Handle identifiers in const declarations like SHIFT in `1 << SHIFT` - [`b1a92e2`](mozilla/cbindgen@b1a92e2) Handle all binary operations and make pattern match exhaustive - [`d996e7c`](mozilla/cbindgen@d996e7c) Refactor pattern match for Syn::Expr::Lit - [`9b7bb8f`](mozilla/cbindgen@9b7bb8f) Update docs to account for the latest char => char32_t change. - [`0355c3c`](mozilla/cbindgen@0355c3c) v0.11.0 - Additional commits viewable in [compare view](mozilla/cbindgen@v0.10.0...v0.11.1) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=cbindgen&package-manager=cargo&previous-version=0.10.0&new-version=0.11.1)](https://dependabot.com/compatibility-score.html?dependency-name=cbindgen&package-manager=cargo&previous-version=0.10.0&new-version=0.11.1) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com): - Update frequency (including time of day and day of week) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) </details> 135: Bump nix from 0.15.0 to 0.16.0 r=MikailBag a=dependabot-preview[bot] Bumps [nix](https://github.com/nix-rust/nix) from 0.15.0 to 0.16.0. <details> <summary>Changelog</summary> *Sourced from [nix's changelog](https://github.com/nix-rust/nix/blob/master/CHANGELOG.md).* > ## [0.16.0] - 1 December 2019 > ### Added > - Added `ptrace::seize()`: similar to `attach()` on Linux > but with better-defined semantics. > (#[1154](https://github-redirect.dependabot.com/nix-rust/nix/pull/1154)) > > - Added `Signal::as_str()`: returns signal name as `&'static str` > (#[1138](https://github-redirect.dependabot.com/nix-rust/nix/pull/1138)) > > - Added `posix_fallocate`. > ([#1105](https://github-redirect.dependabot.com/nix-rust/nix/pull/1105)) > > - Implemented `Default` for `FdSet` > ([#1107](https://github-redirect.dependabot.com/nix-rust/nix/pull/1107)) > > - Added `NixPath::is_empty`. > ([#1107](https://github-redirect.dependabot.com/nix-rust/nix/pull/1107)) > > - Added `mkfifoat` > ([#1133](https://github-redirect.dependabot.com/nix-rust/nix/pull/1133)) > > - Added `User::from_uid`, `User::from_name`, `User::from_gid` and > `Group::from_name`, > ([#1139](https://github-redirect.dependabot.com/nix-rust/nix/pull/1139)) > > - Added `linkat` > ([#1101](https://github-redirect.dependabot.com/nix-rust/nix/pull/1101)) > > - Added `sched_getaffinity`. > ([#1148](https://github-redirect.dependabot.com/nix-rust/nix/pull/1148)) > > - Added optional `Signal` argument to `ptrace::{detach, syscall}` for signal > injection. ([#1083](https://github-redirect.dependabot.com/nix-rust/nix/pull/1083)) > > ### Changed > - `sys::termios::BaudRate` now implements `TryFrom<speed_t>` instead of > `From<speed_t>`. The old `From` implementation would panic on failure. > ([#1159](https://github-redirect.dependabot.com/nix-rust/nix/pull/1159)) > > - `sys::socket::ControlMessage::ScmCredentials` and > `sys::socket::ControlMessageOwned::ScmCredentials` now wrap `UnixCredentials` > rather than `libc::ucred`. > ([#1160](https://github-redirect.dependabot.com/nix-rust/nix/pull/1160)) > > - `sys::socket::recvmsg` now takes a plain `Vec` instead of a `CmsgBuffer` > implementor. If you were already using `cmsg_space!`, then you needn't worry. > ([#1156](https://github-redirect.dependabot.com/nix-rust/nix/pull/1156)) > > - `sys::socket::recvfrom` now returns > `Result<(usize, Option<SockAddr>)>` instead of `Result<(usize, SockAddr)>`. ></tr></table> ... (truncated) </details> <details> <summary>Commits</summary> - See full diff in [compare view](https://github.com/nix-rust/nix/commits/v0.16.0) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=nix&package-manager=cargo&previous-version=0.15.0&new-version=0.16.0)](https://dependabot.com/compatibility-score.html?dependency-name=nix&package-manager=cargo&previous-version=0.15.0&new-version=0.16.0) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com): - Update frequency (including time of day and day of week) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) </details> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
The PR adds support for parenthesized expressions, identifiers and bit wise or operators. Fixes #412.
See individual commits for details.
When handling identifiers, I've resorted to a naive impl like this
Let me know if this is not OK, I'm not sure if
ir::Path
is really necessary in this case.