From 751920ef6462fdabc4cb26372c29bf2ce3eeaa10 Mon Sep 17 00:00:00 2001 From: naoNao89 <90588855+naoNao89@users.noreply.github.com> Date: Tue, 16 Dec 2025 04:37:38 +0700 Subject: [PATCH] refactor: centralize comment generation checks (fixes #445) - Add doc_comment() method to FieldData that checks generate_comments flag - Add doc_comment() method to EnumVariant that checks generate_comments flag - Remove redundant generate_comments checks from field codegen - Remove redundant generate_comments checks from enum variant codegen This makes the code more idiomatic by having types that generate comments handle the flag check themselves, eliminating duplication and centralizing the decision logic. --- bindgen/codegen/mod.rs | 20 +++++++------------- bindgen/ir/comp.rs | 15 +++++++++++++++ bindgen/ir/enum_ty.rs | 13 ++++++++++--- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index a5aa73b5d8..4015c2ce18 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1619,11 +1619,8 @@ impl FieldCodegen<'_> for FieldData { }; let mut field = quote! {}; - if ctx.options().generate_comments { - if let Some(raw_comment) = self.comment() { - let comment = ctx.options().process_comment(raw_comment); - field = attributes::doc(&comment); - } + if let Some(comment) = self.doc_comment(ctx) { + field = attributes::doc(&comment); } let field_name = self @@ -3933,14 +3930,11 @@ impl CodeGenerator for Enum { continue; } - let mut variant_doc = quote! {}; - if ctx.options().generate_comments { - if let Some(raw_comment) = variant.comment() { - let processed_comment = - ctx.options().process_comment(raw_comment); - variant_doc = attributes::doc(&processed_comment); - } - } + let variant_doc = if let Some(comment) = variant.doc_comment(ctx) { + attributes::doc(&comment) + } else { + quote! {} + }; match seen_values.entry(variant.val()) { Entry::Occupied(ref entry) => { diff --git a/bindgen/ir/comp.rs b/bindgen/ir/comp.rs index 7ba65de9cc..a41fa6f0b2 100644 --- a/bindgen/ir/comp.rs +++ b/bindgen/ir/comp.rs @@ -142,6 +142,7 @@ pub(crate) trait FieldMethods { fn ty(&self) -> TypeId; /// Get the comment for this field. + #[allow(dead_code)] // Used by trait implementations in Field wrapper types fn comment(&self) -> Option<&str>; /// If this is a bitfield, how many bits does it need? @@ -902,6 +903,20 @@ impl FieldMethods for FieldData { } } +impl FieldData { + /// Get this field's documentation comment, if it has any, already preprocessed + /// and with the right indentation. Returns `None` if comment generation is disabled. + pub(crate) fn doc_comment(&self, ctx: &BindgenContext) -> Option { + if !ctx.options().generate_comments { + return None; + } + + self.comment + .as_ref() + .map(|comment| ctx.options().process_comment(comment)) + } +} + /// The kind of inheritance a base class is using. #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum BaseKind { diff --git a/bindgen/ir/enum_ty.rs b/bindgen/ir/enum_ty.rs index 9b08da3bce..3c84c9b84f 100644 --- a/bindgen/ir/enum_ty.rs +++ b/bindgen/ir/enum_ty.rs @@ -302,9 +302,16 @@ impl EnumVariant { self.val } - /// Get this variant's documentation. - pub(crate) fn comment(&self) -> Option<&str> { - self.comment.as_deref() + /// Get this variant's documentation comment, if it has any, already preprocessed + /// and with the right indentation. Returns `None` if comment generation is disabled. + pub(crate) fn doc_comment(&self, ctx: &BindgenContext) -> Option { + if !ctx.options().generate_comments { + return None; + } + + self.comment + .as_ref() + .map(|comment| ctx.options().process_comment(comment)) } /// Returns whether this variant should be enforced to be a constant by code