Skip to content

Commit 4d459ec

Browse files
Allow macro definitions to pick up precise C types
Upon activating the `--macro-const-use-ctypes` flag, `bindgen` should generate bindings with precise type resolution to the exact C types. This flag implies `--clang-macro-fallback` as its pre-requisite. Signed-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>
1 parent 0173bbd commit 4d459ec

File tree

9 files changed

+283
-41
lines changed

9 files changed

+283
-41
lines changed

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

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/test_macro_fallback_non_system_dir.rs

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/headers/issue-753.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
#define CONST UINT32_C(5)
99
#define OTHER_CONST UINT32_C(6)
10-
#define LARGE_CONST UINT32_C(6 << 8)
10+
#define LARGE_CONST (UINT32_C(6) << 8)
1111

1212
#endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// bindgen-flags: --macro-const-use-ctypes
2+
#define ULONG_ZERO 0UL
3+
#define ULONGLONG_ZERO 0ULL
4+
#define CHAR_ZERO ((char)'\0')

bindgen/clang.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,8 +2308,8 @@ impl EvalResult {
23082308
{
23092309
let mut found_cant_eval = false;
23102310
cursor.visit(|c| {
2311-
if c.kind() == CXCursor_TypeRef &&
2312-
c.cur_type().canonical_type().kind() == CXType_Unexposed
2311+
if c.kind() == CXCursor_TypeRef
2312+
&& c.cur_type().canonical_type().kind() == CXType_Unexposed
23132313
{
23142314
found_cant_eval = true;
23152315
return CXChildVisit_Break;
@@ -2328,7 +2328,7 @@ impl EvalResult {
23282328
})
23292329
}
23302330

2331-
fn kind(&self) -> CXEvalResultKind {
2331+
pub(crate) fn kind(&self) -> CXEvalResultKind {
23322332
unsafe { clang_EvalResult_getKind(self.x) }
23332333
}
23342334

@@ -2369,6 +2369,26 @@ impl EvalResult {
23692369
Some(value as i64)
23702370
}
23712371

2372+
pub(crate) fn as_str_literal(&self) -> Option<Vec<u8>> {
2373+
if !matches!(
2374+
self.kind(),
2375+
CXEval_StrLiteral | CXEval_CFStr | CXEval_ObjCStrLiteral,
2376+
) {
2377+
return None;
2378+
}
2379+
// Safety: we are only copying the content, not assuming a borrow.
2380+
// TODO(@dingxiangfei2009): LLVM Libclang does not return the true size
2381+
// of a string literal, which could be truncated due to a null character
2382+
// '\0' in the middle.
2383+
let value =
2384+
unsafe { CStr::from_ptr(clang_EvalResult_getAsStr(self.x)) };
2385+
Some(value.to_bytes().into())
2386+
}
2387+
2388+
pub(crate) fn value_type(&self) -> Type {
2389+
self.ty
2390+
}
2391+
23722392
/// Evaluates the expression as a literal string, that may or may not be
23732393
/// valid utf-8.
23742394
pub(crate) fn as_literal_string(&self) -> Option<Vec<u8>> {

0 commit comments

Comments
 (0)