Skip to content

Commit

Permalink
parser: force proc macro fallback
Browse files Browse the repository at this point in the history
Incorporate dtolnay/proc-macro2#220 to fix
the case where a binary which is built with `panic = "abort"` aborts
when calling into cbindgen functionality.

mozilla#569

Signed-off-by: Grant Elbert <elbe0046@gmail.com>
  • Loading branch information
elbe0046 committed Jun 20, 2021
1 parent 63c1043 commit 57918e9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bindgen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl<'a> Parser<'a> {
self.config.parse.expand.profile,
)
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
proc_macro2::fallback::force();
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
crate_name: pkg.name.clone(),
src_path: "".to_owned(),
Expand Down Expand Up @@ -243,6 +244,7 @@ impl<'a> Parser<'a> {
src_path: mod_path.to_str().unwrap().to_owned(),
})?;

proc_macro2::fallback::force();
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
crate_name: pkg.name.clone(),
src_path: mod_path.to_string_lossy().into(),
Expand Down
1 change: 1 addition & 0 deletions tests/basic_cbindgen_wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cargo.lock
13 changes: 13 additions & 0 deletions tests/basic_cbindgen_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "panic_abort"
version = "0.1.0"
edition = "2018"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
cbindgen = { path = "../.." }
23 changes: 23 additions & 0 deletions tests/basic_cbindgen_wrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::{env, process};

fn exit_with_usage(exe_name: impl AsRef<str>) -> ! {
eprintln!("Usage: {} MANIFEST_PATH PKG_NAME", exe_name.as_ref());
process::exit(1);
}

/// A cbindgen wrapper whose return status can be leveraged by tests. Of note is that it is built
/// with `panic = "abort"` with respect to https://github.com/alexcrichton/proc-macro2/pull/220.
pub fn main() {
let opts = env::args().collect::<Vec<String>>();
if opts.len() < 3 {
exit_with_usage(&opts[0]);
}
let crate_dir = &opts[1];
let pkg_name = &opts[2];

cbindgen::Builder::new()
.with_crate(crate_dir)
.with_parse_expand(&vec![pkg_name])
.generate()
.expect("Unable to generate bindings");
}
31 changes: 31 additions & 0 deletions tests/panic_abort_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{env, path::PathBuf, process::Command};

#[test]
fn test_panic_abort_strategy() {
// Validates cbindgen's incorporation of https://github.com/alexcrichton/proc-macro2/issues/218.
//
// Run a binary whose profile specifies `panic = "abort"` and where the binary proceeds to
// call into cbindgen's generation functionality. Prior to the incorporation of the above this
// would result in the binary aborting.
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let mut cmd = Command::new(cargo);

let tests_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("tests");
let wrapper_manifest_path = tests_dir.join("basic_cbindgen_wrapper").join("Cargo.toml");
let arg_manifest_dir = tests_dir.join("rust").join("expand_dep");

cmd.arg("run");
cmd.arg("--manifest-path");
cmd.arg(wrapper_manifest_path.as_path().to_str().unwrap());
cmd.arg("--");
cmd.arg(arg_manifest_dir.as_path().to_str().unwrap());
cmd.arg("expand-dep");

let output = cmd.output().expect("Failed to run cargo command");

assert!(
output.status.success(),
"Cargo run failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}

0 comments on commit 57918e9

Please sign in to comment.