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

test: check attribute/derive ordering #3034

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ quickcheck = "1.0"
quote = { version = "1", default-features = false }
regex = { version = "1.5.3", default-features = false }
rustc-hash = "2.1.0"
serde = { version = "1", features = ["derive"] }
shlex = "1"
similar = "2.2.1"
syn = "2.0"
Expand Down
1 change: 1 addition & 0 deletions bindgen-tests/tests/expectations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition.workspace = true
block.workspace = true
libloading.workspace = true
objc.workspace = true
serde.workspace = true

# Both of these sections need to be copied here from the workspace because
# Cargo currently does not allow overriding workspace settings in a member
Expand Down

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

8 changes: 8 additions & 0 deletions bindgen-tests/tests/headers/derive-and-attribute-order.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --no-layout-tests
// bindgen-parse-callbacks: derive-uppercase-serialize=color
// bindgen-skip-formatting
typedef struct {
int red;
int green;
int blue;
} color;
27 changes: 26 additions & 1 deletion bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ impl ParseCallbacks for WrapAsVariadicFn {
}
}

#[derive(Debug)]
struct DeriveTransparentSerialize(String);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to call this DeriveUppercaseSerialize? That would make sense to me ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha yes, I forgot to change that one


impl ParseCallbacks for DeriveTransparentSerialize {
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
if info.name == &self.0 {
vec!["serde::Serialize".to_owned()]
} else {
vec![]
}
}

fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
if info.name == &self.0 {
vec!["#[serde(rename_all = \"UPPERCASE\")]".to_owned()]
} else {
vec![]
}
}
}

pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
"enum-variant-rename" => Box::new(EnumVariantRename),
Expand All @@ -155,7 +176,11 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
"type-visibility" => Box::new(TypeVisibility),
call_back => {
if let Some(prefix) =
if let Some(name) =
call_back.strip_prefix("derive-uppercase-serialize=")
{
Box::new(DeriveTransparentSerialize(name.to_owned()))
} else if let Some(prefix) =
call_back.strip_prefix("remove-function-prefix-")
{
let lnopc = RemovePrefixParseCallback::new(prefix);
Expand Down
26 changes: 19 additions & 7 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,23 @@ fn compare_generated_header(
looked_at,
),
};

let do_formatting = builder.do_formatting;
let (builder, roundtrip_builder) = builder.into_builder(check_roundtrip)?;

// We skip the generate() error here so we get a full diff below
let actual = match builder.generate() {
Ok(bindings) => format_code(bindings.to_string()).map_err(|err| {
Error::new(
ErrorKind::Other,
format!("Cannot parse the generated bindings: {err}"),
)
})?,
Ok(bindings) => {
if do_formatting {
format_code(bindings.to_string()).map_err(|err| {
Error::new(
ErrorKind::Other,
format!("Cannot parse the generated bindings: {err}"),
)
})?
} else {
bindings.to_string()
}
}
Err(_) => "/* error generating bindings */\n".into(),
};

Expand Down Expand Up @@ -237,6 +243,7 @@ fn builder() -> Builder {
struct BuilderState {
builder: Builder,
parse_callbacks: Option<String>,
do_formatting: bool,
}

impl BuilderState {
Expand All @@ -255,6 +262,7 @@ impl BuilderState {
Some(BuilderState {
builder,
parse_callbacks: self.parse_callbacks,
do_formatting: self.do_formatting,
})
} else {
None
Expand All @@ -273,6 +281,7 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
// Scoop up bindgen-flags from test header
let mut flags = Vec::with_capacity(2);
let mut parse_callbacks = None;
let mut do_formatting = true;

for line in reader.lines() {
let line = line?;
Expand All @@ -298,6 +307,8 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
let parse_cb =
line.split("bindgen-parse-callbacks: ").last().unwrap();
parse_callbacks = Some(parse_cb.to_owned());
} else if line.contains("bindgen-skip-formatting") {
do_formatting = false;
}
}

Expand Down Expand Up @@ -345,6 +356,7 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
Ok(BuilderState {
builder,
parse_callbacks,
do_formatting,
})
}

Expand Down
Loading