Skip to content

Commit

Permalink
Add bindgen config option to disable generating inner attributes (#2683)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaoliello authored Oct 18, 2023
1 parent 4c234b5 commit 462f941
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn from_reader(reader: &metadata::Reader, filter: &metadata::Filter, mut con
writer.sys = writer.std || config.remove("sys").is_some();
writer.implement = config.remove("implement").is_some();
writer.minimal = config.remove("minimal").is_some();
writer.no_inner_attributes = config.remove("no-inner-attributes").is_some();

if writer.package && writer.flatten {
return Err(Error::new("cannot combine `package` and `flatten` configuration values"));
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/try_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn try_format(writer: &super::Writer, tokens: &str) -> String {
};

// Packaging - e.g. windows/windows-sys crates - assumes the crate will allow whatever warnings it deems fit.
let allow = if writer.package { "" } else { "#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]\n" };
let allow = if writer.package || writer.no_inner_attributes { "" } else { "#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]\n" };
let tokens = format!("{preamble}{allow}{tokens}");

let Ok(mut child) = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn() else {
Expand Down
25 changes: 19 additions & 6 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,29 @@ pub struct Writer<'a> {
//
// Maybe this macro is the embedable version of the IDL format?! like a more intelligient
// version of the existing interface macro...
pub std: bool, // tweaks for internal std library support
pub sys: bool, // writer sys-style bindings
pub flatten: bool, // strips out namespaces - implies !package
pub package: bool, // default is single file with no cfg - implies !flatten
pub minimal: bool, // strips out enumerators - in future possibly other helpers as well
pub std: bool, // tweaks for internal std library support
pub sys: bool, // writer sys-style bindings
pub flatten: bool, // strips out namespaces - implies !package
pub package: bool, // default is single file with no cfg - implies !flatten
pub minimal: bool, // strips out enumerators - in future possibly other helpers as well
pub no_inner_attributes: bool, // skips the inner attributes at the start of the file
}

impl<'a> Writer<'a> {
pub fn new(reader: &'a Reader, filter: &'a metadata::Filter, output: &'a str) -> Self {
Self { reader, filter, output, namespace: "", implement: false, std: false, sys: false, flatten: false, package: false, minimal: false }
Self {
reader,
filter,
output,
namespace: "",
implement: false,
std: false,
sys: false,
flatten: false,
package: false,
minimal: false,
no_inner_attributes: false,
}
}

//
Expand Down
15 changes: 15 additions & 0 deletions crates/tests/standalone/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ fn main() {
"Windows.Win32.Security.Cryptography.CMC_ADD_ATTRIBUTES",
],
);

// Ensure that no-inner-attribute works, and the resulting
// file can be `include!` inside a mod{} block.
write_no_inner_attr(
"src/b_include_me.rs",
&["Windows.Win32.System.SystemInformation.GetVersion"],
);
}

fn write_sys(output: &str, filter: &[&str]) {
Expand All @@ -149,6 +156,14 @@ fn write_std(output: &str, filter: &[&str]) {
riddle(output, filter, &["flatten", "std", "minimal"]);
}

fn write_no_inner_attr(output: &str, filter: &[&str]) {
riddle(
output,
filter,
&["flatten", "no-inner-attributes", "minimal"],
);
}

fn riddle(output: &str, filter: &[&str], config: &[&str]) {
std::fs::remove_file(output).expect("Failed to delete output");

Expand Down
7 changes: 7 additions & 0 deletions crates/tests/standalone/src/b_include_me.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Bindings generated by `windows-bindgen` 0.52.0

#[inline]
pub unsafe fn GetVersion() -> u32 {
::windows_targets::link!("kernel32.dll" "system" fn GetVersion() -> u32);
GetVersion()
}
11 changes: 11 additions & 0 deletions crates/tests/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ mod b_test;
mod b_unknown;
mod b_uri;
mod b_win_enumerator;
#[allow(non_snake_case)]
mod included {
include!("b_include_me.rs");
}

#[test]
fn bstr() {
Expand Down Expand Up @@ -166,3 +170,10 @@ fn calendar() -> windows_core::Result<()> {
calendar.SetYear(year)?;
Ok(())
}

#[test]
fn from_included() {
unsafe {
included::GetVersion();
}
}

0 comments on commit 462f941

Please sign in to comment.