Skip to content

Commit

Permalink
Use panic hooks instead of using catch_unwind
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pvdrz authored and emilio committed Oct 22, 2022
1 parent 61ced32 commit 0142c0a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
17 changes: 8 additions & 9 deletions bindgen-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 1 addition & 2 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 0142c0a

Please sign in to comment.