-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rename rule for generated associated constant's base name
In FFI Rust code it is often necessary to use constants instead of enums for compatibility reasons. The most natural thing to do to somewhat preserve grouping is to use associated constants. E.g.: ```rust #[repr(C)] struct Foo {} impl Foo { pub const FLAG1: u32 = 10; pub const FLAG2: u32 = 11; ... ``` For the above, the generated constants would be called Foo_FLAG1 & Foo_FLAG2. Many Linux core C libraries adhere to a slightly different casing, however, where the type prefix is fully upper cased: FOO_FLAG1. Currently, it does not seem possible to generate such constants. This change introduces a new config option to structured types that enables this use case: the struct.associated_constants_rename_base_name option expects a rename rule (as other rename related config options) and applies it to the struct base name as used in conjunction with associated constants to form the final name of the constant. Signed-off-by: Daniel Müller <[email protected]>
- Loading branch information
Showing
15 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/expectations/associated_constants_rename_base_name.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct { | ||
|
||
} Foo; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
void root(Foo x); |
20 changes: 20 additions & 0 deletions
20
tests/expectations/associated_constants_rename_base_name.compat.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct { | ||
|
||
} Foo; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
void root(Foo x); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif // __cplusplus |
17 changes: 17 additions & 0 deletions
17
tests/expectations/associated_constants_rename_base_name.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <cstdarg> | ||
#include <cstdint> | ||
#include <cstdlib> | ||
#include <ostream> | ||
#include <new> | ||
|
||
struct Foo { | ||
|
||
}; | ||
constexpr static const int32_t FOO_GA = 10; | ||
constexpr static const float FOO_ZO = 3.14; | ||
|
||
extern "C" { | ||
|
||
void root(Foo x); | ||
|
||
} // extern "C" |
14 changes: 14 additions & 0 deletions
14
tests/expectations/associated_constants_rename_base_name.pyx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t | ||
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t | ||
cdef extern from *: | ||
ctypedef bint bool | ||
ctypedef struct va_list | ||
|
||
cdef extern from *: | ||
|
||
ctypedef struct Foo: | ||
pass | ||
const int32_t FOO_GA # = 10 | ||
const float FOO_ZO # = 3.14 | ||
|
||
void root(Foo x); |
12 changes: 12 additions & 0 deletions
12
tests/expectations/associated_constants_rename_base_name_both.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct Foo { | ||
|
||
} Foo; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
void root(struct Foo x); |
20 changes: 20 additions & 0 deletions
20
tests/expectations/associated_constants_rename_base_name_both.compat.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct Foo { | ||
|
||
} Foo; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
void root(struct Foo x); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif // __cplusplus |
12 changes: 12 additions & 0 deletions
12
tests/expectations/associated_constants_rename_base_name_tag.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
struct Foo { | ||
|
||
}; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
void root(struct Foo x); |
20 changes: 20 additions & 0 deletions
20
tests/expectations/associated_constants_rename_base_name_tag.compat.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
struct Foo { | ||
|
||
}; | ||
#define FOO_GA 10 | ||
#define FOO_ZO 3.14 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
void root(struct Foo x); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif // __cplusplus |
14 changes: 14 additions & 0 deletions
14
tests/expectations/associated_constants_rename_base_name_tag.pyx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t | ||
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t | ||
cdef extern from *: | ||
ctypedef bint bool | ||
ctypedef struct va_list | ||
|
||
cdef extern from *: | ||
|
||
cdef struct Foo: | ||
pass | ||
const int32_t FOO_GA # = 10 | ||
const float FOO_ZO # = 3.14 | ||
|
||
void root(Foo x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// cbindgen:associated-constants-rename-base-name=UpperCase | ||
#[repr(C)] | ||
struct Foo {} | ||
|
||
impl Foo { | ||
pub const GA: i32 = 10; | ||
pub const ZO: f32 = 3.14; | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn root(x: Foo) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[struct] | ||
associated_constants_rename_base_name = "UpperCase" |