Skip to content

Generate extern wrappers for inlined functions #2335

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 37 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
afbc4d8
Generate extern wrappers for inlined functions
pvdrz Nov 4, 2022
ad0f161
add(static inlined): tests
amanjeev Jan 19, 2023
101cde0
add the `experimental` feature
pvdrz Jan 24, 2023
61b2b44
Rename options
pvdrz Jan 26, 2023
65a80e0
Fixup: Rename options
pvdrz Jan 26, 2023
eb823b8
merge filename and directory arguments
pvdrz Jan 26, 2023
8b0bdcd
avoid second run
pvdrz Jan 26, 2023
39ac21b
move all c serialization to codegen
pvdrz Jan 26, 2023
fa47c2b
some nits
pvdrz Jan 27, 2023
e33237b
update docs and remove headers
pvdrz Jan 27, 2023
d65527f
keep code serialization in the happy path
pvdrz Jan 27, 2023
c80ed2d
rename `non-extern` to `static`
pvdrz Jan 27, 2023
9ecbde4
Remove headers
pvdrz Jan 31, 2023
34278f4
update integration tests
pvdrz Jan 31, 2023
eb2a3d3
run rustfmt
pvdrz Jan 31, 2023
595ce78
add experimental feature to dependency
pvdrz Jan 31, 2023
cb93924
use static kind
pvdrz Jan 31, 2023
e36e088
force function names
pvdrz Jan 31, 2023
ceebca1
refactor c serialization
pvdrz Jan 31, 2023
f7df058
add types for serialized functions and support pointer types
pvdrz Feb 1, 2023
c55aa5b
buffer all the code before writing
pvdrz Feb 1, 2023
c12645f
stop bindgen if there's a serialization error
pvdrz Feb 2, 2023
3eb7f69
add missing space
pvdrz Feb 2, 2023
b682c6c
track location while reporting errors
pvdrz Feb 2, 2023
6721b96
fix test
pvdrz Feb 2, 2023
ab6ea25
add support for Comp types
pvdrz Feb 2, 2023
bc7f6bf
run rustfmt
pvdrz Feb 2, 2023
e823b32
support `char`
pvdrz Feb 2, 2023
0aafbee
add `Extra` associated type
pvdrz Feb 2, 2023
58f740d
add proper support for functions
pvdrz Feb 2, 2023
affcb2f
fix tests and remove dbg
pvdrz Feb 2, 2023
4819ea4
run rustfmt
pvdrz Feb 2, 2023
7b8fe72
handle type aliases
pvdrz Feb 2, 2023
7698c71
handle constness
pvdrz Feb 2, 2023
9326863
use void for empty args
pvdrz Feb 2, 2023
5a9e5ca
pass amanfmt :trollface:
pvdrz Feb 6, 2023
572d1a4
some nits
pvdrz Feb 7, 2023
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
Prev Previous commit
Next Next commit
rename non-extern to static
  • Loading branch information
pvdrz committed Jan 27, 2023
commit c80ed2d5dc71cdd34b01118f77b7598b5f324fe7
24 changes: 12 additions & 12 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,15 @@ struct BindgenCommand {
with_derive_custom_union: Vec<String>,
/// Generate wrappers for `static` and `static inline` functions.
#[arg(long, requires = "experimental")]
wrap_non_extern_fns: bool,
wrap_static_fns: bool,
/// Sets the path for the source file that must be created due to the presence of `static` and
/// `static inline` functions.
#[arg(long, requires = "experimental", value_name = "PATH")]
wrap_non_extern_fns_path: Option<PathBuf>,
wrap_static_fns_path: Option<PathBuf>,
/// Sets the suffix added to the extern wrapper functions generated for `static` and `static
/// inline` functions.
#[arg(long, requires = "experimental", value_name = "SUFFIX")]
wrap_non_extern_fns_suffix: Option<String>,
wrap_static_fns_suffix: Option<String>,
/// Enables experimental features.
#[arg(long)]
experimental: bool,
Expand Down Expand Up @@ -487,9 +487,9 @@ where
with_derive_custom_struct,
with_derive_custom_enum,
with_derive_custom_union,
wrap_non_extern_fns,
wrap_non_extern_fns_path,
wrap_non_extern_fns_suffix,
wrap_static_fns,
wrap_static_fns_path,
wrap_static_fns_suffix,
experimental: _,
version,
clang_args,
Expand Down Expand Up @@ -996,16 +996,16 @@ where
}
}

if wrap_non_extern_fns {
builder = builder.wrap_non_extern_fns(true);
if wrap_static_fns {
builder = builder.wrap_static_fns(true);
}

if let Some(path) = wrap_non_extern_fns_path {
builder = builder.wrap_non_extern_fns_path(path);
if let Some(path) = wrap_static_fns_path {
builder = builder.wrap_static_fns_path(path);
}

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

Ok((builder, output, verbose))
Expand Down
6 changes: 3 additions & 3 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn setup_extern_test() {
let input_header_dir = PathBuf::from("../bindgen-tests/tests/headers/")
.canonicalize()
.expect("Cannot canonicalize libdir path");
let input_header_file_path = input_header_dir.join("wrap-non-extern-fns.h");
let input_header_file_path = input_header_dir.join("wrap-static-fns.h");
let input_header_file_path_str = input_header_file_path
.to_str()
.expect("Path could not be converted to a str");
Expand All @@ -223,8 +223,8 @@ fn setup_extern_test() {
let bindings = Builder::default()
.header(input_header_file_path_str)
.parse_callbacks(Box::new(CargoCallbacks))
.wrap_non_extern_fns(true)
.non_extern_fns_directory(out_path.display().to_string())
.wrap_static_fns(true)
.static_fns_directory(out_path.display().to_string())
.generate()
.expect("Unable to generate bindings");

Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/tests/expectations/tests/generated/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated C, C++, Header files

This directory contains files for features where extra files are generated
as a part of the feature. For example, `--wrap-non-extern-fns`.
as a part of the feature. For example, `--wrap-static-fns`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --experimental --wrap-non-extern-fns
// bindgen-flags: --experimental --wrap-static-fns

static inline int foo() {
return 11;
Expand Down
6 changes: 3 additions & 3 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,9 @@ fn test_extern_generated_headers() {
println!("Out path is ::: {}", generated_path.display());

let _bindings = Builder::default()
.header("tests/headers/wrap-non-extern-fns.h")
.wrap_non_extern_fns(true)
.wrap_non_extern_fns_path(generated_path.display().to_string())
.header("tests/headers/wrap-static-fns.h")
.wrap_static_fns(true)
.wrap_static_fns_path(generated_path.display().to_string())
.generate()
.expect("Failed to generate bindings");

Expand Down
12 changes: 4 additions & 8 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4008,7 +4008,7 @@ impl CodeGenerator for Function {
let is_internal = matches!(self.linkage(), Linkage::Internal);

if is_internal {
if ctx.options().wrap_non_extern_fns {
if ctx.options().wrap_static_fns {
match CItem::from_function(self, ctx) {
Ok(c_item) => result.c_items.push(c_item),
Err(err) => warn!("Serialization failed: {:?}", err),
Expand Down Expand Up @@ -4148,12 +4148,8 @@ impl CodeGenerator for Function {
quote! { #[link(wasm_import_module = #name)] }
});

if is_internal &&
ctx.options().wrap_non_extern_fns &&
!has_link_name_attr
{
let name =
canonical_name.clone() + ctx.wrap_non_extern_fns_suffix();
if is_internal && ctx.options().wrap_static_fns && !has_link_name_attr {
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();

attributes.push(attributes::link_name(&name));
}
Expand Down Expand Up @@ -4544,7 +4540,7 @@ pub mod utils {
) -> Result<(), std::io::Error> {
let path = context
.options()
.wrap_non_extern_fns_path
.wrap_static_fns_path
.as_ref()
.map(|path| PathBuf::from(path))
.unwrap_or_else(|| {
Expand Down
2 changes: 1 addition & 1 deletion bindgen/codegen/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl CItem {
" {}{}(",
name,
ctx.options()
.wrap_non_extern_fns_suffix
.wrap_static_fns_suffix
.as_deref()
.unwrap_or(DEFAULT_NON_EXTERN_FNS_SUFFIX)
)?;
Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2793,9 +2793,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}
}

pub(crate) fn wrap_non_extern_fns_suffix(&self) -> &str {
pub(crate) fn wrap_static_fns_suffix(&self) -> &str {
self.options()
.wrap_non_extern_fns_suffix
.wrap_static_fns_suffix
.as_deref()
.unwrap_or(crate::DEFAULT_NON_EXTERN_FNS_SUFFIX)
}
Expand Down
16 changes: 3 additions & 13 deletions bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ pub struct Function {

/// The linkage of the function.
linkage: Linkage,

/// Is this function inlined?.
is_inlined_function: bool,
}

impl Function {
Expand All @@ -108,7 +105,6 @@ impl Function {
comment: Option<String>,
kind: FunctionKind,
linkage: Linkage,
is_inlined_function: bool,
) -> Self {
Function {
name,
Expand All @@ -117,7 +113,6 @@ impl Function {
comment,
kind,
linkage,
is_inlined_function,
}
}

Expand Down Expand Up @@ -150,11 +145,6 @@ impl Function {
pub fn linkage(&self) -> Linkage {
self.linkage
}

/// Is this function inlined?
pub fn is_inlined_function(&self) -> bool {
self.is_inlined_function
}
}

impl DotAttributes for Function {
Expand Down Expand Up @@ -696,7 +686,7 @@ impl ClangSubItemParser for Function {
.map_or(false, |x| x.is_inlined_function())
{
if !context.options().generate_inline_functions &&
!context.options().wrap_non_extern_fns
!context.options().wrap_static_fns
{
return Err(ParseError::Continue);
}
Expand All @@ -705,7 +695,8 @@ impl ClangSubItemParser for Function {
return Err(ParseError::Continue);
}

if context.options().wrap_non_extern_fns &&
// We cannot handle `inline` functions that are not `static`.
if context.options().wrap_static_fns &&
cursor.is_inlined_function() &&
matches!(linkage, Linkage::External)
{
Expand Down Expand Up @@ -752,7 +743,6 @@ impl ClangSubItemParser for Function {
comment,
kind,
linkage,
cursor.is_inlined_function(),
);

Ok(ParseResult::New(function, Some(cursor)))
Expand Down
42 changes: 19 additions & 23 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,17 +658,17 @@ impl Builder {
for callbacks in &self.options.parse_callbacks {
output_vector.extend(callbacks.cli_args());
}
if self.options.wrap_non_extern_fns {
output_vector.push("--wrap-non-extern-fns".into())
if self.options.wrap_static_fns {
output_vector.push("--wrap-static-fns".into())
}

if let Some(ref path) = self.options.wrap_non_extern_fns_path {
output_vector.push("--wrap-non-extern-fns-path".into());
if let Some(ref path) = self.options.wrap_static_fns_path {
output_vector.push("--wrap-static-fns-path".into());
output_vector.push(path.display().to_string());
}

if let Some(ref suffix) = self.options.wrap_non_extern_fns_suffix {
output_vector.push("--wrap-non-extern-fns-suffix".into());
if let Some(ref suffix) = self.options.wrap_static_fns_suffix {
output_vector.push("--wrap-static-fns-suffix".into());
output_vector.push(suffix.clone());
}

Expand Down Expand Up @@ -1810,30 +1810,26 @@ impl Builder {
#[cfg(feature = "experimental")]
/// Whether to generate extern wrappers for `static` and `static inline` functions. Defaults to
/// false.
pub fn wrap_non_extern_fns(mut self, doit: bool) -> Self {
self.options.wrap_non_extern_fns = doit;
pub fn wrap_static_fns(mut self, doit: bool) -> Self {
self.options.wrap_static_fns = doit;
self
}

#[cfg(feature = "experimental")]
/// Set the path for the source code file that would be created if any wrapper functions must
/// be generated due to the presence of non-extern functions.
/// be generated due to the presence of static functions.
///
/// Bindgen will automatically add the right extension to the header and source code files.
pub fn wrap_non_extern_fns_path<T: AsRef<Path>>(mut self, path: T) -> Self {
self.options.wrap_non_extern_fns_path = Some(path.as_ref().to_owned());
pub fn wrap_static_fns_path<T: AsRef<Path>>(mut self, path: T) -> Self {
self.options.wrap_static_fns_path = Some(path.as_ref().to_owned());
self
}

#[cfg(feature = "experimental")]
/// Set the suffix added to the extern wrapper functions generated for `static` and `static
/// inline` functions.
pub fn wrap_non_extern_fns_suffix<T: AsRef<str>>(
mut self,
suffix: T,
) -> Self {
self.options.wrap_non_extern_fns_suffix =
Some(suffix.as_ref().to_owned());
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());
self
}
}
Expand Down Expand Up @@ -2177,11 +2173,11 @@ struct BindgenOptions {
/// Whether to wrap unsafe operations in unsafe blocks or not.
wrap_unsafe_ops: bool,

wrap_non_extern_fns: bool,
wrap_static_fns: bool,

wrap_non_extern_fns_suffix: Option<String>,
wrap_static_fns_suffix: Option<String>,

wrap_non_extern_fns_path: Option<PathBuf>,
wrap_static_fns_path: Option<PathBuf>,
}

impl BindgenOptions {
Expand Down Expand Up @@ -2374,9 +2370,9 @@ impl Default for BindgenOptions {
merge_extern_blocks,
abi_overrides,
wrap_unsafe_ops,
wrap_non_extern_fns,
wrap_non_extern_fns_suffix,
wrap_non_extern_fns_path,
wrap_static_fns,
wrap_static_fns_suffix,
wrap_static_fns_path,
}
}
}
Expand Down