Skip to content

Commit a10ba8a

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 5813198 commit a10ba8a

File tree

10 files changed

+302
-40
lines changed

10 files changed

+302
-40
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,8 @@ impl EvalResult {
23292329
})
23302330
}
23312331

2332-
fn kind(&self) -> CXEvalResultKind {
2332+
/// Return the kind of the evaluation result.
2333+
pub(crate) fn kind(&self) -> CXEvalResultKind {
23332334
unsafe { clang_EvalResult_getKind(self.x) }
23342335
}
23352336

@@ -2370,6 +2371,29 @@ impl EvalResult {
23702371
Some(value as i64)
23712372
}
23722373

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

bindgen/ir/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub(crate) struct BindgenContext {
353353
/// hard errors while parsing duplicated macros, as well to allow macro
354354
/// expression parsing.
355355
///
356-
/// This needs to be an `std::HashMap` because the `cexpr` API requires it.
356+
/// This needs to be an `std::HashMap` because the [`cexpr`] API requires it.
357357
parsed_macros: StdHashMap<Vec<u8>, cexpr::expr::EvalResult>,
358358

359359
/// A map with all include locations.
@@ -2129,6 +2129,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
21292129
}
21302130

21312131
/// Get the currently parsed macros.
2132+
/// This map only contains macros accepted by [`cexpr`]
21322133
pub(crate) fn parsed_macros(
21332134
&self,
21342135
) -> &StdHashMap<Vec<u8>, cexpr::expr::EvalResult> {

0 commit comments

Comments
 (0)