Skip to content

Commit 0182bb0

Browse files
committed
add --cxx flag
1 parent 73ce4bc commit 0182bb0

File tree

7 files changed

+21
-9
lines changed

7 files changed

+21
-9
lines changed

bindgen-cli/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ where
568568
Arg::new("merge-extern-blocks")
569569
.long("merge-extern-blocks")
570570
.help("Deduplicates extern blocks."),
571+
Arg::new("cxx")
572+
.long("cxx")
573+
.help("Enables parsing C++ headers."),
571574
Arg::new("V")
572575
.long("version")
573576
.help("Prints the version, and exits"),
@@ -1088,5 +1091,9 @@ where
10881091
builder = builder.merge_extern_blocks(true);
10891092
}
10901093

1094+
if matches.is_present("cxx") {
1095+
builder = builder.cxx();
1096+
}
1097+
10911098
Ok((builder, output, verbose))
10921099
}

bindgen-tests/tests/headers/dash_language.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: -- -x c++ --std=c++11
1+
// bindgen-flags: --cxx -- --std=c++11
22

33
template<typename T>
44
struct Foo {

bindgen-tests/tests/headers/dupe-enum-variant-in-namespace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --rustified-enum ".*" --enable-cxx-namespaces -- -x c++ -std=c++11
1+
// bindgen-flags: --rustified-enum ".*" --enable-cxx-namespaces --cxx -- -std=c++11
22

33
namespace foo {
44
enum class Bar : unsigned {

bindgen-tests/tests/headers/empty-enum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --rustified-enum '.*Rustified.*' --constified-enum-module '.*Module.*' -- -x c++ --std=c++14
1+
// bindgen-flags: --rustified-enum '.*Rustified.*' --constified-enum-module '.*Module.*' --cxx -- --std=c++14
22

33
// Constified is default, so no flag for that.
44

bindgen-tests/tests/headers/packed-vtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 -- -x c++ -std=c++11
1+
// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 --cxx -- -std=c++11
22

33
#pragma pack(1)
44

bindgen/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,12 @@ impl Builder {
13231323
self
13241324
}
13251325

1326+
/// Tell `clang` that the input files are `c++` files.
1327+
#[inline]
1328+
pub fn cxx(self) -> Builder {
1329+
self.clang_args(["-x", "c++"])
1330+
}
1331+
13261332
/// Enable detecting must_use attributes on C functions.
13271333
///
13281334
/// This is quite slow in some cases (see #1465), so it's disabled by

book/src/cpp.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ be nowhere near as nice as using them in C++. You will have to manually call
77
constructors, destructors, overloaded operators, etc yourself.
88

99
When passing in header files, the file will automatically be treated as C++ if
10-
it ends in `.hpp`. If it doesn't, adding `-x c++` clang args can be used to
11-
force C++ mode. You probably also want to use `-std=c++14` or similar clang args
12-
as well.
10+
it ends in `.hpp`. If it doesn't, adding the `--cxx` flag will force C++ mode.
11+
You probably also want to use the clang arg `-std=c++14` or similar as well.
1312

1413
You pretty much **must** use [allowlisting](./allowlisting.md) when working
1514
with C++ to avoid pulling in all of the `std::.*` types, many of which `bindgen`
@@ -71,8 +70,8 @@ cannot translate into Rust:
7170
generate undefined behaviour. See
7271
[the tracking issue for exceptions](https://github.com/rust-lang/rust-bindgen/issues/1208)
7372
for more details.
74-
73+
7574
* Return value optimization. C++ compilers will in certain circumstances optimize functions
7675
returning a struct type value to instead take an extra hidden argument that refers
7776
to the return value struct. `bindgen` cannot necessarily know about this optimization and
78-
thus at present `bindgen`-interfaces for these kinds of functions are invalid.
77+
thus at present `bindgen`-interfaces for these kinds of functions are invalid.

0 commit comments

Comments
 (0)