Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/bin/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ fn parse_args(args: &[String]) -> ParseResult {
options.gen_bitfield_methods = false;
ix += 1;
}
"-no-namespaced-constants" => {
options.namespaced_constants = false;
ix += 1;
}
"-no-class-constants" => {
options.class_constants = false;
ix += 1;
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ pub struct BindgenOptions {
pub derive_debug: bool,
/// Wether to generate C++ class constants.
pub class_constants: bool,
/// Wether to generate names that are **directly** under namespaces.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling

pub namespaced_constants: bool,
pub override_enum_ty: String,
pub raw_lines: Vec<String>,
/// Attributes for a type with destructor
Expand All @@ -184,6 +186,7 @@ impl Default for BindgenOptions {
enable_cxx_namespaces: false,
override_enum_ty: "".to_string(),
class_constants: true,
namespaced_constants: true,
raw_lines: vec![],
dtor_attrs: vec![],
clang_args: vec![],
Expand Down Expand Up @@ -304,6 +307,7 @@ fn parse_headers(options: &BindgenOptions, logger: &Logger) -> Result<ModuleMap,
match_pat: options.match_pat.clone(),
emit_ast: options.emit_ast,
class_constants: options.class_constants,
namespaced_constants: options.namespaced_constants,
ignore_functions: options.ignore_functions,
fail_on_unknown_type: options.fail_on_unknown_type,
enable_cxx_namespaces: options.enable_cxx_namespaces,
Expand Down
40 changes: 30 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct ClangParserOptions {
pub ignore_functions: bool,
pub enable_cxx_namespaces: bool,
pub class_constants: bool,
pub namespaced_constants: bool,
pub override_enum_ty: Option<il::IKind>,
pub clang_args: Vec<String>,
pub opaque_types: Vec<String>,
Expand All @@ -38,6 +39,9 @@ struct ClangParserCtx<'a> {
builtin_defs: Vec<Cursor>,
module_map: ModuleMap,
current_module_id: ModuleId,
/// This member is used to track down if we're in a namespace if the
/// enable_cxx_namespaces option is disabled.
namespace_depth: usize,
current_translation_unit: TranslationUnit,
logger: &'a (Logger+'a),
err_count: i32,
Expand All @@ -53,6 +57,10 @@ impl<'a> ClangParserCtx<'a> {
self.module(&self.current_module_id)
}

fn in_root_namespace(&self) -> bool {
self.namespace_depth == 0
}

fn current_module_mut(&mut self) -> &mut Module {
self.module_map.get_mut(&self.current_module_id).expect("Module not found!")
}
Expand Down Expand Up @@ -1229,6 +1237,12 @@ fn visit_top(cursor: &Cursor,
CXChildVisit_Continue
}
CXCursor_VarDecl => {
// TODO: At some point we might want to mangle them instead?
// We already have a bunch of that logic.
if !ctx.in_root_namespace() && !ctx.options.namespaced_constants {
return CXChildVisit_Continue;
}

let linkage = cursor.linkage();
if linkage != CXLinkage_External && linkage != CXLinkage_UniqueExternal {
return CXChildVisit_Continue;
Expand Down Expand Up @@ -1287,7 +1301,10 @@ fn visit_top(cursor: &Cursor,
}
CXCursor_Namespace => {
if !ctx.options.enable_cxx_namespaces {
return CXChildVisit_Recurse;
ctx.namespace_depth += 1;
cursor.visit(|cur, _: &Cursor| visit_top(cur, &mut ctx));
ctx.namespace_depth -= 1;
return CXChildVisit_Continue;
}

let namespace_name = match ctx.current_translation_unit.tokens(cursor) {
Expand Down Expand Up @@ -1327,27 +1344,29 @@ fn visit_top(cursor: &Cursor,
let previous_id = ctx.current_module_id;

ctx.current_module_id = mod_id;
ctx.namespace_depth += 1;
cursor.visit(|cur, _: &Cursor| visit_top(cur, &mut ctx));
ctx.namespace_depth -= 1;
ctx.current_module_id = previous_id;

return CXChildVisit_Continue;
}
CXCursor_MacroDefinition => {
let val = parse_int_literal_tokens(cursor, &ctx.current_translation_unit, 1);
if val.is_none() {
// Not an integer literal.
return CXChildVisit_Continue;
}
let val = match val {
None => return CXChildVisit_Continue, // Not an integer literal.
Some(v) => v,
};
let var = decl_name(ctx, cursor);
let vi = var.varinfo();
let mut vi = vi.borrow_mut();
vi.ty = match val {
None => TVoid,
Some(v) if v.abs() > u32::max_value() as i64 => TInt(IULongLong, Layout::new(8, 8)),
_ => TInt(IUInt, Layout::new(4, 4)),
vi.ty = if val.abs() > u32::max_value() as i64 {
TInt(IULongLong, Layout::new(8, 8))
} else {
TInt(IUInt, Layout::new(4, 4))
};
vi.is_const = true;
vi.val = val;
vi.val = Some(val);
ctx.current_module_mut().globals.push(var);

return CXChildVisit_Continue;
Expand Down Expand Up @@ -1386,6 +1405,7 @@ pub fn parse(options: ClangParserOptions, logger: &Logger) -> Result<ModuleMap,
name: HashMap::new(),
builtin_defs: vec!(),
module_map: ModuleMap::new(),
namespace_depth: 0,
current_module_id: ROOT_MODULE_ID,
current_translation_unit: unit,
logger: logger,
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/duplicated_constants_in_ns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* automatically generated by rust-bindgen */


#![feature(const_fn)]
#![allow(non_snake_case)]



7 changes: 7 additions & 0 deletions tests/headers/duplicated_constants_in_ns.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// bindgen-flags: -no-namespaced-constants
namespace foo {
const int FOO = 4;
}
namespace bar {
const int FOO = 5;
}