summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <me@emiliocobos.me>2016-07-04 20:32:33 -0700
committerEmilio Cobos Álvarez <me@emiliocobos.me>2016-07-05 09:54:41 -0700
commitf834d96edab642d403d8a1d5446d6047bd97dd83 (patch)
treeac9a39bd04f2b3d1ce8c433a6af873b984362a27
parente441c20138005e682bf2d0699c338216c4698ec6 (diff)
bindgen: Add -no-namespaced-constants option.
Needed (at least for now, we might do mangling if necessary) for stylo, where bringing in DOM types makes us include a lot of namespaced constants they use for convenience, that end up with conflicting names.
-rw-r--r--src/bin/bindgen.rs4
-rw-r--r--src/lib.rs4
-rw-r--r--src/parser.rs22
3 files changed, 29 insertions, 1 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;
diff --git a/src/lib.rs b/src/lib.rs
index a72e9584..be43ca58 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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..307e7e1d 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,7 +1344,9 @@ 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;
@@ -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,