Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum: take renaming into account for variant prefix #672

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 4 additions & 7 deletions src/bindgen/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 => "",
Expand Down Expand Up @@ -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('_');
}
Expand Down
24 changes: 12 additions & 12 deletions tests/expectations/enum.both.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions tests/expectations/enum.both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions tests/expectations/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -182,7 +182,7 @@ void root(Opaque *opaque,
F f,
G g,
H h,
I i,
ExI i,
J j,
K k,
L l,
Expand Down
20 changes: 10 additions & 10 deletions tests/expectations/enum.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -252,7 +252,7 @@ void root(Opaque *opaque,
F f,
G g,
H h,
I i,
ExI i,
J j,
K k,
L l,
Expand Down
18 changes: 9 additions & 9 deletions tests/expectations/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

Expand Down Expand Up @@ -184,7 +184,7 @@ void root(Opaque *opaque,
F f,
G g,
H h,
I i,
ExI i,
J j,
K k,
L l,
Expand Down
18 changes: 9 additions & 9 deletions tests/expectations/enum.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -152,7 +152,7 @@ cdef extern from *:
F f,
G g,
H h,
I i,
ExI i,
J j,
K k,
L l,
Expand Down
20 changes: 10 additions & 10 deletions tests/expectations/enum.tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

Expand Down Expand Up @@ -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,
Expand Down
Loading