From b842b23f879bd8137029b2aeb5743516fb6b8c0c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 21:40:24 -0700 Subject: [PATCH 1/3] Strip a function prefix strip_function_prefix drops a prefix from top-level function names, the way strip_type_prefix does for types, refusing a rename onto an existing function or a C++ keyword. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 +++ README.md | 2 +- .../src/bindings/cpp/gen_cpp/filters/mod.rs | 2 +- bindgen/src/bindings/cpp/gen_cpp/mod.rs | 6 +++ bindgen/src/bindings/cpp/mod.rs | 53 ++++++++++++++++--- cpp-tests/CMakeLists.txt | 19 +++++++ cpp-tests/function-collision.toml | 2 + cpp-tests/function-keyword.toml | 2 + cpp-tests/renamed.toml | 1 + cpp-tests/tests/expected/main.cpp | 9 ++++ docs/CONFIGURATION.md | 9 ++++ 11 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 cpp-tests/function-collision.toml create mode 100644 cpp-tests/function-keyword.toml diff --git a/CHANGELOG.md b/CHANGELOG.md index 230c45f..9d5afd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +#### Unreleased + +---- +- Core: Add the `strip_function_prefix` option, which drops a prefix from top-level function names + (`foo_connect` becomes `connect`) + #### v0.11.0-kixelated.5+v0.32.2 ---- diff --git a/README.md b/README.md index f3069f7..2ef4bbf 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cargo install uniffi-bindgen-cpp --git https://github.com/NordSecurity/uniffi-bi ``` This fork carries LiveKit's async support (`livekit/uniffi-0.31-async`) ported to UniFFI 0.32, plus -the `error_style = "expected"` and `strip_type_prefix` options. Its tags add a `-kixelated.N` pre-release so they never +the `error_style = "expected"`, `strip_type_prefix`, and `strip_function_prefix` options. Its tags add a `-kixelated.N` pre-release so they never collide with upstream's: ```bash diff --git a/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs b/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs index 733e99a..d3932e5 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs @@ -18,7 +18,7 @@ type Result = std::result::Result; // Keywords and other commonly used words that are in fact macros that // expand to expressions (like `errno`). -const RESERVED_CPP_WORDS: [&str; 100] = [ +pub(crate) const RESERVED_CPP_WORDS: [&str; 100] = [ // Keywords "alignas", "alignof", diff --git a/bindgen/src/bindings/cpp/gen_cpp/mod.rs b/bindgen/src/bindings/cpp/gen_cpp/mod.rs index 1ddcb47..bca91fc 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/mod.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/mod.rs @@ -100,6 +100,8 @@ pub(crate) struct Config { error_style: ErrorStyle, #[serde(default)] strip_type_prefix: Option, + #[serde(default)] + strip_function_prefix: Option, /// Every type renamed by `strip_type_prefix`, across all components, old name to new. #[serde(skip)] pub(crate) renames: HashMap, @@ -125,6 +127,10 @@ impl Config { pub(crate) fn strip_type_prefix(&self) -> Option<&str> { self.strip_type_prefix.as_deref() } + + pub(crate) fn strip_function_prefix(&self) -> Option<&str> { + self.strip_function_prefix.as_deref() + } } #[derive(Clone, Deserialize, Serialize, Debug, Default)] diff --git a/bindgen/src/bindings/cpp/mod.rs b/bindgen/src/bindings/cpp/mod.rs index 603e3a1..26bc204 100644 --- a/bindgen/src/bindings/cpp/mod.rs +++ b/bindgen/src/bindings/cpp/mod.rs @@ -13,7 +13,7 @@ use uniffi_bindgen::{ BindingGenerator, Component, ComponentInterface, GenerationSettings, }; -use self::gen_cpp::{generate_cpp_bindings, Bindings}; +use self::gen_cpp::{filters::RESERVED_CPP_WORDS, generate_cpp_bindings, Bindings}; pub(crate) struct CppBindingGenerator { pub scaffolding_mode: bool, @@ -71,23 +71,33 @@ impl BindingGenerator for CppBindingGenerator { ) -> Result<()> { // Keyed by crate, so a component that imports a renamed type sees the new name too. let mut renames = HashMap::new(); + // Types only: the type oracle renames each type it renders, and functions are not types. + let mut type_renames = HashMap::new(); for Component { ci, config, .. } in components.iter() { + let mut table = toml::Table::new(); if let Some(prefix) = config.strip_type_prefix() { - renames.insert(ci.crate_name().to_string(), strip_type_prefix(ci, prefix)?); + let types = strip_type_prefix(ci, prefix)?; + type_renames.extend( + types + .iter() + .filter_map(|(old, new)| Some((old.clone(), new.as_str()?.to_string()))), + ); + table.extend(types); + } + if let Some(prefix) = config.strip_function_prefix() { + table.extend(strip_function_prefix(ci, prefix)?); + } + if !table.is_empty() { + renames.insert(ci.crate_name().to_string(), table); } } if renames.is_empty() { return Ok(()); } - let flat: HashMap = renames - .values() - .flatten() - .filter_map(|(old, new)| Some((old.clone(), new.as_str()?.to_string()))) - .collect(); for component in components.iter_mut() { rename(&mut component.ci, &renames); - component.config.renames = flat.clone(); + component.config.renames = type_renames.clone(); } Ok(()) } @@ -138,6 +148,33 @@ fn strip_type_prefix(ci: &ComponentInterface, prefix: &str) -> Result` to ``, where `` starts with a +/// lowercase letter. Methods keep their names, since their class already scopes them. Refuses a +/// rename onto a name that is already a function or a C++ keyword. +fn strip_function_prefix(ci: &ComponentInterface, prefix: &str) -> Result { + let names: BTreeSet<&str> = ci.function_definitions().iter().map(|f| f.name()).collect(); + + let mut renames = toml::Table::new(); + for name in &names { + let Some(stripped) = name.strip_prefix(prefix) else { + continue; + }; + if !stripped.starts_with(|c: char| c.is_ascii_lowercase()) { + continue; + } + anyhow::ensure!( + !names.contains(stripped), + "strip_function_prefix = \"{prefix}\" renames `{name}` to `{stripped}`, which is already a function" + ); + anyhow::ensure!( + !RESERVED_CPP_WORDS.contains(&stripped), + "strip_function_prefix = \"{prefix}\" renames `{name}` to `{stripped}`, which is reserved in C++" + ); + renames.insert(name.to_string(), stripped.into()); + } + Ok(renames) +} + /// Writes `.hpp`, `.cpp`, and `_scaffolding.hpp`, plus /// `uniffi_expected.hpp` under the expected error style. pub(crate) fn write_cpp_bindings( diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index 436ac61..ef040f2 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -165,6 +165,25 @@ add_test(NAME strip-type-prefix-collision-test set_tests_properties(strip-type-prefix-collision-test PROPERTIES PASS_REGULAR_EXPRESSION "renames `Flat[A-Za-z]+` to `[A-Za-z]+`, which is already a type") +# `strip_function_prefix` refuses to rename a function onto one that already exists, or onto a +# C++ keyword. +add_test(NAME strip-function-prefix-collision-test + COMMAND ${BINDINGS_BUILD_DIR}/uniffi-bindgen-cpp${CMAKE_EXECUTABLE_SUFFIX} + --library ${UNIFFI_FIXTURES_LIB} --crate uniffi_fixture_async_calls + --config ${CMAKE_CURRENT_SOURCE_DIR}/function-collision.toml + --out-dir ${CMAKE_CURRENT_BINARY_DIR}/function-collision + WORKING_DIRECTORY ${BINDINGS_BUILD_DIR}) +set_tests_properties(strip-function-prefix-collision-test PROPERTIES + PASS_REGULAR_EXPRESSION "renames `async_new_megaphone` to `new_megaphone`, which is already a function") +add_test(NAME strip-function-prefix-keyword-test + COMMAND ${BINDINGS_BUILD_DIR}/uniffi-bindgen-cpp${CMAKE_EXECUTABLE_SUFFIX} + --library ${UNIFFI_FIXTURES_LIB} --crate uniffi_cpp_error_types_builtin + --config ${CMAKE_CURRENT_SOURCE_DIR}/function-keyword.toml + --out-dir ${CMAKE_CURRENT_BINARY_DIR}/function-keyword + WORKING_DIRECTORY ${BINDINGS_BUILD_DIR}) +set_tests_properties(strip-function-prefix-keyword-test PROPERTIES + PASS_REGULAR_EXPRESSION "renames `oops_enum` to `enum`, which is reserved in C\\+\\+") + # The C backend: a C11 header over expected-style C++ bindings, built without exceptions. set(C_BINDINGS_DIR ${BINDINGS_BUILD_DIR}/bindings-c) set(C_BINDING_FILES ${C_BINDINGS_DIR}/capi.cpp ${C_BINDINGS_DIR}/capi_c.cpp) diff --git a/cpp-tests/function-collision.toml b/cpp-tests/function-collision.toml new file mode 100644 index 0000000..321de36 --- /dev/null +++ b/cpp-tests/function-collision.toml @@ -0,0 +1,2 @@ +[bindings.cpp] +strip_function_prefix = "async_" diff --git a/cpp-tests/function-keyword.toml b/cpp-tests/function-keyword.toml new file mode 100644 index 0000000..f8e2725 --- /dev/null +++ b/cpp-tests/function-keyword.toml @@ -0,0 +1,2 @@ +[bindings.cpp] +strip_function_prefix = "oops_" diff --git a/cpp-tests/renamed.toml b/cpp-tests/renamed.toml index 8caf688..1afcb67 100644 --- a/cpp-tests/renamed.toml +++ b/cpp-tests/renamed.toml @@ -1,3 +1,4 @@ [bindings.cpp] error_style = "expected" strip_type_prefix = "Capi" +strip_function_prefix = "capi_" diff --git a/cpp-tests/tests/expected/main.cpp b/cpp-tests/tests/expected/main.cpp index f8c12fa..fa30d73 100644 --- a/cpp-tests/tests/expected/main.cpp +++ b/cpp-tests/tests/expected/main.cpp @@ -105,6 +105,14 @@ void renamed_types() { CHECK(empty.error().to_string() == "invalid: empty name"); } +void renamed_functions() { + // `strip_function_prefix = "capi_"` names each free function without the prefix, async ones too. + CHECK(capi::checksum({1, 2, 3}) == 6); + CHECK(capi::fail(0)); + CHECK(!capi::fail(1)); + CHECK(capi::sum({1, 2, 3}).get() == 6); +} + void async_results() { CHECK(futures::greet("C++") == "Hello, C++"); CHECK(futures::always_ready().get()); @@ -226,6 +234,7 @@ int main(int argc, char **argv) { sync_errors(); error_display(); renamed_types(); + renamed_functions(); async_results(); continuations(); cancellation(); diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index c4dd9ff..d1a6292 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -52,6 +52,15 @@ NOTE: the `enum_style` option is separate for bindings and scaffolding generator strip_type_prefix = "Foo" ``` +- `strip_function_prefix` - the same for top-level functions. With `strip_function_prefix = "foo_"`, + `foo_connect` becomes `connect`. A name continues only after a lowercase letter, and methods + keep their names. Generation fails if a stripped name is already a function or a C++ keyword. + + ```toml + [bindings.cpp] + strip_function_prefix = "foo_" + ``` + ### `error_style = "expected"` A fallible function returns `uniffi::expected`, and an async one returns From 353597a6d4cb647aa5f1a9aaf5d43078704c23c3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 21:40:54 -0700 Subject: [PATCH 2/3] Release v0.11.0-kixelated.6+v0.32.2 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- Cargo.lock | 2 +- README.md | 2 +- bindgen/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d5afd5..aa2b630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -#### Unreleased +#### v0.11.0-kixelated.6+v0.32.2 ---- - Core: Add the `strip_function_prefix` option, which drops a prefix from top-level function names diff --git a/Cargo.lock b/Cargo.lock index b975209..f4ae84a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -874,7 +874,7 @@ dependencies = [ [[package]] name = "uniffi-bindgen-cpp" -version = "0.11.0-kixelated.5+v0.32.2" +version = "0.11.0-kixelated.6+v0.32.2" dependencies = [ "anyhow", "askama 0.13.1", diff --git a/README.md b/README.md index 2ef4bbf..2273924 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ the `error_style = "expected"`, `strip_type_prefix`, and `strip_function_prefix` collide with upstream's: ```bash -cargo install uniffi-bindgen-cpp --locked --git https://github.com/kixelated/uniffi-bindgen-cpp --tag v0.11.0-kixelated.5+v0.32.2 +cargo install uniffi-bindgen-cpp --locked --git https://github.com/kixelated/uniffi-bindgen-cpp --tag v0.11.0-kixelated.6+v0.32.2 ``` # How to generate bindings diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml index aa4af62..4c060cc 100644 --- a/bindgen/Cargo.toml +++ b/bindgen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniffi-bindgen-cpp" -version = "0.11.0-kixelated.5+v0.32.2" +version = "0.11.0-kixelated.6+v0.32.2" edition = "2021" [dependencies] From 3634fc2f8272b476d3e01e57fb020515e08970a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 23 Oct 2026 10:00:56 -0700 Subject: [PATCH 3/3] Test a function collision only one crate has --config applies to every crate in the fixtures library, and their order varies by platform, so async_ hit coverall's async_bool first on Windows. async_maybe_ collides only in async-calls. Co-Authored-By: Claude Opus 5.5 --- cpp-tests/CMakeLists.txt | 5 +++-- cpp-tests/function-collision.toml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index ef040f2..989ba31 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -166,7 +166,8 @@ set_tests_properties(strip-type-prefix-collision-test PROPERTIES PASS_REGULAR_EXPRESSION "renames `Flat[A-Za-z]+` to `[A-Za-z]+`, which is already a type") # `strip_function_prefix` refuses to rename a function onto one that already exists, or onto a -# C++ keyword. +# C++ keyword. `--config` applies to every crate in the library, whose order varies by platform, so +# each prefix renames onto a taken name in exactly one crate. add_test(NAME strip-function-prefix-collision-test COMMAND ${BINDINGS_BUILD_DIR}/uniffi-bindgen-cpp${CMAKE_EXECUTABLE_SUFFIX} --library ${UNIFFI_FIXTURES_LIB} --crate uniffi_fixture_async_calls @@ -174,7 +175,7 @@ add_test(NAME strip-function-prefix-collision-test --out-dir ${CMAKE_CURRENT_BINARY_DIR}/function-collision WORKING_DIRECTORY ${BINDINGS_BUILD_DIR}) set_tests_properties(strip-function-prefix-collision-test PROPERTIES - PASS_REGULAR_EXPRESSION "renames `async_new_megaphone` to `new_megaphone`, which is already a function") + PASS_REGULAR_EXPRESSION "renames `async_maybe_new_megaphone` to `new_megaphone`, which is already a function") add_test(NAME strip-function-prefix-keyword-test COMMAND ${BINDINGS_BUILD_DIR}/uniffi-bindgen-cpp${CMAKE_EXECUTABLE_SUFFIX} --library ${UNIFFI_FIXTURES_LIB} --crate uniffi_cpp_error_types_builtin diff --git a/cpp-tests/function-collision.toml b/cpp-tests/function-collision.toml index 321de36..a54704d 100644 --- a/cpp-tests/function-collision.toml +++ b/cpp-tests/function-collision.toml @@ -1,2 +1,2 @@ [bindings.cpp] -strip_function_prefix = "async_" +strip_function_prefix = "async_maybe_"