Skip to content
Open
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

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

13 changes: 13 additions & 0 deletions bindgen-tests/tests/expectations/tests/extern-block-attrs.rs

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

3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/extern-block-attrs-many.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --extern-block-attrs '#[allow(dead_code)]' --extern-block-attrs '#[cfg_attr(not(windows), link(wasm_import_module = "test-module"))]'

void test_function();
6 changes: 6 additions & 0 deletions bindgen-tests/tests/headers/extern-block-attrs-merge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-flags: --extern-block-attrs '#[allow(dead_code)]' --merge-extern-blocks

void test_function();

extern int test_var;
extern const int test_const_var;
3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/extern-block-attrs-wasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --extern-block-attrs '#[allow(dead_code)]' --wasm-import-module-name test-module

void test_function();
6 changes: 6 additions & 0 deletions bindgen-tests/tests/headers/extern-block-attrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-flags: --extern-block-attrs '#[allow(dead_code)]'

void test_function();

extern int test_var;
extern const int test_const_var;
3 changes: 0 additions & 3 deletions bindgen-tests/tests/headers/extern-fn-block-attrs-many.h

This file was deleted.

3 changes: 0 additions & 3 deletions bindgen-tests/tests/headers/extern-fn-block-attrs-wasm.h

This file was deleted.

3 changes: 0 additions & 3 deletions bindgen-tests/tests/headers/extern-fn-block-attrs.h

This file was deleted.

17 changes: 16 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,20 @@ impl CodeGenerator for Var {
}
});

let mut block_attributes = quote! {};
for attr in &ctx.options().extern_block_attrs {
let parsed_attr = proc_macro2::TokenStream::from_str(attr).unwrap_or_else(
|err| {
panic!(
"Error parsing extern static block attribute `{attr}`: {err}"
)
},
);
block_attributes.extend(quote! {
#parsed_attr
});
}

let maybe_mut = if self.is_const() {
quote! {}
} else {
Expand All @@ -857,6 +871,7 @@ impl CodeGenerator for Var {
.then(|| quote!(unsafe));

let tokens = quote!(
#block_attributes
#safety extern "C" {
#(#attrs)*
pub static #maybe_mut #canonical_ident: #ty;
Expand Down Expand Up @@ -4785,7 +4800,7 @@ impl CodeGenerator for Function {
}

let mut block_attributes = quote! {};
for attr in &ctx.options().extern_fn_block_attrs {
for attr in &ctx.options().extern_block_attrs {
let parsed_attr = proc_macro2::TokenStream::from_str(attr).unwrap_or_else(
|err| {
panic!(
Expand Down
8 changes: 4 additions & 4 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ struct BindgenCommand {
/// The NAME to be used in a #[link(wasm_import_module = ...)] statement
#[arg(long, value_name = "NAME")]
wasm_import_module_name: Option<String>,
/// Attributes to apply to the extern function block.
/// Attributes to apply to extern blocks.
#[arg(long, value_name = "ATTRS")]
extern_fn_block_attrs: Vec<String>,
extern_block_attrs: Vec<String>,
/// Use dynamic loading mode with the given library NAME.
#[arg(long, value_name = "NAME")]
dynamic_loading: Option<String>,
Expand Down Expand Up @@ -678,7 +678,7 @@ where
enable_function_attribute_detection,
use_array_pointers_in_arguments,
wasm_import_module_name,
extern_fn_block_attrs,
extern_block_attrs,
dynamic_loading,
dynamic_link_require_all,
prefix_link_name,
Expand Down Expand Up @@ -940,7 +940,7 @@ where
time_phases,
use_array_pointers_in_arguments => Builder::array_pointers_in_arguments,
wasm_import_module_name,
extern_fn_block_attrs => Builder::extern_fn_block_attrs,
extern_block_attrs => Builder::extern_block_attrs,
ctypes_prefix,
anon_fields_prefix,
generate => Builder::with_codegen_config,
Expand Down
10 changes: 5 additions & 5 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,20 +1917,20 @@ options! {
as_args: "--use-array-pointers-in-arguments",
},
/// Attributes to add to all `extern` blocks.
extern_fn_block_attrs: Vec<String> {
extern_block_attrs: Vec<String> {
methods: {
/// Add an attribute to all the `extern` blocks generated by `bindgen`.
///
/// This can be used to add attributes such as `#[link(...)]` to all
/// the `extern` blocks.
pub fn extern_fn_block_attrs<T: Into<String>>(mut self, attr: T) -> Self {
self.options.extern_fn_block_attrs.push(attr.into());
pub fn extern_block_attrs<T: Into<String>>(mut self, attr: T) -> Self {
self.options.extern_block_attrs.push(attr.into());
self
}
},
as_args: |attrs, args| {
for attr in attrs {
args.push("--extern-fn-block-attrs".to_owned());
args.push("--extern-block-attrs".to_owned());
args.push(attr.clone());
}
},
Expand All @@ -1946,7 +1946,7 @@ options! {
mut self,
import_name: T,
) -> Self {
self.options.extern_fn_block_attrs.push(format!(
self.options.extern_block_attrs.push(format!(
"#[link(wasm_import_module = \"{}\")]", import_name.into()
));
self
Expand Down
Loading