Skip to content

allow renaming rules for C enums, structs, and typedefs #3191

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cexpr = "0.6"
clang-sys = "1"
clap = "4"
clap_complete = "4"
convert_case = "0.8.0"
env_logger = "0.10.0"
itertools = { version = ">=0.10,<0.14", default-features = false }
libloading = "0.8"
Expand Down
1 change: 1 addition & 0 deletions bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cexpr.workspace = true
clang-sys = { workspace = true, features = ["clang_11_0"] }
clap = { workspace = true, features = ["derive"], optional = true }
clap_complete = { workspace = true, optional = true }
convert_case.workspace = true
itertools = { workspace = true }
log = { workspace = true, optional = true }
prettyplease = { workspace = true, optional = true, features = ["verbatim"] }
Expand Down
6 changes: 3 additions & 3 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,15 @@ pub(crate) struct BindgenContext {
/// Whether a bindgen float16 was generated
generated_bindgen_float16: Cell<bool>,

/// The set of `ItemId`s that are allowlisted. This the very first thing
/// The set of [`ItemId`]s that are allowlisted. This the very first thing
/// computed after parsing our IR, and before running any of our analyses.
allowlisted: Option<ItemSet>,

/// Cache for calls to `ParseCallbacks::blocklisted_type_implements_trait`
/// Cache for calls to [`crate::callbacks::ParseCallbacks::blocklisted_type_implements_trait`]
blocklisted_types_implement_traits:
RefCell<HashMap<DeriveTrait, HashMap<ItemId, CanDerive>>>,

/// The set of `ItemId`s that are allowlisted for code generation _and_ that
/// The set of [`ItemId`]s that are allowlisted for code generation _and_ that
/// we should generate accounting for the codegen options.
///
/// It's computed right after computing the allowlisted items.
Expand Down
38 changes: 38 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod features;
mod ir;
mod parse;
mod regex_set;
mod renamer;

pub use codegen::{
AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle,
Expand All @@ -55,6 +56,7 @@ pub use ir::annotations::FieldVisibilityKind;
pub use ir::function::Abi;
#[cfg(feature = "__cli")]
pub use options::cli::builder_from_flags;
pub use renamer::{Case, IdentRenamer, Regex, Renamer};

use codegen::CodegenError;
use features::RustFeatures;
Expand Down Expand Up @@ -1312,6 +1314,42 @@ impl callbacks::ParseCallbacks for CargoCallbacks {
}
}

/// Macro to help define renaming rules for an enum and its values. See an example in the [`Renamer`] documentation.
#[macro_export]
macro_rules! rename_enum {
( $cb:expr,
$c_name:literal => $rust_name:literal
$(, remove: $remove:literal)*
$(, case: $case:ident)?
$(, $itm:literal => $ren:literal)*
$(,)?
) => {
$cb.rename_item($c_name, $rust_name);
#[allow(clippy::needless_update)]
$cb.rename_enum_val(
Some(concat!("enum ", $c_name)),
$crate::IdentRenamer {
remove: {
let patterns: Vec<&str> = vec![$($remove),*];
if patterns.is_empty() {
None
} else {
Some(
patterns
.into_iter()
.map(|v| $crate::Regex::new(v).expect("Unable to compile regex for remove parameter"))
.collect()
)
}
},
$( case: Some($crate::Case::$case), )?
renames: vec![$( ($itm.into(), $ren.into()), )*].into_iter().collect(),
..$crate::IdentRenamer::default_case($crate::Case::Pascal)
}
);
};
}

/// Test `command_line_flag` function.
#[test]
fn commandline_flag_unit_test_function() {
Expand Down
Loading
Loading