From 68d54e0046fbe153c943658a0f35f44f1c155e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 23 Mar 2021 12:19:29 +0400 Subject: [PATCH 1/2] bindgen: use exported name for enum prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau --- src/bindgen/ir/enumeration.rs | 9 +++++++-- src/bindgen/rename.rs | 11 ++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index 9d96b4c3b..d525830d6 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -552,8 +552,13 @@ impl Item for Enum { .iter() .map(|variant| { EnumVariant::new( - r.apply(&variant.export_name, IdentifierType::EnumVariant(self)) - .into_owned(), + r.apply( + &variant.export_name, + IdentifierType::EnumVariant { + prefix: &self.export_name, + }, + ) + .into_owned(), variant.discriminant.clone(), match variant.body { VariantBody::Empty(..) => variant.body.clone(), diff --git a/src/bindgen/rename.rs b/src/bindgen/rename.rs index 73569c33c..c573ed794 100644 --- a/src/bindgen/rename.rs +++ b/src/bindgen/rename.rs @@ -5,13 +5,11 @@ use std::borrow::Cow; use std::str::FromStr; -use crate::bindgen::ir::{Enum, Item}; - /// The type of identifier to be renamed. #[derive(Debug, Clone, Copy)] pub enum IdentifierType<'a> { StructMember, - EnumVariant(&'a Enum), + EnumVariant { prefix: &'a str }, FunctionArg, Type, Enum, @@ -21,7 +19,7 @@ impl<'a> IdentifierType<'a> { fn to_str(&'a self) -> &'static str { match *self { IdentifierType::StructMember => "m", - IdentifierType::EnumVariant(..) => "", + IdentifierType::EnumVariant { .. } => "", IdentifierType::FunctionArg => "a", IdentifierType::Type => "", IdentifierType::Enum => "", @@ -81,10 +79,9 @@ impl RenameRule { RenameRule::QualifiedScreamingSnakeCase => { let mut result = String::new(); - if let IdentifierType::EnumVariant(e) = context { + if let IdentifierType::EnumVariant { prefix } = context { result.push_str( - &RenameRule::ScreamingSnakeCase - .apply(e.path().name(), IdentifierType::Enum), + &RenameRule::ScreamingSnakeCase.apply(prefix, IdentifierType::Enum), ); result.push('_'); } From 31f31d6d2cbbdba73d0fb38472a30906a0ddf01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 23 Mar 2021 12:30:19 +0400 Subject: [PATCH 2/2] tests: check renaming enum affects variant prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau --- tests/expectations/enum.both.c | 24 ++++++++++++------------ tests/expectations/enum.both.compat.c | 24 ++++++++++++------------ tests/expectations/enum.c | 20 ++++++++++---------- tests/expectations/enum.compat.c | 20 ++++++++++---------- tests/expectations/enum.cpp | 18 +++++++++--------- tests/expectations/enum.pyx | 18 +++++++++--------- tests/expectations/enum.tag.c | 20 ++++++++++---------- tests/expectations/enum.tag.compat.c | 20 ++++++++++---------- tests/expectations/enum.tag.pyx | 18 +++++++++--------- tests/rust/enum.toml | 3 +++ 10 files changed, 94 insertions(+), 91 deletions(-) diff --git a/tests/expectations/enum.both.c b/tests/expectations/enum.both.c index a42de0dca..e4de60f94 100644 --- a/tests/expectations/enum.both.c +++ b/tests/expectations/enum.both.c @@ -129,27 +129,27 @@ typedef struct H { }; } H; -enum I_Tag { - I_Foo, - I_Bar, - I_Baz, +enum ExI_Tag { + ExI_Foo, + ExI_Bar, + ExI_Baz, }; -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; -typedef struct I_Bar_Body { +typedef struct ExI_Bar_Body { uint8_t x; int16_t y; -} I_Bar_Body; +} ExI_Bar_Body; -typedef struct I { - I_Tag tag; +typedef struct ExI { + ExI_Tag tag; union { struct { int16_t foo; }; - I_Bar_Body bar; + ExI_Bar_Body bar; }; -} I; +} ExI; enum P_Tag { P0, @@ -182,7 +182,7 @@ void root(struct Opaque *opaque, F f, union G g, struct H h, - struct I i, + struct ExI i, struct J j, struct K k, enum L l, diff --git a/tests/expectations/enum.both.compat.c b/tests/expectations/enum.both.compat.c index 5378b67b2..d34fc1988 100644 --- a/tests/expectations/enum.both.compat.c +++ b/tests/expectations/enum.both.compat.c @@ -183,33 +183,33 @@ typedef struct H { }; } H; -enum I_Tag +enum ExI_Tag #ifdef __cplusplus : uint8_t #endif // __cplusplus { - I_Foo, - I_Bar, - I_Baz, + ExI_Foo, + ExI_Bar, + ExI_Baz, }; #ifndef __cplusplus -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; #endif // __cplusplus -typedef struct I_Bar_Body { +typedef struct ExI_Bar_Body { uint8_t x; int16_t y; -} I_Bar_Body; +} ExI_Bar_Body; -typedef struct I { - I_Tag tag; +typedef struct ExI { + ExI_Tag tag; union { struct { int16_t foo; }; - I_Bar_Body bar; + ExI_Bar_Body bar; }; -} I; +} ExI; enum P_Tag #ifdef __cplusplus @@ -252,7 +252,7 @@ void root(struct Opaque *opaque, F f, union G g, struct H h, - struct I i, + struct ExI i, struct J j, struct K k, enum L l, diff --git a/tests/expectations/enum.c b/tests/expectations/enum.c index bc94647d3..f747c88f6 100644 --- a/tests/expectations/enum.c +++ b/tests/expectations/enum.c @@ -129,27 +129,27 @@ typedef struct { }; } H; -enum I_Tag { - I_Foo, - I_Bar, - I_Baz, +enum ExI_Tag { + ExI_Foo, + ExI_Bar, + ExI_Baz, }; -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; typedef struct { uint8_t x; int16_t y; -} I_Bar_Body; +} ExI_Bar_Body; typedef struct { - I_Tag tag; + ExI_Tag tag; union { struct { int16_t foo; }; - I_Bar_Body bar; + ExI_Bar_Body bar; }; -} I; +} ExI; enum P_Tag { P0, @@ -182,7 +182,7 @@ void root(Opaque *opaque, F f, G g, H h, - I i, + ExI i, J j, K k, L l, diff --git a/tests/expectations/enum.compat.c b/tests/expectations/enum.compat.c index 5829c6b23..2ccfa7adf 100644 --- a/tests/expectations/enum.compat.c +++ b/tests/expectations/enum.compat.c @@ -183,33 +183,33 @@ typedef struct { }; } H; -enum I_Tag +enum ExI_Tag #ifdef __cplusplus : uint8_t #endif // __cplusplus { - I_Foo, - I_Bar, - I_Baz, + ExI_Foo, + ExI_Bar, + ExI_Baz, }; #ifndef __cplusplus -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; #endif // __cplusplus typedef struct { uint8_t x; int16_t y; -} I_Bar_Body; +} ExI_Bar_Body; typedef struct { - I_Tag tag; + ExI_Tag tag; union { struct { int16_t foo; }; - I_Bar_Body bar; + ExI_Bar_Body bar; }; -} I; +} ExI; enum P_Tag #ifdef __cplusplus @@ -252,7 +252,7 @@ void root(Opaque *opaque, F f, G g, H h, - I i, + ExI i, J j, K k, L l, diff --git a/tests/expectations/enum.cpp b/tests/expectations/enum.cpp index b3f15b1b8..a693939ad 100644 --- a/tests/expectations/enum.cpp +++ b/tests/expectations/enum.cpp @@ -127,26 +127,26 @@ struct H { }; }; -struct I { +struct ExI { enum class Tag : uint8_t { - I_Foo, - I_Bar, - I_Baz, + ExI_Foo, + ExI_Bar, + ExI_Baz, }; - struct I_Foo_Body { + struct ExI_Foo_Body { int16_t _0; }; - struct I_Bar_Body { + struct ExI_Bar_Body { uint8_t x; int16_t y; }; Tag tag; union { - I_Foo_Body foo; - I_Bar_Body bar; + ExI_Foo_Body foo; + ExI_Bar_Body bar; }; }; @@ -184,7 +184,7 @@ void root(Opaque *opaque, F f, G g, H h, - I i, + ExI i, J j, K k, L l, diff --git a/tests/expectations/enum.pyx b/tests/expectations/enum.pyx index e410d81f8..14b648e36 100644 --- a/tests/expectations/enum.pyx +++ b/tests/expectations/enum.pyx @@ -114,19 +114,19 @@ cdef extern from *: H_Bar_Body bar; cdef enum: - I_Foo, - I_Bar, - I_Baz, - ctypedef uint8_t I_Tag; + ExI_Foo, + ExI_Bar, + ExI_Baz, + ctypedef uint8_t ExI_Tag; - ctypedef struct I_Bar_Body: + ctypedef struct ExI_Bar_Body: uint8_t x; int16_t y; - ctypedef struct I: - I_Tag tag; + ctypedef struct ExI: + ExI_Tag tag; int16_t foo; - I_Bar_Body bar; + ExI_Bar_Body bar; cdef enum: P0, @@ -152,7 +152,7 @@ cdef extern from *: F f, G g, H h, - I i, + ExI i, J j, K k, L l, diff --git a/tests/expectations/enum.tag.c b/tests/expectations/enum.tag.c index 019d4b9e8..ca734bafb 100644 --- a/tests/expectations/enum.tag.c +++ b/tests/expectations/enum.tag.c @@ -129,25 +129,25 @@ struct H { }; }; -enum I_Tag { - I_Foo, - I_Bar, - I_Baz, +enum ExI_Tag { + ExI_Foo, + ExI_Bar, + ExI_Baz, }; -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; -struct I_Bar_Body { +struct ExI_Bar_Body { uint8_t x; int16_t y; }; -struct I { - I_Tag tag; +struct ExI { + ExI_Tag tag; union { struct { int16_t foo; }; - struct I_Bar_Body bar; + struct ExI_Bar_Body bar; }; }; @@ -182,7 +182,7 @@ void root(struct Opaque *opaque, F f, union G g, struct H h, - struct I i, + struct ExI i, struct J j, struct K k, enum L l, diff --git a/tests/expectations/enum.tag.compat.c b/tests/expectations/enum.tag.compat.c index 890ba7183..71b543c7f 100644 --- a/tests/expectations/enum.tag.compat.c +++ b/tests/expectations/enum.tag.compat.c @@ -183,31 +183,31 @@ struct H { }; }; -enum I_Tag +enum ExI_Tag #ifdef __cplusplus : uint8_t #endif // __cplusplus { - I_Foo, - I_Bar, - I_Baz, + ExI_Foo, + ExI_Bar, + ExI_Baz, }; #ifndef __cplusplus -typedef uint8_t I_Tag; +typedef uint8_t ExI_Tag; #endif // __cplusplus -struct I_Bar_Body { +struct ExI_Bar_Body { uint8_t x; int16_t y; }; -struct I { - I_Tag tag; +struct ExI { + ExI_Tag tag; union { struct { int16_t foo; }; - struct I_Bar_Body bar; + struct ExI_Bar_Body bar; }; }; @@ -252,7 +252,7 @@ void root(struct Opaque *opaque, F f, union G g, struct H h, - struct I i, + struct ExI i, struct J j, struct K k, enum L l, diff --git a/tests/expectations/enum.tag.pyx b/tests/expectations/enum.tag.pyx index bc956a77e..e74be3392 100644 --- a/tests/expectations/enum.tag.pyx +++ b/tests/expectations/enum.tag.pyx @@ -114,19 +114,19 @@ cdef extern from *: H_Bar_Body bar; cdef enum: - I_Foo, - I_Bar, - I_Baz, - ctypedef uint8_t I_Tag; + ExI_Foo, + ExI_Bar, + ExI_Baz, + ctypedef uint8_t ExI_Tag; - cdef struct I_Bar_Body: + cdef struct ExI_Bar_Body: uint8_t x; int16_t y; - cdef struct I: - I_Tag tag; + cdef struct ExI: + ExI_Tag tag; int16_t foo; - I_Bar_Body bar; + ExI_Bar_Body bar; cdef enum: P0, @@ -152,7 +152,7 @@ cdef extern from *: F f, G g, H h, - I i, + ExI i, J j, K k, L l, diff --git a/tests/rust/enum.toml b/tests/rust/enum.toml index 807507bfa..dd2dd79df 100644 --- a/tests/rust/enum.toml +++ b/tests/rust/enum.toml @@ -14,3 +14,6 @@ static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P"); ' ''' #endif """ + +[export.rename] +"I" = "ExI"