Skip to content

Commit d0d0726

Browse files
Nico Chatziemilio
Nico Chatzi
authored andcommitted
Fix docstring comment for constants
1 parent 696455d commit d0d0726

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/codegen/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,18 @@ impl CodeGenerator for Var {
618618
return;
619619
}
620620

621+
let mut attrs = vec![];
622+
if let Some(comment) = item.comment(ctx) {
623+
attrs.push(attributes::doc(comment));
624+
}
625+
621626
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
622627

623628
if let Some(val) = self.val() {
624629
match *val {
625630
VarType::Bool(val) => {
626631
result.push(quote! {
632+
#(#attrs)*
627633
pub const #canonical_ident : #ty = #val ;
628634
});
629635
}
@@ -643,6 +649,7 @@ impl CodeGenerator for Var {
643649
helpers::ast_ty::uint_expr(val as _)
644650
};
645651
result.push(quote! {
652+
#(#attrs)*
646653
pub const #canonical_ident : #ty = #val ;
647654
});
648655
}
@@ -660,12 +667,14 @@ impl CodeGenerator for Var {
660667
Ok(string) => {
661668
let cstr = helpers::ast_ty::cstr_expr(string);
662669
result.push(quote! {
670+
#(#attrs)*
663671
pub const #canonical_ident : &'static #ty = #cstr ;
664672
});
665673
}
666674
Err(..) => {
667675
let bytes = helpers::ast_ty::byte_array_expr(bytes);
668676
result.push(quote! {
677+
#(#attrs)*
669678
pub const #canonical_ident : #ty = #bytes ;
670679
});
671680
}
@@ -674,20 +683,20 @@ impl CodeGenerator for Var {
674683
VarType::Float(f) => {
675684
match helpers::ast_ty::float_expr(ctx, f) {
676685
Ok(expr) => result.push(quote! {
686+
#(#attrs)*
677687
pub const #canonical_ident : #ty = #expr ;
678688
}),
679689
Err(..) => return,
680690
}
681691
}
682692
VarType::Char(c) => {
683693
result.push(quote! {
694+
#(#attrs)*
684695
pub const #canonical_ident : #ty = #c ;
685696
});
686697
}
687698
}
688699
} else {
689-
let mut attrs = vec![];
690-
691700
// If necessary, apply a `#[link_name]` attribute
692701
let link_name = self.mangled_name().unwrap_or(self.name());
693702
if !utils::names_will_be_identical_after_mangling(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
/// This is a constant that has a docstring
9+
///
10+
/// And expected to be found in generated bindings code too.
11+
pub const FOO: ::std::os::raw::c_int = 1;
12+
/// This is a constant that has a docstring
13+
///
14+
/// And expected to be found in generated bindings code too.
15+
#[repr(C)]
16+
#[derive(Debug, Default, Copy, Clone)]
17+
pub struct Bar {
18+
pub baz: ::std::os::raw::c_int,
19+
}
20+
#[test]
21+
fn bindgen_test_layout_Bar() {
22+
assert_eq!(
23+
::std::mem::size_of::<Bar>(),
24+
4usize,
25+
concat!("Size of: ", stringify!(Bar))
26+
);
27+
assert_eq!(
28+
::std::mem::align_of::<Bar>(),
29+
4usize,
30+
concat!("Alignment of ", stringify!(Bar))
31+
);
32+
assert_eq!(
33+
unsafe { &(*(::std::ptr::null::<Bar>())).baz as *const _ as usize },
34+
0usize,
35+
concat!("Offset of field: ", stringify!(Bar), "::", stringify!(baz))
36+
);
37+
}

tests/headers/issue-1995.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// This is a constant that has a docstring
2+
///
3+
/// And expected to be found in generated bindings code too.
4+
const int FOO = 1;
5+
6+
/// This is a constant that has a docstring
7+
///
8+
/// And expected to be found in generated bindings code too.
9+
struct Bar
10+
{
11+
int baz;
12+
};

0 commit comments

Comments
 (0)