Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f9984d7

Browse files
committedFeb 8, 2025··
Generate doc comment when type alias is hidden
If a type is not documented, but a type alias with the same canonical path is, then generate the documentation of the typealias onto the type, since otherwise it would be lost.
1 parent 03d49b6 commit f9984d7

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
 

‎bindgen-tests/tests/expectations/tests/3119_overlapping_alias_comment.rs

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --no-layout-tests
2+
3+
/**
4+
* This is a forward declared struct alias with overlapping names
5+
* and documentation.
6+
*/
7+
typedef struct Struct Struct;

‎bindgen/codegen/mod.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,12 @@ impl CodeGenerator for CompInfo {
24102410
let mut needs_debug_impl = false;
24112411
let mut needs_partialeq_impl = false;
24122412
let needs_flexarray_impl = flex_array_generic.is_some();
2413-
if let Some(comment) = item.comment(ctx) {
2413+
let type_id = item.id().expect_type_id(ctx);
2414+
2415+
if let Some(comment) = item
2416+
.comment(ctx)
2417+
.or_else(|| Self::get_typedef_fallback_comment(ctx, &type_id))
2418+
{
24142419
attributes.push(attributes::doc(&comment));
24152420
}
24162421

@@ -2987,6 +2992,48 @@ impl CompInfo {
29872992
}
29882993
}
29892994
}
2995+
2996+
/// Use a fallback comment from a type alias to this type if necessary
2997+
///
2998+
/// The documentation for a type could get lost in the following circumstances:
2999+
///
3000+
/// - We have a type and a type alias with the same canonical path
3001+
/// - The Documentation is only associated with the type alias
3002+
///
3003+
/// In this case bindgen will not generate the type alias and the documentation would be lost.
3004+
/// To avoid this, we check here if there is any type alias to this type, which has
3005+
/// the same canonical path and return the comment as a fallback, if our type does
3006+
/// not have documentation.
3007+
fn get_typedef_fallback_comment(
3008+
ctx: &BindgenContext,
3009+
type_id: &crate::ir::context::TypeId,
3010+
) -> Option<String> {
3011+
if !ctx.options().generate_comments {
3012+
return None;
3013+
}
3014+
let type_alias_comment = ctx
3015+
.items()
3016+
.filter(|(_id, alias)| {
3017+
let Some(this_ty) = alias.as_type() else {
3018+
return false;
3019+
};
3020+
let TypeKind::Alias(alias_to) = this_ty.kind() else {
3021+
return false;
3022+
};
3023+
//
3024+
match ctx.resolve_type(*alias_to).kind() {
3025+
TypeKind::ResolvedTypeRef(resolved_typeid) => {
3026+
resolved_typeid == type_id &&
3027+
alias.canonical_path(ctx) ==
3028+
type_id.canonical_path(ctx)
3029+
}
3030+
_ => false,
3031+
}
3032+
})
3033+
.filter_map(|(_id, item)| item.comment(ctx));
3034+
let alias_comment: Vec<String> = type_alias_comment.collect();
3035+
alias_comment.get(0).cloned()
3036+
}
29903037
}
29913038

29923039
impl Method {

0 commit comments

Comments
 (0)
Please sign in to comment.