Skip to content

Commit

Permalink
Distinguish char16_t.
Browse files Browse the repository at this point in the history
With a new command-line option, this ensures that char16_t
is distinct from uint16_t in generated bindings. On some platforms
these are distinct types, so it can be important for downstream
post processors to spot the difference.

See the documentation on the new command-line option for
expected behavior and usage here.

Part of google/autocxx#124.
  • Loading branch information
adetaylor committed Feb 16, 2025
1 parent 76920aa commit 3a63b4d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
27 changes: 27 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.

9 changes: 9 additions & 0 deletions bindgen-tests/tests/headers/char16_t.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// bindgen-flags: --use-distinct-char16-t -- -x c++ -std=c++14

#include <stdint.h>

void receive_char16_t(char16_t input) {
}

void receive_uint16_t(uint16_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

0 comments on commit 3a63b4d

Please sign in to comment.