From 2fa8409d1652b5f81c01d8ccb53bc30202249d95 Mon Sep 17 00:00:00 2001 From: Emilio Cobos Álvarez Date: Tue, 22 Nov 2016 19:38:22 +0100 Subject: codegen: Don't bother generating an empty module. --- libbindgen/src/codegen/mod.rs | 13 ++++++++++--- .../tests/expectations/tests/duplicated-namespaces.rs | 4 ---- libbindgen/tests/expectations/tests/namespace.rs | 4 ---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index 0469a5be..ba52fa83 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -269,7 +269,7 @@ impl CodeGenerator for Module { item: &Item) { debug!("::codegen: item = {:?}", item); - let codegen_self = |result: &mut CodegenResult| { + let codegen_self = |result: &mut CodegenResult, found_any: &mut bool| { // FIXME: This could be less expensive, I guess. for &whitelisted_item in whitelisted_items { if whitelisted_item == item.id() { @@ -278,21 +278,28 @@ impl CodeGenerator for Module { let child = ctx.resolve_item(whitelisted_item); if child.parent_id() == item.id() { + *found_any = true; child.codegen(ctx, result, whitelisted_items, &()); } } }; if !ctx.options().enable_cxx_namespaces { - codegen_self(result); + codegen_self(result, &mut false); return; } + let mut found_any = false; let inner_items = result.inner(|result| { result.push(root_import(ctx)); - codegen_self(result); + codegen_self(result, &mut found_any); }); + // Don't bother creating an empty module. + if !found_any { + return; + } + let module = ast::ItemKind::Mod(ast::Mod { inner: ctx.span(), items: inner_items, diff --git a/libbindgen/tests/expectations/tests/duplicated-namespaces.rs b/libbindgen/tests/expectations/tests/duplicated-namespaces.rs index 786fe1a5..7a283160 100644 --- a/libbindgen/tests/expectations/tests/duplicated-namespaces.rs +++ b/libbindgen/tests/expectations/tests/duplicated-namespaces.rs @@ -7,8 +7,4 @@ pub mod root { #[allow(unused_imports)] use root; - pub mod std { - #[allow(unused_imports)] - use root; - } } diff --git a/libbindgen/tests/expectations/tests/namespace.rs b/libbindgen/tests/expectations/tests/namespace.rs index 54bc08dc..34c2da1e 100644 --- a/libbindgen/tests/expectations/tests/namespace.rs +++ b/libbindgen/tests/expectations/tests/namespace.rs @@ -23,10 +23,6 @@ pub mod root { pub mod _bindgen_mod_id_13 { #[allow(unused_imports)] use root; - pub mod empty { - #[allow(unused_imports)] - use root; - } extern "C" { #[link_name = "_ZN12_GLOBAL__N_13fooEv"] pub fn foo(); -- cgit v1.2.3