Skip to content

Conversation

@dingxiangfei2009
Copy link

@dingxiangfei2009 dingxiangfei2009 commented Nov 28, 2025

Fix #923.
Fix #3200. This is due to a fallout from how libclang type would be processed with this patch.

This patch introduce a new generator flag --macro-const-use-ctypes, which resolves the macro definition expression to a precise type advised by Clang if activated.

A macro expansion result adapter is introduced to maintain compatibility with cexpr, which has a limited capacity to resolve to a precise C type.

There is a change to the testing code because up to this point our tests, which can be executed concurrently, use the same path to the precomputed headers. Since this patch introduces a second test that relies on libclang macro fallback, the precompiled headers from the two different tests would overwrite each other. The new addition is that the backing files are now temporarily stored at different directories named after the header files under tests.

@dingxiangfei2009
Copy link
Author

Apparently the clang fallback has race condition. If I set thread number to 1, we don't have truncated output anymore.

@dingxiangfei2009
Copy link
Author

dingxiangfei2009 commented Nov 28, 2025

I have a theory about the test setup leading to the racing. The test setup uses the fallback translation unit backed by a file, which is located in ./.macro_eval.c by default. If the tests run concurrently, they are racing on this shared file. That probably explains why I get evaluation failure "randomly."

Update: yes, the two tests are racing on writing into the precompiled header.

@dingxiangfei2009 dingxiangfei2009 force-pushed the precise-macro-value-type branch 4 times, most recently from e03d7e7 to 4d04b3a Compare November 30, 2025 21:40
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>
@dingxiangfei2009 dingxiangfei2009 force-pushed the precise-macro-value-type branch from 4d04b3a to 2de8281 Compare December 1, 2025 09:54
@dingxiangfei2009
Copy link
Author

I am holding off CHANGELOG in case we want a better name for the flag.

@dingxiangfei2009
Copy link
Author

r? @emilio

🙏

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, some (optional) nits.

let mut header_names_to_compile = Vec::new();
let mut header_paths = Vec::new();
let mut header_includes = Vec::new();
let single_header = self.options().input_headers.last().cloned()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems drive-by, right? Seems fine tho.

/// Use C platform-specific types for generated macro constants.
///
/// This implies `clang_macro_fallback` and takes precedence over
/// `default_macro_constant_type`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have this be a little less weird, but for now it seems fine.

// Safety: we are only copying the content, not assuming a borrow.
// TODO(@dingxiangfei2009): LLVM Libclang does not return the true size
// of a string literal, which could be truncated due to a null character
// '\0' in the middle.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, hopefully this is rare, but maybe file an upstream issue and reference it here?


let source = fs::File::open(header)?;
let reader = BufReader::new(source);
// Safety: assume that headers in our test suite have distinct names.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no unsafe here tho, right?

This seems fine but might be worth a comment like "this is to prevent tests running in parallel from clobbering each other's results".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fails to generate correct code when UINT64_C is used Macro defining a constant with unsigned long suffix generates unsigned integer

2 participants