Skip to content

Commit b9b425b

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 b9b425b

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,20 @@ impl FieldMethods for FieldData {
902902
}
903903
}
904904

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

bindgen/ir/enum_ty.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ impl EnumVariant {
307307
self.comment.as_deref()
308308
}
309309

310+
/// Get this variant's documentation comment, if it has any, already preprocessed
311+
/// and with the right indentation. Returns `None` if comment generation is disabled.
312+
pub(crate) fn doc_comment(&self, ctx: &BindgenContext) -> Option<String> {
313+
if !ctx.options().generate_comments {
314+
return None;
315+
}
316+
317+
self.comment
318+
.as_ref()
319+
.map(|comment| ctx.options().process_comment(comment))
320+
}
321+
310322
/// Returns whether this variant should be enforced to be a constant by code
311323
/// generation.
312324
pub(crate) fn force_constification(&self) -> bool {

0 commit comments

Comments
 (0)