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:🧵:spawn`.

Fixes #2147.
This commit is contained in:
Christian Poveda 2022-10-21 13:02:54 -05:00 committed by Emilio Cobos Álvarez
parent 61ced32b93
commit 0142c0aaf7
3 changed files with 9 additions and 16 deletions

View File

@ -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) => {

View File

@ -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

View File

@ -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 = [