Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added non-exhaustive enums to the CLI #2847

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ struct BindgenCommand {
/// Mark any enum whose name matches REGEX as a Rust enum.
#[arg(long, value_name = "REGEX")]
rustified_enum: Vec<String>,
/// Mark any enum whose name matches REGEX as a non-exhaustive Rust enum.
#[arg(long, value_name = "REGEX")]
rustified_non_exhaustive_enum: Vec<String>,
/// Mark any enum whose name matches REGEX as a series of constants.
#[arg(long, value_name = "REGEX")]
constified_enum: Vec<String>,
Expand Down Expand Up @@ -469,6 +472,7 @@ where
newtype_enum,
newtype_global_enum,
rustified_enum,
rustified_non_exhaustive_enum,
constified_enum,
constified_enum_module,
default_macro_constant_type,
Expand Down Expand Up @@ -635,6 +639,10 @@ where
builder = builder.rustified_enum(regex);
}

for regex in rustified_non_exhaustive_enum {
builder = builder.rustified_non_exhaustive_enum(regex);
}

for regex in constified_enum {
builder = builder.constified_enum(regex);
}
Expand Down Expand Up @@ -1081,8 +1089,8 @@ where
&self,
info: &bindgen::callbacks::DeriveInfo<'_>,
) -> Vec<String> {
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
self.regex_set.matches(info.name)
if self.kind.map(|kind| kind == info.kind).unwrap_or(true)
&& self.regex_set.matches(info.name)
{
return self.derives.clone();
}
Expand Down
17 changes: 9 additions & 8 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_";
const DEFAULT_NON_EXTERN_FNS_SUFFIX: &str = "__extern";

fn file_is_cpp(name_file: &str) -> bool {
name_file.ends_with(".hpp") ||
name_file.ends_with(".hxx") ||
name_file.ends_with(".hh") ||
name_file.ends_with(".h++")
name_file.ends_with(".hpp")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... This must be my rustfmt settings. Strange, I'd have expected my IDE to grab them from the rustfmt.toml at the top

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need nightly rust to format bindgen's code

|| name_file.ends_with(".hxx")
|| name_file.ends_with(".hh")
|| name_file.ends_with(".h++")
}

fn args_are_cpp(clang_args: &[Box<str>]) -> bool {
Expand Down Expand Up @@ -237,6 +237,7 @@ impl std::fmt::Display for Formatter {
/// 2. [`bitfield_enum()`](#method.bitfield_enum)
/// 3. [`newtype_enum()`](#method.newtype_enum)
/// 4. [`rustified_enum()`](#method.rustified_enum)
/// 4. [`rustified_non_exhaustive_enum()`](#method.rustified_non_exhaustive_enum)
kulp marked this conversation as resolved.
Show resolved Hide resolved
///
/// For each C enum, bindgen tries to match the pattern in the following order:
///
Expand Down Expand Up @@ -798,8 +799,8 @@ impl Bindings {
return false;
}

if arg.starts_with("-I") ||
arg.starts_with("--include-directory=")
if arg.starts_with("-I")
|| arg.starts_with("--include-directory=")
{
return false;
}
Expand All @@ -826,8 +827,8 @@ impl Bindings {
debug!("Found clang: {:?}", clang);

// Whether we are working with C or C++ inputs.
let is_cpp = args_are_cpp(&options.clang_args) ||
options.input_headers.iter().any(|h| file_is_cpp(h));
let is_cpp = args_are_cpp(&options.clang_args)
|| options.input_headers.iter().any(|h| file_is_cpp(h));

let search_paths = if is_cpp {
clang.cpp_search_paths
Expand Down
Loading