Skip to content

Commit 03dc80c

Browse files
committed
Merge branch 'all-modules-raw-line-2'
2 parents 716a534 + 88a2125 commit 03dc80c

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
@@ -608,6 +608,7 @@ impl CodeGenerator for Module {
608608
.namespace_aware_canonical_path(ctx)
609609
.join("::")
610610
.into_boxed_str();
611+
611612
if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
612613
for raw_line in raw_lines {
613614
found_any = true;
@@ -616,6 +617,14 @@ impl CodeGenerator for Module {
616617
);
617618
}
618619
}
620+
// For lines we add to all modules, don't modify `found_any`
621+
// since we don't want to generate this module unless there's
622+
// other stuff present.
623+
result.extend(ctx.options().every_module_raw_lines.iter().map(
624+
|raw_line| {
625+
proc_macro2::TokenStream::from_str(raw_line).unwrap()
626+
},
627+
));
619628

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

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>,
@@ -604,6 +607,7 @@ where
604607
opaque_type,
605608
output,
606609
raw_line,
610+
every_module_raw_line,
607611
module_raw_line,
608612
rust_target,
609613
rust_edition,
@@ -911,6 +915,7 @@ where
911915
block_extern_crate,
912916
opaque_type,
913917
raw_line,
918+
every_module_raw_line,
914919
use_core => |b, _| b.use_core(),
915920
distrust_clang_mangling => |b, _| b.trust_clang_mangling(false),
916921
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
@@ -1218,6 +1218,24 @@ options! {
12181218
}
12191219
},
12201220
},
1221+
/// The set of raw lines to be prepended to every module of the generated Rust code.
1222+
every_module_raw_lines: Vec<Box<str>> {
1223+
methods: {
1224+
/// Add a line of Rust code at the beginning of each module (i.e.
1225+
/// C++ namespace) in the generated code. The string is
1226+
/// passed through without any modification.
1227+
pub fn every_module_raw_line<T: Into<String>>(mut self, arg: T) -> Self {
1228+
self.options.every_module_raw_lines.push(arg.into().into_boxed_str());
1229+
self
1230+
}
1231+
},
1232+
as_args: |every_module_raw_lines, args| {
1233+
for line in every_module_raw_lines {
1234+
args.push("--every-module-raw-line".to_owned());
1235+
args.push(line.clone().into());
1236+
}
1237+
},
1238+
},
12211239
/// The set of raw lines to prepend to different modules.
12221240
module_lines: HashMap<Box<str>, Vec<Box<str>>> {
12231241
methods: {

0 commit comments

Comments
 (0)