Skip to content
Merged
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
13 changes: 13 additions & 0 deletions bindgen-tests/tests/expectations/tests/issue-2618.rs

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

18 changes: 18 additions & 0 deletions bindgen-tests/tests/headers/issue-2618.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// bindgen-flags: --allowlist-var "val[0-9]+"

typedef __UINT32_TYPE__ uint32_t;
typedef __UINT64_TYPE__ uint64_t;

static const uint32_t val1 = 0x7fffffff;
static const uint32_t val2 = 0x80000000;
static const uint32_t val3 = 0xffffffff;
static const uint64_t val4 = 0x7fffffffffffffff;
static const uint64_t val5 = 0x8000000000000000;
static const uint64_t val6 = 0xffffffffffffffff;

static const uint32_t val7 = (0x7fffffff);
static const uint32_t val8 = (0x80000000);
static const uint32_t val9 = (0xffffffff);
static const uint64_t val10 = (0x7fffffffffffffff);
static const uint64_t val11 = (0x8000000000000000);
static const uint64_t val12 = (0xffffffffffffffff);
3 changes: 2 additions & 1 deletion bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2351,10 +2351,11 @@ impl EvalResult {

if unsafe { clang_EvalResult_isUnsignedInt(self.x) } != 0 {
let value = unsafe { clang_EvalResult_getAsUnsigned(self.x) };
if value > i64::MAX as c_ulonglong {
if value > u64::MAX as c_ulonglong {
return None;
}

// Do a wrapping cast to i64. This will be losslessly cast back to u64 later.
return Some(value as i64);
}

Expand Down
5 changes: 0 additions & 5 deletions bindgen/ir/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,4 @@ impl IntKind {
_ => return None,
})
}

/// Whether this type's signedness matches the value.
pub(crate) fn signedness_matches(&self, val: i64) -> bool {
val >= 0 || self.is_signed()
}
}
2 changes: 1 addition & 1 deletion bindgen/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl ClangSubItemParser for Var {
};

let mut val = cursor.evaluate().and_then(|v| v.as_int());
if val.is_none() || !kind.signedness_matches(val.unwrap()) {
if val.is_none() {
val = get_integer_literal_from_cursor(&cursor);
}

Expand Down