Repository: rust-lang/rust-bindgen
Pull request #2298 (2022-10-06 16:37:48)

Update `CHANGELOG.md`
Just that


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6c2e64e6..d83e33d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -143,11 +143,38 @@
 
 ## Added
 
- * new feature: `--sort-semantically` flag to sort the output in a predefined manner [(#1743)]
+ * new feature: `--sort-semantically` flag to sort the output in a predefined
+   manner [(#1743)].
+ * new feature: `Bindgen::emit_warnings` method to emit warnings to stderr in
+   build scripts.
+ * new feature: `--newtype-global-enum` flag to generate enum variants as
+   global constants.
+ * new feature: `--default-non-copy-union-style` flag to set the default style
+   of code used to generate unions with non-`Copy` members.
+ * new feature: `--bindgen-wrapper-union` flag to mark any union that matches a
+   regex and has a non-Copy member to use a bindgen-generated wrapper for its
+   fields.
+ * new feature: `--manually-drop-union` flag to mark any union that matches a
+   regex and has a non-`Copy` member to use `ManuallyDrop`.
+ * new feature: `--merge-extern-blocks` flag to merge several `extern` blocks
+   that have the same ABI.
+ * new feature: `--no-size_t-is-usize` flag to not bind `size_t` as `usize`.
 
 ## Changed
 
- * clap has been updated, new msrv is 1.57.
+ * clap and regex have been updated, new msrv is 1.57.
+ * The `--enable-function-attribute-detection` flag is also used to detect
+   diverging functions so the generated bindings use `!` as the return type.
+ * The `--size_t-is-usize` flag is enabled by default.
+ * Unused type aliases for `<stdint.h>` types are no longer emitted.
+ * The `blocklist` options now can be used to block objective-C methods.
+ * The `core::ffi` module is used the sized raw integer types
+   instead of `std::os::raw` if the Rust target version is `1.64` or higher and
+   the `--use-core` flag is enabled.
+ * The `bindgen` CLI utility must be installed using `cargo install
+   bindgen-cli` now.
+ * Using `bindgen` as a library no longer pulls clap and any other CLI
+   related dependencies.
 
 ## Removed
 
diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs
index 9b345449..d0eb4667 100644
--- a/bindgen/callbacks.rs
+++ b/bindgen/callbacks.rs
@@ -31,6 +31,12 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
         MacroParsingBehavior::Default
     }
 
+    /// This function will run for every function. The returned value determines the name visible
+    /// in the bindings.
+    fn generated_name_override(&self, _function_name: &str) -> Option<String> {
+        None
+    }
+
     /// The integer kind an integer macro should have, given a name and the
     /// value of that macro, or `None` if you want the default to be chosen.
     fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
diff --git a/bindgen/ir/function.rs b/bindgen/ir/function.rs
index 928b5aad..c160ed81 100644
--- a/bindgen/ir/function.rs
+++ b/bindgen/ir/function.rs
@@ -664,6 +664,12 @@ impl ClangSubItemParser for Function {
             // but seems easy enough to handle it here.
             name.push_str("_destructor");
         }
+        if let Some(callbacks) = context.parse_callbacks() {
+            if let Some(nm) = callbacks.generated_name_override(&name) {
+                name = nm;
+            }
+        }
+        assert!(!name.is_empty(), "Empty function name.");
 
         let mangled_name = cursor_mangling(context, &cursor);
         let comment = cursor.raw_comment();


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/issue-1375-prefixed-functions.rs b/bindgen-tests/tests/expectations/tests/issue-1375-prefixed-functions.rs
new file mode 100644
index 00000000..835b7579
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/issue-1375-prefixed-functions.rs
@@ -0,0 +1,11 @@
+#![allow(
+    dead_code,
+    non_snake_case,
+    non_camel_case_types,
+    non_upper_case_globals
+)]
+
+extern "C" {
+    #[link_name = "\u{1}my_custom_prefix_function_name"]
+    pub fn function_name(x: ::std::os::raw::c_int);
+}
diff --git a/bindgen-tests/tests/headers/issue-1375-prefixed-functions.h b/bindgen-tests/tests/headers/issue-1375-prefixed-functions.h
new file mode 100644
index 00000000..4264e52d
--- /dev/null
+++ b/bindgen-tests/tests/headers/issue-1375-prefixed-functions.h
@@ -0,0 +1,4 @@
+// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_
+
+void my_custom_prefix_function_name(const int x);
+
diff --git a/bindgen-tests/tests/parse_callbacks/mod.rs b/bindgen-tests/tests/parse_callbacks/mod.rs
index b94b54de..6ade71c2 100644
--- a/bindgen-tests/tests/parse_callbacks/mod.rs
+++ b/bindgen-tests/tests/parse_callbacks/mod.rs
@@ -1,5 +1,29 @@
 use bindgen::callbacks::*;
 
+#[derive(Debug)]
+pub struct RemoveFunctionPrefixParseCallback {
+    pub remove_function_prefix: Option<String>,
+}
+
+impl RemoveFunctionPrefixParseCallback {
+    pub fn new(prefix: &str) -> Self {
+        RemoveFunctionPrefixParseCallback {
+            remove_function_prefix: Some(prefix.to_string()),
+        }
+    }
+}
+
+impl ParseCallbacks for RemoveFunctionPrefixParseCallback {
+    fn generated_name_override(&self, function_name: &str) -> Option<String> {
+        if let Some(prefix) = &self.remove_function_prefix {
+            if let Some(name) = function_name.strip_prefix(prefix) {
+                return Some(name.to_string());
+            }
+        }
+        None
+    }
+}
+
 #[derive(Debug)]
 struct EnumVariantRename;
 
@@ -37,6 +61,18 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
         "blocklisted-type-implements-trait" => {
             Box::new(BlocklistedTypeImplementsTrait)
         }
-        _ => panic!("Couldn't find name ParseCallbacks: {}", cb),
+        call_back => {
+            if call_back.starts_with("remove-function-prefix-") {
+                let prefix = call_back
+                    .split("remove-function-prefix-")
+                    .last()
+                    .to_owned();
+                let lnopc =
+                    RemoveFunctionPrefixParseCallback::new(prefix.unwrap());
+                Box::new(lnopc)
+            } else {
+                panic!("Couldn't find name ParseCallbacks: {}", cb)
+            }
+        }
     }
 }