Skip to content

Commit 5345bae

Browse files
committed
Update the supported clang version for running header-translator
I had to modify the struct/typedef code a bit, since clang now does those differently.
1 parent 2eff74f commit 5345bae

File tree

9 files changed

+140
-156
lines changed

9 files changed

+140
-156
lines changed

crates/header-translator/src/library.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl fmt::Display for Library {
135135
// NOTE: some SDK files have '+' in the file name
136136
let name = name.replace('+', "_");
137137
for stmt in &file.stmts {
138-
let mut iter = stmt.declared_types();
138+
let mut iter = stmt.declared_types().filter(|item| !item.starts_with('_'));
139139
if let Some(item) = iter.next() {
140140
// Use a set to deduplicate features, and to have them in
141141
// a consistent order

crates/header-translator/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn main() -> Result<(), BoxError> {
2929
// metadata.is_span() && metadata.level() == &tracing::Level::INFO
3030
// })),
3131
// )
32+
// .with(tracing_subscriber::fmt::Layer::default().with_filter(LevelFilter::ERROR))
3233
.with(
3334
HierarchicalLayer::new(2)
3435
.with_targets(false)

crates/header-translator/src/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ impl MemoryManagement {
222222
}
223223
}
224224
} else if let MethodModifiers {
225-
designated_initializer: false,
226225
// TODO: Maybe we can use this to emit things with lifetime of:
227226
// `'self + 'autoreleasepool`
228227
returns_inner_pointer: _,
229228
consumes_self: false,
230229
returns_retained: false,
231230
returns_not_retained: false,
231+
designated_initializer: false,
232232
..
233233
} = modifiers
234234
{

crates/header-translator/src/rust_type.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ impl Inner {
574574

575575
let _span = debug_span!("ty3", ?ty).entered();
576576

577+
let elaborated_ty = ty;
578+
579+
if let Some(true) = ty.is_elaborated() {
580+
ty = ty.get_elaborated_type().expect("elaborated");
581+
}
582+
583+
let _span = debug_span!("ty4", ?ty).entered();
584+
577585
let get_is_const = |new: bool| {
578586
if new {
579587
if !attributed_ty.is_const_qualified() || ty.is_const_qualified() {
@@ -609,6 +617,26 @@ impl Inner {
609617
ULongLong => Self::ULongLong,
610618
Float => Self::Float,
611619
Double => Self::Double,
620+
Record => {
621+
let declaration = ty.get_declaration().expect("record declaration");
622+
let name = ty
623+
.get_display_name()
624+
.trim_start_matches("struct ")
625+
.to_string();
626+
Self::Struct {
627+
id: ItemIdentifier::with_name(name, &declaration, context),
628+
}
629+
}
630+
Enum => {
631+
let declaration = ty.get_declaration().expect("enum declaration");
632+
let name = ty
633+
.get_display_name()
634+
.trim_start_matches("enum ")
635+
.to_string();
636+
Self::Enum {
637+
id: ItemIdentifier::with_name(name, &declaration, context),
638+
}
639+
}
612640
ObjCId => {
613641
let mut parser = AttributeParser::new(&attributed_name, "id");
614642

@@ -785,13 +813,27 @@ impl Inner {
785813
drop(parser);
786814

787815
let is_const = if is_const1 || is_const2 {
788-
if !attributed_ty.is_const_qualified() && !ty.is_const_qualified() {
789-
warn!(?attributed_ty, ?ty, ?typedef_name, ?is_const1, ?is_const2, attr = ?attributed_ty.is_const_qualified(), ty = ?ty.is_const_qualified(), "unnecessarily stripped const");
816+
if !attributed_ty.is_const_qualified()
817+
&& !elaborated_ty.is_const_qualified()
818+
&& !ty.is_const_qualified()
819+
{
820+
warn!(
821+
?attributed_ty,
822+
?elaborated_ty,
823+
?ty,
824+
?typedef_name,
825+
?is_const1,
826+
?is_const2,
827+
attr = ?attributed_ty.is_const_qualified(),
828+
elaborated = ?elaborated_ty.is_const_qualified(),
829+
ty = ?ty.is_const_qualified(),
830+
"typedef unnecessarily stripped const",
831+
);
790832
}
791833
true
792834
} else {
793835
if ty.is_const_qualified() {
794-
warn!("type was const but that could not be stripped");
836+
warn!("typedef was const but that could not be stripped");
795837
}
796838
false
797839
};
@@ -889,32 +931,6 @@ impl Inner {
889931
}
890932
}
891933
}
892-
Elaborated => {
893-
let ty = ty.get_elaborated_type().expect("elaborated");
894-
match ty.get_kind() {
895-
TypeKind::Record => {
896-
let declaration = ty.get_declaration().expect("record declaration");
897-
let name = ty
898-
.get_display_name()
899-
.trim_start_matches("struct ")
900-
.to_string();
901-
Self::Struct {
902-
id: ItemIdentifier::with_name(name, &declaration, context),
903-
}
904-
}
905-
TypeKind::Enum => {
906-
let declaration = ty.get_declaration().expect("enum declaration");
907-
let name = ty
908-
.get_display_name()
909-
.trim_start_matches("enum ")
910-
.to_string();
911-
Self::Enum {
912-
id: ItemIdentifier::with_name(name, &declaration, context),
913-
}
914-
}
915-
_ => panic!("unknown elaborated type {ty:?}"),
916-
}
917-
}
918934
FunctionPrototype => {
919935
let call_conv = ty.get_calling_convention().expect("fn calling convention");
920936
assert_eq!(
@@ -1368,12 +1384,10 @@ impl Ty {
13681384
match &mut ty {
13691385
// Handled by Stmt::EnumDecl
13701386
Inner::Enum { .. } => None,
1371-
// Handled above and in Stmt::StructDecl
1387+
// No need to output a typedef if it'll just point to the same thing.
1388+
//
1389+
// TODO: We're discarding a slight bit of availability data this way.
13721390
Inner::Struct { id } if id.name == typedef_name => None,
1373-
Inner::Struct { id } if id.name != typedef_name => {
1374-
warn!(?id, "invalid struct in typedef");
1375-
None
1376-
}
13771391
// Opaque structs
13781392
Inner::Pointer { pointee, .. } if matches!(&**pointee, Inner::Struct { .. }) => {
13791393
**pointee = Inner::Void;

crates/header-translator/src/stmt.rs

Lines changed: 78 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -459,48 +459,6 @@ pub enum Stmt {
459459
},
460460
}
461461

462-
fn parse_struct(
463-
entity: &Entity<'_>,
464-
context: &Context<'_>,
465-
) -> (bool, Vec<(String, Ty)>, Option<bool>) {
466-
let mut boxable = false;
467-
let mut fields = Vec::new();
468-
let mut sendable = None;
469-
470-
immediate_children(entity, |entity, span| match entity.get_kind() {
471-
EntityKind::UnexposedAttr => {
472-
if let Some(attr) = UnexposedAttr::parse(&entity, context) {
473-
match attr {
474-
UnexposedAttr::Sendable => sendable = Some(true),
475-
UnexposedAttr::NonSendable => sendable = Some(false),
476-
attr => error!(?attr, "unknown attribute"),
477-
}
478-
}
479-
}
480-
EntityKind::FieldDecl => {
481-
drop(span);
482-
let name = entity.get_name().expect("struct field name");
483-
let _span = debug_span!("field", name).entered();
484-
485-
let ty = entity.get_type().expect("struct field type");
486-
let ty = Ty::parse_struct_field(ty, context);
487-
488-
if entity.is_bit_field() {
489-
error!("unsound struct bitfield");
490-
}
491-
492-
fields.push((name, ty))
493-
}
494-
EntityKind::ObjCBoxable => {
495-
boxable = true;
496-
}
497-
EntityKind::UnionDecl => error!("can't handle unions in structs yet"),
498-
_ => error!("unknown"),
499-
});
500-
501-
(boxable, fields, sendable)
502-
}
503-
504462
fn parse_fn_param_children(entity: &Entity<'_>, context: &Context<'_>) {
505463
immediate_children(entity, |entity, _span| match entity.get_kind() {
506464
EntityKind::UnexposedAttr => {
@@ -890,9 +848,6 @@ impl Stmt {
890848
EntityKind::TypedefDecl => {
891849
let id = ItemIdentifier::new(entity, context);
892850
let availability = Availability::parse(entity, context);
893-
let mut struct_ = None;
894-
let mut encoding_name = "?".to_string();
895-
let mut skip_struct = false;
896851
let mut kind = None;
897852

898853
immediate_children(entity, |entity, _span| match entity.get_kind() {
@@ -908,55 +863,14 @@ impl Stmt {
908863
}
909864
}
910865
}
911-
EntityKind::StructDecl => {
912-
if context
913-
.struct_data
914-
.get(&id.name)
915-
.map(|data| data.skipped)
916-
.unwrap_or_default()
917-
{
918-
skip_struct = true;
919-
return;
920-
}
921-
922-
if let Some(name) = entity.get_name() {
923-
// if the struct has a name use it
924-
// otherwise it will be the default "?"
925-
encoding_name = name;
926-
}
927-
928-
if encoding_name == "?" || encoding_name.starts_with('_') {
929-
// If this struct doesn't have a name, or the
930-
// name is private, let's parse it with the
931-
// typedef name.
932-
struct_ = Some(parse_struct(&entity, context))
933-
} else {
934-
skip_struct = true;
935-
}
936-
}
937-
EntityKind::ObjCClassRef
866+
EntityKind::StructDecl
867+
| EntityKind::ObjCClassRef
938868
| EntityKind::ObjCProtocolRef
939869
| EntityKind::TypeRef
940870
| EntityKind::ParmDecl => {}
941871
_ => error!("unknown"),
942872
});
943873

944-
if let Some((boxable, fields, sendable)) = struct_ {
945-
assert_eq!(kind, None, "should not have parsed a kind");
946-
return vec![Self::StructDecl {
947-
id,
948-
encoding_name: Some(encoding_name),
949-
availability,
950-
boxable,
951-
fields,
952-
sendable,
953-
}];
954-
}
955-
956-
if skip_struct {
957-
return vec![];
958-
}
959-
960874
if context
961875
.typedef_data
962876
.get(&id.name)
@@ -981,37 +895,76 @@ impl Stmt {
981895
}
982896
}
983897
EntityKind::StructDecl => {
984-
if let Some(name) = entity.get_name() {
985-
let availability = Availability::parse(entity, context);
986-
let id = ItemIdentifier::with_name(name, entity, context);
987-
988-
if context
989-
.struct_data
990-
.get(&id.name)
991-
.map(|data| data.skipped)
992-
.unwrap_or_default()
993-
{
994-
return vec![];
995-
}
898+
let id = ItemIdentifier::new(entity, context);
996899

997-
// See https://github.com/rust-lang/rust-bindgen/blob/95fd17b874910184cc0fcd33b287fa4e205d9d7a/bindgen/ir/comp.rs#L1392-L1408
998-
if !entity.is_definition() {
999-
return vec![];
900+
let availability = Availability::parse(entity, context);
901+
902+
if context
903+
.struct_data
904+
.get(&id.name)
905+
.map(|data| data.skipped)
906+
.unwrap_or_default()
907+
{
908+
return vec![];
909+
}
910+
911+
// See https://github.com/rust-lang/rust-bindgen/blob/95fd17b874910184cc0fcd33b287fa4e205d9d7a/bindgen/ir/comp.rs#L1392-L1408
912+
if !entity.is_definition() {
913+
return vec![];
914+
}
915+
916+
let ty = entity.get_type().unwrap();
917+
let enc = ty.get_objc_encoding().unwrap();
918+
let encoding_name = enc.strip_prefix('{').unwrap().split_once('=').unwrap().0;
919+
let encoding_name = if encoding_name == id.name {
920+
None
921+
} else {
922+
Some(encoding_name.to_string())
923+
};
924+
925+
let mut boxable = false;
926+
let mut fields = Vec::new();
927+
let mut sendable = None;
928+
929+
immediate_children(entity, |entity, span| match entity.get_kind() {
930+
EntityKind::UnexposedAttr => {
931+
if let Some(attr) = UnexposedAttr::parse(&entity, context) {
932+
match attr {
933+
UnexposedAttr::Sendable => sendable = Some(true),
934+
UnexposedAttr::NonSendable => sendable = Some(false),
935+
attr => error!(?attr, "unknown attribute"),
936+
}
937+
}
1000938
}
939+
EntityKind::FieldDecl => {
940+
drop(span);
941+
let name = entity.get_name().expect("struct field name");
942+
let _span = debug_span!("field", name).entered();
1001943

1002-
if !id.name.starts_with('_') {
1003-
let (boxable, fields, sendable) = parse_struct(entity, context);
1004-
return vec![Self::StructDecl {
1005-
id,
1006-
encoding_name: None,
1007-
availability,
1008-
boxable,
1009-
fields,
1010-
sendable,
1011-
}];
944+
let ty = entity.get_type().expect("struct field type");
945+
let ty = Ty::parse_struct_field(ty, context);
946+
947+
if entity.is_bit_field() {
948+
error!("unsound struct bitfield");
949+
}
950+
951+
fields.push((name, ty))
1012952
}
1013-
}
1014-
vec![]
953+
EntityKind::ObjCBoxable => {
954+
boxable = true;
955+
}
956+
EntityKind::UnionDecl => error!("can't handle unions in structs yet"),
957+
_ => error!("unknown"),
958+
});
959+
960+
return vec![Self::StructDecl {
961+
id,
962+
encoding_name,
963+
availability,
964+
boxable,
965+
fields,
966+
sendable,
967+
}];
1015968
}
1016969
EntityKind::EnumDecl => {
1017970
// Enum declarations show up twice for some reason, but
@@ -1020,7 +973,16 @@ impl Stmt {
1020973
return vec![];
1021974
}
1022975

1023-
let id = ItemIdentifier::new_optional(entity, context);
976+
let mut id = ItemIdentifier::new_optional(entity, context);
977+
978+
if id
979+
.name
980+
.as_deref()
981+
.map(|name| name.starts_with("enum (unnamed at"))
982+
.unwrap_or(false)
983+
{
984+
id.name = None;
985+
}
1024986

1025987
let data = context
1026988
.enum_data

0 commit comments

Comments
 (0)