Skip to content

Commit 1716936

Browse files
committed
Struct detection works better now
1 parent 7480298 commit 1716936

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

libbindgen/src/clang.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ impl Cursor {
190190
unsafe { clang_getCursorKind(self.x) }
191191
}
192192

193+
pub fn is_definition(&self) -> bool {
194+
unsafe { clang_isCursorDefinition(self.x) == 0 }
195+
}
196+
193197
/// Is the referent an anonymous record definition?
194198
pub fn is_anonymous(&self) -> bool {
195199
unsafe { clang_Cursor_isAnonymous(self.x) != 0 }

libbindgen/src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl CodeGenerator for CompInfo {
724724
}
725725

726726
// generate an opaque enum if struct is a forward declaration
727-
if self.kind() == CompKind::Struct && self.is_declaration() {
727+
if self.kind() == CompKind::Struct && self.is_forward_declaration() {
728728
let name = item.canonical_name(ctx);
729729
let builder = EnumBuilder::new(
730730
aster::AstBuilder::new().item().pub_(),

libbindgen/src/ir/comp.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ pub struct CompInfo {
237237
/// around the template arguments.
238238
detect_has_destructor_cycle: Cell<bool>,
239239

240-
is_declaration: bool,
240+
/// Used to indicate when a struct has been forward declared. Usually used
241+
/// in headers so that APIs can't modify them directly.
242+
/// e.g: struct forward_declared_struct;
243+
is_forward_declaration: bool,
241244
}
242245

243246
impl CompInfo {
@@ -262,7 +265,7 @@ impl CompInfo {
262265
found_unknown_attr: false,
263266
detect_derive_debug_cycle: Cell::new(false),
264267
detect_has_destructor_cycle: Cell::new(false),
265-
is_declaration: false,
268+
is_forward_declaration: false,
266269
}
267270
}
268271

@@ -509,7 +512,8 @@ impl CompInfo {
509512
debug!("CompInfo::from_ty({:?}, {:?})", kind, cursor);
510513

511514
let mut ci = CompInfo::new(kind);
512-
ci.is_declaration = cursor.definition().unwrap().is_declaration();
515+
ci.is_forward_declaration = location.map_or(true, |cur|
516+
cur.is_definition() && cur.kind() == CXCursor_StructDecl);
513517
ci.is_anonymous = cursor.is_anonymous();
514518
ci.template_args = match ty.template_args() {
515519
// In forward declarations and not specializations,
@@ -861,8 +865,8 @@ impl CompInfo {
861865
})
862866
}
863867

864-
pub fn is_declaration(&self) -> bool {
865-
self.is_declaration
868+
pub fn is_forward_declaration(&self) -> bool {
869+
self.is_forward_declaration
866870
}
867871
}
868872

libbindgen/tests/expectations/tests/anon_union.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,8 @@ pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
4242
#[repr(i32)]
4343
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4444
pub enum TErrorResult_UnionState { HasMessage = 0, }
45-
#[repr(C)]
46-
#[derive(Debug, Copy, Clone)]
47-
pub struct TErrorResult_Message<T> {
48-
pub _address: u8,
49-
pub _phantom_0: ::std::marker::PhantomData<T>,
50-
}
51-
#[repr(C)]
52-
#[derive(Debug, Copy, Clone)]
53-
pub struct TErrorResult_DOMExceptionInfo<T> {
54-
pub _address: u8,
55-
pub _phantom_0: ::std::marker::PhantomData<T>,
56-
}
45+
pub enum TErrorResult_Message { }
46+
pub enum TErrorResult_DOMExceptionInfo { }
5747
#[repr(C)]
5848
#[derive(Debug, Copy, Clone)]
5949
pub struct TErrorResult__bindgen_ty_1<T> {

libbindgen/tests/expectations/tests/namespace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub mod root {
6666
#[link_name = "_ZN1w3fooEv"]
6767
pub fn foo() -> root::C<::std::os::raw::c_int>;
6868
}
69+
pub enum C { }
6970
extern "C" {
7071
#[link_name = "_ZN1w4barrEv"]
7172
pub fn barr() -> root::C<f32>;

libbindgen/tests/expectations/tests/same_struct_name_in_different_namespaces.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#![allow(non_snake_case)]
55

66

7-
#[repr(C)]
8-
#[derive(Debug, Copy)]
9-
pub struct JS_Zone {
10-
pub _address: u8,
11-
}
12-
impl Clone for JS_Zone {
13-
fn clone(&self) -> Self { *self }
14-
}
7+
pub enum JS_Zone { }
158
#[repr(C)]
169
#[derive(Debug, Copy)]
1710
pub struct JS_shadow_Zone {

0 commit comments

Comments
 (0)