Skip to content

Commit

Permalink
Rename wrap_static_fns_suffix and use it for functional macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Abestanis committed Oct 6, 2023
1 parent 1485cd0 commit 74f7bef
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 45 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@
`CargoCallbacks` constant was added to mitigate the breaking nature of this
change. This constant has been marked as deprecated and users will have to
use the new `CargoCallbacks::new` method in the future.
- Renamed `--wrap-static-fns-path` argument to `-wrapper-code-generation-path` and the
- Renamed `--wrap-static-fns-path` argument to `--wrapper-code-generation-path` and the
corresponding `wrap_static_fns_path` builder function to `wrapper_code_generation_path`.
- Renamed `--wrap-static-fns-suffix` argument to `--wrapper-function-suffix` and the
corresponding `wrap_static_fns_suffix` builder function to `wrapper_function_suffix`.
## Removed
## Fixed
- Allow compiling `bindgen-cli` with a static libclang.
Expand Down
12 changes: 6 additions & 6 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ struct BindgenCommand {
/// Sets the path of the file where generated code for wrapper functions will be emitted.
#[arg(long, requires = "experimental", value_name = "PATH")]
wrapper_code_generation_path: Option<PathBuf>,
/// Sets the SUFFIX added to the extern wrapper functions generated for `static` and `static
/// inline` functions.
/// Sets the SUFFIX added to the wrapper functions generated for `static` and `static inline`
/// functions and functional macros.
#[arg(long, requires = "experimental", value_name = "SUFFIX")]
wrap_static_fns_suffix: Option<String>,
wrapper_function_suffix: Option<String>,
/// Create a wrapper function for the macro. The MACRO value must be of the shape
/// `[<return type>] <macro name>[(<comma separated list of arguments>)]`.
#[arg(long, requires = "experimental", value_name = "MACRO")]
Expand Down Expand Up @@ -560,7 +560,7 @@ where
with_derive_custom_union,
wrap_static_fns,
wrapper_code_generation_path,
wrap_static_fns_suffix,
wrapper_function_suffix,
macro_function,
default_visibility,
emit_diagnostics,
Expand Down Expand Up @@ -1099,8 +1099,8 @@ where
builder = builder.wrapper_code_generation_path(path);
}

if let Some(suffix) = wrap_static_fns_suffix {
builder = builder.wrap_static_fns_suffix(suffix);
if let Some(suffix) = wrapper_function_suffix {
builder = builder.wrapper_function_suffix(suffix);
}

if let Some(macro_functions) = macro_function {
Expand Down
10 changes: 5 additions & 5 deletions bindgen-tests/tests/expectations/tests/function_macros.rs

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// Macro function wrappers

void SIMPLE__macro(void) { SIMPLE; }
void INDIRECT_SIMPLE__macro(void) { INDIRECT_SIMPLE; }
float COMPLEX__macro(uint32_t x) { return COMPLEX(x); }
float INDIRECT_COMPLEX__macro(uint32_t x) { return INDIRECT_COMPLEX(x); }
float CONDITIONAL_COMPLEX__macro(bool condition, uint32_t x) { return CONDITIONAL_COMPLEX(condition, x); }
void SIMPLE__extern(void) { SIMPLE; }
void INDIRECT_SIMPLE__extern(void) { INDIRECT_SIMPLE; }
float COMPLEX__extern(uint32_t x) { return COMPLEX(x); }
float INDIRECT_COMPLEX__extern(uint32_t x) { return INDIRECT_COMPLEX(x); }
float CONDITIONAL_COMPLEX__extern(bool condition, uint32_t x) { return CONDITIONAL_COMPLEX(condition, x); }
7 changes: 2 additions & 5 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4262,11 +4262,8 @@ impl CodeGenerator for Function {
let should_wrap =
is_internal && ctx.options().wrap_static_fns && !has_link_name_attr;

if should_wrap {
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
attributes.push(attributes::link_name::<true>(&name));
} else if is_function_macro && !has_link_name_attr {
let name = canonical_name.clone() + "__macro";
if should_wrap || (is_function_macro && !has_link_name_attr) {
let name = canonical_name.clone() + ctx.wrapper_function_suffix();
attributes.push(attributes::link_name::<true>(&name));
}

Expand Down
10 changes: 1 addition & 9 deletions bindgen/codegen/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,7 @@ impl<'a> CSerialize<'a> for Function {
self.kind() != FunctionKind::Macro || !args.is_empty();

// The name used for the wrapper self.
let wrap_name = format!(
"{}{}",
name,
if self.kind() == FunctionKind::Macro {
"__macro"
} else {
ctx.wrap_static_fns_suffix()
}
);
let wrap_name = format!("{name}{}", ctx.wrapper_function_suffix());

// The function's return type
let (ret_item, ret_ty) = {
Expand Down
11 changes: 6 additions & 5 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2842,13 +2842,14 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}
}

/// Get the suffix to be added to `static` functions if the `--wrap-static-fns` option is
/// enabled.
pub(crate) fn wrap_static_fns_suffix(&self) -> &str {
/// Get the suffix to be added to generated wrapper functions like
/// `static` functions if the `--wrap-static-fns` option is enabled
/// or functional macros.
pub(crate) fn wrapper_function_suffix(&self) -> &str {
self.options()
.wrap_static_fns_suffix
.wrapper_function_suffix
.as_deref()
.unwrap_or(crate::DEFAULT_NON_EXTERN_FNS_SUFFIX)
.unwrap_or(crate::DEFAULT_WRAPPER_FUNCTION_SUFFIX)
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ type HashSet<K> = rustc_hash::FxHashSet<K>;

/// Default prefix for the anon fields.
pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_";

const DEFAULT_NON_EXTERN_FNS_SUFFIX: &str = "__extern";
/// Default suffix for wrapper functions.
const DEFAULT_WRAPPER_FUNCTION_SUFFIX: &str = "__extern";

fn file_is_cpp(name_file: &str) -> bool {
name_file.ends_with(".hpp") ||
Expand Down
14 changes: 7 additions & 7 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1979,22 +1979,22 @@ options! {
},
as_args: "--wrap-static-fns",
},
/// The suffix to be added to the function wrappers for `static` functions.
wrap_static_fns_suffix: Option<String> {
/// The suffix to be added to the function wrappers for `static` functions and functional macros.
wrapper_function_suffix: Option<String> {
methods: {
#[cfg(feature = "experimental")]
/// Set the suffix added to the wrappers for `static` functions.
/// Set the suffix added to the wrappers for `static` functions and functional macros.
///
/// This option only comes into effect if `true` is passed to the
/// [`Builder::wrap_static_fns`] method.
/// [`Builder::wrap_static_fns`] method if a functional macro is defined.
///
/// The default suffix is `__extern`.
pub fn wrap_static_fns_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self {
self.options.wrap_static_fns_suffix = Some(suffix.as_ref().to_owned());
pub fn wrapper_function_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self {
self.options.wrapper_function_suffix = Some(suffix.as_ref().to_owned());
self
}
},
as_args: "--wrap-static-fns-suffix",
as_args: "--wrapper-function-suffix",
},
/// The path of the file where generated native code will be emitted.
wrapper_code_generation_path: Option<PathBuf> {
Expand Down

0 comments on commit 74f7bef

Please sign in to comment.