Skip to content

Commit 31a365d

Browse files
committed
parser: force proc macro fallback
Incorporate dtolnay/proc-macro2#220 to fix the case where a binary which is built with `panic = "abort"` aborts when calling into cbdingen functionality. mozilla#569 Signed-off-by: Grant Elbert <grant.elbert@smartthings.com>
1 parent 63c1043 commit 31a365d

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

src/bindgen/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'a> Parser<'a> {
206206
self.config.parse.expand.profile,
207207
)
208208
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
209+
proc_macro2::fallback::force();
209210
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
210211
crate_name: pkg.name.clone(),
211212
src_path: "".to_owned(),
@@ -243,6 +244,7 @@ impl<'a> Parser<'a> {
243244
src_path: mod_path.to_str().unwrap().to_owned(),
244245
})?;
245246

247+
proc_macro2::fallback::force();
246248
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
247249
crate_name: pkg.name.clone(),
248250
src_path: mod_path.to_string_lossy().into(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cargo.lock
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "panic_abort"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[profile.dev]
7+
panic = "abort"
8+
9+
[profile.release]
10+
panic = "abort"
11+
12+
[dependencies]
13+
cbindgen = { path = "../.." }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::{env, process};
2+
3+
fn exit_with_usage(exe_name: impl AsRef<str>) -> ! {
4+
eprintln!("Usage: {} MANIFEST_PATH PKG_NAME", exe_name.as_ref());
5+
process::exit(1);
6+
}
7+
8+
/// A cbindgen wrapper whose return status can be leveraged by tests. Of note is that it is built
9+
/// with `panic = "abort"` with respect to https://github.com/alexcrichton/proc-macro2/pull/220.
10+
pub fn main() {
11+
let opts = env::args().collect::<Vec<String>>();
12+
if opts.len() < 3 {
13+
exit_with_usage(&opts[0]);
14+
}
15+
let crate_dir = &opts[1];
16+
let pkg_name = &opts[2];
17+
18+
cbindgen::Builder::new()
19+
.with_crate(crate_dir)
20+
.with_parse_expand(&vec![pkg_name])
21+
.generate()
22+
.expect("Unable to generate bindings");
23+
}

tests/panic_abort_strategy.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::{env, path::PathBuf, process::Command};
2+
3+
#[test]
4+
fn test_panic_abort_strategy() {
5+
// Validates cbindgen's incorporation of https://github.com/alexcrichton/proc-macro2/issues/218.
6+
//
7+
// Run a binary whose profile specifies `panic = "abort"` and where the binary proceeds to
8+
// call into cbindgen's generation functionality. Prior to the incorporation of the above this
9+
// would result in the binary aborting.
10+
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
11+
let mut cmd = Command::new(cargo);
12+
13+
let tests_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("tests");
14+
let wrapper_manifest_path = tests_dir.join("basic_cbindgen_wrapper").join("Cargo.toml");
15+
let arg_manifest_dir = tests_dir.join("rust").join("expand_dep");
16+
17+
cmd.arg("run");
18+
cmd.arg("--manifest-path");
19+
cmd.arg(wrapper_manifest_path.as_path().to_str().unwrap());
20+
cmd.arg("--");
21+
cmd.arg(arg_manifest_dir.as_path().to_str().unwrap());
22+
cmd.arg("expand-dep");
23+
24+
let output = cmd.output().expect("Failed to run cargo command");
25+
26+
assert!(
27+
output.status.success(),
28+
"Cargo run failed: {}",
29+
String::from_utf8_lossy(&output.stderr)
30+
);
31+
}

0 commit comments

Comments
 (0)