Support multiple cxx.h header sections for the same guard
diff --git a/gen/src/include.rs b/gen/src/include.rs
index be0bd8e..0ed76ac 100644
--- a/gen/src/include.rs
+++ b/gen/src/include.rs
@@ -1,21 +1,42 @@
+use crate::gen::out::OutFile;
use std::fmt::{self, Display};
pub static HEADER: &str = include_str!("include/cxx.h");
-pub fn get(guard: &str) -> &'static str {
+pub(super) fn write(out: &mut OutFile, needed: bool, guard: &str) {
let ifndef = format!("#ifndef {}", guard);
+ let define = format!("#define {}", guard);
let endif = format!("#endif // {}", guard);
- let begin = find_line(&ifndef);
- let end = find_line(&endif);
- if let (Some(begin), Some(end)) = (begin, end) {
- &HEADER[begin..end + endif.len()]
- } else {
- panic!("not found in cxx.h header: {}", guard)
+
+ let mut offset = 0;
+ loop {
+ let begin = find_line(offset, &ifndef);
+ let end = find_line(offset, &endif);
+ if let (Some(begin), Some(end)) = (begin, end) {
+ if !needed {
+ return;
+ }
+ out.next_section();
+ if offset == 0 {
+ writeln!(out, "{}", ifndef);
+ writeln!(out, "{}", define);
+ }
+ for line in HEADER[begin + ifndef.len()..end].trim().lines() {
+ if line != define && !line.trim_start().starts_with("//") {
+ writeln!(out, "{}", line);
+ }
+ }
+ offset = end + endif.len();
+ } else if offset == 0 {
+ panic!("not found in cxx.h header: {}", guard)
+ } else {
+ writeln!(out, "{}", endif);
+ return;
+ }
}
}
-fn find_line(line: &str) -> Option<usize> {
- let mut offset = 0;
+fn find_line(mut offset: usize, line: &str) -> Option<usize> {
loop {
offset += HEADER[offset..].find(line)?;
let rest = &HEADER[offset + line.len()..];
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 12f2839..c06fce9 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -249,15 +249,15 @@
writeln!(out, "struct unsafe_bitcopy_t;");
}
- write_header_section(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
- write_header_section(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
- write_header_section(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
- write_header_section(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
- write_header_section(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
- write_header_section(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
- write_header_section(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
- write_header_section(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
- write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
+ include::write(out, needs_rust_string, "CXXBRIDGE03_RUST_STRING");
+ include::write(out, needs_rust_str, "CXXBRIDGE03_RUST_STR");
+ include::write(out, needs_rust_slice, "CXXBRIDGE03_RUST_SLICE");
+ include::write(out, needs_rust_box, "CXXBRIDGE03_RUST_BOX");
+ include::write(out, needs_rust_vec, "CXXBRIDGE03_RUST_VEC");
+ include::write(out, needs_rust_fn, "CXXBRIDGE03_RUST_FN");
+ include::write(out, needs_rust_error, "CXXBRIDGE03_RUST_ERROR");
+ include::write(out, needs_rust_isize, "CXXBRIDGE03_RUST_ISIZE");
+ include::write(out, needs_unsafe_bitcopy, "CXXBRIDGE03_RUST_BITCOPY");
if needs_manually_drop {
out.next_section();
@@ -311,18 +311,6 @@
out.end_block("namespace rust");
}
-fn write_header_section(out: &mut OutFile, needed: bool, section: &str) {
- let section = include::get(section);
- if needed {
- out.next_section();
- for line in section.lines() {
- if !line.trim_start().starts_with("//") {
- writeln!(out, "{}", line);
- }
- }
- }
-}
-
fn write_struct(out: &mut OutFile, strct: &Struct) {
for line in strct.doc.to_string().lines() {
writeln!(out, "//{}", line);