summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poveda <christian.poveda@ferrous-systems.com>2022-10-21 13:02:54 -0500
committerEmilio Cobos Álvarez <emilio@crisal.io>2022-10-22 13:11:45 +0200
commit0142c0aaf72a3859e319640224740965c2ddeab3 (patch)
tree37c8c21588ccbf132a8e5d7ca2a331164aad9c4d
parent61ced32b93bb17cb7f31bb252bd07abb125443dc (diff)
Use panic hooks instead of using `catch_unwind`
One of the advantages of doing this is that `ParseCallbacks` no longer needs to implement `UnwindSafe` which means that users can rely on `RefCell` and `Cell` to extract information from the callbacks. Users relying on `catch_unwind` can still achieve similar behavior using `std::thread::spawn`. Fixes #2147.
-rw-r--r--bindgen-cli/main.rs17
-rw-r--r--bindgen/callbacks.rs3
-rw-r--r--bindgen/lib.rs5
3 files changed, 9 insertions, 16 deletions
diff --git a/bindgen-cli/main.rs b/bindgen-cli/main.rs
index a61f67ad..fed454af 100644
--- a/bindgen-cli/main.rs
+++ b/bindgen-cli/main.rs
@@ -6,7 +6,6 @@ extern crate env_logger;
extern crate log;
use std::env;
-use std::panic;
mod options;
use crate::options::builder_from_flags;
@@ -41,18 +40,18 @@ pub fn main() {
Ok((builder, output, verbose)) => {
#[cfg(feature = "logging")]
clang_version_check();
- let builder_result = panic::catch_unwind(|| {
- builder.generate().expect("Unable to generate bindings")
- });
- if builder_result.is_err() {
+ std::panic::set_hook(Box::new(move |_info| {
if verbose {
- print_verbose_err();
+ print_verbose_err()
}
- std::process::exit(1);
- }
+ }));
+
+ let bindings =
+ builder.generate().expect("Unable to generate bindings");
+
+ let _ = std::panic::take_hook();
- let bindings = builder_result.unwrap();
bindings.write(output).expect("Unable to write output");
}
Err(error) => {
diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs
index d0eb4667..fb84c8c3 100644
--- a/bindgen/callbacks.rs
+++ b/bindgen/callbacks.rs
@@ -5,7 +5,6 @@ pub use crate::ir::derive::CanDerive as ImplementsTrait;
pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
pub use crate::ir::int::IntKind;
use std::fmt;
-use std::panic::UnwindSafe;
/// An enum to allow ignoring parsing of macros.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -25,7 +24,7 @@ impl Default for MacroParsingBehavior {
/// A trait to allow configuring different kinds of types in different
/// situations.
-pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
+pub trait ParseCallbacks: fmt::Debug {
/// This function will be run on every macro that is identified.
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
MacroParsingBehavior::Default
diff --git a/bindgen/lib.rs b/bindgen/lib.rs
index 880d52a9..22ce603c 100644
--- a/bindgen/lib.rs
+++ b/bindgen/lib.rs
@@ -2103,11 +2103,6 @@ struct BindgenOptions {
merge_extern_blocks: 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) {
let mut regex_sets = [