Skip to content

Commit 09bc835

Browse files
committed
Added catch_unwind to catch panic at generator due to missing or incorrect flags
1 parent c1aaa6a commit 09bc835

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/chooser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
pub use ir::int::IntKind;
44
pub use ir::enum_ty::{EnumVariantValue, EnumVariantCustomBehavior};
55
use std::fmt;
6+
use std::panic::UnwindSafe;
67

78
/// A trait to allow configuring different kinds of types in different
89
/// situations.
9-
pub trait TypeChooser: fmt::Debug {
10+
pub trait TypeChooser: fmt::Debug + UnwindSafe {
1011
/// The integer kind an integer macro should have, given a name and the
1112
/// value of that macro, or `None` if you want the default to be chosen.
1213
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ pub struct BindgenOptions {
555555
pub objc_extern_crate: bool,
556556
}
557557

558+
/// TODO(emilio): This is sort of a lie (see the error message that results from
559+
/// removing this), but since we don't share references across panic boundaries
560+
/// it's ok.
561+
impl ::std::panic::UnwindSafe for BindgenOptions {}
562+
558563
impl BindgenOptions {
559564
fn build(&mut self) {
560565
self.whitelisted_vars.build();

src/main.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate rustc_serialize;
88

99
use bindgen::clang_version;
1010
use std::env;
11+
use std::panic;
1112

1213
mod options;
1314
use options::builder_from_flags;
@@ -44,8 +45,24 @@ pub fn main() {
4445

4546
match builder_from_flags(bind_args.into_iter()) {
4647
Ok((builder, output)) => {
47-
let mut bindings = builder.generate()
48-
.expect("Unable to generate bindings");
48+
49+
let builder_result =
50+
panic::catch_unwind(||
51+
builder.generate().expect("Unable to generate bindings")
52+
);
53+
54+
if builder_result.is_err() {
55+
println!("Bindgen unexpectedly panicked");
56+
println!("This may be caused by one of the known-unsupported \
57+
things (https://github.com/servo/rust-bindgen#c), \
58+
please modify the bindgen flags to work around it as \
59+
described in https://github.com/servo/rust-bindgen#c");
60+
println!("Otherwise, please file an issue at \
61+
https://github.com/servo/rust-bindgen/issues/new");
62+
std::process::exit(1);
63+
}
64+
65+
let mut bindings = builder_result.unwrap();
4966
bindings.write(output)
5067
.expect("Unable to write output");
5168
bindings.write_dummy_uses()

0 commit comments

Comments
 (0)