Skip to content

Commit

Permalink
clang: Detect anonymous items explicitly, rather than relying on empt…
Browse files Browse the repository at this point in the history
…y names.

In Clang 16, anonymous items may return names like
`(anonymous union at ..)` rather than empty names.

The right way to detect them is using clang_Cursor_isAnonymous.

Fixes #2312
Closes #2316

Co-Authored-by: Patrick Walton <[email protected]>
  • Loading branch information
emilio and pcwalton committed Oct 22, 2022
1 parent 0142c0a commit 8073801
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 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
3 changes: 1 addition & 2 deletions bindgen/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,7 @@ impl CompInfo {

// A declaration of an union or a struct without name
// could also be an unnamed field, unfortunately.
if cur.spelling().is_empty() &&
cur.kind() != CXCursor_EnumDecl
if cur.is_anonymous() && cur.kind() != CXCursor_EnumDecl
{
let ty = cur.cur_type();
let public = cur.public_accessible();
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()).filter(|n| !n.is_empty())
};

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 8073801

Please sign in to comment.