Skip to content

Commit 8d938d5

Browse files
qinghonlidongshengxdayu
authored andcommitted
enum: add support hash name for unnamed enum
1 parent 6bb8512 commit 8d938d5

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

bindgen/ir/enum_ty.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

bindgen/ir/ty.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,25 @@ impl Type {
282282
.sanitized_name(ctx)
283283
.map(|name| format!("{prefix}_{name}").into())
284284
} else {
285-
self.name().map(Self::sanitize_name)
285+
match self.name() {
286+
Some(name) => Some(Self::sanitize_name(name)),
287+
None => match self.kind() {
288+
TypeKind::Enum(inner) => {
289+
if ctx.options().hash_unnamed_enum {
290+
Some(
291+
format!(
292+
"bindgen_enum_{:x}",
293+
inner.variants_name_hash()
294+
)
295+
.into(),
296+
)
297+
} else {
298+
None
299+
}
300+
}
301+
_ => None,
302+
},
303+
}
286304
}
287305
}
288306

bindgen/options/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ struct BindgenCommand {
349349
/// Do not prepend the enum name to constant or newtype variants.
350350
#[arg(long)]
351351
no_prepend_enum_name: bool,
352+
#[arg(long)]
353+
hash_unnamed_enum: bool,
352354
/// Do not try to detect default include paths
353355
#[arg(long)]
354356
no_include_path_detection: bool,
@@ -646,6 +648,7 @@ where
646648
ignore_methods,
647649
no_convert_floats,
648650
no_prepend_enum_name,
651+
hash_unnamed_enum,
649652
no_include_path_detection,
650653
fit_macro_constant_types,
651654
opaque_type,
@@ -935,6 +938,7 @@ where
935938
with_derive_ord => Builder::derive_ord,
936939
no_derive_default => |b, _| b.derive_default(false),
937940
no_prepend_enum_name => |b, _| b.prepend_enum_name(false),
941+
hash_unnamed_enum => |b,_|b.hash_unnamed_enum(),
938942
no_include_path_detection => |b, _| b.detect_include_paths(false),
939943
fit_macro_constant_types => Builder::fit_macro_constants,
940944
time_phases,

bindgen/options/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,4 +2376,17 @@ options! {
23762376
}
23772377
},
23782378
},
2379+
/// use hash name for unnamed enum
2380+
hash_unnamed_enum: bool {
2381+
default: false,
2382+
methods: {
2383+
/// Generate a hash-based name for unnamed enums,
2384+
/// using the hash of their variant names.
2385+
pub fn hash_unnamed_enum(mut self) -> Self {
2386+
self.options.hash_unnamed_enum = true;
2387+
self
2388+
}
2389+
},
2390+
as_args: "--hash-unnamed-enum",
2391+
}
23792392
}

0 commit comments

Comments
 (0)