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 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
256ea2e
Change `Callbacks` API.
reitermarkus Feb 21, 2024
402ebea
Gather all includes, even ones without a source file.
reitermarkus Jun 16, 2023
7298d44
Fix test on macOS.
reitermarkus Jun 16, 2023
e411d78
Add test for nested includes.
reitermarkus Jun 17, 2023
ff84f48
Handle all cases in `cmp_by_source_order`.
reitermarkus Jun 17, 2023
610c6f6
Add source order test for headers that are siblings.
reitermarkus Jun 19, 2023
4ef7437
Make `SourceLocation` an `enum`.
reitermarkus Jun 20, 2023
d4cba9c
Don't count include locations of main header file.
reitermarkus Jun 21, 2023
5991e2c
Don't eagerly absolutize source file paths.
reitermarkus Feb 20, 2024
6731cd7
Use name instead of absolute path for checking allow/blocklist.
reitermarkus Feb 20, 2024
9589d54
Use name instead of absolute path for diagnostics.
reitermarkus Feb 20, 2024
b31453b
Support complex macros.
reitermarkus Sep 8, 2022
4cf5064
Remove `as` casts.
reitermarkus Jun 16, 2023
e15e9f0
Resolve struct field types in macros.
reitermarkus Jun 18, 2023
61cb3b6
`semver` doesn't accept partial versions.
reitermarkus Oct 6, 2023
af8fba3
Fix tests.
reitermarkus Oct 6, 2023
b4b0c94
Fix output formatting.
reitermarkus Nov 21, 2023
40f8b48
Define macros added via command-line arguments.
reitermarkus Nov 21, 2023
c58a747
Remove whitespace.
reitermarkus Feb 21, 2024
e8afaec
Ignore blocklisted enum variants.
reitermarkus Feb 21, 2024
4cc61e4
Support enum types in macros.
reitermarkus Feb 21, 2024
de07585
Fix variable name.
reitermarkus Feb 21, 2024
bcaae59
Fix tests.
reitermarkus May 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
## Changed
- Remove which and lazy-static dependencies (#2809, #2817).
- Generate compile-time layout tests (#2787).
- `ParseCallbacks::int_macro` now takes an `i128` instead of an `i64`.
- `ParseCallbacks::func_macro` was renamed to `ParseCallbacks::fn_macro` and now takes a single `FnMacroInfo` argument.
## Removed
## Fixed
- Fix `--formatter=prettyplease` not working in `bindgen-cli` by adding `prettyplease` feature and
Expand Down
30 changes: 20 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,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
Original file line number Diff line number Diff line change
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 @@ -501,6 +504,7 @@ where
objc_extern_crate,
generate_block,
generate_cstr,
generate_fn_macros,
block_extern_crate,
distrust_clang_mangling,
builtins,
Expand Down Expand Up @@ -850,6 +854,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
48 changes: 25 additions & 23 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate bindgen;

use bindgen::callbacks::{
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
DeriveInfo, FnMacroInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
};
use bindgen::{Builder, EnumVariation, Formatter};
use std::collections::HashSet;
Expand All @@ -27,7 +27,7 @@ impl ParseCallbacks for MacroCallback {
MacroParsingBehavior::Default
}

fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
fn int_macro(&self, name: &str, _value: i128) -> Option<IntKind> {
match name {
"TESTMACRO_CUSTOMINTKIND_PATH" => Some(IntKind::Custom {
name: "crate::MacroInteger",
Expand Down Expand Up @@ -59,43 +59,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", r#""\xFF\xFF""#, ")"]);
*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 @@ -145,7 +147,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-integration/cpp/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
a
//#define TESTMACRO_INVALID("string") // A conforming preprocessor rejects this
#define TESTMACRO_STRING_EXPR ("string")
#define TESTMACRO_STRING_FUNC_NON_UTF8(x) (x "ÿÿ") /* invalid UTF-8 on purpose */
#define TESTMACRO_STRING_FUNC_NON_UTF8(x) (x "\xFF\xFF") /* invalid UTF-8 on purpose */

enum {
MY_ANNOYING_MACRO =
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.

Loading
Loading