From 0182bb041ff422d53c66306e231894d75bc50bca Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 9 Sep 2022 10:58:12 -0500 Subject: [PATCH] add `--cxx` flag --- bindgen-cli/options.rs | 7 +++++++ bindgen-tests/tests/headers/dash_language.h | 2 +- .../tests/headers/dupe-enum-variant-in-namespace.h | 2 +- bindgen-tests/tests/headers/empty-enum.h | 2 +- bindgen-tests/tests/headers/packed-vtable.h | 2 +- bindgen/lib.rs | 6 ++++++ book/src/cpp.md | 9 ++++----- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 5c3960e92b..ed4bffbb50 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -568,6 +568,9 @@ where Arg::new("merge-extern-blocks") .long("merge-extern-blocks") .help("Deduplicates extern blocks."), + Arg::new("cxx") + .long("cxx") + .help("Enables parsing C++ headers."), Arg::new("V") .long("version") .help("Prints the version, and exits"), @@ -1088,5 +1091,9 @@ where builder = builder.merge_extern_blocks(true); } + if matches.is_present("cxx") { + builder = builder.cxx(); + } + Ok((builder, output, verbose)) } diff --git a/bindgen-tests/tests/headers/dash_language.h b/bindgen-tests/tests/headers/dash_language.h index 4c8bb58dd7..531e54f2e0 100644 --- a/bindgen-tests/tests/headers/dash_language.h +++ b/bindgen-tests/tests/headers/dash_language.h @@ -1,4 +1,4 @@ -// bindgen-flags: -- -x c++ --std=c++11 +// bindgen-flags: --cxx -- --std=c++11 template struct Foo { diff --git a/bindgen-tests/tests/headers/dupe-enum-variant-in-namespace.h b/bindgen-tests/tests/headers/dupe-enum-variant-in-namespace.h index 2b6fb93a8e..7f9539dfdb 100644 --- a/bindgen-tests/tests/headers/dupe-enum-variant-in-namespace.h +++ b/bindgen-tests/tests/headers/dupe-enum-variant-in-namespace.h @@ -1,4 +1,4 @@ -// bindgen-flags: --rustified-enum ".*" --enable-cxx-namespaces -- -x c++ -std=c++11 +// bindgen-flags: --rustified-enum ".*" --enable-cxx-namespaces --cxx -- -std=c++11 namespace foo { enum class Bar : unsigned { diff --git a/bindgen-tests/tests/headers/empty-enum.h b/bindgen-tests/tests/headers/empty-enum.h index 8b7502e671..8735eeff6c 100644 --- a/bindgen-tests/tests/headers/empty-enum.h +++ b/bindgen-tests/tests/headers/empty-enum.h @@ -1,4 +1,4 @@ -// bindgen-flags: --rustified-enum '.*Rustified.*' --constified-enum-module '.*Module.*' -- -x c++ --std=c++14 +// bindgen-flags: --rustified-enum '.*Rustified.*' --constified-enum-module '.*Module.*' --cxx -- --std=c++14 // Constified is default, so no flag for that. diff --git a/bindgen-tests/tests/headers/packed-vtable.h b/bindgen-tests/tests/headers/packed-vtable.h index d2413d4571..6584dead82 100644 --- a/bindgen-tests/tests/headers/packed-vtable.h +++ b/bindgen-tests/tests/headers/packed-vtable.h @@ -1,4 +1,4 @@ -// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 -- -x c++ -std=c++11 +// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 --cxx -- -std=c++11 #pragma pack(1) diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 6e6fa225d6..e64218e7cd 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -1323,6 +1323,12 @@ impl Builder { self } + /// Tell `clang` that the input files are `c++` files. + #[inline] + pub fn cxx(self) -> Builder { + self.clang_args(["-x", "c++"]) + } + /// Enable detecting must_use attributes on C functions. /// /// This is quite slow in some cases (see #1465), so it's disabled by diff --git a/book/src/cpp.md b/book/src/cpp.md index 75ef855494..d7a69a5cbb 100644 --- a/book/src/cpp.md +++ b/book/src/cpp.md @@ -7,9 +7,8 @@ be nowhere near as nice as using them in C++. You will have to manually call constructors, destructors, overloaded operators, etc yourself. When passing in header files, the file will automatically be treated as C++ if -it ends in `.hpp`. If it doesn't, adding `-x c++` clang args can be used to -force C++ mode. You probably also want to use `-std=c++14` or similar clang args -as well. +it ends in `.hpp`. If it doesn't, adding the `--cxx` flag will force C++ mode. +You probably also want to use the clang arg `-std=c++14` or similar as well. You pretty much **must** use [allowlisting](./allowlisting.md) when working with C++ to avoid pulling in all of the `std::.*` types, many of which `bindgen` @@ -71,8 +70,8 @@ cannot translate into Rust: generate undefined behaviour. See [the tracking issue for exceptions](https://github.com/rust-lang/rust-bindgen/issues/1208) for more details. - + * Return value optimization. C++ compilers will in certain circumstances optimize functions returning a struct type value to instead take an extra hidden argument that refers to the return value struct. `bindgen` cannot necessarily know about this optimization and - thus at present `bindgen`-interfaces for these kinds of functions are invalid. + thus at present `bindgen`-interfaces for these kinds of functions are invalid.