Skip to content

Commit

Permalink
Add opt-in flag for respecting C++ access specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
SilensAngelusNex committed Jan 12, 2021
1 parent a91f185 commit 2d4e71e
Show file tree
Hide file tree
Showing 115 changed files with 434 additions and 346 deletions.
22 changes: 16 additions & 6 deletions src/codegen/mod.rs
Expand Up @@ -1271,10 +1271,18 @@ impl<'a> FieldCodegen<'a> for FieldData {
}
}

let is_private = !self.is_public() ||

let is_private = (!self.is_public() && ctx.options().respect_cxx_access_specs) ||
self.annotations()
.private_fields()
.unwrap_or(fields_should_be_private);
println!("is_pub({}), respect_cxx_access_specs({}), annotations({}) => {}",
self.is_public(),
ctx.options().respect_cxx_access_specs,
self.annotations()
.private_fields()
.unwrap_or(fields_should_be_private),
is_private);

let accessor_kind =
self.annotations().accessor_kind().unwrap_or(accessor_kind);
Expand Down Expand Up @@ -1395,8 +1403,10 @@ impl Bitfield {
}
}

fn access_specifier(is_pub: bool) -> proc_macro2::TokenStream {
if is_pub {
fn access_specifier(ctx: &BindgenContext, is_pub: bool) -> proc_macro2::TokenStream {
println!("is_pub({}), respect_cxx_access_specs({}) => {}", is_pub,
ctx.options().respect_cxx_access_specs, is_pub || !ctx.options().respect_cxx_access_specs);
if is_pub || !ctx.options().respect_cxx_access_specs {
quote! { pub }
} else {
quote! {}
Expand Down Expand Up @@ -1516,7 +1526,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
ctor_impl = bf.extend_ctor_impl(ctx, param_name, ctor_impl);
}

let access_spec = access_specifier(access_spec);
let access_spec = access_specifier(ctx, access_spec);

let field = quote! {
#access_spec #unit_field_ident : #field_ty ,
Expand Down Expand Up @@ -1604,7 +1614,7 @@ impl<'a> FieldCodegen<'a> for Bitfield {
let offset = self.offset_into_unit();
let width = self.width() as u8;
let access_spec =
access_specifier(self.is_public() && !fields_should_be_private);
access_specifier(ctx, self.is_public() && !fields_should_be_private);

if parent.is_union() && !parent.can_be_rust_union(ctx) {
methods.extend(Some(quote! {
Expand Down Expand Up @@ -1730,7 +1740,7 @@ impl CodeGenerator for CompInfo {

struct_layout.saw_base(inner_item.expect_type());

let access_spec = access_specifier(base.is_public());
let access_spec = access_specifier(ctx, base.is_public());
fields.push(quote! {
#access_spec #field_name: #inner,
});
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Expand Up @@ -545,6 +545,10 @@ impl Builder {
output_vector.push(name.clone());
}

if self.options.respect_cxx_access_specs {
output_vector.push("--respect-cxx-access-specs".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1518,6 +1522,12 @@ impl Builder {
self.options.dynamic_library_name = Some(dynamic_library_name.into());
self
}

/// Generate bindings as `pub` only if the bound item is publically accessible by C++.
pub fn respect_cxx_access_specs(mut self, doit: bool) -> Self {
self.options.respect_cxx_access_specs = doit;
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -1805,6 +1815,10 @@ struct BindgenOptions {
/// The name of the dynamic library (if we are generating bindings for a shared library). If
/// this is None, no dynamic bindings are created.
dynamic_library_name: Option<String>,

/// Only make generated bindings `pub` if the items would be publically accessible
/// by C++.
respect_cxx_access_specs: bool
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand Down Expand Up @@ -1941,6 +1955,7 @@ impl Default for BindgenOptions {
array_pointers_in_arguments: false,
wasm_import_module_name: None,
dynamic_library_name: None,
respect_cxx_access_specs: false,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/options.rs
Expand Up @@ -493,6 +493,9 @@ where
.long("dynamic-loading")
.takes_value(true)
.help("Use dynamic loading mode with the given library name."),
Arg::with_name("respect-cxx-access-specs")
.long("respect-cxx-access-specs")
.help("Makes generated bindings `pub` only for items if the items are publically accessible in C++."),
]) // .args()
.get_matches_from(args);

Expand Down Expand Up @@ -915,6 +918,11 @@ where
builder = builder.dynamic_library_name(dynamic_library_name);
}

if matches.is_present("respect-cxx-access-specs") {
println!("Respecting private items.");
builder = builder.respect_cxx_access_specs(true);
}

let verbose = matches.is_present("verbose");

Ok((builder, output, verbose))
Expand Down
10 changes: 5 additions & 5 deletions tests/expectations/tests/class_nested.rs
Expand Up @@ -13,7 +13,7 @@ pub struct A {
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct A_B {
member_b: ::std::os::raw::c_int,
pub member_b: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_A_B() {
Expand Down Expand Up @@ -43,7 +43,7 @@ fn bindgen_test_layout_A_B() {
#[repr(C)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct A_D<T> {
foo: T,
pub foo: T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl<T> Default for A_D<T> {
Expand Down Expand Up @@ -77,7 +77,7 @@ fn bindgen_test_layout_A() {
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct A_C {
baz: ::std::os::raw::c_int,
pub baz: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_A_C() {
Expand Down Expand Up @@ -125,7 +125,7 @@ extern "C" {
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct D {
member: A_B,
pub member: A_B,
}
#[test]
fn bindgen_test_layout_D() {
Expand All @@ -148,7 +148,7 @@ fn bindgen_test_layout_D() {
#[repr(C)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Templated<T> {
member: T,
pub member: T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/class_with_dtor.rs
Expand Up @@ -8,7 +8,7 @@
#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct HandleWithDtor<T> {
ptr: *mut T,
pub ptr: *mut T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl<T> Default for HandleWithDtor<T> {
Expand All @@ -20,7 +20,7 @@ pub type HandleValue = HandleWithDtor<::std::os::raw::c_int>;
#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct WithoutDtor {
shouldBeWithDtor: HandleValue,
pub shouldBeWithDtor: HandleValue,
}
#[test]
fn bindgen_test_layout_WithoutDtor() {
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/tests/class_with_inner_struct.rs
Expand Up @@ -8,9 +8,9 @@
#[repr(C)]
#[derive(Copy, Clone)]
pub struct A {
c: ::std::os::raw::c_uint,
named_union: A__bindgen_ty_1,
__bindgen_anon_1: A__bindgen_ty_2,
pub c: ::std::os::raw::c_uint,
pub named_union: A__bindgen_ty_1,
pub __bindgen_anon_1: A__bindgen_ty_2,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Default for A {
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct B {
d: ::std::os::raw::c_uint,
pub d: ::std::os::raw::c_uint,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -239,8 +239,8 @@ pub enum StepSyntax {
#[repr(C)]
#[derive(Copy, Clone)]
pub struct C {
d: ::std::os::raw::c_uint,
__bindgen_anon_1: C__bindgen_ty_1,
pub d: ::std::os::raw::c_uint,
pub __bindgen_anon_1: C__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/tests/class_with_inner_struct_1_0.rs
Expand Up @@ -51,9 +51,9 @@ impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
pub struct A {
c: ::std::os::raw::c_uint,
named_union: A__bindgen_ty_1,
__bindgen_anon_1: A__bindgen_ty_2,
pub c: ::std::os::raw::c_uint,
pub named_union: A__bindgen_ty_1,
pub __bindgen_anon_1: A__bindgen_ty_2,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -213,7 +213,7 @@ impl Clone for A {
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
pub struct B {
d: ::std::os::raw::c_uint,
pub d: ::std::os::raw::c_uint,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -297,8 +297,8 @@ pub enum StepSyntax {
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq)]
pub struct C {
d: ::std::os::raw::c_uint,
__bindgen_anon_1: C__bindgen_ty_1,
pub d: ::std::os::raw::c_uint,
pub __bindgen_anon_1: C__bindgen_ty_1,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/comment-indent.rs
Expand Up @@ -66,7 +66,7 @@ pub mod root {
/// +------+ +-------+
/// | foo | ----> | bar |
/// +------+ +-------+
member: ::std::os::raw::c_int,
pub member: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_Baz() {
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/const_tparam.rs
Expand Up @@ -8,8 +8,8 @@
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct C<T> {
foo: *const T,
bar: *const T,
pub foo: *const T,
pub bar: *const T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl<T> Default for C<T> {
Expand Down
16 changes: 8 additions & 8 deletions tests/expectations/tests/constify-module-enums-simple-alias.rs
Expand Up @@ -17,14 +17,14 @@ pub use self::Foo_alias2 as Foo_alias3;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
baz1: Foo::Type,
baz2: Foo_alias1,
baz3: Foo_alias2,
baz4: Foo_alias3,
baz_ptr1: *mut Foo::Type,
baz_ptr2: *mut Foo_alias1,
baz_ptr3: *mut Foo_alias2,
baz_ptr4: *mut Foo_alias3,
pub baz1: Foo::Type,
pub baz2: Foo_alias1,
pub baz3: Foo_alias2,
pub baz4: Foo_alias3,
pub baz_ptr1: *mut Foo::Type,
pub baz_ptr2: *mut Foo_alias1,
pub baz_ptr3: *mut Foo_alias2,
pub baz_ptr4: *mut Foo_alias3,
}
#[test]
fn bindgen_test_layout_Bar() {
Expand Down
Expand Up @@ -13,8 +13,8 @@ pub mod one_Foo {
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
baz1: one_Foo::Type,
baz2: *mut one_Foo::Type,
pub baz1: one_Foo::Type,
pub baz2: *mut one_Foo::Type,
}
#[test]
fn bindgen_test_layout_Bar() {
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/constify-module-enums-types.rs
Expand Up @@ -174,7 +174,7 @@ impl Default for bar {
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Baz {
member1: ns2_Foo::Type,
pub member1: ns2_Foo::Type,
}
#[test]
fn bindgen_test_layout_Baz() {
Expand Down Expand Up @@ -212,7 +212,7 @@ pub mod one_Foo {
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
baz: *mut one_Foo::Type,
pub baz: *mut one_Foo::Type,
}
#[test]
fn bindgen_test_layout_Bar() {
Expand Down Expand Up @@ -256,7 +256,7 @@ extern "C" {
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Thing<T> {
thing: T,
pub thing: T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl<T> Default for Thing<T> {
Expand Down
14 changes: 7 additions & 7 deletions tests/expectations/tests/derive-debug-bitfield-core.rs
Expand Up @@ -97,8 +97,8 @@ where
#[derive(Copy, Clone)]
pub struct C {
pub _bitfield_align_1: [u8; 0],
_bitfield_1: __BindgenBitfieldUnit<[u8; 1usize]>,
large_array: [::std::os::raw::c_int; 50usize],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize]>,
pub large_array: [::std::os::raw::c_int; 50usize],
}
#[test]
fn bindgen_test_layout_C() {
Expand Down Expand Up @@ -142,33 +142,33 @@ impl ::core::fmt::Debug for C {
}
impl C {
#[inline]
fn a(&self) -> bool {
pub fn a(&self) -> bool {
unsafe {
::core::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u8)
}
}
#[inline]
fn set_a(&mut self, val: bool) {
pub fn set_a(&mut self, val: bool) {
unsafe {
let val: u8 = ::core::mem::transmute(val);
self._bitfield_1.set(0usize, 1u8, val as u64)
}
}
#[inline]
fn b(&self) -> bool {
pub fn b(&self) -> bool {
unsafe {
::core::mem::transmute(self._bitfield_1.get(1usize, 7u8) as u8)
}
}
#[inline]
fn set_b(&mut self, val: bool) {
pub fn set_b(&mut self, val: bool) {
unsafe {
let val: u8 = ::core::mem::transmute(val);
self._bitfield_1.set(1usize, 7u8, val as u64)
}
}
#[inline]
fn new_bitfield_1(
pub fn new_bitfield_1(
a: bool,
b: bool,
) -> __BindgenBitfieldUnit<[u8; 1usize]> {
Expand Down

0 comments on commit 2d4e71e

Please sign in to comment.