Skip to content

Commit 9b8ddae

Browse files
author
bors-servo
authored
Auto merge of #1308 - emilio:per-module-lines, r=fitzgen
Add an option to add lines per module. Fixes #1307
2 parents af54e58 + 786ed31 commit 9b8ddae

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

bindgen-integration/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn main() {
3838
.enable_cxx_namespaces()
3939
.rustified_enum(".*")
4040
.raw_line("pub use self::root::*;")
41+
.module_raw_line("root::testing", "pub type Bar = i32;")
4142
.header("cpp/Test.h")
4243
.clang_args(&["-x", "c++", "-std=c++11"])
4344
.parse_callbacks(Box::new(MacroCallback {macros: macros.clone()}))

bindgen-integration/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::ffi::CStr;
88
use std::os::raw::c_int;
99
use std::mem;
1010

11+
#[allow(unused)]
12+
use bindings::testing::Bar; // This type is generated from module_raw_line.
13+
1114
#[test]
1215
fn test_static_array() {
1316
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };

src/codegen/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,18 @@ impl CodeGenerator for Module {
422422
let mut found_any = false;
423423
let inner_items = result.inner(|result| {
424424
result.push(root_import(ctx, item));
425+
426+
let path = item.namespace_aware_canonical_path(ctx).join("::");
427+
if let Some(raw_lines) = ctx.options().module_lines.get(&path) {
428+
for raw_line in raw_lines {
429+
found_any = true;
430+
// FIXME(emilio): The use of `Term` is an abuse, but we abuse it
431+
// in a bunch more places.
432+
let line = Term::new(raw_line, Span::call_site());
433+
result.push(quote! { #line });
434+
}
435+
}
436+
425437
codegen_self(result, &mut found_any);
426438
});
427439

@@ -431,16 +443,15 @@ impl CodeGenerator for Module {
431443
}
432444

433445
let name = item.canonical_name(ctx);
434-
435-
result.push(if name == "root" {
446+
let ident = ctx.rust_ident(name);
447+
result.push(if item.id() == ctx.root_module() {
436448
quote! {
437449
#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
438-
pub mod root {
450+
pub mod #ident {
439451
#( #inner_items )*
440452
}
441453
}
442454
} else {
443-
let ident = ctx.rust_ident(name);
444455
quote! {
445456
pub mod #ident {
446457
#( #inner_items )*

src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use parse::{ClangItemParser, ParseError};
8585
use regex_set::RegexSet;
8686

8787
use std::borrow::Cow;
88+
use std::collections::HashMap;
8889
use std::fs::{File, OpenOptions};
8990
use std::io::{self, Write};
9091
use std::iter;
@@ -766,11 +767,40 @@ impl Builder {
766767

767768
/// Add a string to prepend to the generated bindings. The string is passed
768769
/// through without any modification.
769-
pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Builder {
770+
pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Self {
770771
self.options.raw_lines.push(arg.into());
771772
self
772773
}
773774

775+
/// Add a given line to the beginning of module `mod`.
776+
pub fn module_raw_line<T, U>(mut self, mod_: T, line: U) -> Self
777+
where
778+
T: Into<String>,
779+
U: Into<String>,
780+
{
781+
self.options
782+
.module_lines
783+
.entry(mod_.into())
784+
.or_insert_with(Vec::new)
785+
.push(line.into());
786+
self
787+
}
788+
789+
/// Add a given set of lines to the beginning of module `mod`.
790+
pub fn module_raw_lines<T, I>(mut self, mod_: T, lines: I) -> Self
791+
where
792+
T: Into<String>,
793+
I: IntoIterator,
794+
I::Item: Into<String>,
795+
{
796+
self.options
797+
.module_lines
798+
.entry(mod_.into())
799+
.or_insert_with(Vec::new)
800+
.extend(lines.into_iter().map(Into::into));
801+
self
802+
}
803+
774804
/// Add an argument to be passed straight through to clang.
775805
pub fn clang_arg<T: Into<String>>(mut self, arg: T) -> Builder {
776806
self.options.clang_args.push(arg.into());
@@ -1300,9 +1330,15 @@ struct BindgenOptions {
13001330
/// Whether we should convert float types to f32/f64 types.
13011331
convert_floats: bool,
13021332

1303-
/// The set of raw lines to prepend to the generated Rust code.
1333+
/// The set of raw lines to prepend to the top-level module of generated
1334+
/// Rust code.
13041335
raw_lines: Vec<String>,
13051336

1337+
/// The set of raw lines to prepend to each of the modules.
1338+
///
1339+
/// This only makes sense if the `enable_cxx_namespaces` option is set.
1340+
module_lines: HashMap<String, Vec<String>>,
1341+
13061342
/// The set of arguments to pass straight through to Clang.
13071343
clang_args: Vec<String>,
13081344

@@ -1448,6 +1484,7 @@ impl Default for BindgenOptions {
14481484
msvc_mangling: false,
14491485
convert_floats: true,
14501486
raw_lines: vec![],
1487+
module_lines: HashMap::default(),
14511488
clang_args: vec![],
14521489
input_header: None,
14531490
input_unsaved_files: vec![],

0 commit comments

Comments
 (0)