Support an annotation for generating bitfield sizes in C code (: SIZE)

Closes #609
Closes #611
This commit is contained in:
Vadim Petrochenkov
2020-10-27 01:51:31 +03:00
committed by Emilio Cobos Álvarez
parent dc12952e19
commit a689bcabc7
12 changed files with 139 additions and 0 deletions
+3
View File
@@ -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());
}
}
}
+11
View File
@@ -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*);
+19
View File
@@ -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
+11
View File
@@ -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*);
+19
View File
@@ -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
+16
View File
@@ -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"
+11
View File
@@ -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*);
+19
View File
@@ -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
+5
View File
@@ -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"
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "bitfield"
version = "0.1.0"
authors = ["cbindgen"]
[features]
cbindgen = []
+3
View File
@@ -0,0 +1,3 @@
[parse.expand]
crates = ["bitfield"]
features = ["cbindgen"]
+15
View File
@@ -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) {}