rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

bindgen disregards c++ using directives.

Open wangbj opened this issue 3 years ago • 2 comments

Input C/C++ Header

#include <compare>, found in gcc 10.0+, see link:

namespace std _GLIBCXX_VISIBILITY(default)
{
  // [cmp.categories], comparison category types

  namespace __cmp_cat
  {
    using type = signed char;

    enum class _Ord : type { equivalent = 0, less = -1, greater = 1 };

    enum class _Ncmp : type { _Unordered = 2 };

    struct __unspec
    {
      constexpr __unspec(__unspec*) noexcept { }
    };
  }
  // snip
}

Bindgen Invocation

bindgen flags:

        opaque_types = [
            "std::.*",
        ],

bindgen output:

        pub mod __cmp_cat {
            #[allow(unused_imports)]
            use self::super::super::super::root;
            pub type type_ = u8;
            pub const _Ord_equivalent: root::std::__cmp_cat::_Ord = 0;
            pub const _Ord_less: root::std::__cmp_cat::_Ord = -1;
            pub const _Ord_greater: root::std::__cmp_cat::_Ord = 1;
            pub type _Ord = root::std::__cmp_cat::type_;
            pub const _Ncmp__Unordered: root::std::__cmp_cat::_Ncmp = 2;
            pub type _Ncmp = root::std::__cmp_cat::type_;
            #[repr(C)]
            #[repr(align(1))]
            #[derive(Debug, Copy, Clone)]
            pub struct __unspec {
                pub _bindgen_opaque_blob: u8,
            }
        }

Actual Results

error[E0600]: cannot apply unary operator `-` to type `u8`
    --> xxx/lib.rs:1763:63
     |
1763 |             pub const _Ord_less: root::std::__cmp_cat::_Ord = -1;
     |                                                               ^^
     |                                                               |
     |                                                               cannot apply unary operator `-`
     |                                                               help: you may have meant the maximum value of `u8`: `u8::MAX`
     |
     = note: unsigned values cannot be negated

Expected Results

Bindgen should use pub tye type_ = i8 (instead of u8), because c++ has: using type = signed char;, hence the code should compile.

wangbj avatar May 25 '22 15:05 wangbj

This works without the opaque_types("std::*") (we'd generate c_schar). We always generate a blob of bytes for opaque types, which are unsigned. Maybe we could disregard opaque_types for enums or so, or not generate variants of opaque enums.

emilio avatar Jun 05 '22 16:06 emilio

FYI I was able to get around of this by adding "std::__cmp_cat::.*" to blocklist_types.

wangbj avatar Jun 14 '22 23:06 wangbj