Skip to content

Commit 751920e

Browse files
committed
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.
1 parent 953fc76 commit 751920e

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

bindgen/codegen/mod.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,11 +1619,8 @@ impl FieldCodegen<'_> for FieldData {
16191619
};
16201620

16211621
let mut field = quote! {};
1622-
if ctx.options().generate_comments {
1623-
if let Some(raw_comment) = self.comment() {
1624-
let comment = ctx.options().process_comment(raw_comment);
1625-
field = attributes::doc(&comment);
1626-
}
1622+
if let Some(comment) = self.doc_comment(ctx) {
1623+
field = attributes::doc(&comment);
16271624
}
16281625

16291626
let field_name = self
@@ -3933,14 +3930,11 @@ impl CodeGenerator for Enum {
39333930
continue;
39343931
}
39353932

3936-
let mut variant_doc = quote! {};
3937-
if ctx.options().generate_comments {
3938-
if let Some(raw_comment) = variant.comment() {
3939-
let processed_comment =
3940-
ctx.options().process_comment(raw_comment);
3941-
variant_doc = attributes::doc(&processed_comment);
3942-
}
3943-
}
3933+
let variant_doc = if let Some(comment) = variant.doc_comment(ctx) {
3934+
attributes::doc(&comment)
3935+
} else {
3936+
quote! {}
3937+
};
39443938

39453939
match seen_values.entry(variant.val()) {
39463940
Entry::Occupied(ref entry) => {

bindgen/ir/comp.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub(crate) trait FieldMethods {
142142
fn ty(&self) -> TypeId;
143143

144144
/// Get the comment for this field.
145+
#[allow(dead_code)] // Used by trait implementations in Field wrapper types
145146
fn comment(&self) -> Option<&str>;
146147

147148
/// If this is a bitfield, how many bits does it need?
@@ -902,6 +903,20 @@ impl FieldMethods for FieldData {
902903
}
903904
}
904905

906+
impl FieldData {
907+
/// Get this field's documentation comment, if it has any, already preprocessed
908+
/// and with the right indentation. Returns `None` if comment generation is disabled.
909+
pub(crate) fn doc_comment(&self, ctx: &BindgenContext) -> Option<String> {
910+
if !ctx.options().generate_comments {
911+
return None;
912+
}
913+
914+
self.comment
915+
.as_ref()
916+
.map(|comment| ctx.options().process_comment(comment))
917+
}
918+
}
919+
905920
/// The kind of inheritance a base class is using.
906921
#[derive(Clone, Debug, PartialEq, Eq)]
907922
pub(crate) enum BaseKind {

bindgen/ir/enum_ty.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,16 @@ impl EnumVariant {
302302
self.val
303303
}
304304

305-
/// Get this variant's documentation.
306-
pub(crate) fn comment(&self) -> Option<&str> {
307-
self.comment.as_deref()
305+
/// Get this variant's documentation comment, if it has any, already preprocessed
306+
/// and with the right indentation. Returns `None` if comment generation is disabled.
307+
pub(crate) fn doc_comment(&self, ctx: &BindgenContext) -> Option<String> {
308+
if !ctx.options().generate_comments {
309+
return None;
310+
}
311+
312+
self.comment
313+
.as_ref()
314+
.map(|comment| ctx.options().process_comment(comment))
308315
}
309316

310317
/// Returns whether this variant should be enforced to be a constant by code

0 commit comments

Comments
 (0)