diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-08-22 18:53:55 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-09-07 10:52:31 -0700 |
commit | 61743aa190772a1c75acb75d274215274d0e6873 (patch) | |
tree | ca06c845c4ce20dd7ad1022c5bd28da8ff888cc0 /tests/tests.rs | |
parent | 7346811ab5238feab406d6aee858197fa5254fcb (diff) |
Use `quote` instead of `syntex` for Rust code generation
The `syntex` crate is unmaintained. It is slow to build, and additionally it
requires that we pre-process `src/codegen/mod.rs` before we build the `bindgen`
crate.
The `quote` crate provides similar quasi-quoting functionality, is maintained,
and builds faster. It doesn't have a typed API or builders, however; it only
deals with tokens.
Before this commit:
```
$ cargo clean; cargo build
<snip>
Finished dev [unoptimized + debuginfo] target(s) in 98.75 secs
```
After this commit:
```
$ cargo clean; cargo build
<snip>
Finished dev [unoptimized + debuginfo] target(s) in 46.26 secs
```
Build time is cut in half! But what about run time?
Before this commit:
```
Generated Stylo bindings in: Duration { secs: 3, nanos: 521105668 }
```
After this commit:
```
Generated Stylo bindings in: Duration { secs: 3, nanos: 548797242 }
```
So it appears to be about 20ms slower at generating Stylo bindings, but I
suspect this is well within the noise.
Finally, this also lets us remove that nasty `mem::transmute` inside
`bindgen::ir::BindgenContext::gen` that was used for the old `syntex`
context. Now `BindgenContext` doesn't have a lifetime parameter either. This
should make it easier to revisit doing our analyses in parallel with `rayon`,
since that context was one of the things that made it hard for `BindgenContext`
to implement `Sync`.
Fixes #925
Diffstat (limited to 'tests/tests.rs')
-rw-r--r-- | tests/tests.rs | 81 |
1 files changed, 46 insertions, 35 deletions
diff --git a/tests/tests.rs b/tests/tests.rs index e1cb9ad8..953b3527 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -111,26 +111,26 @@ fn compare_generated_header( "compare_generated_header expects a file", ))); - let mut expected = PathBuf::from(header); - expected.pop(); - expected.pop(); - expected.push("expectations"); - expected.push("tests"); - expected.push(file_name); - expected.set_extension("rs"); + let mut expectation = PathBuf::from(header); + expectation.pop(); + expectation.pop(); + expectation.push("expectations"); + expectation.push("tests"); + expectation.push(file_name); + expectation.set_extension("rs"); // If the expectation file doesn't exist, see if we have different test // expectations for different libclang versions. - if !expected.is_file() { - let file_name = expected.file_name().unwrap().to_owned(); - expected.pop(); + if !expectation.is_file() { + let file_name = expectation.file_name().unwrap().to_owned(); + expectation.pop(); if cfg!(feature = "testing_only_libclang_4") { - expected.push("libclang-4"); + expectation.push("libclang-4"); } else if cfg!(feature = "testing_only_libclang_3_9") { - expected.push("libclang-3.9"); + expectation.push("libclang-3.9"); } else if cfg!(feature = "testing_only_libclang_3_8") { - expected.push("libclang-3.8"); + expectation.push("libclang-3.8"); } else { match clang_version().parsed { None => {} @@ -141,42 +141,43 @@ fn compare_generated_header( } else { format!("{}.{}", maj, min) }; - expected.push(format!("libclang-{}", version_str)); + expectation.push(format!("libclang-{}", version_str)); } } } - expected.push(file_name); + expectation.push(file_name); - if !expected.is_file() { + if !expectation.is_file() { panic!( "missing test expectation file and/or 'testing_only_libclang_$VERSION' \ feature for header '{}'; looking for expectation file at '{}'", header.display(), - expected.display() + expectation.display() ); } } // We skip the generate() error here so we get a full diff below - let (bindings, rustfmt_stderr) = match builder.generate() { + let (actual, rustfmt_stderr) = match builder.generate() { Ok(bindings) => { - let bindings = bindings.to_string(); - rustfmt(bindings) + let actual = bindings.to_string(); + rustfmt(actual) } Err(()) => ("<error generating bindings>".to_string(), "".to_string()), }; - let mut buffer = String::new(); + let mut expected = String::new(); { - if let Ok(expected_file) = fs::File::open(&expected) { - try!(BufReader::new(expected_file).read_to_string(&mut buffer)); + if let Ok(expectation_file) = fs::File::open(&expectation) { + try!(BufReader::new(expectation_file).read_to_string(&mut expected)); } } - let (buffer, _) = rustfmt(buffer); - if bindings == buffer { - if !bindings.is_empty() { + let (expected, _) = rustfmt(expected); + + if actual == expected { + if !actual.is_empty() { return Ok(()); } return Err(Error::new( @@ -188,10 +189,10 @@ fn compare_generated_header( println!("{}", rustfmt_stderr); println!("diff expected generated"); - println!("--- expected: {:?}", expected); + println!("--- expected: {:?}", expectation); println!("+++ generated from: {:?}", header); - for diff in diff::lines(&buffer, &bindings) { + for diff in diff::lines(&expected, &actual) { match diff { diff::Result::Left(l) => println!("-{}", l), diff::Result::Both(l, _) => println!(" {}", l), @@ -201,8 +202,8 @@ fn compare_generated_header( // Override the diff. { - let mut expected_file = try!(fs::File::create(&expected)); - try!(expected_file.write_all(bindings.as_bytes())); + let mut expectation_file = try!(fs::File::create(&expectation)); + try!(expectation_file.write_all(actual.as_bytes())); } Err(Error::new(ErrorKind::Other, "Header and binding differ!")) @@ -311,19 +312,25 @@ include!(concat!(env!("OUT_DIR"), "/tests.rs")); #[test] fn test_header_contents() { - let bindings = builder() + let actual = builder() .header_contents("test.h", "int foo(const char* a);") .generate() .unwrap() .to_string(); - assert_eq!( - bindings, - "/* automatically generated by rust-bindgen */ + + let (actual, stderr) = rustfmt(actual); + println!("{}", stderr); + + let (expected, _) = rustfmt("/* automatically generated by rust-bindgen */ extern \"C\" { pub fn foo(a: *const ::std::os::raw::c_char) -> ::std::os::raw::c_int; } -" +".to_string()); + + assert_eq!( + expected, + actual ); } @@ -339,10 +346,14 @@ fn test_multiple_header_calls_in_builder() { .unwrap() .to_string(); + let (actual, stderr) = rustfmt(actual); + println!("{}", stderr); + let expected = include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs" )); + let (expected, _) = rustfmt(expected.to_string()); if actual != expected { println!("Generated bindings differ from expected!"); |