Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for all module raw lines #3163

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindgen-tests/tests/expectations/tests/namespace.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/namespace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --enable-cxx-namespaces --module-raw-line root::whatever 'pub type whatever_other_thing_t = whatever_int_t;'
// 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;'

void top_level();

Expand Down
9 changes: 9 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ impl CodeGenerator for Module {
.namespace_aware_canonical_path(ctx)
.join("::")
.into_boxed_str();

if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
for raw_line in raw_lines {
found_any = true;
Expand All @@ -617,6 +618,14 @@ impl CodeGenerator for Module {
);
}
}
// For lines we add to all modules, don't modify `found_any`
// since we don't want to generate this module unless there's
// other stuff present.
result.extend(ctx.options().every_module_raw_lines.iter().map(
|raw_line| {
proc_macro2::TokenStream::from_str(raw_line).unwrap()
},
));

codegen_self(result, &mut found_any);
});
Expand Down
5 changes: 5 additions & 0 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ struct BindgenCommand {
/// Add a raw line of Rust code at the beginning of output.
#[arg(long)]
raw_line: Vec<String>,
/// Add a raw line of Rust code at the beginning of each module.
#[arg(long)]
every_module_raw_line: Vec<String>,
/// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME.
#[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])]
module_raw_line: Vec<String>,
Expand Down Expand Up @@ -601,6 +604,7 @@ where
opaque_type,
output,
raw_line,
every_module_raw_line,
module_raw_line,
rust_target,
rust_edition,
Expand Down Expand Up @@ -907,6 +911,7 @@ where
block_extern_crate,
opaque_type,
raw_line,
every_module_raw_line,
use_core => |b, _| b.use_core(),
distrust_clang_mangling => |b, _| b.trust_clang_mangling(false),
conservative_inline_namespaces => |b, _| b.conservative_inline_namespaces(),
Expand Down
18 changes: 18 additions & 0 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,24 @@ options! {
}
},
},
/// The set of raw lines to be prepended to every module of the generated Rust code.
every_module_raw_lines: Vec<Box<str>> {
methods: {
/// Add a line of Rust code at the beginning of each module (i.e.
/// C++ namespace) in the generated code. The string is
/// passed through without any modification.
pub fn every_module_raw_line<T: Into<String>>(mut self, arg: T) -> Self {
self.options.every_module_raw_lines.push(arg.into().into_boxed_str());
self
}
},
as_args: |every_module_raw_lines, args| {
for line in every_module_raw_lines {
args.push("--every-module-raw-line".to_owned());
args.push(line.clone().into());
}
},
},
/// The set of raw lines to prepend to different modules.
module_lines: HashMap<Box<str>, Vec<Box<str>>> {
methods: {
Expand Down
Loading