From 4058eecbb0a92126c9694e5ac326fa0ae8b71a42 Mon Sep 17 00:00:00 2001 From: Niklas Nummelin Date: Wed, 24 Jan 2024 20:12:55 +0100 Subject: [PATCH 1/3] Fix for bug when generating the combined bitflags --- physx-sys/pxbind/src/generator/enums.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/physx-sys/pxbind/src/generator/enums.rs b/physx-sys/pxbind/src/generator/enums.rs index e807728c..1ea964ac 100644 --- a/physx-sys/pxbind/src/generator/enums.rs +++ b/physx-sys/pxbind/src/generator/enums.rs @@ -110,17 +110,24 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> { writes!(w, "1 << {}", val.ilog2()); } else { let mut is_combo = false; - // If we're not a power of 2, we're a combination of flags, + + // If we're not a power of 2, we're a combination of flags (or constants), // find which ones and emit them in a friendly way - for (i, which) in enum_binding + + // We keep track of what remaining flag(s) we have to mask into the bitmask + // by removing the bits that have been found as named flags. + let mut remainder = val; + + for (i, (which, bit)) in enum_binding .variants .iter() .filter_map(|var| { let prev = var.value as u64; - (prev & (prev - 1) == 0 && (prev & val) != 0).then_some(var.name) + (prev & (prev - 1) == 0 && (prev & val) != 0).then_some((var.name, prev)) }) .enumerate() { + remainder &= !bit; is_combo = true; if i > 0 { writes!(w, " | "); @@ -134,6 +141,13 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> { if !is_combo { writes!(w, "0x{val:08x}"); } + else if remainder != 0 { + if remainder & (remainder - 1) == 0 { + writes!(w, "1 << {}", remainder.ilog2()); + } else { + writes!(w, "| 0x{remainder:08x}"); + } + } } writesln!(w, ";"); From c8bba562f65d4bcd5d4fc8663650e0b3152ad0d0 Mon Sep 17 00:00:00 2001 From: Niklas Nummelin Date: Wed, 24 Jan 2024 20:23:59 +0100 Subject: [PATCH 2/3] Missing OR :) --- physx-sys/pxbind/src/generator/enums.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physx-sys/pxbind/src/generator/enums.rs b/physx-sys/pxbind/src/generator/enums.rs index 1ea964ac..f723bfe4 100644 --- a/physx-sys/pxbind/src/generator/enums.rs +++ b/physx-sys/pxbind/src/generator/enums.rs @@ -143,7 +143,7 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> { } else if remainder != 0 { if remainder & (remainder - 1) == 0 { - writes!(w, "1 << {}", remainder.ilog2()); + writes!(w, "| 1 << {}", remainder.ilog2()); } else { writes!(w, "| 0x{remainder:08x}"); } From 1a467203657b8c73da3dce213be0a25ca99b3580 Mon Sep 17 00:00:00 2001 From: Niklas Nummelin Date: Wed, 24 Jan 2024 21:09:43 +0100 Subject: [PATCH 3/3] Cargo fmt --- physx-sys/pxbind/src/generator/enums.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/physx-sys/pxbind/src/generator/enums.rs b/physx-sys/pxbind/src/generator/enums.rs index f723bfe4..7be28a5e 100644 --- a/physx-sys/pxbind/src/generator/enums.rs +++ b/physx-sys/pxbind/src/generator/enums.rs @@ -127,7 +127,7 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> { }) .enumerate() { - remainder &= !bit; + remainder &= !bit; is_combo = true; if i > 0 { writes!(w, " | "); @@ -140,11 +140,10 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> { // emit the raw value if !is_combo { writes!(w, "0x{val:08x}"); - } - else if remainder != 0 { + } else if remainder != 0 { if remainder & (remainder - 1) == 0 { writes!(w, "| 1 << {}", remainder.ilog2()); - } else { + } else { writes!(w, "| 0x{remainder:08x}"); } }