Skip to content

Commit 875348d

Browse files
committed
fix: only apply attributes relevant to compilation via type aliases
1 parent 9036b10 commit 875348d

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

bindgen/codegen/mod.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use crate::ir::var::Var;
5757

5858
use proc_macro2::{Ident, Span, TokenStream};
5959
use quote::{ToTokens, TokenStreamExt};
60+
use syn::{parse_quote, Attribute};
6061

6162
use crate::{Entry, HashMap, HashSet};
6263
use std::borrow::Cow;
@@ -1066,28 +1067,46 @@ impl CodeGenerator for Type {
10661067
if let Some(inner_attrs) =
10671068
result.get_attributes(inner_item.id())
10681069
{
1069-
attrs.extend(inner_attrs.iter().cloned());
1070+
// Only apply attributes through type aliases when they are relevant to compilation
1071+
attrs.extend(
1072+
parse_tokens(inner_attrs)
1073+
.into_iter()
1074+
.map(|attr| parse_quote!{ #attr })
1075+
.filter_map(|attr: Attribute|{
1076+
if attr.path().is_ident("cfg") || attr.path().is_ident("link"){
1077+
Some(attr.to_token_stream().to_string())
1078+
} else {
1079+
None
1080+
}
1081+
})
1082+
);
10701083
}
10711084

1085+
// Apply attributes through annotations directly
10721086
attrs.extend(item.annotations().attributes().iter().cloned());
10731087

1088+
// Apply comments to attributes
10741089
if let Some(comment) = item.comment(ctx) {
10751090
attrs.insert(attributes::doc(comment).to_string());
10761091
}
10771092

1093+
// Allow the callbacks to process our attributes
10781094
ctx.options().for_each_callback_mut(|cb| {
10791095
cb.process_attributes(
10801096
&AttributeInfo {
10811097
name: &name,
1082-
// TODO: Perhaps add a TypeAlias variant and provide the alias_style / AliasVariation here as well as the affected item
1098+
// FIXME: Introduce a TypeAlias variant with extra information similar
1099+
// to DiscoveredItem::Alias, indicating this is merely an alias
1100+
// and not a new type definition.
10831101
kind: AttributeItemKind::Struct,
10841102
},
10851103
&mut attrs,
10861104
);
10871105
});
1106+
1107+
// Store the final attributes of this item
10881108
result.set_attributes(item.id(), attrs.clone());
10891109

1090-
// TODO: Only apply attributes relevant to conditional compilation
10911110
let attrs = parse_tokens(attrs);
10921111
let mut tokens = quote! {
10931112
#( #attrs )*

bindgen/codegen/postprocessing/merge_cfg_attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl AttributeSet {
105105
"_".to_string(),
106106
)
107107
.collect::<String>()
108-
.replace(|c| !c.is_alphanumeric(), "_")
108+
.replace(|c: char| !c.is_alphanumeric(), "_")
109109
.chars()
110110
.coalesce(|a, b| {
111111
if a == '_' && b == '_' {

0 commit comments

Comments
 (0)