Skip to content

Commit d2af109

Browse files
authored
Merge pull request #602 from emilio/clang-5
clang: Fix most of the clang 5.0 regressions in our tests.
2 parents 7ed7c24 + 9021854 commit d2af109

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/clang.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,17 @@ impl Cursor {
600600
}
601601
}
602602

603+
/// Checks whether the name looks like an identifier, i.e. is alphanumeric
604+
/// (including '_') and does not start with a digit.
605+
pub fn is_valid_identifier(name: &str) -> bool {
606+
let mut chars = name.chars();
607+
let first_valid = chars.next()
608+
.map(|c| c.is_alphabetic() || c == '_')
609+
.unwrap_or(false);
610+
611+
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
612+
}
613+
603614
extern "C" fn visit_children<Visitor>(cur: CXCursor,
604615
_parent: CXCursor,
605616
data: CXClientData)
@@ -729,7 +740,16 @@ impl Type {
729740

730741
/// Get a raw display name for this type.
731742
pub fn spelling(&self) -> String {
732-
unsafe { cxstring_into_string(clang_getTypeSpelling(self.x)) }
743+
let s = unsafe { cxstring_into_string(clang_getTypeSpelling(self.x)) };
744+
// Clang 5.0 introduced changes in the spelling API so it returned the
745+
// full qualified name. Let's undo that here.
746+
if s.split("::").all(|s| is_valid_identifier(s)) {
747+
if let Some(s) = s.split("::").last() {
748+
return s.to_owned();
749+
}
750+
}
751+
752+
s
733753
}
734754

735755
/// Is this type const qualified?

src/ir/ty.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,7 @@ impl Type {
429429
/// Checks whether the name looks like an identifier,
430430
/// i.e. is alphanumeric (including '_') and does not start with a digit.
431431
pub fn is_valid_identifier(name: &str) -> bool {
432-
let mut chars = name.chars();
433-
let first_valid = chars.next()
434-
.map(|c| c.is_alphabetic() || c == '_')
435-
.unwrap_or(false);
436-
437-
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
432+
clang::is_valid_identifier(name)
438433
}
439434

440435
/// See safe_canonical_type.

tests/expectations/tests/struct_typedef_ns.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,29 @@ pub mod root {
1313
use self::super::super::root;
1414
#[repr(C)]
1515
#[derive(Debug, Default, Copy)]
16-
pub struct _bindgen_ty_1 {
16+
pub struct typedef_struct {
1717
pub foo: ::std::os::raw::c_int,
1818
}
1919
#[test]
20-
fn bindgen_test_layout__bindgen_ty_1() {
21-
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize ,
22-
concat ! ( "Size of: " , stringify ! ( _bindgen_ty_1 )
20+
fn bindgen_test_layout_typedef_struct() {
21+
assert_eq!(::std::mem::size_of::<typedef_struct>() , 4usize ,
22+
concat ! ( "Size of: " , stringify ! ( typedef_struct )
2323
));
24-
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize ,
24+
assert_eq! (::std::mem::align_of::<typedef_struct>() , 4usize ,
2525
concat ! (
26-
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
26+
"Alignment of " , stringify ! ( typedef_struct ) ));
2727
assert_eq! (unsafe {
28-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . foo as *
28+
& ( * ( 0 as * const typedef_struct ) ) . foo as *
2929
const _ as usize } , 0usize , concat ! (
30-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 )
31-
, "::" , stringify ! ( foo ) ));
30+
"Alignment of field: " , stringify ! ( typedef_struct
31+
) , "::" , stringify ! ( foo ) ));
3232
}
33-
impl Clone for _bindgen_ty_1 {
33+
impl Clone for typedef_struct {
3434
fn clone(&self) -> Self { *self }
3535
}
36-
pub type typedef_struct = root::whatever::_bindgen_ty_1;
37-
pub const whatever_BAR: root::whatever::_bindgen_ty_2 =
38-
_bindgen_ty_2::BAR;
3936
#[repr(u32)]
4037
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
41-
pub enum _bindgen_ty_2 { BAR = 1, }
42-
pub use self::super::super::root::whatever::_bindgen_ty_2 as
43-
typedef_enum;
38+
pub enum typedef_enum { BAR = 1, }
4439
}
4540
pub mod _bindgen_mod_id_12 {
4641
#[allow(unused_imports)]

0 commit comments

Comments
 (0)