diff --git a/bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-many.rs b/bindgen-tests/tests/expectations/tests/extern-block-attrs-many.rs similarity index 100% rename from bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-many.rs rename to bindgen-tests/tests/expectations/tests/extern-block-attrs-many.rs diff --git a/bindgen-tests/tests/expectations/tests/extern-fn-block-attrs.rs b/bindgen-tests/tests/expectations/tests/extern-block-attrs-merge.rs similarity index 59% rename from bindgen-tests/tests/expectations/tests/extern-fn-block-attrs.rs rename to bindgen-tests/tests/expectations/tests/extern-block-attrs-merge.rs index 657cec106a..5d2e5713d0 100644 --- a/bindgen-tests/tests/expectations/tests/extern-fn-block-attrs.rs +++ b/bindgen-tests/tests/expectations/tests/extern-block-attrs-merge.rs @@ -2,4 +2,6 @@ #[allow(dead_code)] unsafe extern "C" { pub fn test_function(); + pub static mut test_var: ::std::os::raw::c_int; + pub static test_const_var: ::std::os::raw::c_int; } diff --git a/bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-wasm.rs b/bindgen-tests/tests/expectations/tests/extern-block-attrs-wasm.rs similarity index 100% rename from bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-wasm.rs rename to bindgen-tests/tests/expectations/tests/extern-block-attrs-wasm.rs diff --git a/bindgen-tests/tests/expectations/tests/extern-block-attrs.rs b/bindgen-tests/tests/expectations/tests/extern-block-attrs.rs new file mode 100644 index 0000000000..61a27ae338 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/extern-block-attrs.rs @@ -0,0 +1,13 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#[allow(dead_code)] +unsafe extern "C" { + pub fn test_function(); +} +#[allow(dead_code)] +unsafe extern "C" { + pub static mut test_var: ::std::os::raw::c_int; +} +#[allow(dead_code)] +unsafe extern "C" { + pub static test_const_var: ::std::os::raw::c_int; +} diff --git a/bindgen-tests/tests/headers/extern-block-attrs-many.h b/bindgen-tests/tests/headers/extern-block-attrs-many.h new file mode 100644 index 0000000000..082340722d --- /dev/null +++ b/bindgen-tests/tests/headers/extern-block-attrs-many.h @@ -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(); diff --git a/bindgen-tests/tests/headers/extern-block-attrs-merge.h b/bindgen-tests/tests/headers/extern-block-attrs-merge.h new file mode 100644 index 0000000000..f1b1493f3e --- /dev/null +++ b/bindgen-tests/tests/headers/extern-block-attrs-merge.h @@ -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; diff --git a/bindgen-tests/tests/headers/extern-block-attrs-wasm.h b/bindgen-tests/tests/headers/extern-block-attrs-wasm.h new file mode 100644 index 0000000000..7db1a308fb --- /dev/null +++ b/bindgen-tests/tests/headers/extern-block-attrs-wasm.h @@ -0,0 +1,3 @@ +// bindgen-flags: --extern-block-attrs '#[allow(dead_code)]' --wasm-import-module-name test-module + +void test_function(); diff --git a/bindgen-tests/tests/headers/extern-block-attrs.h b/bindgen-tests/tests/headers/extern-block-attrs.h new file mode 100644 index 0000000000..00102ac343 --- /dev/null +++ b/bindgen-tests/tests/headers/extern-block-attrs.h @@ -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; diff --git a/bindgen-tests/tests/headers/extern-fn-block-attrs-many.h b/bindgen-tests/tests/headers/extern-fn-block-attrs-many.h deleted file mode 100644 index 0de6eebb36..0000000000 --- a/bindgen-tests/tests/headers/extern-fn-block-attrs-many.h +++ /dev/null @@ -1,3 +0,0 @@ -// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]' --extern-fn-block-attrs '#[cfg_attr(not(windows), link(wasm_import_module = "test-module"))]' - -void test_function(); \ No newline at end of file diff --git a/bindgen-tests/tests/headers/extern-fn-block-attrs-wasm.h b/bindgen-tests/tests/headers/extern-fn-block-attrs-wasm.h deleted file mode 100644 index 2f475f1ed1..0000000000 --- a/bindgen-tests/tests/headers/extern-fn-block-attrs-wasm.h +++ /dev/null @@ -1,3 +0,0 @@ -// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]' --wasm-import-module-name test-module - -void test_function(); \ No newline at end of file diff --git a/bindgen-tests/tests/headers/extern-fn-block-attrs.h b/bindgen-tests/tests/headers/extern-fn-block-attrs.h deleted file mode 100644 index 26480e2ef0..0000000000 --- a/bindgen-tests/tests/headers/extern-fn-block-attrs.h +++ /dev/null @@ -1,3 +0,0 @@ -// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]' - -void test_function(); \ No newline at end of file diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 7a998c8fac..dfe22abc4e 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -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 { @@ -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; @@ -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!( diff --git a/bindgen/options/cli.rs b/bindgen/options/cli.rs index 5304862584..18b16cfcba 100644 --- a/bindgen/options/cli.rs +++ b/bindgen/options/cli.rs @@ -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, - /// Attributes to apply to the extern function block. + /// Attributes to apply to extern blocks. #[arg(long, value_name = "ATTRS")] - extern_fn_block_attrs: Vec, + extern_block_attrs: Vec, /// Use dynamic loading mode with the given library NAME. #[arg(long, value_name = "NAME")] dynamic_loading: Option, @@ -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, @@ -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, diff --git a/bindgen/options/mod.rs b/bindgen/options/mod.rs index baa541c5ac..bc0cb75a33 100644 --- a/bindgen/options/mod.rs +++ b/bindgen/options/mod.rs @@ -1917,20 +1917,20 @@ options! { as_args: "--use-array-pointers-in-arguments", }, /// Attributes to add to all `extern` blocks. - extern_fn_block_attrs: Vec { + extern_block_attrs: Vec { 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>(mut self, attr: T) -> Self { - self.options.extern_fn_block_attrs.push(attr.into()); + pub fn extern_block_attrs>(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()); } }, @@ -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