Skip to content

Commit

Permalink
Handle new unexpected_cfgs warnings (#3022)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored May 6, 2024
1 parent 1256fbe commit 991d87c
Show file tree
Hide file tree
Showing 27 changed files with 77 additions and 5,673 deletions.
14 changes: 13 additions & 1 deletion crates/libs/bindgen/src/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Cfg {
pub types: std::collections::BTreeMap<&'static str, std::collections::BTreeSet<metadata::TypeDef>>,
pub core_types: std::collections::BTreeSet<metadata::Type>,
pub arches: std::collections::BTreeSet<&'static str>,
pub deprecated: bool,
}

impl Cfg {
Expand All @@ -19,6 +20,17 @@ impl Cfg {
union.arches.append(&mut other.arches);
union
}

pub fn included(&self, writer: &Writer) -> bool {
if writer.package {
for namespace in self.types.keys() {
if !writer.reader.includes_namespace(namespace) {
return false;
}
}
}
true
}
}

pub fn field_cfg(writer: &Writer, row: metadata::Field) -> Cfg {
Expand Down Expand Up @@ -134,7 +146,7 @@ fn cfg_add_attributes<R: AsRow + Into<metadata::HasAttribute>>(cfg: &mut Cfg, ro
}
}
"DeprecatedAttribute" => {
cfg.add_feature("deprecated");
cfg.deprecated = true;
}
_ => {}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/rust/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface
let where_clause = writer.where_clause(&signature.params);
let mut cfg = cfg::signature_cfg(writer, method);
cfg.add_feature(def.namespace());

if !cfg.included(writer) {
return quote! {};
}

let features = writer.cfg_features(&cfg);

if kind == metadata::InterfaceKind::None {
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/rust/delegates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ fn gen_callback(writer: &Writer, def: metadata::TypeDef) -> TokenStream {

let return_type = writer.return_sig(&signature);
let cfg = cfg::type_def_cfg(writer, def, &[]);

if !cfg.included(writer) {
return quote! {};
}

let features = writer.cfg_features(&cfg);

let params = signature.params.iter().map(|p| {
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub fn writer(writer: &Writer, namespace: &str, def: metadata::MethodDef) -> Tok
fn gen_sys_function(writer: &Writer, namespace: &str, def: metadata::MethodDef) -> TokenStream {
let signature = metadata::method_def_signature(namespace, def, &[]);
let cfg = cfg::signature_cfg(writer, def);

if !cfg.included(writer) {
return quote! {};
}

let mut tokens = writer.cfg_features(&cfg);
tokens.combine(&gen_link(writer, namespace, &signature));
tokens
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/rust/implements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
let generic_names = writer.generic_names(generics);
let named_phantoms = writer.generic_named_phantoms(generics);
let cfg = cfg::type_def_cfg_impl(writer, def, generics);

if !cfg.included(writer) {
return quote! {};
}

let features = writer.cfg_features(&cfg);
let mut requires = quote! {};
let type_ident = quote! { #type_ident<#generic_names> };
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/bindgen/src/rust/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ fn gen_struct_with_name(writer: &Writer, def: metadata::TypeDef, struct_name: &s
let flags = def.flags();
let cfg = cfg.union(cfg::type_def_cfg(writer, def, &[]));

if !cfg.included(writer) {
return quote! {};
}

let repr = if let Some(layout) = def.class_layout() {
let packing = Literal::usize_unsuffixed(layout.packing_size());
quote! { #[repr(C, packed(#packing))] }
Expand Down
33 changes: 25 additions & 8 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ impl Writer {
compact.push(feature);
}
}

if cfg.deprecated {
compact.push("deprecated");
}
}
compact
}
Expand Down Expand Up @@ -795,18 +799,25 @@ impl Writer {
let mut cfg = cfg::signature_cfg(self, method);
let signature = self.vtbl_signature(def, false, &signature);
cfg.add_feature(def.namespace());
let cfg_all = self.cfg_features(&cfg);
let cfg_not = self.cfg_not_features(&cfg);

let signature = quote! { pub #name: unsafe extern "system" fn #signature, };
if cfg.included(self) {
let cfg_all = self.cfg_features(&cfg);
let cfg_not = self.cfg_not_features(&cfg);

let signature = quote! { pub #name: unsafe extern "system" fn #signature, };

if cfg_all.is_empty() {
methods.combine(&signature);
if cfg_all.is_empty() {
methods.combine(&signature);
} else {
methods.combine(&quote! {
#cfg_all
#signature
#cfg_not
#name: usize,
});
}
} else {
methods.combine(&quote! {
#cfg_all
#signature
#cfg_not
#name: usize,
});
}
Expand Down Expand Up @@ -1209,6 +1220,7 @@ fn const_ptrs(pointers: usize) -> TokenStream {

pub fn cfg_features(cfg: &cfg::Cfg) -> Vec<String> {
let mut compact = Vec::<&'static str>::new();

for feature in cfg.types.keys() {
if !feature.is_empty() {
for pos in 0..compact.len() {
Expand All @@ -1220,6 +1232,11 @@ pub fn cfg_features(cfg: &cfg::Cfg) -> Vec<String> {
compact.push(feature);
}
}

if cfg.deprecated {
compact.push("deprecated");
}

compact.into_iter().map(to_feature).collect()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs
*/

#![doc(html_no_source)]
#![allow(non_snake_case)]
#![allow(non_snake_case, unexpected_cfgs)]
#![cfg_attr(windows_debugger_visualizer, debugger_visualizer(natvis_file = "../.natvis"))]

extern crate self as windows_core;
Expand Down
1 change: 1 addition & 0 deletions crates/libs/result/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
*/

#![allow(unexpected_cfgs)]
#![cfg_attr(
windows_debugger_visualizer,
debugger_visualizer(natvis_file = "../.natvis")
Expand Down
Loading

0 comments on commit 991d87c

Please sign in to comment.