Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang: Detect anonymous items explicitly, rather than relying on empty names. #2319

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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