summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ir/context.rs4
-rw-r--r--src/ir/enum_ty.rs15
-rw-r--r--tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs9
-rw-r--r--tests/headers/parsecb-anonymous-enum-variant-rename.h6
-rw-r--r--tests/parse_callbacks/mod.rs15
5 files changed, 46 insertions, 3 deletions
diff --git a/src/ir/context.rs b/src/ir/context.rs
index 6a0f07d9..a0d4de1e 100644
--- a/src/ir/context.rs
+++ b/src/ir/context.rs
@@ -2299,7 +2299,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
let mut prefix_path =
parent.path_for_allowlisting(self).clone();
enum_.variants().iter().any(|variant| {
- prefix_path.push(variant.name().into());
+ prefix_path.push(
+ variant.name_for_allowlisting().into(),
+ );
let name = prefix_path[1..].join("::");
prefix_path.pop().unwrap();
self.options().allowlisted_vars.matches(&name)
diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs
index c6cd89ce..15d41368 100644
--- a/src/ir/enum_ty.rs
+++ b/src/ir/enum_ty.rs
@@ -118,7 +118,7 @@ impl Enum {
}
});
- let name = ctx
+ let new_name = ctx
.parse_callbacks()
.and_then(|callbacks| {
callbacks.enum_variant_name(type_name, &name, val)
@@ -130,10 +130,11 @@ impl Enum {
.last()
.cloned()
})
- .unwrap_or(name);
+ .unwrap_or_else(|| name.clone());
let comment = cursor.raw_comment();
variants.push(EnumVariant::new(
+ new_name,
name,
comment,
val,
@@ -224,6 +225,9 @@ pub struct EnumVariant {
/// The name of the variant.
name: String,
+ /// The original name of the variant (without user mangling)
+ name_for_allowlisting: String,
+
/// An optional doc comment.
comment: Option<String>,
@@ -251,12 +255,14 @@ impl EnumVariant {
/// Construct a new enumeration variant from the given parts.
pub fn new(
name: String,
+ name_for_allowlisting: String,
comment: Option<String>,
val: EnumVariantValue,
custom_behavior: Option<EnumVariantCustomBehavior>,
) -> Self {
EnumVariant {
name,
+ name_for_allowlisting,
comment,
val,
custom_behavior,
@@ -268,6 +274,11 @@ impl EnumVariant {
&self.name
}
+ /// Get this variant's name.
+ pub fn name_for_allowlisting(&self) -> &str {
+ &self.name_for_allowlisting
+ }
+
/// Get this variant's value.
pub fn val(&self) -> EnumVariantValue {
self.val
diff --git a/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs b/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs
new file mode 100644
index 00000000..e615486e
--- /dev/null
+++ b/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs
@@ -0,0 +1,9 @@
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
+pub type _bindgen_ty_1 = ::std::os::raw::c_uint;
diff --git a/tests/headers/parsecb-anonymous-enum-variant-rename.h b/tests/headers/parsecb-anonymous-enum-variant-rename.h
new file mode 100644
index 00000000..9336cf89
--- /dev/null
+++ b/tests/headers/parsecb-anonymous-enum-variant-rename.h
@@ -0,0 +1,6 @@
+// bindgen-flags: --allowlist-var ^MyVal$
+// bindgen-parse-callbacks: enum-variant-rename
+
+enum {
+ MyVal = 0,
+};
diff --git a/tests/parse_callbacks/mod.rs b/tests/parse_callbacks/mod.rs
index c60aaa19..01993cfc 100644
--- a/tests/parse_callbacks/mod.rs
+++ b/tests/parse_callbacks/mod.rs
@@ -1,7 +1,22 @@
use bindgen::callbacks::ParseCallbacks;
+#[derive(Debug)]
+struct EnumVariantRename;
+
+impl ParseCallbacks for EnumVariantRename {
+ fn enum_variant_name(
+ &self,
+ _enum_name: Option<&str>,
+ original_variant_name: &str,
+ _variant_value: bindgen::callbacks::EnumVariantValue,
+ ) -> Option<String> {
+ Some(format!("RENAMED_{}", original_variant_name))
+ }
+}
+
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
+ "enum-variant-rename" => Box::new(EnumVariantRename),
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
}
}