From a978cc79120216138e59e5e21391dc76ca3107ca Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 15 Mar 2023 00:47:57 +0100 Subject: [PATCH] Support macros and functions with the same name. --- Cargo.lock | 44 +++++++++---------- Cargo.toml | 3 ++ .../expectations/tests/macro_func_wrapper.rs | 8 ++++ .../tests/headers/macro_func_wrapper.h | 1 + bindgen/Cargo.toml | 5 +-- bindgen/codegen/macro_def.rs | 4 +- bindgen/codegen/mod.rs | 10 +++++ 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfcab415a2..89129e17ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,9 +135,7 @@ dependencies = [ [[package]] name = "cmacro" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3591eea3551b2c8fc9dc565a37e7147872800426da26f5b7d624ab252fbf4dc0" +version = "0.1.3" dependencies = [ "nom", "proc-macro2", @@ -404,9 +402,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" dependencies = [ "unicode-ident", ] @@ -434,9 +432,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.23" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -645,9 +643,9 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -660,42 +658,42 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" [[package]] name = "windows_aarch64_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] name = "windows_i686_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] name = "windows_i686_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] name = "windows_x86_64_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] name = "windows_x86_64_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/Cargo.toml b/Cargo.toml index a9d9d41ee6..ae670fa0ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ default-members = [ "bindgen-cli", "bindgen-tests", ] + +# [patch.crates-io] +# cmacro = { path = "../cmacro-rs" } diff --git a/bindgen-tests/tests/expectations/tests/macro_func_wrapper.rs b/bindgen-tests/tests/expectations/tests/macro_func_wrapper.rs index 0a7cd6693b..ca4fe9227e 100644 --- a/bindgen-tests/tests/expectations/tests/macro_func_wrapper.rs +++ b/bindgen-tests/tests/expectations/tests/macro_func_wrapper.rs @@ -11,6 +11,14 @@ pub const Y: u32 = 7; pub unsafe extern "C" fn wrapper_func(mut x: ::std::os::raw::c_int) { func(x, 7); } +#[doc(hidden)] +#[macro_export] +macro_rules! __cmacro__func { + ($ x : expr) => { + func(($x).into(), 7) + }; +} +pub use __cmacro__func as func; extern "C" { pub fn func(arg1: ::std::os::raw::c_int, arg2: ::std::os::raw::c_int); } diff --git a/bindgen-tests/tests/headers/macro_func_wrapper.h b/bindgen-tests/tests/headers/macro_func_wrapper.h index 46cb9ab41e..775cd8ad85 100644 --- a/bindgen-tests/tests/headers/macro_func_wrapper.h +++ b/bindgen-tests/tests/headers/macro_func_wrapper.h @@ -2,3 +2,4 @@ void func(int, int); #define Y 7 #define wrapper_func(x) func(x, Y) +#define func(x) func(x, Y) diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml index c6cfd07de6..a95ead52c0 100644 --- a/bindgen/Cargo.toml +++ b/bindgen/Cargo.toml @@ -26,7 +26,7 @@ path = "lib.rs" [dependencies] bitflags = "1.0.3" -cmacro = "0.1.2" +cmacro = "0.1.3" clang-sys = { version = "1", features = ["clang_6_0"] } lazycell = "1" lazy_static = "1" @@ -55,6 +55,3 @@ experimental = [] testing_only_extra_assertions = [] testing_only_libclang_9 = [] testing_only_libclang_5 = [] - -# [patch."https://github.com/reitermarkus/cmacro-rs"] -# cmacro = { path = "../cmacro-rs" } diff --git a/bindgen/codegen/macro_def.rs b/bindgen/codegen/macro_def.rs index 51082eea9d..3451fec9e3 100644 --- a/bindgen/codegen/macro_def.rs +++ b/bindgen/codegen/macro_def.rs @@ -62,10 +62,10 @@ impl CodeGenerator for MacroDef { match self { Self::Fn(name) => { - if result.seen_function(&canonical_name) { + if result.seen_fn_macro(&canonical_name) { return; } - result.saw_function(&canonical_name); + result.saw_fn_macro(&canonical_name); let mut fn_macro = ctx.fn_macro(name).unwrap().clone(); let generated_value = match fn_macro.generate(ctx) { diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index c5c26857ba..bd3d4bbd0c 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -262,6 +262,7 @@ struct CodegenResult<'a> { /// Being these two different declarations. functions_seen: HashSet, vars_seen: HashSet, + fn_macros_seen: HashSet, /// Used for making bindings to overloaded functions. Maps from a canonical /// function name to the number of overloads we have already codegen'd for @@ -285,6 +286,7 @@ impl<'a> CodegenResult<'a> { items_seen: Default::default(), functions_seen: Default::default(), vars_seen: Default::default(), + fn_macros_seen: Default::default(), overload_counters: Default::default(), items_to_serialize: Default::default(), } @@ -330,6 +332,14 @@ impl<'a> CodegenResult<'a> { self.functions_seen.insert(name.into()); } + fn seen_fn_macro(&self, name: &str) -> bool { + self.fn_macros_seen.contains(name) + } + + fn saw_fn_macro(&mut self, name: &str) { + self.fn_macros_seen.insert(name.into()); + } + /// Get the overload number for the given function name. Increments the /// counter internally so the next time we ask for the overload for this /// name, we get the incremented value, and so on.