rust-bindgen
rust-bindgen copied to clipboard
Remove unused variable and types
I 'm developing a rust binding library from c, but I encountered a problem making me confused.
As you can see, those generated types and constants which is not used by the generated code, I guess there is opportunities to remove them. On the other hand, user would expect generating more platform independent code when possible. Those generated code can actually compiled on other platforms like linux.
Input C/C++ Header
Click to expand
//
// test.h
// (Header automatically generated by the ispc compiler.)
// DO NOT EDIT THIS FILE.
//
#pragma once
#include <stdint.h>
// #include <stdbool.h>
#ifdef __cplusplus
namespace ispc { /* namespace */
#endif // __cplusplus
#ifndef __ISPC_ALIGN__
#if defined(__clang__) || !defined(_MSC_VER)
// Clang, GCC, ICC
#define __ISPC_ALIGN__(s) __attribute__((aligned(s)))
#define __ISPC_ALIGNED_STRUCT__(s) struct __ISPC_ALIGN__(s)
#else
// Visual Studio
#define __ISPC_ALIGN__(s) __declspec(align(s))
#define __ISPC_ALIGNED_STRUCT__(s) __ISPC_ALIGN__(s) struct
#endif
#endif
#ifndef __ISPC_STRUCT_rgba_surface__
#define __ISPC_STRUCT_rgba_surface__
struct rgba_surface {
uint8_t * ptr;
int32_t width;
int32_t height;
int32_t stride;
};
#endif
#ifndef __ISPC_STRUCT_bc6h_enc_settings__
#define __ISPC_STRUCT_bc6h_enc_settings__
struct bc6h_enc_settings {
// bool slow_mode;
// bool fast_mode;
int32_t refineIterations_1p;
int32_t refineIterations_2p;
int32_t fastSkipTreshold;
};
#endif
#ifndef __ISPC_STRUCT_bc7_enc_settings__
#define __ISPC_STRUCT_bc7_enc_settings__
struct bc7_enc_settings {
// bool mode_selection[4];
int32_t refineIterations[8];
// bool skip_mode2;
int32_t fastSkipTreshold_mode1;
int32_t fastSkipTreshold_mode3;
int32_t fastSkipTreshold_mode7;
int32_t mode45_channel0;
int32_t refineIterations_channel;
int32_t channels;
};
#endif
#ifndef __ISPC_STRUCT_etc_enc_settings__
#define __ISPC_STRUCT_etc_enc_settings__
struct etc_enc_settings {
int32_t fastSkipTreshold;
};
#endif
///////////////////////////////////////////////////////////////////////////
// Functions exported from ispc code
///////////////////////////////////////////////////////////////////////////
#if defined(__cplusplus) && (! defined(__ISPC_NO_EXTERN_C) || !__ISPC_NO_EXTERN_C )
extern "C" {
#endif // __cplusplus
extern void CompressBlocksBC1_ispc(struct rgba_surface * src, uint8_t * dst);
extern void CompressBlocksBC3_ispc(struct rgba_surface * src, uint8_t * dst);
extern void CompressBlocksBC4_ispc(struct rgba_surface * src, uint8_t * dst);
extern void CompressBlocksBC5_ispc(struct rgba_surface * src, uint8_t * dst);
extern void CompressBlocksBC6H_ispc(struct rgba_surface * src, uint8_t * dst, struct bc6h_enc_settings * settings);
extern void CompressBlocksBC7_ispc(struct rgba_surface * src, uint8_t * dst, struct bc7_enc_settings * settings);
extern void CompressBlocksETC1_ispc(struct rgba_surface * src, uint8_t * dst, struct etc_enc_settings * settings);
#if defined(__cplusplus) && (! defined(__ISPC_NO_EXTERN_C) || !__ISPC_NO_EXTERN_C )
} /* end extern C */
#endif // __cplusplus
#ifdef __cplusplus
} /* namespace */
#endif // __cplusplus
Bindgen Invocation
$ bindgen test.h -o bindings.rs
Actual Results
Click to expand
/* automatically generated by rust-bindgen 0.59.2 */
pub const __WORDSIZE: u32 = 64;
pub const __DARWIN_ONLY_64_BIT_INO_T: u32 = 1;
pub const __DARWIN_ONLY_UNIX_CONFORMANCE: u32 = 1;
pub const __DARWIN_ONLY_VERS_1050: u32 = 1;
pub const __DARWIN_UNIX03: u32 = 1;
pub const __DARWIN_64_BIT_INO_T: u32 = 1;
pub const __DARWIN_VERS_1050: u32 = 1;
pub const __DARWIN_NON_CANCELABLE: u32 = 0;
pub const __DARWIN_SUF_EXTSN: &[u8; 14usize] = b"$DARWIN_EXTSN\0";
pub const __DARWIN_C_ANSI: u32 = 4096;
pub const __DARWIN_C_FULL: u32 = 900000;
pub const __DARWIN_C_LEVEL: u32 = 900000;
pub const __STDC_WANT_LIB_EXT1__: u32 = 1;
pub const __DARWIN_NO_LONG_LONG: u32 = 0;
pub const _DARWIN_FEATURE_64_BIT_INODE: u32 = 1;
pub const _DARWIN_FEATURE_ONLY_64_BIT_INODE: u32 = 1;
pub const _DARWIN_FEATURE_ONLY_VERS_1050: u32 = 1;
pub const _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE: u32 = 1;
pub const _DARWIN_FEATURE_UNIX_CONFORMANCE: u32 = 3;
pub const __has_ptrcheck: u32 = 0;
pub const __PTHREAD_SIZE__: u32 = 8176;
pub const __PTHREAD_ATTR_SIZE__: u32 = 56;
pub const __PTHREAD_MUTEXATTR_SIZE__: u32 = 8;
pub const __PTHREAD_MUTEX_SIZE__: u32 = 56;
pub const __PTHREAD_CONDATTR_SIZE__: u32 = 8;
pub const __PTHREAD_COND_SIZE__: u32 = 40;
pub const __PTHREAD_ONCE_SIZE__: u32 = 8;
pub const __PTHREAD_RWLOCK_SIZE__: u32 = 192;
pub const __PTHREAD_RWLOCKATTR_SIZE__: u32 = 16;
pub const INT8_MAX: u32 = 127;
pub const INT16_MAX: u32 = 32767;
pub const INT32_MAX: u32 = 2147483647;
pub const INT64_MAX: u64 = 9223372036854775807;
pub const INT8_MIN: i32 = -128;
pub const INT16_MIN: i32 = -32768;
pub const INT32_MIN: i32 = -2147483648;
pub const INT64_MIN: i64 = -9223372036854775808;
pub const UINT8_MAX: u32 = 255;
pub const UINT16_MAX: u32 = 65535;
pub const UINT32_MAX: u32 = 4294967295;
pub const UINT64_MAX: i32 = -1;
pub const INT_LEAST8_MIN: i32 = -128;
pub const INT_LEAST16_MIN: i32 = -32768;
pub const INT_LEAST32_MIN: i32 = -2147483648;
pub const INT_LEAST64_MIN: i64 = -9223372036854775808;
pub const INT_LEAST8_MAX: u32 = 127;
pub const INT_LEAST16_MAX: u32 = 32767;
pub const INT_LEAST32_MAX: u32 = 2147483647;
pub const INT_LEAST64_MAX: u64 = 9223372036854775807;
pub const UINT_LEAST8_MAX: u32 = 255;
pub const UINT_LEAST16_MAX: u32 = 65535;
pub const UINT_LEAST32_MAX: u32 = 4294967295;
pub const UINT_LEAST64_MAX: i32 = -1;
pub const INT_FAST8_MIN: i32 = -128;
pub const INT_FAST16_MIN: i32 = -32768;
pub const INT_FAST32_MIN: i32 = -2147483648;
pub const INT_FAST64_MIN: i64 = -9223372036854775808;
pub const INT_FAST8_MAX: u32 = 127;
pub const INT_FAST16_MAX: u32 = 32767;
pub const INT_FAST32_MAX: u32 = 2147483647;
pub const INT_FAST64_MAX: u64 = 9223372036854775807;
pub const UINT_FAST8_MAX: u32 = 255;
pub const UINT_FAST16_MAX: u32 = 65535;
pub const UINT_FAST32_MAX: u32 = 4294967295;
pub const UINT_FAST64_MAX: i32 = -1;
pub const INTPTR_MAX: u64 = 9223372036854775807;
pub const INTPTR_MIN: i64 = -9223372036854775808;
pub const UINTPTR_MAX: i32 = -1;
pub const SIZE_MAX: i32 = -1;
pub const RSIZE_MAX: i32 = -1;
pub const WINT_MIN: i32 = -2147483648;
pub const WINT_MAX: u32 = 2147483647;
pub const SIG_ATOMIC_MIN: i32 = -2147483648;
pub const SIG_ATOMIC_MAX: u32 = 2147483647;
pub type int_least8_t = i8;
pub type int_least16_t = i16;
pub type int_least32_t = i32;
pub type int_least64_t = i64;
pub type uint_least8_t = u8;
pub type uint_least16_t = u16;
pub type uint_least32_t = u32;
pub type uint_least64_t = u64;
pub type int_fast8_t = i8;
pub type int_fast16_t = i16;
pub type int_fast32_t = i32;
pub type int_fast64_t = i64;
pub type uint_fast8_t = u8;
pub type uint_fast16_t = u16;
pub type uint_fast32_t = u32;
pub type uint_fast64_t = u64;
pub type __int8_t = ::std::os::raw::c_schar;
pub type __uint8_t = ::std::os::raw::c_uchar;
pub type __int16_t = ::std::os::raw::c_short;
pub type __uint16_t = ::std::os::raw::c_ushort;
pub type __int32_t = ::std::os::raw::c_int;
pub type __uint32_t = ::std::os::raw::c_uint;
pub type __int64_t = ::std::os::raw::c_longlong;
pub type __uint64_t = ::std::os::raw::c_ulonglong;
pub type __darwin_intptr_t = ::std::os::raw::c_long;
pub type __darwin_natural_t = ::std::os::raw::c_uint;
pub type __darwin_ct_rune_t = ::std::os::raw::c_int;
#[repr(C)]
#[derive(Copy, Clone)]
pub union __mbstate_t {
pub __mbstate8: [::std::os::raw::c_char; 128usize],
pub _mbstateL: ::std::os::raw::c_longlong,
}
pub type __darwin_mbstate_t = __mbstate_t;
pub type __darwin_ptrdiff_t = ::std::os::raw::c_long;
pub type __darwin_size_t = ::std::os::raw::c_ulong;
pub type __darwin_va_list = __builtin_va_list;
pub type __darwin_wchar_t = ::std::os::raw::c_int;
pub type __darwin_rune_t = __darwin_wchar_t;
pub type __darwin_wint_t = ::std::os::raw::c_int;
pub type __darwin_clock_t = ::std::os::raw::c_ulong;
pub type __darwin_socklen_t = __uint32_t;
pub type __darwin_ssize_t = ::std::os::raw::c_long;
pub type __darwin_time_t = ::std::os::raw::c_long;
pub type __darwin_blkcnt_t = __int64_t;
pub type __darwin_blksize_t = __int32_t;
pub type __darwin_dev_t = __int32_t;
pub type __darwin_fsblkcnt_t = ::std::os::raw::c_uint;
pub type __darwin_fsfilcnt_t = ::std::os::raw::c_uint;
pub type __darwin_gid_t = __uint32_t;
pub type __darwin_id_t = __uint32_t;
pub type __darwin_ino64_t = __uint64_t;
pub type __darwin_ino_t = __darwin_ino64_t;
pub type __darwin_mach_port_name_t = __darwin_natural_t;
pub type __darwin_mach_port_t = __darwin_mach_port_name_t;
pub type __darwin_mode_t = __uint16_t;
pub type __darwin_off_t = __int64_t;
pub type __darwin_pid_t = __int32_t;
pub type __darwin_sigset_t = __uint32_t;
pub type __darwin_suseconds_t = __int32_t;
pub type __darwin_uid_t = __uint32_t;
pub type __darwin_useconds_t = __uint32_t;
pub type __darwin_uuid_t = [::std::os::raw::c_uchar; 16usize];
pub type __darwin_uuid_string_t = [::std::os::raw::c_char; 37usize];
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct __darwin_pthread_handler_rec {
pub __routine: ::std::option::Option<unsafe extern "C" fn(arg1: *mut ::std::os::raw::c_void)>,
pub __arg: *mut ::std::os::raw::c_void,
pub __next: *mut __darwin_pthread_handler_rec,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_attr_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 56usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_cond_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 40usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_condattr_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 8usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_mutex_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 56usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_mutexattr_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 8usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_once_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 8usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_rwlock_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 192usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_rwlockattr_t {
pub __sig: ::std::os::raw::c_long,
pub __opaque: [::std::os::raw::c_char; 16usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _opaque_pthread_t {
pub __sig: ::std::os::raw::c_long,
pub __cleanup_stack: *mut __darwin_pthread_handler_rec,
pub __opaque: [::std::os::raw::c_char; 8176usize],
}
pub type __darwin_pthread_attr_t = _opaque_pthread_attr_t;
pub type __darwin_pthread_cond_t = _opaque_pthread_cond_t;
pub type __darwin_pthread_condattr_t = _opaque_pthread_condattr_t;
pub type __darwin_pthread_key_t = ::std::os::raw::c_ulong;
pub type __darwin_pthread_mutex_t = _opaque_pthread_mutex_t;
pub type __darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t;
pub type __darwin_pthread_once_t = _opaque_pthread_once_t;
pub type __darwin_pthread_rwlock_t = _opaque_pthread_rwlock_t;
pub type __darwin_pthread_rwlockattr_t = _opaque_pthread_rwlockattr_t;
pub type __darwin_pthread_t = *mut _opaque_pthread_t;
pub type u_int8_t = ::std::os::raw::c_uchar;
pub type u_int16_t = ::std::os::raw::c_ushort;
pub type u_int32_t = ::std::os::raw::c_uint;
pub type u_int64_t = ::std::os::raw::c_ulonglong;
pub type register_t = i64;
pub type user_addr_t = u_int64_t;
pub type user_size_t = u_int64_t;
pub type user_ssize_t = i64;
pub type user_long_t = i64;
pub type user_ulong_t = u_int64_t;
pub type user_time_t = i64;
pub type user_off_t = i64;
pub type syscall_arg_t = u_int64_t;
pub type intmax_t = ::std::os::raw::c_long;
pub type uintmax_t = ::std::os::raw::c_ulong;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct rgba_surface {
pub ptr: *mut u8,
pub width: i32,
pub height: i32,
pub stride: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct bc6h_enc_settings {
pub refineIterations_1p: i32,
pub refineIterations_2p: i32,
pub fastSkipTreshold: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct bc7_enc_settings {
pub refineIterations: [i32; 8usize],
pub fastSkipTreshold_mode1: i32,
pub fastSkipTreshold_mode3: i32,
pub fastSkipTreshold_mode7: i32,
pub mode45_channel0: i32,
pub refineIterations_channel: i32,
pub channels: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct etc_enc_settings {
pub fastSkipTreshold: i32,
}
extern "C" {
pub fn CompressBlocksBC1_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC3_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC4_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC5_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC6H_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut bc6h_enc_settings,
);
}
extern "C" {
pub fn CompressBlocksBC7_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut bc7_enc_settings,
);
}
extern "C" {
pub fn CompressBlocksETC1_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut etc_enc_settings,
);
}
pub type __builtin_va_list = *mut ::std::os::raw::c_char;
Expected Results
Click to expand
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct rgba_surface {
pub ptr: *mut u8,
pub width: i32,
pub height: i32,
pub stride: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct bc6h_enc_settings {
pub refineIterations_1p: i32,
pub refineIterations_2p: i32,
pub fastSkipTreshold: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct bc7_enc_settings {
pub refineIterations: [i32; 8usize],
pub fastSkipTreshold_mode1: i32,
pub fastSkipTreshold_mode3: i32,
pub fastSkipTreshold_mode7: i32,
pub mode45_channel0: i32,
pub refineIterations_channel: i32,
pub channels: i32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct etc_enc_settings {
pub fastSkipTreshold: i32,
}
extern "C" {
pub fn CompressBlocksBC1_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC3_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC4_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC5_ispc(src: *mut rgba_surface, dst: *mut u8);
}
extern "C" {
pub fn CompressBlocksBC6H_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut bc6h_enc_settings,
);
}
extern "C" {
pub fn CompressBlocksBC7_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut bc7_enc_settings,
);
}
extern "C" {
pub fn CompressBlocksETC1_ispc(
src: *mut rgba_surface,
dst: *mut u8,
settings: *mut etc_enc_settings,
);
}
pub type __builtin_va_list = *mut ::std::os::raw::c_char;
At lease we can provide an option like --no-unused or two options --no-unused-vars with --no-unused-types for cleaner code generation
The expected way to do this is --allowlist-function 'CompressBlocks.*', then it will only generate the types used from those functions, does that not work?
The expected way to do this is
--allowlist-function 'CompressBlocks.*', then it will only generate the types used from those functions, does that not work?
I think it is a workaround. I would need to maintain these allowlist as well. An option for removing unused types and vars could save me a lot.
Well, it's hard to automatically know what you want to consider unused vs. not. For example there are lots of functions in C that take ints but expect some sort of enumeration.
If you only want stuff generated from functions you can --allowlist-function '.*', if you want stuff generated from a file you can use --allowlist-file...
This sounds like something that could be handled better by running cargo fix or something similar over the generated bindings instead of adding extra functionality to bindgen.
Well, it's hard to automatically know what you want to consider unused vs. not. For example there are lots of functions in C that take ints but expect some sort of enumeration.
If you only want stuff generated from functions you can
--allowlist-function '.*', if you want stuff generated from a file you can use--allowlist-file...
Closing this issue as --allowlist-file produces the expected output