Skip to content

Distinguish char16_t. #3135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindgen-tests/tests/expectations/tests/char16_t.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bindgen-tests/tests/headers/char16_t.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// bindgen-flags: --use-distinct-char16-t --raw-line '#[repr(transparent)] pub struct bindgen_cchar16_t(u16);' -- -x c++ -std=c++14

void receive_char16_t(char16_t input) {
}
6 changes: 6 additions & 0 deletions bindgen/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ pub(crate) mod ast_ty {
match ik {
IntKind::Bool => syn::parse_quote! { bool },
IntKind::Char { .. } => raw_type(ctx, "c_char"),
// The following is used only when an unusual command-line
// argument is used. bindgen_cchar16_t is not a real type;
// but this allows downstream postprocessors to distinguish
// this case and do something special for C++ bindings
// containing char16_t.
IntKind::Char16 => syn::parse_quote! { bindgen_cchar16_t },
IntKind::SChar => raw_type(ctx, "c_schar"),
IntKind::UChar => raw_type(ctx, "c_uchar"),
IntKind::Short => raw_type(ctx, "c_short"),
Expand Down
3 changes: 3 additions & 0 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
CXType_Short => TypeKind::Int(IntKind::Short),
CXType_UShort => TypeKind::Int(IntKind::UShort),
CXType_WChar => TypeKind::Int(IntKind::WChar),
CXType_Char16 if self.options().use_distinct_char16_t => {
TypeKind::Int(IntKind::Char16)
}
CXType_Char16 => TypeKind::Int(IntKind::U16),
CXType_Char32 => TypeKind::Int(IntKind::U32),
CXType_Long => TypeKind::Int(IntKind::Long),
Expand Down
9 changes: 6 additions & 3 deletions bindgen/ir/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ pub enum IntKind {
/// A 16-bit signed integer.
I16,

/// Either a `char16_t` or a `wchar_t`.
/// A 16-bit integer, used only for enum size representation.
U16,

/// Either a `char16_t` or a `wchar_t`.
Char16,

/// A 32-bit signed integer.
I32,

Expand Down Expand Up @@ -94,7 +97,7 @@ impl IntKind {
// to know whether it is or not right now (unlike char, there's no
// WChar_S / WChar_U).
Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 |
WChar | U32 | U64 | U128 => false,
Char16 | WChar | U32 | U64 | U128 => false,

SChar | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
I128 => true,
Expand All @@ -110,7 +113,7 @@ impl IntKind {
use self::IntKind::*;
Some(match *self {
Bool | UChar | SChar | U8 | I8 | Char { .. } => 1,
U16 | I16 => 2,
U16 | I16 | Char16 => 2,
U32 | I32 => 4,
U64 | I64 => 8,
I128 | U128 => 16,
Expand Down
5 changes: 5 additions & 0 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ struct BindgenCommand {
/// Always output explicit padding fields.
#[arg(long)]
explicit_padding: bool,
/// Use distinct char16_t
#[arg(long)]
use_distinct_char16_t: bool,
/// Enables generation of vtable functions.
#[arg(long)]
vtable_generation: bool,
Expand Down Expand Up @@ -629,6 +632,7 @@ where
translate_enum_integer_types,
c_naming,
explicit_padding,
use_distinct_char16_t,
vtable_generation,
sort_semantically,
merge_extern_blocks,
Expand Down Expand Up @@ -926,6 +930,7 @@ where
translate_enum_integer_types,
c_naming,
explicit_padding,
use_distinct_char16_t,
vtable_generation,
sort_semantically,
merge_extern_blocks,
Expand Down
22 changes: 22 additions & 0 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ macro_rules! options {
}

options! {
/// Whether we should distinguish between 'char16_t' and 'u16'.
/// As standard, bindgen represents `char16_t` as `u16`.
/// Rust does not have a `std::os::raw::c_char16_t` type, and thus
/// we can't use a built-in Rust type in the generated bindings.
/// But for some uses of bindgen, especially when downstream
/// post-processing occurs, it's important to distinguish `char16_t`
/// from normal `uint16_t`. When this option is enabled, bindgen
/// generates a fake type called `bindgen_cchar16_t`. Downstream
/// code post-processors should arrange to replace this with a
/// real type.
use_distinct_char16_t: bool {
methods: {
/// If this is true, denote 'char16_t' as a separate type from 'u16'
/// Disabled by default.
pub fn use_distinct_char16_t(mut self, doit: bool) -> Builder {
self.options.use_distinct_char16_t = doit;
self
}
},
as_args: "--use-distinct-char16-t",
},

/// Types that have been blocklisted and should not appear anywhere in the generated code.
blocklisted_types: RegexSet {
methods: {
Expand Down
Loading