diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/item.rs | 24 | ||||
-rw-r--r-- | src/lib.rs | 17 | ||||
-rw-r--r-- | src/options.rs | 7 |
3 files changed, 47 insertions, 1 deletions
diff --git a/src/ir/item.rs b/src/ir/item.rs index ffb52664..9375c798 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -4,7 +4,7 @@ use super::super::codegen::{EnumVariation, CONSTIFIED_ENUM_MODULE_REPR_NAME}; use super::analysis::{HasVtable, HasVtableResult, Sizedness, SizednessResult}; use super::annotations::Annotations; use super::comment; -use super::comp::MethodKind; +use super::comp::{CompKind, MethodKind}; use super::context::{BindgenContext, ItemId, PartialType, TypeId}; use super::derive::{ CanDeriveCopy, CanDeriveDebug, CanDeriveDefault, CanDeriveEq, @@ -904,6 +904,12 @@ impl Item { names.push(base_name); } + if ctx.options().c_naming { + if let Some(prefix) = self.c_naming_prefix() { + names.insert(0, prefix.to_string()); + } + } + let name = names.join("_"); let name = if opt.user_mangled == UserMangled::Yes { @@ -1054,6 +1060,22 @@ impl Item { path.reverse(); path } + + /// Returns a prefix for the canonical name when C naming is enabled. + fn c_naming_prefix(&self) -> Option<&str> { + if let ItemKind::Type(typ) = &self.kind { + match typ.kind() { + TypeKind::Comp(comp_info) => match comp_info.kind() { + CompKind::Struct => Some("struct"), + CompKind::Union => Some("union"), + }, + TypeKind::Enum(_) => Some("enum"), + _ => None, + } + } else { + None + } + } } impl<T> IsOpaque for T @@ -558,6 +558,10 @@ impl Builder { output_vector.push("--translate-enum-integer-types".into()); } + if self.options.c_naming { + output_vector.push("--c-naming".into()); + } + // Add clang arguments output_vector.push("--".into()); @@ -1616,6 +1620,15 @@ impl Builder { self.options.translate_enum_integer_types = doit; self } + + /// Generate types with C style naming. + /// + /// This will add prefixes to the generated type names. For example instead of a struct `A` we + /// will generate struct `struct_A`. Currently applies to structs, unions, and enums. + pub fn c_naming(mut self, doit: bool) -> Self { + self.options.c_naming = doit; + self + } } /// Configuration options for generated bindings. @@ -1921,6 +1934,9 @@ struct BindgenOptions { /// Always translate enum integer types to native Rust integer types. translate_enum_integer_types: bool, + + /// Generate types with C style naming. + c_naming: bool, } /// TODO(emilio): This is sort of a lie (see the error message that results from @@ -2062,6 +2078,7 @@ impl Default for BindgenOptions { dynamic_link_require_all: false, respect_cxx_access_specs: false, translate_enum_integer_types: false, + c_naming: false, } } } diff --git a/src/options.rs b/src/options.rs index ff2d4282..cbdc945e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -513,6 +513,9 @@ where Arg::with_name("translate-enum-integer-types") .long("translate-enum-integer-types") .help("Always translate enum integer types to native Rust integer types."), + Arg::with_name("c-naming") + .long("c-naming") + .help("Generate types with C style naming."), ]) // .args() .get_matches_from(args); @@ -953,6 +956,10 @@ where builder = builder.translate_enum_integer_types(true); } + if matches.is_present("c-naming") { + builder = builder.c_naming(true); + } + let verbose = matches.is_present("verbose"); Ok((builder, output, verbose)) |