File tree Expand file tree Collapse file tree 4 files changed +49
-1
lines changed Expand file tree Collapse file tree 4 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -235,6 +235,19 @@ impl Enum {
235235 ctx. options ( ) . default_enum_style
236236 }
237237 }
238+ /// generate hash from enum variants
239+ pub ( crate ) fn variants_name_hash ( & self ) -> u64 {
240+ use std:: collections:: hash_map:: DefaultHasher ;
241+ use std:: hash:: { Hash , Hasher } ;
242+
243+ let mut hasher = DefaultHasher :: new ( ) ;
244+
245+ for variant in self . variants ( ) {
246+ variant. name ( ) . hash ( & mut hasher) ;
247+ }
248+
249+ hasher. finish ( )
250+ }
238251}
239252
240253/// A single enum variant, to be contained only in an enum.
Original file line number Diff line number Diff line change @@ -271,7 +271,25 @@ impl Type {
271271 . sanitized_name ( ctx)
272272 . map ( |name| format ! ( "{prefix}_{name}" ) . into ( ) )
273273 } else {
274- self . name ( ) . map ( Self :: sanitize_name)
274+ match self . name ( ) {
275+ Some ( name) => Some ( Self :: sanitize_name ( name) ) ,
276+ None => match self . kind ( ) {
277+ TypeKind :: Enum ( inner) => {
278+ if ctx. options ( ) . hash_unnamed_enum {
279+ Some (
280+ format ! (
281+ "bindgen_enum_{:x}" ,
282+ inner. variants_name_hash( )
283+ )
284+ . into ( ) ,
285+ )
286+ } else {
287+ None
288+ }
289+ }
290+ _ => None ,
291+ } ,
292+ }
275293 }
276294 }
277295
Original file line number Diff line number Diff line change @@ -321,6 +321,8 @@ struct BindgenCommand {
321321 /// Do not prepend the enum name to constant or newtype variants.
322322 #[ arg( long) ]
323323 no_prepend_enum_name : bool ,
324+ #[ arg( long) ]
325+ hash_unnamed_enum : bool ,
324326 /// Do not try to detect default include paths
325327 #[ arg( long) ]
326328 no_include_path_detection : bool ,
@@ -611,6 +613,7 @@ where
611613 ignore_methods,
612614 no_convert_floats,
613615 no_prepend_enum_name,
616+ hash_unnamed_enum,
614617 no_include_path_detection,
615618 fit_macro_constant_types,
616619 opaque_type,
@@ -898,6 +901,7 @@ where
898901 with_derive_ord => Builder :: derive_ord,
899902 no_derive_default => |b, _| b. derive_default( false ) ,
900903 no_prepend_enum_name => |b, _| b. prepend_enum_name( false ) ,
904+ hash_unnamed_enum => |b, _|b. hash_unnamed_enum( ) ,
901905 no_include_path_detection => |b, _| b. detect_include_paths( false ) ,
902906 fit_macro_constant_types => Builder :: fit_macro_constants,
903907 time_phases,
Original file line number Diff line number Diff line change @@ -2283,4 +2283,17 @@ options! {
22832283 } ,
22842284 as_args: "--generate-private-functions" ,
22852285 } ,
2286+ /// use hash name for unnamed enum
2287+ hash_unnamed_enum: bool {
2288+ default : false ,
2289+ methods: {
2290+ /// Generate a hash-based name for unnamed enums,
2291+ /// using the hash of their variant names.
2292+ pub fn hash_unnamed_enum( mut self ) -> Self {
2293+ self . options. hash_unnamed_enum = true ;
2294+ self
2295+ }
2296+ } ,
2297+ as_args: "--hash-unnamed-enum" ,
2298+ }
22862299}
You can’t perform that action at this time.
0 commit comments