Skip to content

Commit 1d3d17f

Browse files
author
bors-servo
authored
Auto merge of #610 - emilio:char, r=fitzgen
ir: Handle char in a more cross-platform way when possible. This should address #603, and supersede #609
2 parents 5e85271 + 87b3f38 commit 1d3d17f

25 files changed

+206
-53
lines changed

src/codegen/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,8 @@ impl TryToRustTy for Type {
24712471
TypeKind::Int(ik) => {
24722472
match ik {
24732473
IntKind::Bool => Ok(aster::ty::TyBuilder::new().bool()),
2474-
IntKind::Char => Ok(raw_type(ctx, "c_schar")),
2474+
IntKind::Char { .. } => Ok(raw_type(ctx, "c_char")),
2475+
IntKind::SChar => Ok(raw_type(ctx, "c_schar")),
24752476
IntKind::UChar => Ok(raw_type(ctx, "c_uchar")),
24762477
IntKind::Short => Ok(raw_type(ctx, "c_short")),
24772478
IntKind::UShort => Ok(raw_type(ctx, "c_ushort")),

src/ir/context.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,38 @@ impl<'ctx> BindgenContext<'ctx> {
10901090
CXType_Bool => TypeKind::Int(IntKind::Bool),
10911091
CXType_Int => TypeKind::Int(IntKind::Int),
10921092
CXType_UInt => TypeKind::Int(IntKind::UInt),
1093-
CXType_SChar | CXType_Char_S => TypeKind::Int(IntKind::Char),
1094-
CXType_UChar | CXType_Char_U => TypeKind::Int(IntKind::UChar),
1093+
CXType_SChar | CXType_Char_S |
1094+
CXType_UChar | CXType_Char_U => {
1095+
let spelling = ty.spelling();
1096+
1097+
debug_assert!(spelling.contains("char"),
1098+
"This is the canonical type, so no aliases or \
1099+
typedefs!");
1100+
1101+
let signed = match ty.kind() {
1102+
CXType_SChar | CXType_Char_S => true,
1103+
_ => false,
1104+
};
1105+
1106+
// Clang only gives us the signedness of the target platform.
1107+
//
1108+
// Match the spelling for common cases we can handle
1109+
// cross-platform.
1110+
match &*spelling {
1111+
"char" | "const char" => {
1112+
TypeKind::Int(IntKind::Char {
1113+
is_signed: signed,
1114+
})
1115+
},
1116+
_ => {
1117+
if signed {
1118+
TypeKind::Int(IntKind::SChar)
1119+
} else {
1120+
TypeKind::Int(IntKind::UChar)
1121+
}
1122+
},
1123+
}
1124+
}
10951125
CXType_Short => TypeKind::Int(IntKind::Short),
10961126
CXType_UShort => TypeKind::Int(IntKind::UShort),
10971127
CXType_WChar | CXType_Char16 => TypeKind::Int(IntKind::U16),

src/ir/int.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ pub enum IntKind {
66
/// A `bool`.
77
Bool,
88

9-
/// A `char`.
10-
Char,
9+
/// A `signed char`.
10+
SChar,
1111

1212
/// An `unsigned char`.
1313
UChar,
1414

15+
/// A platform-dependent `char` type, with the signedness support.
16+
Char {
17+
/// Whether the char is signed for the target platform.
18+
is_signed: bool,
19+
},
20+
1521
/// A `short`.
1622
Short,
1723

@@ -84,9 +90,11 @@ impl IntKind {
8490
Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 |
8591
U32 | U64 | U128 => false,
8692

87-
Char | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
93+
SChar | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
8894
I128 => true,
8995

96+
Char { is_signed } => is_signed,
97+
9098
Custom { is_signed, .. } => is_signed,
9199
}
92100
}
@@ -97,7 +105,7 @@ impl IntKind {
97105
pub fn known_size(&self) -> Option<usize> {
98106
use self::IntKind::*;
99107
Some(match *self {
100-
Bool | UChar | Char | U8 | I8 => 1,
108+
Bool | UChar | SChar | U8 | I8 | Char { .. } => 1,
101109
U16 | I16 => 2,
102110
U32 | I32 => 4,
103111
U64 | I64 => 8,

tests/expectations/tests/anonymous-template-types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl <T> Default for Foo<T> {
1515
#[repr(C)]
1616
#[derive(Debug, Default, Copy, Clone)]
1717
pub struct Bar {
18-
pub member: ::std::os::raw::c_schar,
18+
pub member: ::std::os::raw::c_char,
1919
}
2020
#[repr(C)]
2121
#[derive(Debug, Copy, Clone)]
@@ -28,6 +28,6 @@ impl <V> Default for Quux<V> {
2828
#[repr(C)]
2929
#[derive(Debug, Default, Copy, Clone)]
3030
pub struct Lobo {
31-
pub also_member: ::std::os::raw::c_schar,
31+
pub also_member: ::std::os::raw::c_char,
3232
}
33-
pub type AliasWithAnonType = ::std::os::raw::c_schar;
33+
pub type AliasWithAnonType = ::std::os::raw::c_char;

tests/expectations/tests/arg_keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
extern "C" {
88
#[link_name = "_Z3fooPKc"]
9-
pub fn foo(type_: *const ::std::os::raw::c_schar);
9+
pub fn foo(type_: *const ::std::os::raw::c_char);
1010
}

tests/expectations/tests/bitfield-method-same-name.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ fn bindgen_test_layout_Foo() {
1919
}
2020
extern "C" {
2121
#[link_name = "_ZN3Foo4typeEv"]
22-
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_schar;
22+
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
2323
}
2424
extern "C" {
2525
#[link_name = "_ZN3Foo9set_type_Ec"]
26-
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_schar);
26+
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
2727
}
2828
extern "C" {
2929
#[link_name = "_ZN3Foo8set_typeEc"]
30-
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_schar);
30+
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
3131
}
3232
impl Clone for Foo {
3333
fn clone(&self) -> Self { *self }
3434
}
3535
impl Foo {
3636
#[inline]
37-
pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_schar {
37+
pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char {
3838
let mask = 7usize as u8;
3939
let field_val: u8 =
4040
unsafe { ::std::mem::transmute(self._bitfield_1) };
@@ -43,7 +43,7 @@ impl Foo {
4343
}
4444
#[inline]
4545
pub fn set_type__bindgen_bitfield(&mut self,
46-
val: ::std::os::raw::c_schar) {
46+
val: ::std::os::raw::c_char) {
4747
let mask = 7usize as u8;
4848
let val = val as u8 as u8;
4949
let mut field_val: u8 =
@@ -53,15 +53,15 @@ impl Foo {
5353
self._bitfield_1 = unsafe { ::std::mem::transmute(field_val) };
5454
}
5555
#[inline]
56-
pub unsafe fn type_(&mut self) -> ::std::os::raw::c_schar {
56+
pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char {
5757
Foo_type(self)
5858
}
5959
#[inline]
60-
pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_schar) {
60+
pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_char) {
6161
Foo_set_type_(self, c)
6262
}
6363
#[inline]
64-
pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_schar) {
64+
pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_char) {
6565
Foo_set_type(self, c)
6666
}
6767
}

tests/expectations/tests/char.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub type Char = ::std::os::raw::c_char;
8+
pub type SChar = ::std::os::raw::c_schar;
9+
pub type UChar = ::std::os::raw::c_uchar;
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Test {
13+
pub ch: ::std::os::raw::c_char,
14+
pub u: ::std::os::raw::c_uchar,
15+
pub d: ::std::os::raw::c_schar,
16+
pub cch: ::std::os::raw::c_char,
17+
pub cu: ::std::os::raw::c_uchar,
18+
pub cd: ::std::os::raw::c_schar,
19+
pub Cch: Char,
20+
pub Cu: UChar,
21+
pub Cd: SChar,
22+
pub Ccch: Char,
23+
pub Ccu: UChar,
24+
pub Ccd: SChar,
25+
}
26+
#[test]
27+
fn bindgen_test_layout_Test() {
28+
assert_eq!(::std::mem::size_of::<Test>() , 12usize , concat ! (
29+
"Size of: " , stringify ! ( Test ) ));
30+
assert_eq! (::std::mem::align_of::<Test>() , 1usize , concat ! (
31+
"Alignment of " , stringify ! ( Test ) ));
32+
assert_eq! (unsafe {
33+
& ( * ( 0 as * const Test ) ) . ch as * const _ as usize } ,
34+
0usize , concat ! (
35+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
36+
stringify ! ( ch ) ));
37+
assert_eq! (unsafe {
38+
& ( * ( 0 as * const Test ) ) . u as * const _ as usize } ,
39+
1usize , concat ! (
40+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
41+
stringify ! ( u ) ));
42+
assert_eq! (unsafe {
43+
& ( * ( 0 as * const Test ) ) . d as * const _ as usize } ,
44+
2usize , concat ! (
45+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
46+
stringify ! ( d ) ));
47+
assert_eq! (unsafe {
48+
& ( * ( 0 as * const Test ) ) . cch as * const _ as usize } ,
49+
3usize , concat ! (
50+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
51+
stringify ! ( cch ) ));
52+
assert_eq! (unsafe {
53+
& ( * ( 0 as * const Test ) ) . cu as * const _ as usize } ,
54+
4usize , concat ! (
55+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
56+
stringify ! ( cu ) ));
57+
assert_eq! (unsafe {
58+
& ( * ( 0 as * const Test ) ) . cd as * const _ as usize } ,
59+
5usize , concat ! (
60+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
61+
stringify ! ( cd ) ));
62+
assert_eq! (unsafe {
63+
& ( * ( 0 as * const Test ) ) . Cch as * const _ as usize } ,
64+
6usize , concat ! (
65+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
66+
stringify ! ( Cch ) ));
67+
assert_eq! (unsafe {
68+
& ( * ( 0 as * const Test ) ) . Cu as * const _ as usize } ,
69+
7usize , concat ! (
70+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
71+
stringify ! ( Cu ) ));
72+
assert_eq! (unsafe {
73+
& ( * ( 0 as * const Test ) ) . Cd as * const _ as usize } ,
74+
8usize , concat ! (
75+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
76+
stringify ! ( Cd ) ));
77+
assert_eq! (unsafe {
78+
& ( * ( 0 as * const Test ) ) . Ccch as * const _ as usize } ,
79+
9usize , concat ! (
80+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
81+
stringify ! ( Ccch ) ));
82+
assert_eq! (unsafe {
83+
& ( * ( 0 as * const Test ) ) . Ccu as * const _ as usize } ,
84+
10usize , concat ! (
85+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
86+
stringify ! ( Ccu ) ));
87+
assert_eq! (unsafe {
88+
& ( * ( 0 as * const Test ) ) . Ccd as * const _ as usize } ,
89+
11usize , concat ! (
90+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
91+
stringify ! ( Ccd ) ));
92+
}
93+
impl Clone for Test {
94+
fn clone(&self) -> Self { *self }
95+
}

tests/expectations/tests/class.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
6464
#[repr(C)]
6565
pub struct C {
6666
pub a: ::std::os::raw::c_int,
67-
pub big_array: [::std::os::raw::c_schar; 33usize],
67+
pub big_array: [::std::os::raw::c_char; 33usize],
6868
}
6969
#[test]
7070
fn bindgen_test_layout_C() {
@@ -88,8 +88,8 @@ impl Default for C {
8888
#[repr(C)]
8989
pub struct C_with_zero_length_array {
9090
pub a: ::std::os::raw::c_int,
91-
pub big_array: [::std::os::raw::c_schar; 33usize],
92-
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_schar>,
91+
pub big_array: [::std::os::raw::c_char; 33usize],
92+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
9393
}
9494
#[test]
9595
fn bindgen_test_layout_C_with_zero_length_array() {
@@ -124,8 +124,8 @@ impl Default for C_with_zero_length_array {
124124
#[repr(C)]
125125
pub struct C_with_incomplete_array {
126126
pub a: ::std::os::raw::c_int,
127-
pub big_array: [::std::os::raw::c_schar; 33usize],
128-
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_schar>,
127+
pub big_array: [::std::os::raw::c_char; 33usize],
128+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
129129
}
130130
#[test]
131131
fn bindgen_test_layout_C_with_incomplete_array() {
@@ -142,9 +142,9 @@ impl Default for C_with_incomplete_array {
142142
#[repr(C)]
143143
pub struct C_with_zero_length_array_and_incomplete_array {
144144
pub a: ::std::os::raw::c_int,
145-
pub big_array: [::std::os::raw::c_schar; 33usize],
146-
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_schar>,
147-
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_schar>,
145+
pub big_array: [::std::os::raw::c_char; 33usize],
146+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
147+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
148148
}
149149
#[test]
150150
fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array() {

tests/expectations/tests/class_with_typedef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct C {
1515
pub other_ptr: *mut AnotherInt,
1616
}
1717
pub type C_MyInt = ::std::os::raw::c_int;
18-
pub type C_Lookup = *const ::std::os::raw::c_schar;
18+
pub type C_Lookup = *const ::std::os::raw::c_char;
1919
#[test]
2020
fn bindgen_test_layout_C() {
2121
assert_eq!(::std::mem::size_of::<C>() , 72usize , concat ! (

tests/expectations/tests/constant-evaluate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub const k: EasyToOverflow = 2147483648;
1414
pub const k_expr: EasyToOverflow = 0;
1515
pub const BAZ: ::std::os::raw::c_longlong = 24;
1616
pub const fuzz: f64 = 51.;
17-
pub const BAZZ: ::std::os::raw::c_schar = 53;
18-
pub const WAT: ::std::os::raw::c_schar = 0;
17+
pub const BAZZ: ::std::os::raw::c_char = 53;
18+
pub const WAT: ::std::os::raw::c_char = 0;
1919
pub const bytestring: &'static [u8; 4usize] = b"Foo\x00";
2020
pub const NOT_UTF8: [u8; 5usize] = [240, 40, 140, 40, 0];

tests/expectations/tests/inline_namespace_whitelist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ pub mod root {
1111
pub mod std {
1212
#[allow(unused_imports)]
1313
use self::super::super::root;
14-
pub type string = *const ::std::os::raw::c_schar;
14+
pub type string = *const ::std::os::raw::c_char;
1515
}
1616
}

tests/expectations/tests/issue-493.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct basic_string {
3434
pub _address: u8,
3535
}
3636
pub type basic_string_size_type = ::std::os::raw::c_ulonglong;
37-
pub type basic_string_value_type = ::std::os::raw::c_schar;
37+
pub type basic_string_value_type = ::std::os::raw::c_char;
3838
pub type basic_string_pointer = *mut basic_string_value_type;
3939
#[repr(C)]
4040
#[derive(Debug, Copy, Clone)]

tests/expectations/tests/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { }
4040
#[repr(C, packed)]
4141
#[derive(Debug, Default, Copy)]
4242
pub struct header {
43-
pub proto: ::std::os::raw::c_schar,
43+
pub proto: ::std::os::raw::c_char,
4444
pub size: ::std::os::raw::c_uint,
4545
pub data: __IncompleteArrayField<::std::os::raw::c_uchar>,
4646
pub __bindgen_padding_0: [u8; 11usize],

tests/expectations/tests/layout_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub type rte_mempool_get_count =
5858
#[derive(Debug, Copy)]
5959
pub struct rte_mempool_ops {
6060
/**< Name of mempool ops struct. */
61-
pub name: [::std::os::raw::c_schar; 32usize],
61+
pub name: [::std::os::raw::c_char; 32usize],
6262
/**< Allocate private data. */
6363
pub alloc: rte_mempool_alloc_t,
6464
/**< Free the external pool. */

tests/expectations/tests/layout_cmdline_token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct cmdline_token_ops {
6464
pub parse: ::std::option::Option<unsafe extern "C" fn(arg1:
6565
*mut cmdline_parse_token_hdr_t,
6666
arg2:
67-
*const ::std::os::raw::c_schar,
67+
*const ::std::os::raw::c_char,
6868
arg3:
6969
*mut ::std::os::raw::c_void,
7070
arg4:
@@ -80,15 +80,15 @@ pub struct cmdline_token_ops {
8080
arg2:
8181
::std::os::raw::c_int,
8282
arg3:
83-
*mut ::std::os::raw::c_schar,
83+
*mut ::std::os::raw::c_char,
8484
arg4:
8585
::std::os::raw::c_uint)
8686
-> ::std::os::raw::c_int>,
8787
/** get help for this token (token, dstbuf, size) */
8888
pub get_help: ::std::option::Option<unsafe extern "C" fn(arg1:
8989
*mut cmdline_parse_token_hdr_t,
9090
arg2:
91-
*mut ::std::os::raw::c_schar,
91+
*mut ::std::os::raw::c_char,
9292
arg3:
9393
::std::os::raw::c_uint)
9494
-> ::std::os::raw::c_int>,

0 commit comments

Comments
 (0)