Skip to content

Commit 88a2125

Browse files
committed
Add option for all module raw lines
Provide an option to add a line to every mod generated by bindgen. Part of google/autocxx#124, though this is only in fact necessary if we make the changes given in google/autocxx#1456.
1 parent 20aa65a commit 88a2125

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

bindgen-tests/tests/expectations/tests/namespace.rs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/headers/namespace.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --enable-cxx-namespaces --module-raw-line root::whatever 'pub type whatever_other_thing_t = whatever_int_t;'
1+
// bindgen-flags: --enable-cxx-namespaces --module-raw-line root::whatever 'pub type whatever_other_thing_t = whatever_int_t;' --every-module-raw-line 'struct PerModStruct;'
22

33
void top_level();
44

bindgen/codegen/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ impl CodeGenerator for Module {
609609
.namespace_aware_canonical_path(ctx)
610610
.join("::")
611611
.into_boxed_str();
612+
612613
if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
613614
for raw_line in raw_lines {
614615
found_any = true;
@@ -617,6 +618,14 @@ impl CodeGenerator for Module {
617618
);
618619
}
619620
}
621+
// For lines we add to all modules, don't modify `found_any`
622+
// since we don't want to generate this module unless there's
623+
// other stuff present.
624+
result.extend(ctx.options().every_module_raw_lines.iter().map(
625+
|raw_line| {
626+
proc_macro2::TokenStream::from_str(raw_line).unwrap()
627+
},
628+
));
620629

621630
codegen_self(result, &mut found_any);
622631
});

bindgen/options/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ struct BindgenCommand {
336336
/// Add a raw line of Rust code at the beginning of output.
337337
#[arg(long)]
338338
raw_line: Vec<String>,
339+
/// Add a raw line of Rust code at the beginning of each module.
340+
#[arg(long)]
341+
every_module_raw_line: Vec<String>,
339342
/// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME.
340343
#[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])]
341344
module_raw_line: Vec<String>,
@@ -601,6 +604,7 @@ where
601604
opaque_type,
602605
output,
603606
raw_line,
607+
every_module_raw_line,
604608
module_raw_line,
605609
rust_target,
606610
rust_edition,
@@ -907,6 +911,7 @@ where
907911
block_extern_crate,
908912
opaque_type,
909913
raw_line,
914+
every_module_raw_line,
910915
use_core => |b, _| b.use_core(),
911916
distrust_clang_mangling => |b, _| b.trust_clang_mangling(false),
912917
conservative_inline_namespaces => |b, _| b.conservative_inline_namespaces(),

bindgen/options/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,24 @@ options! {
11611161
}
11621162
},
11631163
},
1164+
/// The set of raw lines to be prepended to every module of the generated Rust code.
1165+
every_module_raw_lines: Vec<Box<str>> {
1166+
methods: {
1167+
/// Add a line of Rust code at the beginning of each module (i.e.
1168+
/// C++ namespace) in the generated code. The string is
1169+
/// passed through without any modification.
1170+
pub fn every_module_raw_line<T: Into<String>>(mut self, arg: T) -> Self {
1171+
self.options.every_module_raw_lines.push(arg.into().into_boxed_str());
1172+
self
1173+
}
1174+
},
1175+
as_args: |every_module_raw_lines, args| {
1176+
for line in every_module_raw_lines {
1177+
args.push("--every-module-raw-line".to_owned());
1178+
args.push(line.clone().into());
1179+
}
1180+
},
1181+
},
11641182
/// The set of raw lines to prepend to different modules.
11651183
module_lines: HashMap<Box<str>, Vec<Box<str>>> {
11661184
methods: {

0 commit comments

Comments
 (0)