From 09bc83526b51f8c2728dbaee2754a3020243d752 Mon Sep 17 00:00:00 2001 From: Jeffrey Deng Date: Mon, 30 Jan 2017 11:36:56 -0500 Subject: Added catch_unwind to catch panic at generator due to missing or incorrect flags --- src/chooser.rs | 3 ++- src/lib.rs | 5 +++++ src/main.rs | 21 +++++++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/chooser.rs b/src/chooser.rs index 51392d70..d7a20771 100644 --- a/src/chooser.rs +++ b/src/chooser.rs @@ -3,10 +3,11 @@ pub use ir::int::IntKind; pub use ir::enum_ty::{EnumVariantValue, EnumVariantCustomBehavior}; use std::fmt; +use std::panic::UnwindSafe; /// A trait to allow configuring different kinds of types in different /// situations. -pub trait TypeChooser: fmt::Debug { +pub trait TypeChooser: fmt::Debug + UnwindSafe { /// The integer kind an integer macro should have, given a name and the /// value of that macro, or `None` if you want the default to be chosen. fn int_macro(&self, _name: &str, _value: i64) -> Option { diff --git a/src/lib.rs b/src/lib.rs index d6f3c66e..f2fdc7b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -555,6 +555,11 @@ pub struct BindgenOptions { pub objc_extern_crate: bool, } +/// TODO(emilio): This is sort of a lie (see the error message that results from +/// removing this), but since we don't share references across panic boundaries +/// it's ok. +impl ::std::panic::UnwindSafe for BindgenOptions {} + impl BindgenOptions { fn build(&mut self) { self.whitelisted_vars.build(); diff --git a/src/main.rs b/src/main.rs index a7bd9618..c5d2b063 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ extern crate rustc_serialize; use bindgen::clang_version; use std::env; +use std::panic; mod options; use options::builder_from_flags; @@ -44,8 +45,24 @@ pub fn main() { match builder_from_flags(bind_args.into_iter()) { Ok((builder, output)) => { - let mut bindings = builder.generate() - .expect("Unable to generate bindings"); + + let builder_result = + panic::catch_unwind(|| + builder.generate().expect("Unable to generate bindings") + ); + + if builder_result.is_err() { + println!("Bindgen unexpectedly panicked"); + println!("This may be caused by one of the known-unsupported \ + things (https://github.com/servo/rust-bindgen#c), \ + please modify the bindgen flags to work around it as \ + described in https://github.com/servo/rust-bindgen#c"); + println!("Otherwise, please file an issue at \ + https://github.com/servo/rust-bindgen/issues/new"); + std::process::exit(1); + } + + let mut bindings = builder_result.unwrap(); bindings.write(output) .expect("Unable to write output"); bindings.write_dummy_uses() -- cgit v1.2.3