Skip to content

Commit

Permalink
Anon fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Oct 18, 2022
1 parent 66388de commit e69e0df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ impl Cursor {
unsafe { clang_isDeclaration(self.kind()) != 0 }
}

/// Is this cursor's referent an anonymous record or so?
pub fn is_anonymous(&self) -> bool {
unsafe { clang_Cursor_isAnonymous(self.x) != 0 }
}

/// Get this cursor's referent's spelling.
pub fn spelling(&self) -> String {
unsafe { cxstring_into_string(clang_getCursorSpelling(self.x)) }
Expand Down
27 changes: 17 additions & 10 deletions bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,12 @@ impl Type {

let layout = ty.fallible_layout(ctx).ok();
let cursor = ty.declaration();
let mut name = cursor.spelling();
let is_anonymous = cursor.is_anonymous();
let mut name = if is_anonymous {
None
} else {
Some(cursor.spelling())
};

debug!(
"from_clang_ty: {:?}, ty: {:?}, loc: {:?}",
Expand Down Expand Up @@ -732,7 +737,7 @@ impl Type {
if is_canonical_objcpointer && is_template_type_param {
// Objective-C generics are just ids with fancy name.
// To keep it simple, just name them ids
name = "id".to_owned();
name = Some("id".to_owned());
}
}

Expand Down Expand Up @@ -861,7 +866,7 @@ impl Type {
return Err(ParseError::Recurse);
}
} else {
name = location.spelling();
name = Some(location.spelling());
}

let complex = CompInfo::from_ty(
Expand Down Expand Up @@ -903,7 +908,7 @@ impl Type {
CXType_Typedef
);

name = current.spelling();
name = Some(location.spelling());

let inner_ty = cur
.typedef_type()
Expand Down Expand Up @@ -1105,10 +1110,10 @@ impl Type {
CXType_Enum => {
let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?");

if name.is_empty() {
if !is_anonymous {
let pretty_name = ty.spelling();
if clang::is_valid_identifier(&pretty_name) {
name = pretty_name;
name = Some(pretty_name);
}
}

Expand All @@ -1123,12 +1128,12 @@ impl Type {
)
.expect("Not a complex type?");

if name.is_empty() {
if !is_anonymous {
// The pretty-printed name may contain typedefed name,
// but may also be "struct (anonymous at .h:1)"
let pretty_name = ty.spelling();
if clang::is_valid_identifier(&pretty_name) {
name = pretty_name;
name = Some(pretty_name);
}
}

Expand Down Expand Up @@ -1168,7 +1173,9 @@ impl Type {
CXType_ObjCClass | CXType_ObjCInterface => {
let interface = ObjCInterface::from_ty(&location, ctx)
.expect("Not a valid objc interface?");
name = interface.rust_name();
if !is_anonymous {
name = Some(interface.rust_name());
}
TypeKind::ObjCInterface(interface)
}
CXType_Dependent => {
Expand All @@ -1186,7 +1193,7 @@ impl Type {
}
};

let name = if name.is_empty() { None } else { Some(name) };
name = name.filter(|n| !n.is_empty());

let is_const = ty.is_const() ||
(ty.kind() == CXType_ConstantArray &&
Expand Down

0 comments on commit e69e0df

Please sign in to comment.