diff options
author | Christian Poveda Ruiz <31802960+pvdrz@users.noreply.github.com> | 2022-11-10 10:43:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 10:43:02 -0500 |
commit | db4ea32e2d810e765a4c40479e053d0a61a875cf (patch) | |
tree | e08d61cdcfb3aa5c7e4fafb0f8dc4eb6261bbfcd /bindgen/ir/ty.rs | |
parent | ed3aa90cd4c4e1e59d15942414a6dbc586ac1ed4 (diff) |
Handle the `const struct *` and `struct *` patterns (#2304)
Given that C keeps a different namespace for `struct`/`enum`/`union` and `typedef` aliases. The
following patterns
```c
typedef const struct foo {
void *inner;
} *foo;
typedef struct bar {
void *inner;
} *bar;
```
are valid C code and produces both a `struct` and a pointer called `foo`
and `bar` in different namespaces. Given that Rust does not make this
distinction, we add the `_ptr` prefix to the pointer type aliases to
avoid any name collisions.
Diffstat (limited to 'bindgen/ir/ty.rs')
-rw-r--r-- | bindgen/ir/ty.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/bindgen/ir/ty.rs b/bindgen/ir/ty.rs index c9403f66..fd6108f7 100644 --- a/bindgen/ir/ty.rs +++ b/bindgen/ir/ty.rs @@ -1094,9 +1094,9 @@ impl Type { } CXType_Typedef => { let inner = cursor.typedef_type().expect("Not valid Type?"); - let inner = + let inner_id = Item::from_ty_or_ref(inner, location, None, ctx); - if inner == potential_id { + if inner_id == potential_id { warn!( "Generating oqaque type instead of self-referential \ typedef"); @@ -1104,7 +1104,22 @@ impl Type { // within the clang parsing. TypeKind::Opaque } else { - TypeKind::Alias(inner) + // Check if this type definition is an alias to a pointer of a `struct` / + // `union` / `enum` with the same name and add the `_ptr` suffix to it to + // avoid name collisions. + if let Some(ref mut name) = name { + if inner.kind() == CXType_Pointer && + !ctx.options().c_naming + { + let pointee = inner.pointee_type().unwrap(); + if pointee.kind() == CXType_Elaborated && + pointee.declaration().spelling() == *name + { + *name += "_ptr"; + } + } + } + TypeKind::Alias(inner_id) } } CXType_Enum => { |