Skip to content

Commit 26f18f5

Browse files
committed
Rename wrap_static_fns_suffix and use it for functional macros
1 parent 10a0ac7 commit 26f18f5

File tree

9 files changed

+37
-45
lines changed

9 files changed

+37
-45
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@
273273
`CargoCallbacks` constant was added to mitigate the breaking nature of this
274274
change. This constant has been marked as deprecated and users will have to
275275
use the new `CargoCallbacks::new` method in the future.
276-
- Renamed `--wrap-static-fns-path` argument to `-wrapper-code-generation-path` and the
276+
- Renamed `--wrap-static-fns-path` argument to `--wrapper-code-generation-path` and the
277277
corresponding `wrap_static_fns_path` builder function to `wrapper_code_generation_path`.
278+
- Renamed `--wrap-static-fns-suffix` argument to `--wrapper-function-suffix` and the
279+
corresponding `wrap_static_fns_suffix` builder function to `wrapper_function_suffix`.
278280
## Removed
279281
## Fixed
280282
- Allow compiling `bindgen-cli` with a static libclang.

bindgen-cli/options.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ struct BindgenCommand {
419419
/// Sets the path of the file where generated code for wrapper functions will be emitted.
420420
#[arg(long, requires = "experimental", value_name = "PATH")]
421421
wrapper_code_generation_path: Option<PathBuf>,
422-
/// Sets the SUFFIX added to the extern wrapper functions generated for `static` and `static
423-
/// inline` functions.
422+
/// Sets the SUFFIX added to the wrapper functions generated for `static` and `static inline`
423+
/// functions and functional macros.
424424
#[arg(long, requires = "experimental", value_name = "SUFFIX")]
425-
wrap_static_fns_suffix: Option<String>,
425+
wrapper_function_suffix: Option<String>,
426426
/// Create a wrapper function for the macro. The MACRO value must be of the shape
427427
/// `[<return type>] <macro name>[(<comma separated list of arguments>)]`.
428428
#[arg(long, requires = "experimental", value_name = "MACRO")]
@@ -564,7 +564,7 @@ where
564564
with_derive_custom_union,
565565
wrap_static_fns,
566566
wrapper_code_generation_path,
567-
wrap_static_fns_suffix,
567+
wrapper_function_suffix,
568568
macro_function,
569569
default_visibility,
570570
emit_diagnostics,
@@ -1111,8 +1111,8 @@ where
11111111
builder = builder.wrapper_code_generation_path(path);
11121112
}
11131113

1114-
if let Some(suffix) = wrap_static_fns_suffix {
1115-
builder = builder.wrap_static_fns_suffix(suffix);
1114+
if let Some(suffix) = wrapper_function_suffix {
1115+
builder = builder.wrapper_function_suffix(suffix);
11161116
}
11171117

11181118
if let Some(macro_functions) = macro_function {

bindgen-tests/tests/expectations/tests/function_macros.rs

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/generated/function_macros.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
// Macro function wrappers
66

7-
void SIMPLE__macro(void) { SIMPLE; }
8-
void INDIRECT_SIMPLE__macro(void) { INDIRECT_SIMPLE; }
9-
float COMPLEX__macro(uint32_t x) { return COMPLEX(x); }
10-
float INDIRECT_COMPLEX__macro(uint32_t x) { return INDIRECT_COMPLEX(x); }
11-
float CONDITIONAL_COMPLEX__macro(bool condition, uint32_t x) { return CONDITIONAL_COMPLEX(condition, x); }
7+
void SIMPLE__extern(void) { SIMPLE; }
8+
void INDIRECT_SIMPLE__extern(void) { INDIRECT_SIMPLE; }
9+
float COMPLEX__extern(uint32_t x) { return COMPLEX(x); }
10+
float INDIRECT_COMPLEX__extern(uint32_t x) { return INDIRECT_COMPLEX(x); }
11+
float CONDITIONAL_COMPLEX__extern(bool condition, uint32_t x) { return CONDITIONAL_COMPLEX(condition, x); }

bindgen/codegen/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -4273,11 +4273,8 @@ impl CodeGenerator for Function {
42734273
let should_wrap =
42744274
is_internal && ctx.options().wrap_static_fns && !has_link_name_attr;
42754275

4276-
if should_wrap {
4277-
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
4278-
attributes.push(attributes::link_name::<true>(&name));
4279-
} else if is_function_macro && !has_link_name_attr {
4280-
let name = canonical_name.clone() + "__macro";
4276+
if should_wrap || (is_function_macro && !has_link_name_attr) {
4277+
let name = canonical_name.clone() + ctx.wrapper_function_suffix();
42814278
attributes.push(attributes::link_name::<true>(&name));
42824279
}
42834280

bindgen/codegen/serialize.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,7 @@ impl<'a> CSerialize<'a> for Function {
119119
self.kind() != FunctionKind::Macro || !args.is_empty();
120120

121121
// The name used for the wrapper self.
122-
let wrap_name = format!(
123-
"{}{}",
124-
name,
125-
if self.kind() == FunctionKind::Macro {
126-
"__macro"
127-
} else {
128-
ctx.wrap_static_fns_suffix()
129-
}
130-
);
122+
let wrap_name = format!("{name}{}", ctx.wrapper_function_suffix());
131123

132124
// The function's return type
133125
let (ret_item, ret_ty) = {

bindgen/ir/context.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2863,13 +2863,14 @@ If you encounter an error missing from this list, please file an issue or a PR!"
28632863
}
28642864
}
28652865

2866-
/// Get the suffix to be added to `static` functions if the `--wrap-static-fns` option is
2867-
/// enabled.
2868-
pub(crate) fn wrap_static_fns_suffix(&self) -> &str {
2866+
/// Get the suffix to be added to generated wrapper functions like
2867+
/// `static` functions if the `--wrap-static-fns` option is enabled
2868+
/// or functional macros.
2869+
pub(crate) fn wrapper_function_suffix(&self) -> &str {
28692870
self.options()
2870-
.wrap_static_fns_suffix
2871+
.wrapper_function_suffix
28712872
.as_deref()
2872-
.unwrap_or(crate::DEFAULT_NON_EXTERN_FNS_SUFFIX)
2873+
.unwrap_or(crate::DEFAULT_WRAPPER_FUNCTION_SUFFIX)
28732874
}
28742875
}
28752876

bindgen/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ type HashSet<K> = rustc_hash::FxHashSet<K>;
8585

8686
/// Default prefix for the anon fields.
8787
pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_";
88-
89-
const DEFAULT_NON_EXTERN_FNS_SUFFIX: &str = "__extern";
88+
/// Default suffix for wrapper functions.
89+
const DEFAULT_WRAPPER_FUNCTION_SUFFIX: &str = "__extern";
9090

9191
fn file_is_cpp(name_file: &str) -> bool {
9292
name_file.ends_with(".hpp") ||

bindgen/options/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2024,22 +2024,22 @@ options! {
20242024
},
20252025
as_args: "--wrap-static-fns",
20262026
},
2027-
/// The suffix to be added to the function wrappers for `static` functions.
2028-
wrap_static_fns_suffix: Option<String> {
2027+
/// The suffix to be added to the function wrappers for `static` functions and functional macros.
2028+
wrapper_function_suffix: Option<String> {
20292029
methods: {
20302030
#[cfg(feature = "experimental")]
2031-
/// Set the suffix added to the wrappers for `static` functions.
2031+
/// Set the suffix added to the wrappers for `static` functions and functional macros.
20322032
///
20332033
/// This option only comes into effect if `true` is passed to the
2034-
/// [`Builder::wrap_static_fns`] method.
2034+
/// [`Builder::wrap_static_fns`] method if a functional macro is defined.
20352035
///
20362036
/// The default suffix is `__extern`.
2037-
pub fn wrap_static_fns_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self {
2038-
self.options.wrap_static_fns_suffix = Some(suffix.as_ref().to_owned());
2037+
pub fn wrapper_function_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self {
2038+
self.options.wrapper_function_suffix = Some(suffix.as_ref().to_owned());
20392039
self
20402040
}
20412041
},
2042-
as_args: "--wrap-static-fns-suffix",
2042+
as_args: "--wrapper-function-suffix",
20432043
},
20442044
/// The path of the file where generated native code will be emitted.
20452045
wrapper_code_generation_path: Option<PathBuf> {

0 commit comments

Comments
 (0)