Skip to content

full support non rust naming conventions of generated code #576

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 0 additions & 1 deletion bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn main() {
let bindings = Builder::default()
.no_unstable_rust()
.enable_cxx_namespaces()
.raw_line("pub use self::root::*;")
.header("cpp/Test.h")
.clang_arg("-x")
.clang_arg("c++")
Expand Down
4 changes: 4 additions & 0 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub mod attributes {
use aster;
use syntax::ast;

pub fn allow(which_ones: &[&str]) -> ast::Attribute {
aster::AstBuilder::new().attr().list("allow").words(which_ones).build()
}

pub fn repr(which: &str) -> ast::Attribute {
aster::AstBuilder::new().attr().list("repr").words(&[which]).build()
}
Expand Down
33 changes: 27 additions & 6 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,29 @@ impl CodeGenerator for Module {
}
};

if !ctx.options().enable_cxx_namespaces ||
let name = item.canonical_name(ctx);
if (name != "root" && !ctx.options().enable_cxx_namespaces) ||
(self.is_inline() && !ctx.options().conservative_inline_namespaces) {
codegen_self(result, &mut false);
return;
}

let mut found_any = false;
let inner_items = result.inner(|result| {
result.push(root_import(ctx, item));
if name != "root" || ctx.options().enable_cxx_namespaces {
result.push(root_import(ctx, item));
} else if name == "root" {
let path = vec![ctx.rust_ident_raw("self"),
ctx.rust_ident_raw("super"),
ctx.rust_ident_raw("*")];
let use_root = aster::AstBuilder::new()
.item()
.use_()
.ids(path)
.build()
.build();
result.push(quote_item!(ctx.ext_cx(), #[allow(unused_imports)] $use_root).unwrap());
}
codegen_self(result, &mut found_any);
});

Expand All @@ -398,11 +412,18 @@ impl CodeGenerator for Module {
items: inner_items,
});

let name = item.canonical_name(ctx);
let item = aster::AstBuilder::new()
let item_builder = aster::AstBuilder::new()
.item()
.pub_()
.build_item_kind(name, module);
.pub_();
let item = if name == "root" {
let attrs = &["non_snake_case",
"non_camel_case_types",
"non_upper_case_globals"];
item_builder.with_attr(attributes::allow(attrs))
.build_item_kind(name, module)
} else {
item_builder.build_item_kind(name, module)
};

result.push(item);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ pub fn cursor_mangling(ctx: &BindgenContext,
}

// Try to undo backend linkage munging (prepended _, generally)
if cfg!(target_os = "macos") {
if get_abi(cursor.cur_type().call_conv()).map_or(true, |abi| abi != abi::Abi::Stdcall) &&
cfg!(target_os = "macos") {
mangling.remove(0);
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,10 @@ impl<'ctx> Bindings<'ctx> {
try!(writer.write("\n".as_bytes()));
}

if self.module.items.len() > 0 {
try!(writer.write("pub use self::root::*;\n\n".as_bytes()));
}

let mut ps = pprust::rust_printer(writer);
try!(ps.print_mod(&self.module, &[]));
try!(ps.print_remaining_comments());
Expand Down
Loading