Skip to content

Commit df469bb

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 b7b501f commit df469bb

File tree

13 files changed

+364
-42
lines changed

13 files changed

+364
-42
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ csmith-fuzzing/platform.info
2323
node_modules
2424
package-lock.json
2525
package.json
26+
27+
# Common IDE settings and temporary files
28+
*.swp
29+
*.swo
30+
Session.vim
31+
.cproject
32+
.idea
33+
*.iml
34+
.vscode
35+
.project
36+
.vim/
37+
.helix/
38+
.zed/
39+
.favorites.json
40+
.settings/
41+
.vs/
42+
.dir-locals.el
43+

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: 7 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/libclang-9/issue-923.rs

Lines changed: 1 addition & 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// bindgen-flags: --macro-const-use-ctypes
2+
3+
#ifndef ISSUE_923_H
4+
#define ISSUE_923_H
5+
6+
#define ULONG_ZERO 0UL
7+
#define ULONGLONG_ZERO 0ULL
8+
#define CHAR_ZERO ((char)'\0')
9+
#define FLOAT_LIT 0.f
10+
#define DOUBLE_LIT ((double)0.f)
11+
#define UINT_LIT 0x80000000
12+
13+
#endif

bindgen-tests/tests/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
269269

270270
let source = fs::File::open(header)?;
271271
let reader = BufReader::new(source);
272+
let basename = header.file_name().unwrap();
273+
let per_test_folder = format!(
274+
"./{}",
275+
basename
276+
.to_str()
277+
.unwrap()
278+
.replace('_', "__")
279+
.replace('.', "_")
280+
);
281+
fs::create_dir_all(&per_test_folder)?;
272282

273283
// Scoop up bindgen-flags from test header
274284
let mut flags = Vec::with_capacity(2);
@@ -334,6 +344,8 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
334344
"#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]",
335345
"--raw-line",
336346
"",
347+
"--clang-macro-fallback-build-dir",
348+
&per_test_folder,
337349
];
338350

339351
let args = prepend.iter().map(ToString::to_string).chain(flags);

bindgen/clang.rs

Lines changed: 26 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,30 @@ 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+
// Tracking issue: https://github.com/llvm/llvm-project/issues/69749
2388+
let value =
2389+
unsafe { CStr::from_ptr(clang_EvalResult_getAsStr(self.x)) };
2390+
Some(value.to_bytes().into())
2391+
}
2392+
2393+
/// Return the type of the value.
2394+
pub(crate) fn value_type(&self) -> Type {
2395+
self.ty
2396+
}
2397+
23732398
/// Evaluates the expression as a literal string, that may or may not be
23742399
/// valid utf-8.
23752400
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.
@@ -2131,6 +2131,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
21312131
}
21322132

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

0 commit comments

Comments
 (0)