From 3d2b6362d2aafab7b8322d16b21b89d3a5901e95 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 16 Jun 2023 12:53:08 +0200 Subject: [PATCH] Gather all includes, even ones without a source file. --- .../expectations/tests/allowlist-file.rs | 2 +- .../expectations/tests/jsval_layout_opaque.rs | 4 +- .../tests/jsval_layout_opaque_1_0.rs | 4 +- .../tests/expectations/tests/layout_arp.rs | 14 ++--- .../tests/expectations/tests/layout_array.rs | 14 ++--- .../expectations/tests/layout_eth_conf.rs | 50 ++++++++--------- .../expectations/tests/layout_eth_conf_1_0.rs | 50 ++++++++--------- .../tests/expectations/tests/namespace.rs | 4 +- ...mplate_instantiation_with_fn_local_type.rs | 30 +++++----- .../test_multiple_header_calls_in_builder.rs | 55 ++++++------------- bindgen-tests/tests/tests.rs | 2 +- bindgen/clang.rs | 53 ++++++++---------- bindgen/ir/context.rs | 6 +- 13 files changed, 130 insertions(+), 158 deletions(-) diff --git a/bindgen-tests/tests/expectations/tests/allowlist-file.rs b/bindgen-tests/tests/expectations/tests/allowlist-file.rs index 1ee7a192ed..63953ccb67 100644 --- a/bindgen-tests/tests/expectations/tests/allowlist-file.rs +++ b/bindgen-tests/tests/expectations/tests/allowlist-file.rs @@ -1,5 +1,4 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] -pub const SOME_DEFUN: u32 = 123; extern "C" { #[link_name = "\u{1}_Z12SomeFunctionv"] pub fn SomeFunction(); @@ -7,6 +6,7 @@ extern "C" { extern "C" { pub static mut someVar: ::std::os::raw::c_int; } +pub const SOME_DEFUN: u32 = 123; #[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct someClass { diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs index cfbf2f61cc..4ec58e7825 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs @@ -84,8 +84,6 @@ where } } pub const JSVAL_TAG_SHIFT: u32 = 47; -pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; -pub const JSVAL_TAG_MASK: i64 = -140737488355328; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueType { @@ -127,6 +125,8 @@ pub enum JSValueShiftedTag { JSVAL_SHIFTED_TAG_NULL = 18445477436314353664, JSVAL_SHIFTED_TAG_OBJECT = 18445618173802708992, } +pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; +pub const JSVAL_TAG_MASK: i64 = -140737488355328; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSWhyMagic { diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs index b97bbac96f..fa55d5c1ce 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs @@ -127,8 +127,6 @@ impl ::std::cmp::PartialEq for __BindgenUnionField { } impl ::std::cmp::Eq for __BindgenUnionField {} pub const JSVAL_TAG_SHIFT: u32 = 47; -pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; -pub const JSVAL_TAG_MASK: i64 = -140737488355328; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueType { @@ -170,6 +168,8 @@ pub enum JSValueShiftedTag { JSVAL_SHIFTED_TAG_NULL = 18445477436314353664, JSVAL_SHIFTED_TAG_OBJECT = 18445618173802708992, } +pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; +pub const JSVAL_TAG_MASK: i64 = -140737488355328; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSWhyMagic { diff --git a/bindgen-tests/tests/expectations/tests/layout_arp.rs b/bindgen-tests/tests/expectations/tests/layout_arp.rs index 1ee045593f..ee6bdf01a8 100644 --- a/bindgen-tests/tests/expectations/tests/layout_arp.rs +++ b/bindgen-tests/tests/expectations/tests/layout_arp.rs @@ -1,12 +1,5 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const ETHER_ADDR_LEN: u32 = 6; -pub const ARP_HRD_ETHER: u32 = 1; -pub const ARP_OP_REQUEST: u32 = 1; -pub const ARP_OP_REPLY: u32 = 2; -pub const ARP_OP_REVREQUEST: u32 = 3; -pub const ARP_OP_REVREPLY: u32 = 4; -pub const ARP_OP_INVREQUEST: u32 = 8; -pub const ARP_OP_INVREPLY: u32 = 9; /** Ethernet address: A universally administered address is uniquely assigned to a device by its manufacturer. The first three octets (in transmission order) contain the @@ -133,3 +126,10 @@ fn bindgen_test_layout_arp_hdr() { stringify!(arp_data)) ); } +pub const ARP_HRD_ETHER: u32 = 1; +pub const ARP_OP_REQUEST: u32 = 1; +pub const ARP_OP_REPLY: u32 = 2; +pub const ARP_OP_REVREQUEST: u32 = 3; +pub const ARP_OP_REVREPLY: u32 = 4; +pub const ARP_OP_INVREQUEST: u32 = 8; +pub const ARP_OP_INVREPLY: u32 = 9; diff --git a/bindgen-tests/tests/expectations/tests/layout_array.rs b/bindgen-tests/tests/expectations/tests/layout_array.rs index d9171be24b..3654c62c7c 100644 --- a/bindgen-tests/tests/expectations/tests/layout_array.rs +++ b/bindgen-tests/tests/expectations/tests/layout_array.rs @@ -1,13 +1,6 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const RTE_CACHE_LINE_SIZE: u32 = 64; pub const RTE_MEMPOOL_OPS_NAMESIZE: u32 = 32; -pub const RTE_MEMPOOL_MAX_OPS_IDX: u32 = 16; -pub const RTE_HEAP_NUM_FREELISTS: u32 = 13; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct rte_mempool { - _unused: [u8; 0], -} /** Prototype for implementation specific data provisioning function. The function should provide the implementation specific memory for @@ -19,6 +12,11 @@ pub struct rte_mempool { pub type rte_mempool_alloc_t = ::std::option::Option< unsafe extern "C" fn(mp: *mut rte_mempool) -> ::std::os::raw::c_int, >; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct rte_mempool { + _unused: [u8; 0], +} /// Free the opaque private data pointed to by mp->pool_data pointer. pub type rte_mempool_free_t = ::std::option::Option< unsafe extern "C" fn(mp: *mut rte_mempool), @@ -118,6 +116,7 @@ impl ::std::cmp::PartialEq for rte_mempool_ops { && self.get_count == other.get_count } } +pub const RTE_MEMPOOL_MAX_OPS_IDX: u32 = 16; /// The rte_spinlock_t type. #[repr(C)] #[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] @@ -199,6 +198,7 @@ impl Default for rte_mempool_ops_table { } } } +pub const RTE_HEAP_NUM_FREELISTS: u32 = 13; /// Structure to hold malloc heap #[repr(C)] #[repr(align(64))] diff --git a/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs b/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs index 1ec0744f0d..0916a16b85 100644 --- a/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs +++ b/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs @@ -90,31 +90,6 @@ pub const ETH_VMDQ_MAX_VLAN_FILTERS: u32 = 64; pub const ETH_DCB_NUM_USER_PRIORITIES: u32 = 8; pub const ETH_VMDQ_DCB_NUM_QUEUES: u32 = 128; pub const ETH_DCB_NUM_QUEUES: u32 = 128; -pub const RTE_ETH_FDIR_MAX_FLEXLEN: u32 = 16; -pub const RTE_ETH_INSET_SIZE_MAX: u32 = 128; -pub const RTE_ETH_FLOW_UNKNOWN: u32 = 0; -pub const RTE_ETH_FLOW_RAW: u32 = 1; -pub const RTE_ETH_FLOW_IPV4: u32 = 2; -pub const RTE_ETH_FLOW_FRAG_IPV4: u32 = 3; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_TCP: u32 = 4; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_UDP: u32 = 5; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_SCTP: u32 = 6; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_OTHER: u32 = 7; -pub const RTE_ETH_FLOW_IPV6: u32 = 8; -pub const RTE_ETH_FLOW_FRAG_IPV6: u32 = 9; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_TCP: u32 = 10; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_UDP: u32 = 11; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_SCTP: u32 = 12; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_OTHER: u32 = 13; -pub const RTE_ETH_FLOW_L2_PAYLOAD: u32 = 14; -pub const RTE_ETH_FLOW_IPV6_EX: u32 = 15; -pub const RTE_ETH_FLOW_IPV6_TCP_EX: u32 = 16; -pub const RTE_ETH_FLOW_IPV6_UDP_EX: u32 = 17; -pub const RTE_ETH_FLOW_PORT: u32 = 18; -pub const RTE_ETH_FLOW_VXLAN: u32 = 19; -pub const RTE_ETH_FLOW_GENEVE: u32 = 20; -pub const RTE_ETH_FLOW_NVGRE: u32 = 21; -pub const RTE_ETH_FLOW_MAX: u32 = 22; #[repr(u32)] /** A set of values to identify what method is to be used to route packets to multiple queues.*/ @@ -1214,6 +1189,8 @@ pub enum rte_eth_payload_type { RTE_ETH_L4_PAYLOAD = 4, RTE_ETH_PAYLOAD_MAX = 8, } +pub const RTE_ETH_FDIR_MAX_FLEXLEN: u32 = 16; +pub const RTE_ETH_INSET_SIZE_MAX: u32 = 128; /** A structure used to select bytes extracted from the protocol layers to flexible payload for filter*/ #[repr(C)] @@ -1286,6 +1263,29 @@ fn bindgen_test_layout_rte_eth_fdir_flex_mask() { stringify!(mask)) ); } +pub const RTE_ETH_FLOW_UNKNOWN: u32 = 0; +pub const RTE_ETH_FLOW_RAW: u32 = 1; +pub const RTE_ETH_FLOW_IPV4: u32 = 2; +pub const RTE_ETH_FLOW_FRAG_IPV4: u32 = 3; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_TCP: u32 = 4; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_UDP: u32 = 5; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_SCTP: u32 = 6; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_OTHER: u32 = 7; +pub const RTE_ETH_FLOW_IPV6: u32 = 8; +pub const RTE_ETH_FLOW_FRAG_IPV6: u32 = 9; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_TCP: u32 = 10; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_UDP: u32 = 11; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_SCTP: u32 = 12; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_OTHER: u32 = 13; +pub const RTE_ETH_FLOW_L2_PAYLOAD: u32 = 14; +pub const RTE_ETH_FLOW_IPV6_EX: u32 = 15; +pub const RTE_ETH_FLOW_IPV6_TCP_EX: u32 = 16; +pub const RTE_ETH_FLOW_IPV6_UDP_EX: u32 = 17; +pub const RTE_ETH_FLOW_PORT: u32 = 18; +pub const RTE_ETH_FLOW_VXLAN: u32 = 19; +pub const RTE_ETH_FLOW_GENEVE: u32 = 20; +pub const RTE_ETH_FLOW_NVGRE: u32 = 21; +pub const RTE_ETH_FLOW_MAX: u32 = 22; /** A structure used to define all flexible payload related setting include flex payload and flex mask*/ #[repr(C)] diff --git a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs index 086384c450..c20732c604 100644 --- a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs @@ -133,31 +133,6 @@ pub const ETH_VMDQ_MAX_VLAN_FILTERS: u32 = 64; pub const ETH_DCB_NUM_USER_PRIORITIES: u32 = 8; pub const ETH_VMDQ_DCB_NUM_QUEUES: u32 = 128; pub const ETH_DCB_NUM_QUEUES: u32 = 128; -pub const RTE_ETH_FDIR_MAX_FLEXLEN: u32 = 16; -pub const RTE_ETH_INSET_SIZE_MAX: u32 = 128; -pub const RTE_ETH_FLOW_UNKNOWN: u32 = 0; -pub const RTE_ETH_FLOW_RAW: u32 = 1; -pub const RTE_ETH_FLOW_IPV4: u32 = 2; -pub const RTE_ETH_FLOW_FRAG_IPV4: u32 = 3; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_TCP: u32 = 4; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_UDP: u32 = 5; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_SCTP: u32 = 6; -pub const RTE_ETH_FLOW_NONFRAG_IPV4_OTHER: u32 = 7; -pub const RTE_ETH_FLOW_IPV6: u32 = 8; -pub const RTE_ETH_FLOW_FRAG_IPV6: u32 = 9; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_TCP: u32 = 10; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_UDP: u32 = 11; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_SCTP: u32 = 12; -pub const RTE_ETH_FLOW_NONFRAG_IPV6_OTHER: u32 = 13; -pub const RTE_ETH_FLOW_L2_PAYLOAD: u32 = 14; -pub const RTE_ETH_FLOW_IPV6_EX: u32 = 15; -pub const RTE_ETH_FLOW_IPV6_TCP_EX: u32 = 16; -pub const RTE_ETH_FLOW_IPV6_UDP_EX: u32 = 17; -pub const RTE_ETH_FLOW_PORT: u32 = 18; -pub const RTE_ETH_FLOW_VXLAN: u32 = 19; -pub const RTE_ETH_FLOW_GENEVE: u32 = 20; -pub const RTE_ETH_FLOW_NVGRE: u32 = 21; -pub const RTE_ETH_FLOW_MAX: u32 = 22; #[repr(u32)] /** A set of values to identify what method is to be used to route packets to multiple queues.*/ @@ -1327,6 +1302,8 @@ pub enum rte_eth_payload_type { RTE_ETH_L4_PAYLOAD = 4, RTE_ETH_PAYLOAD_MAX = 8, } +pub const RTE_ETH_FDIR_MAX_FLEXLEN: u32 = 16; +pub const RTE_ETH_INSET_SIZE_MAX: u32 = 128; /** A structure used to select bytes extracted from the protocol layers to flexible payload for filter*/ #[repr(C)] @@ -1409,6 +1386,29 @@ impl Clone for rte_eth_fdir_flex_mask { *self } } +pub const RTE_ETH_FLOW_UNKNOWN: u32 = 0; +pub const RTE_ETH_FLOW_RAW: u32 = 1; +pub const RTE_ETH_FLOW_IPV4: u32 = 2; +pub const RTE_ETH_FLOW_FRAG_IPV4: u32 = 3; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_TCP: u32 = 4; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_UDP: u32 = 5; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_SCTP: u32 = 6; +pub const RTE_ETH_FLOW_NONFRAG_IPV4_OTHER: u32 = 7; +pub const RTE_ETH_FLOW_IPV6: u32 = 8; +pub const RTE_ETH_FLOW_FRAG_IPV6: u32 = 9; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_TCP: u32 = 10; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_UDP: u32 = 11; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_SCTP: u32 = 12; +pub const RTE_ETH_FLOW_NONFRAG_IPV6_OTHER: u32 = 13; +pub const RTE_ETH_FLOW_L2_PAYLOAD: u32 = 14; +pub const RTE_ETH_FLOW_IPV6_EX: u32 = 15; +pub const RTE_ETH_FLOW_IPV6_TCP_EX: u32 = 16; +pub const RTE_ETH_FLOW_IPV6_UDP_EX: u32 = 17; +pub const RTE_ETH_FLOW_PORT: u32 = 18; +pub const RTE_ETH_FLOW_VXLAN: u32 = 19; +pub const RTE_ETH_FLOW_GENEVE: u32 = 20; +pub const RTE_ETH_FLOW_NVGRE: u32 = 21; +pub const RTE_ETH_FLOW_MAX: u32 = 22; /** A structure used to define all flexible payload related setting include flex payload and flex mask*/ #[repr(C)] diff --git a/bindgen-tests/tests/expectations/tests/namespace.rs b/bindgen-tests/tests/expectations/tests/namespace.rs index 83543b851e..a99711b277 100644 --- a/bindgen-tests/tests/expectations/tests/namespace.rs +++ b/bindgen-tests/tests/expectations/tests/namespace.rs @@ -17,7 +17,7 @@ pub mod root { pub fn in_whatever(); } } - pub mod _bindgen_mod_id_17 { + pub mod _bindgen_mod_id_13 { #[allow(unused_imports)] use self::super::super::root; #[repr(C)] @@ -46,7 +46,7 @@ pub mod root { #[repr(C)] #[derive(Debug)] pub struct C { - pub _base: root::_bindgen_mod_id_17::A, + pub _base: root::_bindgen_mod_id_13::A, pub m_c: T, pub m_c_ptr: *mut T, pub m_c_arr: [T; 10usize], diff --git a/bindgen-tests/tests/expectations/tests/template_instantiation_with_fn_local_type.rs b/bindgen-tests/tests/expectations/tests/template_instantiation_with_fn_local_type.rs index ab87b4160b..5816a6c5e9 100644 --- a/bindgen-tests/tests/expectations/tests/template_instantiation_with_fn_local_type.rs +++ b/bindgen-tests/tests/expectations/tests/template_instantiation_with_fn_local_type.rs @@ -4,10 +4,6 @@ pub struct Foo { pub _address: u8, } -extern "C" { - #[link_name = "\u{1}_Z1fv"] - pub fn f(); -} #[test] fn __bindgen_test_layout_Foo_open0_Bar_close0_instantiation() { assert_eq!( @@ -19,6 +15,21 @@ fn __bindgen_test_layout_Foo_open0_Bar_close0_instantiation() { concat!("Alignment of template specialization: ", stringify!(Foo)) ); } +#[test] +fn __bindgen_test_layout_Foo_open0_Boo_close0_instantiation() { + assert_eq!( + ::std::mem::size_of:: < Foo > (), 1usize, + concat!("Size of template specialization: ", stringify!(Foo)) + ); + assert_eq!( + ::std::mem::align_of:: < Foo > (), 1usize, + concat!("Alignment of template specialization: ", stringify!(Foo)) + ); +} +extern "C" { + #[link_name = "\u{1}_Z1fv"] + pub fn f(); +} #[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct Baz { @@ -34,17 +45,6 @@ fn bindgen_test_layout_Baz() { stringify!(Baz)) ); } -#[test] -fn __bindgen_test_layout_Foo_open0_Boo_close0_instantiation() { - assert_eq!( - ::std::mem::size_of:: < Foo > (), 1usize, - concat!("Size of template specialization: ", stringify!(Foo)) - ); - assert_eq!( - ::std::mem::align_of:: < Foo > (), 1usize, - concat!("Alignment of template specialization: ", stringify!(Foo)) - ); -} #[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct Bar { diff --git a/bindgen-tests/tests/expectations/tests/test_multiple_header_calls_in_builder.rs b/bindgen-tests/tests/expectations/tests/test_multiple_header_calls_in_builder.rs index b594d70c93..a62c78fa15 100644 --- a/bindgen-tests/tests/expectations/tests/test_multiple_header_calls_in_builder.rs +++ b/bindgen-tests/tests/expectations/tests/test_multiple_header_calls_in_builder.rs @@ -27,82 +27,61 @@ pub struct Test { } #[test] fn bindgen_test_layout_Test() { - const UNINIT: ::std::mem::MaybeUninit = - ::std::mem::MaybeUninit::uninit(); + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 12usize, - concat!("Size of: ", stringify!(Test)) + ::std::mem::size_of::< Test > (), 12usize, concat!("Size of: ", stringify!(Test)) ); assert_eq!( - ::std::mem::align_of::(), - 1usize, - concat!("Alignment of ", stringify!(Test)) + ::std::mem::align_of::< Test > (), 1usize, concat!("Alignment of ", + stringify!(Test)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).ch) as usize - ptr as usize }, - 0usize, + unsafe { ::std::ptr::addr_of!((* ptr).ch) as usize - ptr as usize }, 0usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(ch)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).u) as usize - ptr as usize }, - 1usize, + unsafe { ::std::ptr::addr_of!((* ptr).u) as usize - ptr as usize }, 1usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(u)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).d) as usize - ptr as usize }, - 2usize, + unsafe { ::std::ptr::addr_of!((* ptr).d) as usize - ptr as usize }, 2usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(d)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).cch) as usize - ptr as usize }, - 3usize, + unsafe { ::std::ptr::addr_of!((* ptr).cch) as usize - ptr as usize }, 3usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(cch)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).cu) as usize - ptr as usize }, - 4usize, + unsafe { ::std::ptr::addr_of!((* ptr).cu) as usize - ptr as usize }, 4usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(cu)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).cd) as usize - ptr as usize }, - 5usize, + unsafe { ::std::ptr::addr_of!((* ptr).cd) as usize - ptr as usize }, 5usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(cd)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Cch) as usize - ptr as usize }, - 6usize, + unsafe { ::std::ptr::addr_of!((* ptr).Cch) as usize - ptr as usize }, 6usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(Cch)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Cu) as usize - ptr as usize }, - 7usize, + unsafe { ::std::ptr::addr_of!((* ptr).Cu) as usize - ptr as usize }, 7usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(Cu)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Cd) as usize - ptr as usize }, - 8usize, + unsafe { ::std::ptr::addr_of!((* ptr).Cd) as usize - ptr as usize }, 8usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(Cd)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Ccch) as usize - ptr as usize }, - 9usize, - concat!( - "Offset of field: ", - stringify!(Test), - "::", - stringify!(Ccch) - ) + unsafe { ::std::ptr::addr_of!((* ptr).Ccch) as usize - ptr as usize }, 9usize, + concat!("Offset of field: ", stringify!(Test), "::", stringify!(Ccch)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Ccu) as usize - ptr as usize }, - 10usize, + unsafe { ::std::ptr::addr_of!((* ptr).Ccu) as usize - ptr as usize }, 10usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(Ccu)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).Ccd) as usize - ptr as usize }, - 11usize, + unsafe { ::std::ptr::addr_of!((* ptr).Ccd) as usize - ptr as usize }, 11usize, concat!("Offset of field: ", stringify!(Test), "::", stringify!(Ccd)) ); } diff --git a/bindgen-tests/tests/tests.rs b/bindgen-tests/tests/tests.rs index 895e1cac2c..dfecb0ad62 100644 --- a/bindgen-tests/tests/tests.rs +++ b/bindgen-tests/tests/tests.rs @@ -468,9 +468,9 @@ fn test_mixed_header_and_header_contents() { env!("CARGO_MANIFEST_DIR"), "/tests/headers/func_ptr.h" )) - .header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/char.h")) .header_contents("test.h", "int bar(const char* a);") .header_contents("test2.h", "float bar2(const char* b);") + .header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/char.h")) .clang_arg("--target=x86_64-unknown-linux") .generate() .unwrap() diff --git a/bindgen/clang.rs b/bindgen/clang.rs index 51064a558f..c1e7e38e36 100644 --- a/bindgen/clang.rs +++ b/bindgen/clang.rs @@ -508,17 +508,6 @@ impl Cursor { ) where Visitor: FnMut(&mut BindgenContext, Cursor), { - // FIXME(#2556): The current source order stuff doesn't account well for different levels - // of includes, or includes that show up at the same byte offset because they are passed in - // via CLI. - const SOURCE_ORDER_ENABLED: bool = false; - if !SOURCE_ORDER_ENABLED { - return self.visit(|c| { - visitor(ctx, c); - CXChildVisit_Continue - }); - } - let mut children = self.collect_children(); for child in &children { if child.kind() == CXCursor_InclusionDirective { @@ -526,9 +515,8 @@ impl Cursor { let location = child.location(); let (source_file, _, _, offset) = location.location(); - if let Some(source_file) = source_file.name() { - ctx.add_include(source_file, included_file, offset); - } + let source_file = source_file.name(); + ctx.add_include(source_file, included_file, offset); } } } @@ -555,36 +543,41 @@ impl Cursor { let (file, other_file) = match (file.name(), other_file.name()) { (Some(file), Some(other_file)) => (file, other_file), - // Built-in definitions should come first. - (Some(_), None) => return cmp::Ordering::Greater, - (None, Some(_)) => return cmp::Ordering::Less, - (None, None) => return cmp::Ordering::Equal, + // Keep the original sorting of built-in definitions. + (None, _) | (_, None) => return cmp::Ordering::Equal, }; + // If both items are in the same source file, simply compare the offset. if file == other_file { - // Both items are in the same source file, compare by byte offset. return offset.cmp(&other_offset); } + // `None` here means `file`/`other_file` is the main header file. let include_location = ctx.included_file_location(&file); let other_include_location = ctx.included_file_location(&other_file); + match (include_location, other_include_location) { - (Some((file2, offset2)), _) if file2 == other_file => { + // The main header file (`None`) comes after header passed as CLI argument (`Some((None, _))`). + (None, Some((None, _))) => cmp::Ordering::Greater, + (Some((None, _)), None) => cmp::Ordering::Less, + // If an item was included in the same source file as the other item, + // compare its `#include` location offset the offset of the other item. + (Some((Some(file2), offset2)), _) if file2 == other_file => { offset2.cmp(&other_offset) } - (Some(_), None) => cmp::Ordering::Greater, - (_, Some((other_file2, other_offset2))) if file == other_file2 => { + (_, Some((Some(other_file2), other_offset2))) + if file == other_file2 => + { offset.cmp(&other_offset2) } - (None, Some(_)) => cmp::Ordering::Less, - (Some((file2, offset2)), Some((other_file2, other_offset2))) => { - if file2 == other_file2 { - offset2.cmp(&other_offset2) - } else { - cmp::Ordering::Equal - } + // If both items were included in the same file, compare the offset of their `#include` directives. + (Some((file2, offset2)), Some((other_file2, other_offset2))) + if file2 == other_file2 => + { + offset2.cmp(&other_offset2) } - (None, None) => cmp::Ordering::Equal, + // Otherwise, keep the original sorting. + _ => cmp::Ordering::Equal, } } diff --git a/bindgen/ir/context.rs b/bindgen/ir/context.rs index bc9e59c3f0..87af398f1b 100644 --- a/bindgen/ir/context.rs +++ b/bindgen/ir/context.rs @@ -362,7 +362,7 @@ pub(crate) struct BindgenContext { /// /// The key is the included file, the value is a pair of the source file and /// the position of the `#include` directive in the source file. - includes: StdHashMap, + includes: StdHashMap, usize)>, /// A set of all the included filenames. deps: BTreeSet, @@ -646,7 +646,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" /// Add the location of the `#include` directive for the `included_file`. pub(crate) fn add_include( &mut self, - source_file: String, + source_file: Option, included_file: String, offset: usize, ) { @@ -659,7 +659,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" pub(crate) fn included_file_location( &self, included_file: &str, - ) -> Option<(String, usize)> { + ) -> Option<(Option, usize)> { self.includes.get(included_file).cloned() }