diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/bindgen.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/parser.rs | 40 |
3 files changed, 38 insertions, 10 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs index 228c5f95..1fcd9dfd 100644 --- a/src/bin/bindgen.rs +++ b/src/bin/bindgen.rs @@ -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; @@ -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. + pub namespaced_constants: bool, pub override_enum_ty: String, pub raw_lines: Vec<String>, /// Attributes for a type with destructor @@ -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![], @@ -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, diff --git a/src/parser.rs b/src/parser.rs index 1acf1c60..3004eb40 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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>, @@ -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, @@ -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!") } @@ -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; @@ -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) { @@ -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; @@ -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, |