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

Fix handling of anonymous names in Clang 16. #2316

Closed
wants to merge 1 commit into from
Closed
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: 4 additions & 1 deletion bindgen/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::ty::RUST_DERIVE_IN_ARRAY_LIMIT;
use crate::clang;
use crate::codegen::struct_layout::{align_to, bytes_from_bits_pow2};
use crate::ir::derive::CanDeriveCopy;
use crate::ir::item;
use crate::parse::{ClangItemParser, ParseError};
use crate::HashMap;
use crate::NonCopyUnionStyle;
Expand Down Expand Up @@ -1422,7 +1423,9 @@ impl CompInfo {

// A declaration of an union or a struct without name
// could also be an unnamed field, unfortunately.
if cur.spelling().is_empty() &&
let mut spelling = cur.spelling();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bit I had missed in #2312 (comment), thanks for catching it :)

item::normalize_name_for_clang_16(&mut spelling);
if spelling.is_empty() &&
cur.kind() != CXCursor_EnumDecl
{
let ty = cur.cur_type();
Expand Down
13 changes: 13 additions & 0 deletions bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,3 +2016,16 @@ impl<'a> NameOptions<'a> {
self.item.real_canonical_name(self.ctx, self)
}
}

/// Normalizes names so that we can handle them identically in Clang 16 and earlier versions.
///
/// In Clang 16, anonymous names have names like `(anonymous union at foo.c:16)`, whereas in earlier
/// versions of Clang they were the empty string. This function normalizes all such names to the
/// empty string so that we can handle them identically.
pub fn normalize_name_for_clang_16(name: &mut String) {
// This may seem fragile, but ")" is not a valid character in C identifiers, so it should
// actually be a reasonably robust check.
if name.ends_with(")") {
name.truncate(0);
}
}
5 changes: 5 additions & 0 deletions bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::template::{
};
use super::traversal::{EdgeKind, Trace, Tracer};
use crate::clang::{self, Cursor};
use crate::ir::item;
use crate::parse::{ClangItemParser, ParseError, ParseResult};
use std::borrow::Cow;
use std::io;
Expand Down Expand Up @@ -1112,6 +1113,8 @@ impl Type {
}
}

item::normalize_name_for_clang_16(&mut name);

TypeKind::Enum(enum_)
}
CXType_Record => {
Expand All @@ -1132,6 +1135,8 @@ impl Type {
}
}

item::normalize_name_for_clang_16(&mut name);

TypeKind::Comp(complex)
}
CXType_Vector => {
Expand Down