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

Bindgen should not generate bindings for deleted member functions #2043

Closed
martinboehme opened this issue Apr 28, 2021 · 2 comments
Closed

Comments

@martinboehme
Copy link
Contributor

Input C/C++ Header

class A {
 public:
      void foo() = delete;
};

Bindgen Invocation

bindgen input.h --generate-inline-functions -- -x c++

Actual Results

/* automatically generated by rust-bindgen 0.58.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A {
    pub _address: u8,
}
#[test]
fn bindgen_test_layout_A() {
    assert_eq!(
        ::std::mem::size_of::<A>(),
        1usize,
        concat!("Size of: ", stringify!(A))
    );
    assert_eq!(
        ::std::mem::align_of::<A>(),
        1usize,
        concat!("Alignment of ", stringify!(A))
    );
}
extern "C" {
    #[link_name = "\u{1}_ZN1A3fooEv"]
    pub fn A_foo(this: *mut A);
}
impl A {
    #[inline]
    pub unsafe fn foo(&mut self) {
        A_foo(self)
    }
}

Expected Results

Bindgen should not generate a binding for A::foo(). Attempting to compile and link this binding results in a linker error for an undefined symbol; this should not happen.

@martinboehme
Copy link
Contributor Author

This may be hard to fix because, AFAICT, libclang doesn't currently offer a possibility to check whether a member function is deleted, but I thought I would report it anyway.

In the more common case where the --generate-inline-functions flag is not specified, bindgen does the right thing, somewhat coincidentally, because deleted functions are implicitly inline according to paragraph 4 of [dcl.fct.def.delete] in the C++ standard.

@martinboehme
Copy link
Contributor Author

I think I have a plan for how to tackle this.

libclang doesn't have an API for checking if a member function is deleted, but I think we can get a good enough approximation by checking these three criteria:

  • Is the member function inline (check with clang_Cursor_isFunctionInlined())?
  • Does it lack a definition (check with clang_getCursorDefinition())?
  • Is it not defaulted (check with clang_CXXMethod_isDefaulted())?

If these three criteria are true, I think we can relatively safely conclude that the member function is deleted and that we should therefore not generate a binding for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant