Skip to content

Commit d24b97f

Browse files
authored
gen-guest-c: Emit prettier comments. (#455)
Don't insert a newline between a type and its documentation comment. And do insert a newline before the documentation comment. This makes the generated header files prettier. For example this: ```c // A timestamp in nanoseconds. typedef uint64_t wasi_clocks_instant_t; // A time and date in seconds plus nanoseconds. typedef struct { uint64_t seconds; uint32_t nanoseconds; } wasi_clocks_datetime_t; ``` Instead of this: ```c // A timestamp in nanoseconds. typedef uint64_t wasi_clocks_instant_t; // A time and date in seconds plus nanoseconds. typedef struct { uint64_t seconds; uint32_t nanoseconds; } wasi_clocks_datetime_t; ```
1 parent 04da8c2 commit d24b97f

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

crates/gen-guest-c/src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
436436

437437
fn type_record(&mut self, id: TypeId, name: &str, record: &Record, docs: &Docs) {
438438
let prev = mem::take(&mut self.src.h_defs);
439+
self.src.h_defs("\n");
439440
self.docs(docs);
440-
self.src.h_defs("\ntypedef struct {\n");
441+
self.src.h_defs("typedef struct {\n");
441442
for field in record.fields.iter() {
442443
self.print_ty(SourceType::HDefs, &field.ty);
443444
self.src.h_defs(" ");
@@ -453,8 +454,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
453454

454455
fn type_tuple(&mut self, id: TypeId, name: &str, tuple: &Tuple, docs: &Docs) {
455456
let prev = mem::take(&mut self.src.h_defs);
457+
self.src.h_defs("\n");
456458
self.docs(docs);
457-
self.src.h_defs("\ntypedef struct {\n");
459+
self.src.h_defs("typedef struct {\n");
458460
for (i, ty) in tuple.types.iter().enumerate() {
459461
self.print_ty(SourceType::HDefs, ty);
460462
uwriteln!(self.src.h_defs, " f{i};");
@@ -468,8 +470,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
468470

469471
fn type_flags(&mut self, id: TypeId, name: &str, flags: &Flags, docs: &Docs) {
470472
let prev = mem::take(&mut self.src.h_defs);
473+
self.src.h_defs("\n");
471474
self.docs(docs);
472-
self.src.h_defs("\ntypedef ");
475+
self.src.h_defs("typedef ");
473476
let repr = flags_repr(flags);
474477
self.src.h_defs(int_repr(repr));
475478
self.src.h_defs(" ");
@@ -495,8 +498,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
495498

496499
fn type_variant(&mut self, id: TypeId, name: &str, variant: &Variant, docs: &Docs) {
497500
let prev = mem::take(&mut self.src.h_defs);
501+
self.src.h_defs("\n");
498502
self.docs(docs);
499-
self.src.h_defs("\ntypedef struct {\n");
503+
self.src.h_defs("typedef struct {\n");
500504
self.src.h_defs(int_repr(variant.tag()));
501505
self.src.h_defs(" tag;\n");
502506
self.src.h_defs("union {\n");
@@ -532,8 +536,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
532536

533537
fn type_union(&mut self, id: TypeId, name: &str, union: &Union, docs: &Docs) {
534538
let prev = mem::take(&mut self.src.h_defs);
539+
self.src.h_defs("\n");
535540
self.docs(docs);
536-
self.src.h_defs("\ntypedef struct {\n");
541+
self.src.h_defs("typedef struct {\n");
537542
self.src.h_defs(int_repr(union.tag()));
538543
self.src.h_defs(" tag;\n");
539544
self.src.h_defs("union {\n");
@@ -551,8 +556,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
551556

552557
fn type_option(&mut self, id: TypeId, name: &str, payload: &Type, docs: &Docs) {
553558
let prev = mem::take(&mut self.src.h_defs);
559+
self.src.h_defs("\n");
554560
self.docs(docs);
555-
self.src.h_defs("\ntypedef struct {\n");
561+
self.src.h_defs("typedef struct {\n");
556562
self.src.h_defs("bool is_some;\n");
557563
if !self.is_empty_type(payload) {
558564
self.print_ty(SourceType::HDefs, payload);
@@ -567,8 +573,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
567573

568574
fn type_result(&mut self, id: TypeId, name: &str, result: &Result_, docs: &Docs) {
569575
let prev = mem::take(&mut self.src.h_defs);
576+
self.src.h_defs("\n");
570577
self.docs(docs);
571-
self.src.h_defs("\ntypedef struct {\n");
578+
self.src.h_defs("typedef struct {\n");
572579
self.src.h_defs("bool is_err;\n");
573580
self.src.h_defs("union {\n");
574581
if let Some(ok) = self.get_nonempty_type(result.ok.as_ref()) {
@@ -589,9 +596,10 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
589596

590597
fn type_enum(&mut self, id: TypeId, name: &str, enum_: &Enum, docs: &Docs) {
591598
let prev = mem::take(&mut self.src.h_defs);
599+
uwrite!(self.src.h_defs, "\n");
592600
self.docs(docs);
593601
let int_t = int_repr(enum_.tag());
594-
uwrite!(self.src.h_defs, "\ntypedef {int_t} ");
602+
uwrite!(self.src.h_defs, "typedef {int_t} ");
595603
self.print_typedef_target(name);
596604

597605
if enum_.cases.len() > 0 {
@@ -614,8 +622,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
614622

615623
fn type_alias(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
616624
let prev = mem::take(&mut self.src.h_defs);
625+
self.src.h_defs("\n");
617626
self.docs(docs);
618-
self.src.h_defs("\ntypedef ");
627+
self.src.h_defs("typedef ");
619628
self.print_ty(SourceType::HDefs, ty);
620629
self.src.h_defs(" ");
621630
self.print_typedef_target(name);
@@ -625,8 +634,9 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
625634

626635
fn type_list(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
627636
let prev = mem::take(&mut self.src.h_defs);
637+
self.src.h_defs("\n");
628638
self.docs(docs);
629-
self.src.h_defs("\ntypedef struct {\n");
639+
self.src.h_defs("typedef struct {\n");
630640
self.print_ty(SourceType::HDefs, ty);
631641
self.src.h_defs(" *ptr;\n");
632642
self.src.h_defs("size_t len;\n");

0 commit comments

Comments
 (0)