diff options
Diffstat (limited to 'bindgen/codegen')
-rw-r--r-- | bindgen/codegen/helpers.rs | 8 | ||||
-rw-r--r-- | bindgen/codegen/mod.rs | 52 |
2 files changed, 9 insertions, 51 deletions
diff --git a/bindgen/codegen/helpers.rs b/bindgen/codegen/helpers.rs index 5bf36acb..088c7f93 100644 --- a/bindgen/codegen/helpers.rs +++ b/bindgen/codegen/helpers.rs @@ -55,9 +55,11 @@ pub mod attributes { } pub fn doc(comment: String) -> TokenStream { - // NOTE(emilio): By this point comments are already preprocessed and in - // `///` form. Quote turns them into `#[doc]` comments, but oh well. - TokenStream::from_str(&comment).unwrap() + if comment.is_empty() { + quote!() + } else { + quote!(#[doc = #comment]) + } } pub fn link_name(name: &str) -> TokenStream { diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index e758963a..c1e92b77 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -20,7 +20,6 @@ use super::BindgenOptions; use crate::ir::analysis::{HasVtable, Sizedness}; use crate::ir::annotations::FieldAccessorKind; -use crate::ir::comment; use crate::ir::comp::{ Bitfield, BitfieldUnit, CompInfo, CompKind, Field, FieldData, FieldMethods, Method, MethodKind, @@ -1245,7 +1244,6 @@ trait FieldCodegen<'a> { &self, ctx: &BindgenContext, fields_should_be_private: bool, - codegen_depth: usize, accessor_kind: FieldAccessorKind, parent: &CompInfo, result: &mut CodegenResult, @@ -1265,7 +1263,6 @@ impl<'a> FieldCodegen<'a> for Field { &self, ctx: &BindgenContext, fields_should_be_private: bool, - codegen_depth: usize, accessor_kind: FieldAccessorKind, parent: &CompInfo, result: &mut CodegenResult, @@ -1282,7 +1279,6 @@ impl<'a> FieldCodegen<'a> for Field { data.codegen( ctx, fields_should_be_private, - codegen_depth, accessor_kind, parent, result, @@ -1296,7 +1292,6 @@ impl<'a> FieldCodegen<'a> for Field { unit.codegen( ctx, fields_should_be_private, - codegen_depth, accessor_kind, parent, result, @@ -1346,7 +1341,6 @@ impl<'a> FieldCodegen<'a> for FieldData { &self, ctx: &BindgenContext, fields_should_be_private: bool, - codegen_depth: usize, accessor_kind: FieldAccessorKind, parent: &CompInfo, result: &mut CodegenResult, @@ -1392,8 +1386,7 @@ impl<'a> FieldCodegen<'a> for FieldData { let mut field = quote! {}; if ctx.options().generate_comments { if let Some(raw_comment) = self.comment() { - let comment = - comment::preprocess(raw_comment, codegen_depth + 1); + let comment = ctx.options().process_comment(raw_comment); field = attributes::doc(comment); } } @@ -1553,7 +1546,6 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { &self, ctx: &BindgenContext, fields_should_be_private: bool, - codegen_depth: usize, accessor_kind: FieldAccessorKind, parent: &CompInfo, result: &mut CodegenResult, @@ -1630,7 +1622,6 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { bf.codegen( ctx, fields_should_be_private, - codegen_depth, accessor_kind, parent, result, @@ -1705,7 +1696,6 @@ impl<'a> FieldCodegen<'a> for Bitfield { &self, ctx: &BindgenContext, fields_should_be_private: bool, - _codegen_depth: usize, _accessor_kind: FieldAccessorKind, parent: &CompInfo, _result: &mut CodegenResult, @@ -1883,7 +1873,6 @@ impl CodeGenerator for CompInfo { let mut methods = vec![]; if !is_opaque { - let codegen_depth = item.codegen_depth(ctx); let fields_should_be_private = item.annotations().private_fields().unwrap_or(false); let struct_accessor_kind = item @@ -1894,7 +1883,6 @@ impl CodeGenerator for CompInfo { field.codegen( ctx, fields_should_be_private, - codegen_depth, struct_accessor_kind, self, result, @@ -2703,14 +2691,12 @@ impl std::str::FromStr for EnumVariation { /// A helper type to construct different enum variations. enum EnumBuilder<'a> { Rust { - codegen_depth: usize, attrs: Vec<proc_macro2::TokenStream>, ident: Ident, tokens: proc_macro2::TokenStream, emitted_any_variants: bool, }, NewType { - codegen_depth: usize, canonical_name: &'a str, tokens: proc_macro2::TokenStream, is_bitfield: bool, @@ -2718,26 +2704,14 @@ enum EnumBuilder<'a> { }, Consts { variants: Vec<proc_macro2::TokenStream>, - codegen_depth: usize, }, ModuleConsts { - codegen_depth: usize, module_name: &'a str, module_items: Vec<proc_macro2::TokenStream>, }, } impl<'a> EnumBuilder<'a> { - /// Returns the depth of the code generation for a variant of this enum. - fn codegen_depth(&self) -> usize { - match *self { - EnumBuilder::Rust { codegen_depth, .. } | - EnumBuilder::NewType { codegen_depth, .. } | - EnumBuilder::ModuleConsts { codegen_depth, .. } | - EnumBuilder::Consts { codegen_depth, .. } => codegen_depth, - } - } - /// Returns true if the builder is for a rustified enum. fn is_rust_enum(&self) -> bool { matches!(*self, EnumBuilder::Rust { .. }) @@ -2750,7 +2724,6 @@ impl<'a> EnumBuilder<'a> { mut attrs: Vec<proc_macro2::TokenStream>, repr: proc_macro2::TokenStream, enum_variation: EnumVariation, - enum_codegen_depth: usize, ) -> Self { let ident = Ident::new(name, Span::call_site()); @@ -2759,7 +2732,6 @@ impl<'a> EnumBuilder<'a> { is_bitfield, is_global, } => EnumBuilder::NewType { - codegen_depth: enum_codegen_depth, canonical_name: name, tokens: quote! { #( #attrs )* @@ -2774,7 +2746,6 @@ impl<'a> EnumBuilder<'a> { attrs.insert(0, quote! { #[repr( #repr )] }); let tokens = quote!(); EnumBuilder::Rust { - codegen_depth: enum_codegen_depth + 1, attrs, ident, tokens, @@ -2790,10 +2761,7 @@ impl<'a> EnumBuilder<'a> { pub type #ident = #repr; }); - EnumBuilder::Consts { - variants, - codegen_depth: enum_codegen_depth, - } + EnumBuilder::Consts { variants } } EnumVariation::ModuleConsts => { @@ -2807,7 +2775,6 @@ impl<'a> EnumBuilder<'a> { }; EnumBuilder::ModuleConsts { - codegen_depth: enum_codegen_depth + 1, module_name: name, module_items: vec![type_definition], } @@ -2839,8 +2806,7 @@ impl<'a> EnumBuilder<'a> { let mut doc = quote! {}; if ctx.options().generate_comments { if let Some(raw_comment) = variant.comment() { - let comment = - comment::preprocess(raw_comment, self.codegen_depth()); + let comment = ctx.options().process_comment(raw_comment); doc = attributes::doc(comment); } } @@ -2851,13 +2817,11 @@ impl<'a> EnumBuilder<'a> { ident, tokens, emitted_any_variants: _, - codegen_depth, } => { let name = ctx.rust_ident(variant_name); EnumBuilder::Rust { attrs, ident, - codegen_depth, tokens: quote! { #tokens #doc @@ -2918,7 +2882,6 @@ impl<'a> EnumBuilder<'a> { self } EnumBuilder::ModuleConsts { - codegen_depth, module_name, mut module_items, } => { @@ -2932,7 +2895,6 @@ impl<'a> EnumBuilder<'a> { EnumBuilder::ModuleConsts { module_name, module_items, - codegen_depth, } } } @@ -3211,13 +3173,7 @@ impl CodeGenerator for Enum { let repr = repr.to_rust_ty_or_opaque(ctx, item); - let mut builder = EnumBuilder::new( - &name, - attrs, - repr, - variation, - item.codegen_depth(ctx), - ); + let mut builder = EnumBuilder::new(&name, attrs, repr, variation); // A map where we keep a value -> variant relation. let mut seen_values = HashMap::<_, Ident>::default(); |