diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2018-06-04 10:31:31 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-04 10:31:31 -0400 |
commit | 440374102538f05eb4208fd0bef1db9214a4d48a (patch) | |
tree | 1df8c8e99b03652050ae36320a69049c6c7f0141 /src | |
parent | cfd0fa5e559327006d6fbc1e37357a2981aba224 (diff) | |
parent | 393e77952af3c42362b8cd5755ce4df74dfe9e6d (diff) |
Auto merge of #1328 - db48x:default-enum-variant, r=emilio
Add an option to set the default codegen style for all enums
Diffstat (limited to 'src')
-rw-r--r-- | src/codegen/mod.rs | 37 | ||||
-rw-r--r-- | src/lib.rs | 20 | ||||
-rw-r--r-- | src/options.rs | 13 |
3 files changed, 65 insertions, 5 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index b6aabb17..387ff495 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -40,6 +40,7 @@ use ir::var::Var; use quote; use proc_macro2::{self, Term, Span}; +use std; use std::borrow::Cow; use std::cell::Cell; use std::collections::{HashSet, VecDeque}; @@ -2103,11 +2104,15 @@ impl MethodCodegen for Method { } /// A helper type that represents different enum variations. -#[derive(Copy, Clone)] -enum EnumVariation { +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum EnumVariation { + /// The code for this enum will use a Rust enum Rust, + /// The code for this enum will use a bitfield Bitfield, + /// The code for this enum will use consts Consts, + /// The code for this enum will use a module containing consts ModuleConsts } @@ -2136,6 +2141,31 @@ impl EnumVariation { } } +impl Default for EnumVariation { + fn default() -> EnumVariation { + EnumVariation::Consts + } +} + +impl std::str::FromStr for EnumVariation { + type Err = std::io::Error; + + /// Create a `EnumVariation` from a string. + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "rust" => Ok(EnumVariation::Rust), + "bitfield" => Ok(EnumVariation::Bitfield), + "consts" => Ok(EnumVariation::Consts), + "moduleconsts" => Ok(EnumVariation::ModuleConsts), + _ => Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, + concat!("Got an invalid EnumVariation. Accepted values ", + "are 'rust', 'bitfield', 'consts', and ", + "'moduleconsts'."))), + } + } +} + + /// A helper type to construct different enum variations. enum EnumBuilder<'a> { Rust { @@ -2491,8 +2521,7 @@ impl CodeGenerator for Enum { } else if self.is_rustified_enum(ctx, item) { EnumVariation::Rust } else { - // We generate consts by default - EnumVariation::Consts + ctx.options().default_enum_style }; let mut attrs = vec![]; @@ -83,6 +83,7 @@ use ir::context::{BindgenContext, ItemId}; use ir::item::Item; use parse::{ClangItemParser, ParseError}; use regex_set::RegexSet; +pub use codegen::EnumVariation; use std::borrow::Cow; use std::collections::HashMap; @@ -203,6 +204,16 @@ impl Builder { output_vector.push("--rust-target".into()); output_vector.push(self.options.rust_target.into()); + if self.options.default_enum_style != Default::default() { + output_vector.push("--default-enum-variant=".into()); + output_vector.push(match self.options.default_enum_style { + codegen::EnumVariation::Rust => "rust", + codegen::EnumVariation::Bitfield => "bitfield", + codegen::EnumVariation::Consts => "consts", + codegen::EnumVariation::ModuleConsts => "moduleconsts", + }.into()) + } + self.options .bitfield_enums .get_items() @@ -729,6 +740,11 @@ impl Builder { self.whitelist_var(arg) } + /// Set the default style of code to generate for enums + pub fn default_enum_style(mut self, arg: codegen::EnumVariation) -> Builder { + self.options.default_enum_style = arg; + self + } /// Mark the given enum (or set of enums, if using a pattern) as being /// bitfield-like. Regular expressions are supported. @@ -1240,6 +1256,9 @@ struct BindgenOptions { /// Whitelisted variables. See docs for `whitelisted_types` for more. whitelisted_vars: RegexSet, + /// The default style of code to generate for enums + default_enum_style: codegen::EnumVariation, + /// The enum patterns to mark an enum as bitfield. bitfield_enums: RegexSet, @@ -1458,6 +1477,7 @@ impl Default for BindgenOptions { whitelisted_types: Default::default(), whitelisted_functions: Default::default(), whitelisted_vars: Default::default(), + default_enum_style: Default::default(), bitfield_enums: Default::default(), rustified_enums: Default::default(), constified_enum_modules: Default::default(), diff --git a/src/options.rs b/src/options.rs index b3ddbbfd..97b2556b 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,4 +1,4 @@ -use bindgen::{Builder, CodegenConfig, RUST_TARGET_STRINGS, RustTarget, builder}; +use bindgen::{Builder, CodegenConfig, RUST_TARGET_STRINGS, RustTarget, builder, EnumVariation}; use clap::{App, Arg}; use std::fs::File; use std::io::{self, Error, ErrorKind, Write, stderr}; @@ -26,6 +26,13 @@ where Arg::with_name("header") .help("C or C++ header file") .required(true), + Arg::with_name("default-enum-style") + .long("default-enum-style") + .help("The default style of code used to generate enums.") + .value_name("variant") + .default_value("consts") + .possible_values(&["consts", "moduleconsts", "bitfield", "rust"]) + .multiple(false), Arg::with_name("bitfield-enum") .long("bitfield-enum") .help("Mark any enum whose name matches <regex> as a set of \ @@ -303,6 +310,10 @@ where builder = builder.rust_target(RustTarget::from_str(rust_target)?); } + if let Some(variant) = matches.value_of("default-enum-style") { + builder = builder.default_enum_style(EnumVariation::from_str(variant)?) + } + if let Some(bitfields) = matches.values_of("bitfield-enum") { for regex in bitfields { builder = builder.bitfield_enum(regex); |