Skip to content

Commit 27ec386

Browse files
committed
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 27ec386

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-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

+47-1
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,11 @@ 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.comment(ctx)
2416+
.or_else(|| Self::get_typedef_fallback_comment(ctx, &type_id))
2417+
{
24142418
attributes.push(attributes::doc(&comment));
24152419
}
24162420

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

29923038
impl Method {

0 commit comments

Comments
 (0)