Skip to content

Commit 5ec00b1

Browse files
committed
add --cxx flag
1 parent 6de2d3d commit 5ec00b1

File tree

7 files changed

+21
-9
lines changed

7 files changed

+21
-9
lines changed

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.

src/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

src/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ where
563563
Arg::new("merge-extern-blocks")
564564
.long("merge-extern-blocks")
565565
.help("Deduplicates extern blocks."),
566+
Arg::new("cxx")
567+
.long("cxx")
568+
.help("Enables parsing C++ headers."),
566569
Arg::new("V")
567570
.long("version")
568571
.help("Prints the version, and exits"),
@@ -1083,5 +1086,9 @@ where
10831086
builder = builder.merge_extern_blocks(true);
10841087
}
10851088

1089+
if matches.is_present("cxx") {
1090+
builder = builder.cxx();
1091+
}
1092+
10861093
Ok((builder, output, verbose))
10871094
}

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 {

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 {

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

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

0 commit comments

Comments
 (0)