From 4bd7b07d2fecfcdc8aa57e41b9fc6497780a7c88 Mon Sep 17 00:00:00 2001 From: Rasmus Eneman Date: Sat, 24 Aug 2019 01:09:50 +0200 Subject: [PATCH] Add support for adding "using namespace" statements --- src/bindgen/bindings.rs | 8 ++++++++ src/bindgen/config.rs | 3 +++ tests/expectations/both/using.c | 6 ++++++ tests/expectations/both/using.compat.c | 16 ++++++++++++++++ tests/expectations/tag/using.c | 6 ++++++ tests/expectations/tag/using.compat.c | 16 ++++++++++++++++ tests/expectations/using.c | 6 ++++++ tests/expectations/using.compat.c | 16 ++++++++++++++++ tests/expectations/using.cpp | 12 ++++++++++++ tests/rust/using.rs | 3 +++ tests/rust/using.toml | 1 + 11 files changed, 93 insertions(+) create mode 100644 tests/expectations/both/using.c create mode 100644 tests/expectations/both/using.compat.c create mode 100644 tests/expectations/tag/using.c create mode 100644 tests/expectations/tag/using.compat.c create mode 100644 tests/expectations/using.c create mode 100644 tests/expectations/using.compat.c create mode 100644 tests/expectations/using.cpp create mode 100644 tests/rust/using.rs create mode 100644 tests/rust/using.toml diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index aec45d0..b857764 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -217,6 +217,14 @@ impl Bindings { } if self.config.language == Language::Cxx || self.config.cpp_compat { + if let Some(ref using) = self.config.using { + for namespace in using { + out.new_line(); + write!(out, "using namespace {};", namespace); + } + out.new_line(); + } + out.new_line(); out.write("extern \"C\" {"); out.new_line(); diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index c1f6ec0..aa0d18f 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -621,6 +621,8 @@ pub struct Config { pub namespace: Option, /// An optional list of namespaces. Only applicable when language="C++" pub namespaces: Option>, + /// An optional list of namespaces to ddeclare as using. Only applicable when language="C++" + pub using: Option>, /// The style to use for braces pub braces: Braces, /// The preferred length of a line, used for auto breaking function arguments @@ -672,6 +674,7 @@ impl Default for Config { no_includes: false, namespace: None, namespaces: None, + using: None, braces: Braces::SameLine, line_length: 100, tab_width: 2, diff --git a/tests/expectations/both/using.c b/tests/expectations/both/using.c new file mode 100644 index 0000000..376246e --- /dev/null +++ b/tests/expectations/both/using.c @@ -0,0 +1,6 @@ +#include +#include +#include +#include + +void root(void); diff --git a/tests/expectations/both/using.compat.c b/tests/expectations/both/using.compat.c new file mode 100644 index 0000000..5659475 --- /dev/null +++ b/tests/expectations/both/using.compat.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +using namespace std; + +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/tag/using.c b/tests/expectations/tag/using.c new file mode 100644 index 0000000..376246e --- /dev/null +++ b/tests/expectations/tag/using.c @@ -0,0 +1,6 @@ +#include +#include +#include +#include + +void root(void); diff --git a/tests/expectations/tag/using.compat.c b/tests/expectations/tag/using.compat.c new file mode 100644 index 0000000..5659475 --- /dev/null +++ b/tests/expectations/tag/using.compat.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +using namespace std; + +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/using.c b/tests/expectations/using.c new file mode 100644 index 0000000..376246e --- /dev/null +++ b/tests/expectations/using.c @@ -0,0 +1,6 @@ +#include +#include +#include +#include + +void root(void); diff --git a/tests/expectations/using.compat.c b/tests/expectations/using.compat.c new file mode 100644 index 0000000..5659475 --- /dev/null +++ b/tests/expectations/using.compat.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +using namespace std; + +extern "C" { +#endif // __cplusplus + +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/using.cpp b/tests/expectations/using.cpp new file mode 100644 index 0000000..cd881fa --- /dev/null +++ b/tests/expectations/using.cpp @@ -0,0 +1,12 @@ +#include +#include +#include +#include + +using namespace std; + +extern "C" { + +void root(); + +} // extern "C" diff --git a/tests/rust/using.rs b/tests/rust/using.rs new file mode 100644 index 0000000..03de69f --- /dev/null +++ b/tests/rust/using.rs @@ -0,0 +1,3 @@ +#[no_mangle] +pub extern "C" fn root() { +} diff --git a/tests/rust/using.toml b/tests/rust/using.toml new file mode 100644 index 0000000..986e255 --- /dev/null +++ b/tests/rust/using.toml @@ -0,0 +1 @@ +using = ["std"] \ No newline at end of file