Skip to content

Add --cxx flag #2269

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -1088,5 +1091,9 @@ where
builder = builder.merge_extern_blocks(true);
}

if matches.is_present("cxx") {
builder = builder.cxx();
}

Ok((builder, output, verbose))
}
2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/dash_language.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: -- -x c++ --std=c++11
// bindgen-flags: --cxx -- --std=c++11

template<typename T>
struct Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/empty-enum.h
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/packed-vtable.h
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
6 changes: 6 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions book/src/cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.