Skip to content
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

Support casts in macros. #2268

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 58 additions & 105 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ testing_only_docs = []
testing_only_extra_assertions = []
testing_only_libclang_9 = []
testing_only_libclang_5 = []

[patch.crates-io]
cexpr = { git = "https://github.com/reitermarkus/rust-cexpr", branch = "cast-expr" }
# cexpr = { path = "./rust-cexpr" }
3 changes: 3 additions & 0 deletions bindgen-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ testing_only_docs = ["bindgen/testing_only_docs"]
testing_only_extra_assertions = ["bindgen/testing_only_extra_assertions"]
testing_only_libclang_9 = ["bindgen/testing_only_libclang_9"]
testing_only_libclang_5 = ["bindgen/testing_only_libclang_5"]

[patch.crates-io]
cexpr = { git = "https://github.com/reitermarkus/rust-cexpr", branch = "cast-expr" }
8 changes: 8 additions & 0 deletions minimal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <limits.h>
#include <stdint.h>

#define MY_USHORT (unsigned short) ULONG_MAX

typedef unsigned long TickType_t;
#define portMAX_DELAY (TickType_t) ULONG_MAX
const TickType_t __portMAX_DELAY = portMAX_DELAY;
16 changes: 13 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,18 @@ impl CodeGenerator for Var {
attrs.push(attributes::doc(comment));
}

let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
let var_ty = if let Some(var_ty) = self.ty(ctx) {
var_ty
} else {
// FIXME: Parse/output macro variables as the last step when all types are known.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this is not possible, I believe, because the type the macro refers to by definition depends on context at the time of expansion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time of expansion is after everything is parsed, so it should be the last type with the corresponding name.

Copy link
Contributor Author

@reitermarkus reitermarkus Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, bindgen currently is not doing the right thing for the following:

#define MY_INT 1
#define MY_INT 2

In C, MY_INT expands to 2. bindgen currently generates const MY_INT: u32 = 1.

So macros should be expanded/evaluated after parsing everything, not at the time we see them.

warn!(
"Failed to determine type for macro variable {}.",
canonical_name
);
return;
};

let ty = var_ty.to_rust_ty_or_opaque(ctx, &());

if let Some(val) = self.val() {
match *val {
Expand All @@ -641,8 +652,7 @@ impl CodeGenerator for Var {
});
}
VarType::Int(val) => {
let int_kind = self
.ty()
let int_kind = var_ty
.into_resolver()
.through_type_aliases()
.through_type_refs()
Expand Down
Loading