Extract a function for writing docs in generated C++

This commit is contained in:
David Tolnay 2022-11-18 00:03:17 -08:00
parent a3030a0460
commit 75c0454f1d
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -227,6 +227,12 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
}
}
fn write_doc(out: &mut OutFile, indent: &str, doc: &Doc) {
for line in doc.to_string().lines() {
writeln!(out, "{}//{}", indent, line);
}
}
fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
let operator_eq = derive::contains(&strct.derives, Trait::PartialEq);
let operator_ord = derive::contains(&strct.derives, Trait::PartialOrd);
@ -235,15 +241,11 @@ fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&Extern
let guard = format!("CXXBRIDGE1_STRUCT_{}", strct.name.to_symbol());
writeln!(out, "#ifndef {}", guard);
writeln!(out, "#define {}", guard);
for line in strct.doc.to_string().lines() {
writeln!(out, "//{}", line);
}
write_doc(out, "", &strct.doc);
writeln!(out, "struct {} final {{", strct.name.cxx);
for field in &strct.fields {
for line in field.doc.to_string().lines() {
writeln!(out, " //{}", line);
}
write_doc(out, " ", &field.doc);
write!(out, " ");
write_type_space(out, &field.ty);
writeln!(out, "{};", field.name.cxx);
@ -255,9 +257,7 @@ fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&Extern
if !method.doc.is_empty() {
out.next_section();
}
for line in method.doc.to_string().lines() {
writeln!(out, " //{}", line);
}
write_doc(out, " ", &method.doc);
write!(out, " ");
let sig = &method.sig;
let local_name = method.name.cxx.to_string();
@ -336,9 +336,7 @@ fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[
let guard = format!("CXXBRIDGE1_STRUCT_{}", ety.name.to_symbol());
writeln!(out, "#ifndef {}", guard);
writeln!(out, "#define {}", guard);
for line in ety.doc.to_string().lines() {
writeln!(out, "//{}", line);
}
write_doc(out, "", &ety.doc);
out.builtin.opaque = true;
writeln!(
@ -351,9 +349,7 @@ fn write_opaque_type<'a>(out: &mut OutFile<'a>, ety: &'a ExternType, methods: &[
if i > 0 && !method.doc.is_empty() {
out.next_section();
}
for line in method.doc.to_string().lines() {
writeln!(out, " //{}", line);
}
write_doc(out, " ", &method.doc);
write!(out, " ");
let sig = &method.sig;
let local_name = method.name.cxx.to_string();
@ -390,16 +386,12 @@ fn write_enum<'a>(out: &mut OutFile<'a>, enm: &'a Enum) {
let guard = format!("CXXBRIDGE1_ENUM_{}", enm.name.to_symbol());
writeln!(out, "#ifndef {}", guard);
writeln!(out, "#define {}", guard);
for line in enm.doc.to_string().lines() {
writeln!(out, "//{}", line);
}
write_doc(out, "", &enm.doc);
write!(out, "enum class {} : ", enm.name.cxx);
write_atom(out, repr);
writeln!(out, " {{");
for variant in &enm.variants {
for line in variant.doc.to_string().lines() {
writeln!(out, " //{}", line);
}
write_doc(out, " ", &variant.doc);
writeln!(out, " {} = {},", variant.name.cxx, variant.discriminant);
}
writeln!(out, "}};");
@ -1001,9 +993,7 @@ fn write_rust_function_shim_impl(
}
if sig.receiver.is_none() {
// Member functions already documented at their declaration.
for line in doc.to_string().lines() {
writeln!(out, "//{}", line);
}
write_doc(out, "", doc);
}
write_rust_function_shim_decl(out, local_name, sig, indirect_call);
if out.header {