Support an annotation for generating bitfield sizes in C code (: SIZE)
Closes #609 Closes #611
This commit is contained in:
committed by
Emilio Cobos Álvarez
parent
dc12952e19
commit
a689bcabc7
@@ -46,5 +46,8 @@ impl Source for Field {
|
||||
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
|
||||
self.documentation.write(config, out);
|
||||
cdecl::write_field(out, &self.ty, &self.name, config);
|
||||
if let Some(bitfield) = self.annotations.atom("bitfield") {
|
||||
write!(out, ": {}", bitfield.unwrap_or_default());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct HasBitfields {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
} HasBitfields;
|
||||
|
||||
void root(const HasBitfields*);
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct HasBitfields {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
} HasBitfields;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
void root(const HasBitfields*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
} HasBitfields;
|
||||
|
||||
void root(const HasBitfields*);
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
} HasBitfields;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
void root(const HasBitfields*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <new>
|
||||
|
||||
struct HasBitfields {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
void root(const HasBitfields*);
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct HasBitfields {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
};
|
||||
|
||||
void root(const struct HasBitfields*);
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct HasBitfields {
|
||||
uint64_t foo: 8;
|
||||
uint64_t bar: 56;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
void root(const struct HasBitfields*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
Generated
+5
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "bitfield"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "bitfield"
|
||||
version = "0.1.0"
|
||||
authors = ["cbindgen"]
|
||||
|
||||
[features]
|
||||
cbindgen = []
|
||||
@@ -0,0 +1,3 @@
|
||||
[parse.expand]
|
||||
crates = ["bitfield"]
|
||||
features = ["cbindgen"]
|
||||
@@ -0,0 +1,15 @@
|
||||
#[repr(C)]
|
||||
pub struct HasBitfields {
|
||||
#[cfg(not(feature = "cbindgen"))]
|
||||
foo_and_bar: u64,
|
||||
|
||||
#[cfg(feature = "cbindgen")]
|
||||
/// cbindgen:bitfield=8
|
||||
foo: u64,
|
||||
#[cfg(feature = "cbindgen")]
|
||||
/// cbindgen:bitfield=56
|
||||
bar: u64,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn root(_: &HasBitfields) {}
|
||||
Reference in New Issue
Block a user