From e261d2dc3528874535e57d0216d8e4e48fbd0ab6 Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sat, 11 Jan 2020 16:18:32 -0800 Subject: [PATCH 1/9] Added [export.body_prepend] --- docs.md | 11 ++++++- src/bindgen/config.rs | 10 +++++- src/bindgen/ir/enumeration.rs | 12 +++++++- src/bindgen/ir/structure.rs | 10 +++++- src/bindgen/ir/union.rs | 10 +++++- src/bindgen/writer.rs | 1 - tests/expectations/body.c | 52 +++++++++++++++++++++++++++++++- tests/expectations/body.compat.c | 52 +++++++++++++++++++++++++++++++- tests/expectations/body.cpp | 52 +++++++++++++++++++++++++++++++- 9 files changed, 201 insertions(+), 9 deletions(-) diff --git a/docs.md b/docs.md index 725e31af3..24c53c643 100644 --- a/docs.md +++ b/docs.md @@ -488,7 +488,16 @@ renaming_overrides_prefixing = true "MyType" = "my_cool_type" "my_function" = "BetterFunctionName" -# Table of things to add to the body of any struct, union, or enum that has the +# Table of things to prepend to the body of any struct, union, or enum that has the +# given name. This can be used to add things like methods which don't change ABI, +# mark fields private, etc +[export.body_prepend] +"MyType" = """ + MyType() = delete; +private: +""" + +# Table of things to append to the body of any struct, union, or enum that has the # given name. This can be used to add things like methods which don't change ABI. [export.body] "MyType" = """ diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 620963afd..f493097a5 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -214,6 +214,8 @@ pub struct ExportConfig { pub exclude: Vec, /// Table of name conversions to apply to item names pub rename: HashMap, + /// Table of raw strings to prepend to the body of items. + pub body_prepend: HashMap, /// Table of raw strings to append to the body of items. pub body: HashMap, /// A prefix to add before the name of every item @@ -229,7 +231,13 @@ impl ExportConfig { self.item_types.is_empty() || self.item_types.contains(&item_type) } - pub(crate) fn extra_body(&self, path: &Path) -> Option<&str> { + pub(crate) fn body_prepend(&self, path: &Path) -> Option<&str> { + self.body_prepend + .get(path.name()) + .map(|s| s.trim_matches('\n')) + } + + pub(crate) fn body_append(&self, path: &Path) -> Option<&str> { self.body.get(path.name()).map(|s| s.trim_matches('\n')) } diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index 8592d5f27..e73dd2874 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -577,6 +577,8 @@ impl Source for Enum { } } out.open_brace(); + + // Emit variants for (i, variant) in self.variants.iter().enumerate() { if i != 0 { out.new_line() @@ -644,6 +646,12 @@ impl Source for Enum { out.open_brace(); } + // Emit the body_prepend section, if relevant + if let Some(body) = config.export.body_prepend(&self.path) { + out.write_raw_block(body); + out.new_line(); + } + // C++ allows accessing only common initial sequence of union // branches so we need to wrap tag into an anonymous struct let wrap_tag = config.language == Language::Cxx && !separate_tag; @@ -1008,7 +1016,9 @@ impl Source for Enum { } } - if let Some(body) = config.export.extra_body(&self.path) { + // Emit the body_append section, if relevant + if let Some(body) = config.export.body_append(&self.path) { + out.new_line(); out.write_raw_block(body); } diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index cd5080531..8d4a65537 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -452,6 +452,12 @@ impl Source for Struct { out.open_brace(); + // Emit the body_prepend section, if relevant + if let Some(body) = config.export.body_prepend(&self.path) { + out.write_raw_block(body); + out.new_line(); + } + if config.documentation { out.write_vertical_source_list(&self.fields, ListType::Cap(";")); } else { @@ -600,7 +606,9 @@ impl Source for Struct { } } - if let Some(body) = config.export.extra_body(&self.path) { + // Emit the body_append section, if relevant + if let Some(body) = config.export.body_append(&self.path) { + out.new_line(); out.write_raw_block(body); } diff --git a/src/bindgen/ir/union.rs b/src/bindgen/ir/union.rs index afb4cf0b5..6eba01de0 100644 --- a/src/bindgen/ir/union.rs +++ b/src/bindgen/ir/union.rs @@ -299,6 +299,12 @@ impl Source for Union { out.open_brace(); + // Emit the body_prepend section, if relevant + if let Some(body) = config.export.body_prepend(&self.path) { + out.write_raw_block(body); + out.new_line(); + } + if config.documentation { out.write_vertical_source_list(&self.fields, ListType::Cap(";")); } else { @@ -310,7 +316,9 @@ impl Source for Union { out.write_vertical_source_list(&vec[..], ListType::Cap(";")); } - if let Some(body) = config.export.extra_body(&self.path) { + // Emit the body_append section, if relevant + if let Some(body) = config.export.body_append(&self.path) { + out.new_line(); out.write_raw_block(body); } diff --git a/src/bindgen/writer.rs b/src/bindgen/writer.rs index 1fdb0a0e1..78218586c 100644 --- a/src/bindgen/writer.rs +++ b/src/bindgen/writer.rs @@ -176,7 +176,6 @@ impl<'a, F: Write> SourceWriter<'a, F> { } pub fn write_raw_block(&mut self, block: &str) { - self.new_line(); self.line_started = true; write!(self, "{}", block); } diff --git a/tests/expectations/body.c b/tests/expectations/body.c index e0cb7c13a..57ebafd55 100644 --- a/tests/expectations/body.c +++ b/tests/expectations/body.c @@ -9,6 +9,12 @@ typedef enum { Baz1, } MyCLikeEnum; +typedef enum { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +} MyCLikeEnum_Prepended; + typedef struct { int32_t i; #ifdef __cplusplus @@ -47,4 +53,48 @@ typedef union { int32_t extra_member; // yolo } MyUnion; -void root(MyFancyStruct s, MyFancyEnum e, MyCLikeEnum c, MyUnion u); +typedef struct { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +} MyFancyStruct_Prepended; + +typedef enum { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +} MyFancyEnum_Prepended_Tag; + +typedef struct { + int32_t _0; +} Bar_Prepended_Body; + +typedef struct { + int32_t _0; +} Baz_Prepended_Body; + +typedef struct { + // important information about this enum + MyFancyEnum_Prepended_Tag tag; + union { + Bar_Prepended_Body bar_prepended; + Baz_Prepended_Body baz_prepended; + }; +} MyFancyEnum_Prepended; + +typedef union { + int32_t extra_member; // yolo + float f; + uint32_t u; +} MyUnion_Prepended; + +void root(MyFancyStruct s, + MyFancyEnum e, + MyCLikeEnum c, + MyUnion u, + MyFancyStruct_Prepended sp, + MyFancyEnum_Prepended ep, + MyCLikeEnum_Prepended cp, + MyUnion_Prepended up); diff --git a/tests/expectations/body.compat.c b/tests/expectations/body.compat.c index 6ae82cfb8..d1cad991a 100644 --- a/tests/expectations/body.compat.c +++ b/tests/expectations/body.compat.c @@ -9,6 +9,12 @@ typedef enum { Baz1, } MyCLikeEnum; +typedef enum { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +} MyCLikeEnum_Prepended; + typedef struct { int32_t i; #ifdef __cplusplus @@ -47,11 +53,55 @@ typedef union { int32_t extra_member; // yolo } MyUnion; +typedef struct { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +} MyFancyStruct_Prepended; + +typedef enum { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +} MyFancyEnum_Prepended_Tag; + +typedef struct { + int32_t _0; +} Bar_Prepended_Body; + +typedef struct { + int32_t _0; +} Baz_Prepended_Body; + +typedef struct { + // important information about this enum + MyFancyEnum_Prepended_Tag tag; + union { + Bar_Prepended_Body bar_prepended; + Baz_Prepended_Body baz_prepended; + }; +} MyFancyEnum_Prepended; + +typedef union { + int32_t extra_member; // yolo + float f; + uint32_t u; +} MyUnion_Prepended; + #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(MyFancyStruct s, MyFancyEnum e, MyCLikeEnum c, MyUnion u); +void root(MyFancyStruct s, + MyFancyEnum e, + MyCLikeEnum c, + MyUnion u, + MyFancyStruct_Prepended sp, + MyFancyEnum_Prepended ep, + MyCLikeEnum_Prepended cp, + MyUnion_Prepended up); #ifdef __cplusplus } // extern "C" diff --git a/tests/expectations/body.cpp b/tests/expectations/body.cpp index bce30ce3d..a6d4583e1 100644 --- a/tests/expectations/body.cpp +++ b/tests/expectations/body.cpp @@ -9,6 +9,12 @@ enum class MyCLikeEnum { Baz1, }; +enum class MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +}; + struct MyFancyStruct { int32_t i; #ifdef __cplusplus @@ -47,8 +53,52 @@ union MyUnion { int32_t extra_member; // yolo }; +struct MyFancyStruct_Prepended { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +}; + +struct MyFancyEnum_Prepended { + enum class Tag { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, + }; + + struct Bar_Prepended_Body { + int32_t _0; + }; + + struct Baz_Prepended_Body { + int32_t _0; + }; + + // important information about this enum + Tag tag; + union { + Bar_Prepended_Body bar_prepended; + Baz_Prepended_Body baz_prepended; + }; +}; + +union MyUnion_Prepended { + int32_t extra_member; // yolo + float f; + uint32_t u; +}; + extern "C" { -void root(MyFancyStruct s, MyFancyEnum e, MyCLikeEnum c, MyUnion u); +void root(MyFancyStruct s, + MyFancyEnum e, + MyCLikeEnum c, + MyUnion u, + MyFancyStruct_Prepended sp, + MyFancyEnum_Prepended ep, + MyCLikeEnum_Prepended cp, + MyUnion_Prepended up); } // extern "C" From d8cbca7de59cb2b0211d653fd8a2dbe4c65cb45d Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sat, 11 Jan 2020 16:22:08 -0800 Subject: [PATCH 2/9] Back out unnecessary comment --- src/bindgen/ir/enumeration.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index e73dd2874..6a846032f 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -577,8 +577,6 @@ impl Source for Enum { } } out.open_brace(); - - // Emit variants for (i, variant) in self.variants.iter().enumerate() { if i != 0 { out.new_line() From f06eef78091f25e827f15aa281e99c02ddf4e004 Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sat, 11 Jan 2020 16:50:20 -0800 Subject: [PATCH 3/9] Added missing expectations --- tests/expectations/body.c | 4 +- tests/expectations/body.compat.c | 4 +- tests/expectations/body.cpp | 4 +- tests/expectations/both/body.c | 54 ++++++++++++++++++++++++++- tests/expectations/both/body.compat.c | 54 ++++++++++++++++++++++++++- tests/expectations/tag/body.c | 54 ++++++++++++++++++++++++++- tests/expectations/tag/body.compat.c | 54 ++++++++++++++++++++++++++- 7 files changed, 221 insertions(+), 7 deletions(-) diff --git a/tests/expectations/body.c b/tests/expectations/body.c index 57ebafd55..58f6c9f50 100644 --- a/tests/expectations/body.c +++ b/tests/expectations/body.c @@ -76,7 +76,9 @@ typedef struct { } Baz_Prepended_Body; typedef struct { - // important information about this enum + #ifdef __cplusplus + inline void wohoo(); + #endif MyFancyEnum_Prepended_Tag tag; union { Bar_Prepended_Body bar_prepended; diff --git a/tests/expectations/body.compat.c b/tests/expectations/body.compat.c index d1cad991a..03d37e7d1 100644 --- a/tests/expectations/body.compat.c +++ b/tests/expectations/body.compat.c @@ -76,7 +76,9 @@ typedef struct { } Baz_Prepended_Body; typedef struct { - // important information about this enum + #ifdef __cplusplus + inline void wohoo(); + #endif MyFancyEnum_Prepended_Tag tag; union { Bar_Prepended_Body bar_prepended; diff --git a/tests/expectations/body.cpp b/tests/expectations/body.cpp index a6d4583e1..308394a29 100644 --- a/tests/expectations/body.cpp +++ b/tests/expectations/body.cpp @@ -76,7 +76,9 @@ struct MyFancyEnum_Prepended { int32_t _0; }; - // important information about this enum + #ifdef __cplusplus + inline void wohoo(); + #endif Tag tag; union { Bar_Prepended_Body bar_prepended; diff --git a/tests/expectations/both/body.c b/tests/expectations/both/body.c index 58e3c335c..43e012ceb 100644 --- a/tests/expectations/both/body.c +++ b/tests/expectations/both/body.c @@ -9,6 +9,12 @@ typedef enum MyCLikeEnum { Baz1, } MyCLikeEnum; +typedef enum MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +} MyCLikeEnum_Prepended; + typedef struct MyFancyStruct { int32_t i; #ifdef __cplusplus @@ -47,4 +53,50 @@ typedef union MyUnion { int32_t extra_member; // yolo } MyUnion; -void root(MyFancyStruct s, MyFancyEnum e, MyCLikeEnum c, MyUnion u); +typedef struct MyFancyStruct_Prepended { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +} MyFancyStruct_Prepended; + +typedef enum MyFancyEnum_Prepended_Tag { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +} MyFancyEnum_Prepended_Tag; + +typedef struct Bar_Prepended_Body { + int32_t _0; +} Bar_Prepended_Body; + +typedef struct Baz_Prepended_Body { + int32_t _0; +} Baz_Prepended_Body; + +typedef struct MyFancyEnum_Prepended { + #ifdef __cplusplus + inline void wohoo(); + #endif + MyFancyEnum_Prepended_Tag tag; + union { + Bar_Prepended_Body bar_prepended; + Baz_Prepended_Body baz_prepended; + }; +} MyFancyEnum_Prepended; + +typedef union MyUnion_Prepended { + int32_t extra_member; // yolo + float f; + uint32_t u; +} MyUnion_Prepended; + +void root(MyFancyStruct s, + MyFancyEnum e, + MyCLikeEnum c, + MyUnion u, + MyFancyStruct_Prepended sp, + MyFancyEnum_Prepended ep, + MyCLikeEnum_Prepended cp, + MyUnion_Prepended up); diff --git a/tests/expectations/both/body.compat.c b/tests/expectations/both/body.compat.c index bb15aabde..9afb730ae 100644 --- a/tests/expectations/both/body.compat.c +++ b/tests/expectations/both/body.compat.c @@ -9,6 +9,12 @@ typedef enum MyCLikeEnum { Baz1, } MyCLikeEnum; +typedef enum MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +} MyCLikeEnum_Prepended; + typedef struct MyFancyStruct { int32_t i; #ifdef __cplusplus @@ -47,11 +53,57 @@ typedef union MyUnion { int32_t extra_member; // yolo } MyUnion; +typedef struct MyFancyStruct_Prepended { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +} MyFancyStruct_Prepended; + +typedef enum MyFancyEnum_Prepended_Tag { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +} MyFancyEnum_Prepended_Tag; + +typedef struct Bar_Prepended_Body { + int32_t _0; +} Bar_Prepended_Body; + +typedef struct Baz_Prepended_Body { + int32_t _0; +} Baz_Prepended_Body; + +typedef struct MyFancyEnum_Prepended { + #ifdef __cplusplus + inline void wohoo(); + #endif + MyFancyEnum_Prepended_Tag tag; + union { + Bar_Prepended_Body bar_prepended; + Baz_Prepended_Body baz_prepended; + }; +} MyFancyEnum_Prepended; + +typedef union MyUnion_Prepended { + int32_t extra_member; // yolo + float f; + uint32_t u; +} MyUnion_Prepended; + #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(MyFancyStruct s, MyFancyEnum e, MyCLikeEnum c, MyUnion u); +void root(MyFancyStruct s, + MyFancyEnum e, + MyCLikeEnum c, + MyUnion u, + MyFancyStruct_Prepended sp, + MyFancyEnum_Prepended ep, + MyCLikeEnum_Prepended cp, + MyUnion_Prepended up); #ifdef __cplusplus } // extern "C" diff --git a/tests/expectations/tag/body.c b/tests/expectations/tag/body.c index 97bdcb709..01dc52bac 100644 --- a/tests/expectations/tag/body.c +++ b/tests/expectations/tag/body.c @@ -9,6 +9,12 @@ enum MyCLikeEnum { Baz1, }; +enum MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +}; + struct MyFancyStruct { int32_t i; #ifdef __cplusplus @@ -47,4 +53,50 @@ union MyUnion { int32_t extra_member; // yolo }; -void root(struct MyFancyStruct s, struct MyFancyEnum e, enum MyCLikeEnum c, union MyUnion u); +struct MyFancyStruct_Prepended { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +}; + +enum MyFancyEnum_Prepended_Tag { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +}; + +struct Bar_Prepended_Body { + int32_t _0; +}; + +struct Baz_Prepended_Body { + int32_t _0; +}; + +struct MyFancyEnum_Prepended { + #ifdef __cplusplus + inline void wohoo(); + #endif + enum MyFancyEnum_Prepended_Tag tag; + union { + struct Bar_Prepended_Body bar_prepended; + struct Baz_Prepended_Body baz_prepended; + }; +}; + +union MyUnion_Prepended { + int32_t extra_member; // yolo + float f; + uint32_t u; +}; + +void root(struct MyFancyStruct s, + struct MyFancyEnum e, + enum MyCLikeEnum c, + union MyUnion u, + struct MyFancyStruct_Prepended sp, + struct MyFancyEnum_Prepended ep, + enum MyCLikeEnum_Prepended cp, + union MyUnion_Prepended up); diff --git a/tests/expectations/tag/body.compat.c b/tests/expectations/tag/body.compat.c index 5d43001da..d1be791a9 100644 --- a/tests/expectations/tag/body.compat.c +++ b/tests/expectations/tag/body.compat.c @@ -9,6 +9,12 @@ enum MyCLikeEnum { Baz1, }; +enum MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +}; + struct MyFancyStruct { int32_t i; #ifdef __cplusplus @@ -47,11 +53,57 @@ union MyUnion { int32_t extra_member; // yolo }; +struct MyFancyStruct_Prepended { +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif + int32_t i; +}; + +enum MyFancyEnum_Prepended_Tag { + Foo_Prepended, + Bar_Prepended, + Baz_Prepended, +}; + +struct Bar_Prepended_Body { + int32_t _0; +}; + +struct Baz_Prepended_Body { + int32_t _0; +}; + +struct MyFancyEnum_Prepended { + #ifdef __cplusplus + inline void wohoo(); + #endif + enum MyFancyEnum_Prepended_Tag tag; + union { + struct Bar_Prepended_Body bar_prepended; + struct Baz_Prepended_Body baz_prepended; + }; +}; + +union MyUnion_Prepended { + int32_t extra_member; // yolo + float f; + uint32_t u; +}; + #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(struct MyFancyStruct s, struct MyFancyEnum e, enum MyCLikeEnum c, union MyUnion u); +void root(struct MyFancyStruct s, + struct MyFancyEnum e, + enum MyCLikeEnum c, + union MyUnion u, + struct MyFancyStruct_Prepended sp, + struct MyFancyEnum_Prepended ep, + enum MyCLikeEnum_Prepended cp, + union MyUnion_Prepended up); #ifdef __cplusplus } // extern "C" From 36e21d43c2b56ae48c98169140402858127501e0 Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sat, 11 Jan 2020 17:06:14 -0800 Subject: [PATCH 4/9] Added missing files --- tests/rust/body.rs | 29 ++++++++++++++++++++++++++++- tests/rust/body.toml | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/rust/body.rs b/tests/rust/body.rs index 31dc0c24a..fb275f9f4 100644 --- a/tests/rust/body.rs +++ b/tests/rust/body.rs @@ -24,5 +24,32 @@ pub union MyUnion { pub u: u32, } + +#[repr(C)] +pub struct MyFancyStruct_Prepended { + i: i32, +} + +#[repr(C)] +pub enum MyFancyEnum_Prepended { + Foo_Prepended, + Bar_Prepended(i32), + Baz_Prepended(i32), +} + +#[repr(C)] +pub enum MyCLikeEnum_Prepended { + Foo1_Prepended, + Bar1_Prepended, + Baz1_Prepended, +} + +#[repr(C)] +pub union MyUnion_Prepended { + pub f: f32, + pub u: u32, +} + + #[no_mangle] -pub extern "C" fn root(s: MyFancyStruct, e: MyFancyEnum, c: MyCLikeEnum, u: MyUnion) {} +pub extern "C" fn root(s: MyFancyStruct, e: MyFancyEnum, c: MyCLikeEnum, u: MyUnion, sp: MyFancyStruct_Prepended, ep: MyFancyEnum_Prepended, cp: MyCLikeEnum_Prepended, up: MyUnion_Prepended) {} diff --git a/tests/rust/body.toml b/tests/rust/body.toml index 977f7f67f..862de94ef 100644 --- a/tests/rust/body.toml +++ b/tests/rust/body.toml @@ -18,3 +18,26 @@ "MyUnion" = """ int32_t extra_member; // yolo """ + +[export.body_prepend] +"MyFancyStruct_Prepended" = """ +#ifdef __cplusplus + MyFancyStruct_Prepended() = delete; +private: +#endif +""" + +"MyFancyEnum_Prepended" = """ + #ifdef __cplusplus + inline void wohoo(); + #endif +""" + +"MyCLikeEnum_Prepended" = """ + BogusVariantForSerializationForExample, +""" + +"MyUnion_Prepended" = """ + int32_t extra_member; // yolo +""" + From 5be1710c0b1586c094d4d667c5005ac15da5d6af Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sat, 11 Jan 2020 17:47:45 -0800 Subject: [PATCH 5/9] Use a normal member function in the prepend struct test, not a deleted constructor --- tests/expectations/body.c | 3 +-- tests/expectations/body.compat.c | 3 +-- tests/expectations/body.cpp | 3 +-- tests/expectations/both/body.c | 3 +-- tests/expectations/both/body.compat.c | 3 +-- tests/expectations/tag/body.c | 3 +-- tests/expectations/tag/body.compat.c | 3 +-- tests/rust/body.toml | 3 +-- 8 files changed, 8 insertions(+), 16 deletions(-) diff --git a/tests/expectations/body.c b/tests/expectations/body.c index 58f6c9f50..4dc550620 100644 --- a/tests/expectations/body.c +++ b/tests/expectations/body.c @@ -55,8 +55,7 @@ typedef union { typedef struct { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; } MyFancyStruct_Prepended; diff --git a/tests/expectations/body.compat.c b/tests/expectations/body.compat.c index 03d37e7d1..3db7c7ee4 100644 --- a/tests/expectations/body.compat.c +++ b/tests/expectations/body.compat.c @@ -55,8 +55,7 @@ typedef union { typedef struct { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; } MyFancyStruct_Prepended; diff --git a/tests/expectations/body.cpp b/tests/expectations/body.cpp index 308394a29..ee4c0f36b 100644 --- a/tests/expectations/body.cpp +++ b/tests/expectations/body.cpp @@ -55,8 +55,7 @@ union MyUnion { struct MyFancyStruct_Prepended { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; }; diff --git a/tests/expectations/both/body.c b/tests/expectations/both/body.c index 43e012ceb..743422be4 100644 --- a/tests/expectations/both/body.c +++ b/tests/expectations/both/body.c @@ -55,8 +55,7 @@ typedef union MyUnion { typedef struct MyFancyStruct_Prepended { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; } MyFancyStruct_Prepended; diff --git a/tests/expectations/both/body.compat.c b/tests/expectations/both/body.compat.c index 9afb730ae..46b11c730 100644 --- a/tests/expectations/both/body.compat.c +++ b/tests/expectations/both/body.compat.c @@ -55,8 +55,7 @@ typedef union MyUnion { typedef struct MyFancyStruct_Prepended { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; } MyFancyStruct_Prepended; diff --git a/tests/expectations/tag/body.c b/tests/expectations/tag/body.c index 01dc52bac..fd3125e0a 100644 --- a/tests/expectations/tag/body.c +++ b/tests/expectations/tag/body.c @@ -55,8 +55,7 @@ union MyUnion { struct MyFancyStruct_Prepended { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; }; diff --git a/tests/expectations/tag/body.compat.c b/tests/expectations/tag/body.compat.c index d1be791a9..9b1e657c4 100644 --- a/tests/expectations/tag/body.compat.c +++ b/tests/expectations/tag/body.compat.c @@ -55,8 +55,7 @@ union MyUnion { struct MyFancyStruct_Prepended { #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif int32_t i; }; diff --git a/tests/rust/body.toml b/tests/rust/body.toml index 862de94ef..6ebac60a2 100644 --- a/tests/rust/body.toml +++ b/tests/rust/body.toml @@ -22,8 +22,7 @@ [export.body_prepend] "MyFancyStruct_Prepended" = """ #ifdef __cplusplus - MyFancyStruct_Prepended() = delete; -private: + inline void prepended_wohoo(); #endif """ From 020bb0f7e56207fcc514d1c3483dbe547dbd7781 Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sun, 12 Jan 2020 13:12:11 -0800 Subject: [PATCH 6/9] Renamed 'body_prepend' to 'pre_body' --- docs.md | 2 +- src/bindgen/config.rs | 8 ++++---- src/bindgen/ir/enumeration.rs | 4 ++-- src/bindgen/ir/structure.rs | 6 +++--- src/bindgen/ir/union.rs | 4 ++-- tests/rust/body.toml | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs.md b/docs.md index 24c53c643..4b0c68a6a 100644 --- a/docs.md +++ b/docs.md @@ -491,7 +491,7 @@ renaming_overrides_prefixing = true # Table of things to prepend to the body of any struct, union, or enum that has the # given name. This can be used to add things like methods which don't change ABI, # mark fields private, etc -[export.body_prepend] +[export.pre_body] "MyType" = """ MyType() = delete; private: diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index f493097a5..935fa94fa 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -215,7 +215,7 @@ pub struct ExportConfig { /// Table of name conversions to apply to item names pub rename: HashMap, /// Table of raw strings to prepend to the body of items. - pub body_prepend: HashMap, + pub pre_body: HashMap, /// Table of raw strings to append to the body of items. pub body: HashMap, /// A prefix to add before the name of every item @@ -231,13 +231,13 @@ impl ExportConfig { self.item_types.is_empty() || self.item_types.contains(&item_type) } - pub(crate) fn body_prepend(&self, path: &Path) -> Option<&str> { - self.body_prepend + pub(crate) fn pre_body(&self, path: &Path) -> Option<&str> { + self.pre_body .get(path.name()) .map(|s| s.trim_matches('\n')) } - pub(crate) fn body_append(&self, path: &Path) -> Option<&str> { + pub(crate) fn post_body(&self, path: &Path) -> Option<&str> { self.body.get(path.name()).map(|s| s.trim_matches('\n')) } diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index 6a846032f..6068608f7 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -645,7 +645,7 @@ impl Source for Enum { } // Emit the body_prepend section, if relevant - if let Some(body) = config.export.body_prepend(&self.path) { + if let Some(body) = config.export.pre_body(&self.path) { out.write_raw_block(body); out.new_line(); } @@ -1015,7 +1015,7 @@ impl Source for Enum { } // Emit the body_append section, if relevant - if let Some(body) = config.export.body_append(&self.path) { + if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); } diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index 8d4a65537..deaa8bd4e 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -452,8 +452,8 @@ impl Source for Struct { out.open_brace(); - // Emit the body_prepend section, if relevant - if let Some(body) = config.export.body_prepend(&self.path) { + // Emit the pre_body section, if relevant + if let Some(body) = config.export.pre_body(&self.path) { out.write_raw_block(body); out.new_line(); } @@ -607,7 +607,7 @@ impl Source for Struct { } // Emit the body_append section, if relevant - if let Some(body) = config.export.body_append(&self.path) { + if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); } diff --git a/src/bindgen/ir/union.rs b/src/bindgen/ir/union.rs index 6eba01de0..34f673814 100644 --- a/src/bindgen/ir/union.rs +++ b/src/bindgen/ir/union.rs @@ -300,7 +300,7 @@ impl Source for Union { out.open_brace(); // Emit the body_prepend section, if relevant - if let Some(body) = config.export.body_prepend(&self.path) { + if let Some(body) = config.export.pre_body(&self.path) { out.write_raw_block(body); out.new_line(); } @@ -317,7 +317,7 @@ impl Source for Union { } // Emit the body_append section, if relevant - if let Some(body) = config.export.body_append(&self.path) { + if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); } diff --git a/tests/rust/body.toml b/tests/rust/body.toml index 6ebac60a2..973a47fd9 100644 --- a/tests/rust/body.toml +++ b/tests/rust/body.toml @@ -19,7 +19,7 @@ int32_t extra_member; // yolo """ -[export.body_prepend] +[export.pre_body] "MyFancyStruct_Prepended" = """ #ifdef __cplusplus inline void prepended_wohoo(); From 187916eb33eacf8b4842f9d2e6e629012c7d75be Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sun, 12 Jan 2020 13:43:41 -0800 Subject: [PATCH 7/9] Write the body prepend to the correct location on c++ --- src/bindgen/ir/enumeration.rs | 20 +++++++++++++++----- tests/expectations/body.cpp | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index 6068608f7..bda252c39 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -527,6 +527,14 @@ impl Source for Enum { write!(out, " {}", self.export_name()); out.open_brace(); + + // Emit the pre_body section, if relevant + // Only do this here if we're writing C++, since the struct that wraps everything is starting here. + // If we're writing C, we aren't wrapping the enum and variant structs definitions, so the actual enum struct willstart down below + if let Some(body) = config.export.pre_body(&self.path) { + out.write_raw_block(body); + out.new_line(); + } } let enum_name = if let Some(ref tag) = self.tag { @@ -642,12 +650,14 @@ impl Source for Enum { } out.open_brace(); - } - // Emit the body_prepend section, if relevant - if let Some(body) = config.export.pre_body(&self.path) { - out.write_raw_block(body); - out.new_line(); + // Emit the pre_body section, if relevant + // Only do this if we're writing C, since the struct is starting right here. + // For C++, the struct wraps all of the above variant structs too, and we write the pre_body section at the begining of that + if let Some(body) = config.export.pre_body(&self.path) { + out.write_raw_block(body); + out.new_line(); + } } // C++ allows accessing only common initial sequence of union diff --git a/tests/expectations/body.cpp b/tests/expectations/body.cpp index ee4c0f36b..8fbc524c5 100644 --- a/tests/expectations/body.cpp +++ b/tests/expectations/body.cpp @@ -61,6 +61,9 @@ struct MyFancyStruct_Prepended { }; struct MyFancyEnum_Prepended { + #ifdef __cplusplus + inline void wohoo(); + #endif enum class Tag { Foo_Prepended, Bar_Prepended, @@ -75,9 +78,6 @@ struct MyFancyEnum_Prepended { int32_t _0; }; - #ifdef __cplusplus - inline void wohoo(); - #endif Tag tag; union { Bar_Prepended_Body bar_prepended; From 5019a82ec14fdbdaf89201006d38f73ed33be4ca Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sun, 12 Jan 2020 13:53:32 -0800 Subject: [PATCH 8/9] Made sure comments reflect new field names --- src/bindgen/ir/enumeration.rs | 2 +- src/bindgen/ir/structure.rs | 2 +- src/bindgen/ir/union.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index bda252c39..9451feda3 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -1024,7 +1024,7 @@ impl Source for Enum { } } - // Emit the body_append section, if relevant + // Emit the post_body section, if relevant if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index deaa8bd4e..946f41cfe 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -606,7 +606,7 @@ impl Source for Struct { } } - // Emit the body_append section, if relevant + // Emit the post_body section, if relevant if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); diff --git a/src/bindgen/ir/union.rs b/src/bindgen/ir/union.rs index 34f673814..96a0a861d 100644 --- a/src/bindgen/ir/union.rs +++ b/src/bindgen/ir/union.rs @@ -299,7 +299,7 @@ impl Source for Union { out.open_brace(); - // Emit the body_prepend section, if relevant + // Emit the pre_body section, if relevant if let Some(body) = config.export.pre_body(&self.path) { out.write_raw_block(body); out.new_line(); @@ -316,7 +316,7 @@ impl Source for Union { out.write_vertical_source_list(&vec[..], ListType::Cap(";")); } - // Emit the body_append section, if relevant + // Emit the post_body section, if relevant if let Some(body) = config.export.post_body(&self.path) { out.new_line(); out.write_raw_block(body); From ba7acf214075c7ebe25020a691ca66123b39a8b0 Mon Sep 17 00:00:00 2001 From: Elliott Mahler Date: Sun, 12 Jan 2020 14:53:13 -0800 Subject: [PATCH 9/9] Ran cargo fmt --- src/bindgen/config.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 935fa94fa..0c3834eff 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -232,9 +232,7 @@ impl ExportConfig { } pub(crate) fn pre_body(&self, path: &Path) -> Option<&str> { - self.pre_body - .get(path.name()) - .map(|s| s.trim_matches('\n')) + self.pre_body.get(path.name()).map(|s| s.trim_matches('\n')) } pub(crate) fn post_body(&self, path: &Path) -> Option<&str> {