Skip to content

Put blocks behind a switch. #1410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
repository = "https://github.com/rust-lang-nursery/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang-nursery.github.io/rust-bindgen/"
version = "0.41.0"
version = "0.42.0"
build = "build.rs"

include = [
Expand Down
8 changes: 8 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ impl CodeGenerator for Type {
inst.codegen(ctx, result, item)
}
TypeKind::BlockPointer(inner) => {
if !ctx.options().generate_block {
return;
}

let inner_item = inner.into_resolver()
.through_type_refs()
.resolve(ctx);
Expand Down Expand Up @@ -3135,6 +3139,10 @@ impl TryToRustTy for Type {
TypeKind::TemplateAlias(..) |
TypeKind::Alias(..) |
TypeKind::BlockPointer(..) => {
if self.is_block_pointer() && !ctx.options().generate_block {
let void = raw_type(ctx, "c_void");
return Ok(void.to_ptr(/* is_const = */ false));
}
let template_params = item.used_template_params(ctx)
.into_iter()
.filter(|param| param.is_template_param(ctx, &()))
Expand Down
8 changes: 8 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ impl Type {
self.name.as_ref().map(|name| &**name)
}

/// Whether this is a block pointer type.
pub fn is_block_pointer(&self) -> bool {
match self.kind {
TypeKind::BlockPointer(..) => true,
_ => false,
}
}

/// Is this a compound type?
pub fn is_comp(&self) -> bool {
match self.kind {
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ impl Builder {
output_vector.push("--objc-extern-crate".into());
}

if self.options.generate_block {
output_vector.push("--generate-block".into());
}

if self.options.block_extern_crate {
output_vector.push("--block-extern-crate".into());
}
Expand Down Expand Up @@ -704,6 +708,12 @@ impl Builder {
self
}

/// Generate proper block signatures instead of void pointers.
pub fn generate_block(mut self, doit: bool) -> Self {
self.options.generate_block = doit;
self
}

/// Generate `#[macro_use] extern crate block;` instead of `use block;`
/// in the prologue of the files generated from apple block files
pub fn block_extern_crate(mut self, doit: bool) -> Self {
Expand Down Expand Up @@ -1465,6 +1475,10 @@ struct BindgenOptions {
/// generate '#[macro_use] extern crate objc;'
objc_extern_crate: bool,

/// Instead of emitting 'use block;' to files generated from objective c files,
/// generate '#[macro_use] extern crate block;'
generate_block: bool,

/// Instead of emitting 'use block;' to files generated from objective c files,
/// generate '#[macro_use] extern crate block;'
block_extern_crate: bool,
Expand Down Expand Up @@ -1593,6 +1607,7 @@ impl Default for BindgenOptions {
generate_comments: true,
generate_inline_functions: false,
whitelist_recursively: true,
generate_block: false,
objc_extern_crate: false,
block_extern_crate: false,
enable_mangling: true,
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ where
Arg::with_name("objc-extern-crate")
.long("objc-extern-crate")
.help("Use extern crate instead of use for objc."),
Arg::with_name("generate-block")
.long("generate-block")
.help("Generate block signatures instead of void pointers."),
Arg::with_name("block-extern-crate")
.long("block-extern-crate")
.help("Use extern crate instead of use for block."),
Expand Down Expand Up @@ -496,6 +499,10 @@ where
builder = builder.objc_extern_crate(true);
}

if matches.is_present("generate-block") {
builder = builder.generate_block(true);
}

if matches.is_present("block-extern-crate") {
builder = builder.block_extern_crate(true);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/expectations/tests/blocks-signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* automatically generated by rust-bindgen */

#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]
#![cfg(target_os = "macos")]

extern crate block;
extern "C" {
#[link_name = "\u{1}_Z8atexit_bU13block_pointerFvvE"]
pub fn atexit_b(arg1: _bindgen_ty_id_20);
}
pub type dispatch_data_t = *mut ::std::os::raw::c_void;
pub type dispatch_data_applier_t = _bindgen_ty_id_27;
extern "C" {
#[link_name = "\u{1}_Z19dispatch_data_applyPvU13block_pointerFbS_yPKvyE"]
pub fn dispatch_data_apply(data: dispatch_data_t, applier: dispatch_data_applier_t) -> bool;
}
pub type _bindgen_ty_id_20 = *const ::block::Block<(), ()>;
pub type _bindgen_ty_id_27 =
*const ::block::Block<(dispatch_data_t, usize, *const ::std::os::raw::c_void, usize), bool>;
8 changes: 2 additions & 6 deletions tests/expectations/tests/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
)]
#![cfg(target_os = "macos")]

extern crate block;
extern "C" {
#[link_name = "\u{1}_Z8atexit_bU13block_pointerFvvE"]
pub fn atexit_b(arg1: _bindgen_ty_id_19);
pub fn atexit_b(arg1: *mut ::std::os::raw::c_void);
}
pub type dispatch_data_t = *mut ::std::os::raw::c_void;
pub type dispatch_data_applier_t = _bindgen_ty_id_26;
pub type dispatch_data_applier_t = *mut ::std::os::raw::c_void;
extern "C" {
#[link_name = "\u{1}_Z19dispatch_data_applyPvU13block_pointerFbS_yPKvyE"]
pub fn dispatch_data_apply(data: dispatch_data_t, applier: dispatch_data_applier_t) -> bool;
}
pub type _bindgen_ty_id_19 = *const ::block::Block<(), ()>;
pub type _bindgen_ty_id_26 =
*const ::block::Block<(dispatch_data_t, usize, *const ::std::os::raw::c_void, usize), bool>;
4 changes: 4 additions & 0 deletions tests/headers/blocks-signature.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// bindgen-flags: --generate-block --block-extern-crate -- -fblocks
// bindgen-osx-only

#include "blocks.hpp"