Skip to content

Commit

Permalink
Support macros and functions with the same name.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Mar 14, 2023
1 parent b6a242e commit a978cc7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
44 changes: 21 additions & 23 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ default-members = [
"bindgen-cli",
"bindgen-tests",
]

# [patch.crates-io]
# cmacro = { path = "../cmacro-rs" }
8 changes: 8 additions & 0 deletions bindgen-tests/tests/expectations/tests/macro_func_wrapper.rs

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

1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/macro_func_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ void func(int, int);

#define Y 7
#define wrapper_func(x) func(x, Y)
#define func(x) func(x, Y)
5 changes: 1 addition & 4 deletions bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
4 changes: 2 additions & 2 deletions bindgen/codegen/macro_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ struct CodegenResult<'a> {
/// Being these two different declarations.
functions_seen: HashSet<String>,
vars_seen: HashSet<String>,
fn_macros_seen: HashSet<String>,

/// Used for making bindings to overloaded functions. Maps from a canonical
/// function name to the number of overloads we have already codegen'd for
Expand All @@ -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(),
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a978cc7

Please sign in to comment.