Skip to content

Commit 040149b

Browse files
authored
Generate BindgenOptions and the Builder methods using macros (#2473)
* Generate `BindgenOptions` and the `Builder` methods using macros This is done so the definition, default value and `Builder` methods for each field of `BindgenOptions` are kept in the same region of code. Before this change, adding (or modifying) a new option for `bindgen` required: - Updating the fields of the `BindgenOptions` type. - Updating the `Default` implementation for `BindgenOptions`. - Updating one or several `Builder` methods with proper documentation explaining the default value of the option. - Updating the `Builder::command_line_flags` method. Each one of these steps was done in a different place inside `bindgen/lib.rs`. With this change, all these 4 steps are done in the same place. This should make less likely to have bugs. Clearly using macros implies properly documenting how to use such macros and makes adding new options a bit harder because most (all?) editors are not able to format code and suggest completions inside macros. This change also moves all the code related to setting `BindgenOptions` to the new `bindgen/options.rs` file. * Factor out the "Regex are supported" docs. All the options that support regular expressions had the following sentence in their documentation: > Regular expressions are supported This comment was factored out to the macro that documents options based on `RegexSet`s. * Rename `fn_with_regex_arg` to `regex_option` * Allow optional commas in the `options` macro. This is done to avoid hard to detect macro parsing issues due to missing commas. * Fix typo * Add the `AsArg` trait. This trait eases the conversion of `BindgenOptions` fields into CLI args. This commit also changes the `options` macro so the flag can be passed to `as_args` instead of a closure. * Document the `option` macro * Document the `options` module * Rewrite some documentation * Run rustfmt * Avoid examples with `bool` parameters * More documentation changes * Reorganize the `options` module * Run rustfmt
1 parent c94367c commit 040149b

File tree

5 files changed

+2441
-2279
lines changed

5 files changed

+2441
-2279
lines changed

bindgen/codegen/mod.rs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use crate::{Entry, HashMap, HashSet};
5757
use std::borrow::Cow;
5858
use std::cell::Cell;
5959
use std::collections::VecDeque;
60-
use std::fmt::Write;
60+
use std::fmt::{self, Write};
6161
use std::ops;
6262
use std::str::FromStr;
6363

@@ -73,13 +73,13 @@ impl From<std::io::Error> for CodegenError {
7373
}
7474
}
7575

76-
impl std::fmt::Display for CodegenError {
77-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76+
impl fmt::Display for CodegenError {
77+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7878
match self {
79-
CodegenError::Serialize { msg, loc } => {
79+
Self::Serialize { msg, loc } => {
8080
write!(f, "serialization error at {}: {}", loc, msg)
8181
}
82-
CodegenError::Io(err) => err.fmt(f),
82+
Self::Io(err) => err.fmt(f),
8383
}
8484
}
8585
}
@@ -2736,6 +2736,35 @@ impl Default for EnumVariation {
27362736
}
27372737
}
27382738

2739+
impl fmt::Display for EnumVariation {
2740+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2741+
let s = match self {
2742+
Self::Rust {
2743+
non_exhaustive: false,
2744+
} => "rust",
2745+
Self::Rust {
2746+
non_exhaustive: true,
2747+
} => "rust_non_exhaustive",
2748+
Self::NewType {
2749+
is_bitfield: true, ..
2750+
} => "bitfield",
2751+
Self::NewType {
2752+
is_bitfield: false,
2753+
is_global,
2754+
} => {
2755+
if *is_global {
2756+
"newtype_global"
2757+
} else {
2758+
"newtype"
2759+
}
2760+
}
2761+
Self::Consts => "consts",
2762+
Self::ModuleConsts => "moduleconsts",
2763+
};
2764+
s.fmt(f)
2765+
}
2766+
}
2767+
27392768
impl std::str::FromStr for EnumVariation {
27402769
type Err = std::io::Error;
27412770

@@ -3422,13 +3451,13 @@ pub enum MacroTypeVariation {
34223451
Unsigned,
34233452
}
34243453

3425-
impl MacroTypeVariation {
3426-
/// Convert a `MacroTypeVariation` to its str representation.
3427-
pub(crate) fn as_str(&self) -> &str {
3428-
match self {
3429-
MacroTypeVariation::Signed => "signed",
3430-
MacroTypeVariation::Unsigned => "unsigned",
3431-
}
3454+
impl fmt::Display for MacroTypeVariation {
3455+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3456+
let s = match self {
3457+
Self::Signed => "signed",
3458+
Self::Unsigned => "unsigned",
3459+
};
3460+
s.fmt(f)
34323461
}
34333462
}
34343463

@@ -3468,14 +3497,15 @@ pub enum AliasVariation {
34683497
NewTypeDeref,
34693498
}
34703499

3471-
impl AliasVariation {
3472-
/// Convert an `AliasVariation` to its str representation.
3473-
pub(crate) fn as_str(&self) -> &str {
3474-
match self {
3475-
AliasVariation::TypeAlias => "type_alias",
3476-
AliasVariation::NewType => "new_type",
3477-
AliasVariation::NewTypeDeref => "new_type_deref",
3478-
}
3500+
impl fmt::Display for AliasVariation {
3501+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3502+
let s = match self {
3503+
Self::TypeAlias => "type_alias",
3504+
Self::NewType => "new_type",
3505+
Self::NewTypeDeref => "new_type_deref",
3506+
};
3507+
3508+
s.fmt(f)
34793509
}
34803510
}
34813511

@@ -3505,10 +3535,10 @@ impl std::str::FromStr for AliasVariation {
35053535
}
35063536
}
35073537

3508-
/// Enum for how non-Copy unions should be translated.
3538+
/// Enum for how non-`Copy` `union`s should be translated.
35093539
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
35103540
pub enum NonCopyUnionStyle {
3511-
/// Wrap members in a type generated by bindgen.
3541+
/// Wrap members in a type generated by `bindgen`.
35123542
BindgenWrapper,
35133543
/// Wrap members in [`::core::mem::ManuallyDrop`].
35143544
///
@@ -3517,13 +3547,14 @@ pub enum NonCopyUnionStyle {
35173547
ManuallyDrop,
35183548
}
35193549

3520-
impl NonCopyUnionStyle {
3521-
/// Convert an `NonCopyUnionStyle` to its str representation.
3522-
pub(crate) fn as_str(&self) -> &'static str {
3523-
match self {
3550+
impl fmt::Display for NonCopyUnionStyle {
3551+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3552+
let s = match self {
35243553
Self::BindgenWrapper => "bindgen_wrapper",
35253554
Self::ManuallyDrop => "manually_drop",
3526-
}
3555+
};
3556+
3557+
s.fmt(f)
35273558
}
35283559
}
35293560

0 commit comments

Comments
 (0)