Skip to content

Commit e2460d9

Browse files
zypemilio
authored andcommitted
Allow constant expressions that doesn't fit in i64.
Fixes #2618
1 parent e604f6b commit e2460d9

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

bindgen-tests/tests/expectations/tests/issue-2618.rs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// bindgen-flags: --allowlist-var "val[0-9]+"
2+
3+
typedef __UINT32_TYPE__ uint32_t;
4+
typedef __UINT64_TYPE__ uint64_t;
5+
6+
static const uint32_t val1 = 0x7fffffff;
7+
static const uint32_t val2 = 0x80000000;
8+
static const uint32_t val3 = 0xffffffff;
9+
static const uint64_t val4 = 0x7fffffffffffffff;
10+
static const uint64_t val5 = 0x8000000000000000;
11+
static const uint64_t val6 = 0xffffffffffffffff;
12+
13+
static const uint32_t val7 = (0x7fffffff);
14+
static const uint32_t val8 = (0x80000000);
15+
static const uint32_t val9 = (0xffffffff);
16+
static const uint64_t val10 = (0x7fffffffffffffff);
17+
static const uint64_t val11 = (0x8000000000000000);
18+
static const uint64_t val12 = (0xffffffffffffffff);

bindgen/clang.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,10 +2351,11 @@ impl EvalResult {
23512351

23522352
if unsafe { clang_EvalResult_isUnsignedInt(self.x) } != 0 {
23532353
let value = unsafe { clang_EvalResult_getAsUnsigned(self.x) };
2354-
if value > i64::MAX as c_ulonglong {
2354+
if value > u64::MAX as c_ulonglong {
23552355
return None;
23562356
}
23572357

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

bindgen/ir/int.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,4 @@ impl IntKind {
120120
_ => return None,
121121
})
122122
}
123-
124-
/// Whether this type's signedness matches the value.
125-
pub(crate) fn signedness_matches(&self, val: i64) -> bool {
126-
val >= 0 || self.is_signed()
127-
}
128123
}

bindgen/ir/var.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl ClangSubItemParser for Var {
341341
};
342342

343343
let mut val = cursor.evaluate().and_then(|v| v.as_int());
344-
if val.is_none() || !kind.signedness_matches(val.unwrap()) {
344+
if val.is_none() {
345345
val = get_integer_literal_from_cursor(&cursor);
346346
}
347347

0 commit comments

Comments
 (0)