Skip to content
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

Support complex macros. #2369

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ae4d951
Gather all includes, even ones without a source file.
reitermarkus Jun 16, 2023
449acd4
Fix test on macOS.
reitermarkus Jun 16, 2023
74f75b7
Add test for nested includes.
reitermarkus Jun 17, 2023
309d694
Handle all cases in `cmp_by_source_order`.
reitermarkus Jun 17, 2023
565a98f
Add source order test for headers that are siblings.
reitermarkus Jun 19, 2023
9655451
Make `SourceLocation` an `enum`.
reitermarkus Jun 20, 2023
b64191b
Don't count include locations of main header file.
reitermarkus Jun 21, 2023
03971b4
Fix tests.
reitermarkus Oct 4, 2023
524bb5e
Don't eagerly absolutize source file paths.
reitermarkus Feb 20, 2024
ac3d37e
Use name instead of absolute path for checking allow/blocklist.
reitermarkus Feb 20, 2024
c3536a5
Use name instead of absolute path for diagnostics.
reitermarkus Feb 20, 2024
91b44fd
Support complex macros.
reitermarkus Sep 8, 2022
2ca5ab0
Remove `as` casts.
reitermarkus Jun 16, 2023
0b942a9
Resolve struct field types in macros.
reitermarkus Jun 18, 2023
8288be6
`semver` doesn't accept partial versions.
reitermarkus Oct 6, 2023
d44e1e9
Fix tests.
reitermarkus Oct 6, 2023
e41a43b
Fix output formatting.
reitermarkus Nov 21, 2023
201b576
Define macros added via command-line arguments.
reitermarkus Nov 21, 2023
a747712
Remove whitespace.
reitermarkus Feb 21, 2024
d248c37
Ignore blocklisted enum variants.
reitermarkus Feb 21, 2024
68c3ee7
Support enum types in macros.
reitermarkus Feb 21, 2024
a799531
Fix variable name.
reitermarkus Feb 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 30 additions & 10 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -40,3 +40,7 @@ release = false
[profile.dist]
inherits = "release"
lto = "thin"

[patch.crates-io]
# cmacro = { path = "../cmacro-rs" }
cmacro = { git = "https://github.com/reitermarkus/cmacro-rs" }
8 changes: 8 additions & 0 deletions bindgen-cli/options.rs
Expand Up @@ -210,6 +210,9 @@ struct BindgenCommand {
/// Generate string constants as `&CStr` instead of `&[u8]`.
#[arg(long)]
generate_cstr: bool,
/// Generate code for function-like macros.
#[arg(long, requires = "experimental")]
generate_fn_macros: bool,
/// Use extern crate instead of use for block.
#[arg(long)]
block_extern_crate: bool,
Expand Down Expand Up @@ -492,6 +495,7 @@ where
objc_extern_crate,
generate_block,
generate_cstr,
generate_fn_macros,
block_extern_crate,
distrust_clang_mangling,
builtins,
Expand Down Expand Up @@ -838,6 +842,10 @@ where
builder = builder.generate_cstr(true);
}

if generate_fn_macros {
builder = builder.generate_fn_macros(true);
}

if block_extern_crate {
builder = builder.block_extern_crate(true);
}
Expand Down
46 changes: 24 additions & 22 deletions bindgen-integration/build.rs
Expand Up @@ -2,7 +2,7 @@ extern crate bindgen;
extern crate cc;

use bindgen::callbacks::{
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
DeriveInfo, FnMacroInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
};
use bindgen::{Builder, CargoCallbacks, EnumVariation, Formatter};
use std::collections::HashSet;
Expand Down Expand Up @@ -60,43 +60,45 @@ impl ParseCallbacks for MacroCallback {
}
}

fn func_macro(&self, name: &str, value: &[&[u8]]) {
match name {
fn fn_macro(&self, info: &FnMacroInfo) {
let args = info.args();
let body = info.body();

match info.name() {
"TESTMACRO_NONFUNCTIONAL" => {
panic!("func_macro was called for a non-functional macro");
panic!("fn_macro was called for a non-functional macro");
}
"TESTMACRO_FUNCTIONAL_NONEMPTY(TESTMACRO_INTEGER)" => {
"TESTMACRO_FUNCTIONAL_NONEMPTY" => {
// Spaces are inserted into the right-hand side of a functional
// macro during reconstruction from the tokenization. This might
// change in the future, but it is safe by the definition of a
// token in C, whereas leaving the spaces out could change
// tokenization.
assert_eq!(value, &[b"-" as &[u8], b"TESTMACRO_INTEGER"]);
assert_eq!(args, &["TESTMACRO_INTEGER"]);
assert_eq!(body, &["-", "TESTMACRO_INTEGER"]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_EMPTY(TESTMACRO_INTEGER)" => {
assert_eq!(value, &[] as &[&[u8]]);
"TESTMACRO_FUNCTIONAL_EMPTY" => {
assert_eq!(args, &["TESTMACRO_INTEGER"]);
assert_eq!(body, &[] as &[&str]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_TOKENIZED(a,b,c,d,e)" => {
assert_eq!(
value,
&[b"a" as &[u8], b"/", b"b", b"c", b"d", b"##", b"e"]
);
"TESTMACRO_FUNCTIONAL_TOKENIZED" => {
assert_eq!(args, &["a", "b", "c", "d", "e"]);
assert_eq!(body, &["a", "/", "b", "c", "d", "##", "e"]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_SPLIT(a,b)" => {
assert_eq!(value, &[b"b", b",", b"a"]);
"TESTMACRO_FUNCTIONAL_SPLIT" => {
assert_eq!(args, &["a", "b"]);
assert_eq!(body, &["b", ",", "a"]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_STRING_FUNC_NON_UTF8(x)" => {
assert_eq!(
value,
&[b"(" as &[u8], b"x", b"\"\xff\xff\"", b")"]
);
"TESTMACRO_STRING_FUNC_NON_UTF8" => {
assert_eq!(args, &["x"]);
assert_eq!(body, &["(", "x", "\"ÿÿ\"", ")"]);
*self.seen_funcs.lock().unwrap() += 1;
}
_ => {
name => {
// The system might provide lots of functional macros.
// Ensure we did not miss handling one that we meant to handle.
assert!(!name.starts_with("TESTMACRO_"), "name = {}", name);
Expand Down Expand Up @@ -144,7 +146,7 @@ impl Drop for MacroCallback {
assert_eq!(
*self.seen_funcs.lock().unwrap(),
5,
"func_macro handle was not called once for all relevant macros"
"fn_macro handle was not called once for all relevant macros"
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindgen-tests/tests/expectations/tests/allowlist-file.rs

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

2 changes: 1 addition & 1 deletion bindgen-tests/tests/expectations/tests/allowlist_item.rs

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

4 changes: 2 additions & 2 deletions bindgen-tests/tests/expectations/tests/infinite-macro.rs

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

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

12 changes: 10 additions & 2 deletions bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs

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

12 changes: 10 additions & 2 deletions bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs

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

14 changes: 7 additions & 7 deletions bindgen-tests/tests/expectations/tests/layout_arp.rs

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

14 changes: 7 additions & 7 deletions bindgen-tests/tests/expectations/tests/layout_array.rs

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