Skip to content

Commit fa89978

Browse files
author
bors-servo
authored
Auto merge of #906 - emilio:stylo-build-woes, r=fitzgen,Manishearth
ir: Sanitize base names more aggressively This fixes stylo build failures when template instantiations tests caused a rust compile error due to a bad test name. This commit makes names better sanitized, preventing similar errors in the future.
2 parents fdee9f1 + 20f2ed2 commit fa89978

File tree

5 files changed

+66
-18
lines changed

5 files changed

+66
-18
lines changed

src/ir/context.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1452,13 +1452,7 @@ impl<'ctx> BindgenContext<'ctx> {
14521452
_ => return None,
14531453
};
14541454

1455-
let mut spelling = ty.spelling();
1456-
// avoid the allocation if possible
1457-
if spelling.contains(' ') {
1458-
// These names are used in generated test names,
1459-
// they should be valid identifiers
1460-
spelling = spelling.replace(' ', "_");
1461-
}
1455+
let spelling = ty.spelling();
14621456
let is_const = ty.is_const();
14631457
let layout = ty.fallible_layout().ok();
14641458
let ty = Type::new(Some(spelling), layout, type_kind, is_const);

src/ir/item.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -695,18 +695,32 @@ impl Item {
695695
}
696696
ItemKind::Type(ref ty) => {
697697
let name = match *ty.kind() {
698-
TypeKind::ResolvedTypeRef(..) => panic!("should have resolved this in name_target()"),
698+
TypeKind::ResolvedTypeRef(..) => {
699+
panic!("should have resolved this in name_target()")
700+
}
699701
TypeKind::Pointer(inner) => {
700702
ctx.resolve_item(inner)
701703
.expect_type().name()
702-
.map(|name| format!("ptr_{}", name))
704+
.map(|name| {
705+
format!("ptr_{}", Type::sanitize_name(name))
706+
})
703707
}
704708
TypeKind::Array(inner, length) => {
705709
ctx.resolve_item(inner)
706710
.expect_type().name()
707-
.map(|name| format!("array_{}_{}", name, length))
711+
.map(|name| {
712+
format!(
713+
"array_{}_{}",
714+
Type::sanitize_name(name),
715+
length,
716+
)
717+
})
718+
}
719+
_ => {
720+
ty.name()
721+
.map(Type::sanitize_name)
722+
.map(Into::into)
708723
}
709-
_ => ty.name().map(ToOwned::to_owned)
710724
};
711725
name.unwrap_or_else(|| {
712726
format!("_bindgen_ty_{}", self.exposed_id(ctx))

src/ir/ty.rs

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use super::template::{AsTemplateParam, TemplateInstantiation, TemplateParameters
1313
use super::traversal::{EdgeKind, Trace, Tracer};
1414
use clang::{self, Cursor};
1515
use parse::{ClangItemParser, ParseError, ParseResult};
16+
use std::borrow::Cow;
1617
use std::io;
1718
use std::mem;
1819

@@ -272,6 +273,18 @@ impl Type {
272273
clang::is_valid_identifier(name)
273274
}
274275

276+
/// Takes `name`, and returns a suitable identifier representation for it.
277+
pub fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> {
278+
if Self::is_valid_identifier(name) {
279+
return Cow::Borrowed(name)
280+
}
281+
282+
let name = name.replace(|c| c == ' ' || c == ':' || c == '.' , "_");
283+
debug_assert!(Self::is_valid_identifier(&name));
284+
285+
Cow::Owned(name)
286+
}
287+
275288
/// See safe_canonical_type.
276289
pub fn canonical_type<'tr>(&'tr self,
277290
ctx: &'tr BindgenContext)

tests/expectations/tests/template.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ extern "C" {
2929
pub fn bar(foo: Foo<::std::os::raw::c_int>);
3030
}
3131
#[repr(C)]
32+
#[derive(Debug, Copy, Clone)]
33+
pub struct mozilla_Foo {
34+
_unused: [u8; 0],
35+
}
36+
#[repr(C)]
3237
#[derive(Debug, Copy, Hash)]
3338
pub struct C {
3439
pub mB: B<::std::os::raw::c_uint>,
3540
pub mBConstPtr: B<*const ::std::os::raw::c_int>,
41+
pub mBConstStructPtr: B<*const mozilla_Foo>,
3642
pub mBConst: B<::std::os::raw::c_int>,
3743
pub mBVolatile: B<::std::os::raw::c_int>,
3844
pub mBConstBool: B<bool>,
@@ -41,7 +47,7 @@ pub struct C {
4147
}
4248
#[test]
4349
fn bindgen_test_layout_C() {
44-
assert_eq!(::std::mem::size_of::<C>() , 32usize , concat ! (
50+
assert_eq!(::std::mem::size_of::<C>() , 40usize , concat ! (
4551
"Size of: " , stringify ! ( C ) ));
4652
assert_eq! (::std::mem::align_of::<C>() , 8usize , concat ! (
4753
"Alignment of " , stringify ! ( C ) ));
@@ -54,29 +60,34 @@ fn bindgen_test_layout_C() {
5460
} , 8usize , concat ! (
5561
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
5662
! ( mBConstPtr ) ));
63+
assert_eq! (unsafe {
64+
& ( * ( 0 as * const C ) ) . mBConstStructPtr as * const _ as
65+
usize } , 16usize , concat ! (
66+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
67+
! ( mBConstStructPtr ) ));
5768
assert_eq! (unsafe {
5869
& ( * ( 0 as * const C ) ) . mBConst as * const _ as usize } ,
59-
16usize , concat ! (
70+
24usize , concat ! (
6071
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
6172
! ( mBConst ) ));
6273
assert_eq! (unsafe {
6374
& ( * ( 0 as * const C ) ) . mBVolatile as * const _ as usize
64-
} , 20usize , concat ! (
75+
} , 28usize , concat ! (
6576
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
6677
! ( mBVolatile ) ));
6778
assert_eq! (unsafe {
6879
& ( * ( 0 as * const C ) ) . mBConstBool as * const _ as usize
69-
} , 24usize , concat ! (
80+
} , 32usize , concat ! (
7081
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
7182
! ( mBConstBool ) ));
7283
assert_eq! (unsafe {
7384
& ( * ( 0 as * const C ) ) . mBConstChar as * const _ as usize
74-
} , 26usize , concat ! (
85+
} , 34usize , concat ! (
7586
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
7687
! ( mBConstChar ) ));
7788
assert_eq! (unsafe {
7889
& ( * ( 0 as * const C ) ) . mBArray as * const _ as usize } ,
79-
28usize , concat ! (
90+
36usize , concat ! (
8091
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
8192
! ( mBArray ) ));
8293
}
@@ -331,6 +342,17 @@ fn __bindgen_test_layout_B_open0_ptr_const_int_close0_instantiation() {
331342
B<*const ::std::os::raw::c_int> ) ));
332343
}
333344
#[test]
345+
fn __bindgen_test_layout_B_open0_ptr_const_mozilla__Foo_close0_instantiation() {
346+
assert_eq!(::std::mem::size_of::<B<*const mozilla_Foo>>() , 8usize ,
347+
concat ! (
348+
"Size of template specialization: " , stringify ! (
349+
B<*const mozilla_Foo> ) ));
350+
assert_eq!(::std::mem::align_of::<B<*const mozilla_Foo>>() , 8usize ,
351+
concat ! (
352+
"Alignment of template specialization: " , stringify ! (
353+
B<*const mozilla_Foo> ) ));
354+
}
355+
#[test]
334356
fn __bindgen_test_layout_B_open0_const_int_close0_instantiation() {
335357
assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_int>>() , 4usize ,
336358
concat ! (

tests/headers/template.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ template<typename T, typename U> class Foo {
77
};
88

99
template<typename T> class B {
10-
T m_member;
10+
T m_member { 0 };
1111
};
1212

1313
void bar(Foo<int, int> foo);
1414

15+
namespace mozilla {
16+
class Foo;
17+
};
18+
1519
struct C {
1620
B<unsigned int> mB;
1721
B<const int*> mBConstPtr;
22+
B<const mozilla::Foo*> mBConstStructPtr;
1823
B<const int> mBConst;
1924
B<volatile int> mBVolatile;
2025
B<const bool> mBConstBool;

0 commit comments

Comments
 (0)