From 8cacb00d2cc0e5963bd3d884a94fcfd2e1e1d1eb Mon Sep 17 00:00:00 2001 From: GrayJack Date: Sun, 27 Oct 2024 14:43:00 -0300 Subject: [PATCH 1/3] feat: Add support for constants defined with `if` + `cfg!` macro Cython equivalent of `#error` directive still unimplemented --- src/bindgen/ir/constant.rs | 191 +++++++++++++++++++++---- src/bindgen/language_backend/clike.rs | 13 +- src/bindgen/language_backend/cython.rs | 3 + src/bindgen/parser.rs | 40 +++--- tests/expectations/cfg.c | 36 +++++ tests/expectations/cfg.compat.c | 36 +++++ tests/expectations/cfg.cpp | 36 +++++ tests/expectations/cfg_both.c | 36 +++++ tests/expectations/cfg_both.compat.c | 36 +++++ tests/expectations/cfg_tag.c | 36 +++++ tests/expectations/cfg_tag.compat.c | 36 +++++ tests/rust/cfg.rs | 56 +++++--- 12 files changed, 490 insertions(+), 65 deletions(-) diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 2f1d3bff..3f2e5da4 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -107,6 +107,7 @@ pub enum Literal { ty: Type, value: Box, }, + PanicMacro(PanicMacroKind), } impl Literal { @@ -156,6 +157,7 @@ impl Literal { } } Literal::Expr(..) => {} + Literal::PanicMacro(_) => {} } } @@ -181,6 +183,7 @@ impl Literal { Literal::FieldAccess { ref base, .. } => base.is_valid(bindings), Literal::Struct { ref path, .. } => bindings.struct_exists(path), Literal::Cast { ref value, .. } => value.is_valid(bindings), + Literal::PanicMacro(_) => true, } } @@ -210,6 +213,7 @@ impl Literal { true } Literal::Cast { ref value, .. } => value.visit(visitor), + Literal::PanicMacro(_) => true, } } @@ -285,9 +289,87 @@ impl Literal { ty.rename_for_config(config, &GenericParams::default()); value.rename_for_config(config); } + Literal::PanicMacro(_) => {} } } + // Handles `if` expressions with `cfg!` macros + pub fn load_many( + expr: &syn::Expr, + og_cfg: Option<&Cfg>, + prev_cfgs: &mut Vec, + ) -> Result)>, String> { + let mut vec = Vec::new(); + match *expr { + syn::Expr::If(syn::ExprIf { + ref cond, + ref then_branch, + ref else_branch, + .. + }) => match cond.as_ref() { + syn::Expr::Macro(syn::ExprMacro { mac, .. }) => { + if !mac.path.is_ident("cfg") { + return Err(format!( + "Unsupported if expression with macro {:?}", + mac.path + )); + } + + let cfg: Cfg = + syn::parse2(mac.tokens.clone()).map_err(|err| err.to_string())?; + + let lit = then_branch + .stmts + .last() + .map(|stmt| match stmt { + syn::Stmt::Expr(expr, None) => Literal::load(expr), + syn::Stmt::Macro(stmt_macro) => Literal::from_macro(&stmt_macro.mac), + _ => Err(format!("Unsupported block without expression")), + }) + .ok_or(format!("Unsupported block without expression"))??; + + vec.push((lit, Cfg::append(og_cfg, Some(cfg.clone())))); + prev_cfgs.push(cfg); + + if let Some((_, else_expr)) = else_branch { + match else_expr.as_ref() { + syn::Expr::Block(expr_block) => { + let lit = expr_block + .block + .stmts + .last() + .map(|stmt| match stmt { + syn::Stmt::Expr(expr, None) => Literal::load(expr), + syn::Stmt::Macro(stmt_macro) => { + Literal::from_macro(&stmt_macro.mac) + } + _ => Err(format!("Unsupported block without expression")), + }) + .ok_or(format!("Unsupported block without expression"))??; + let cfg = Cfg::append( + og_cfg, + Some(Cfg::Not(Box::new(Cfg::Any(prev_cfgs.clone())))), + ); + vec.push((lit, cfg)); + } + syn::Expr::If(_) => vec.extend( + Literal::load_many(&else_expr, og_cfg, prev_cfgs)?.into_iter(), + ), + _ => unreachable!(), + } + } + } + _ => return Err(format!("Unsupported if expression. {:?}", *expr)), + }, + _ => { + let lit = Literal::load(expr)?; + let cfg = Cfg::append(og_cfg, None); + vec.push((lit, cfg)); + } + } + Ok(vec) + } + // Translate from full blown `syn::Expr` into a simpler `Literal` type pub fn load(expr: &syn::Expr) -> Result { match *expr { @@ -476,11 +558,33 @@ impl Literal { } } + syn::Expr::Macro(syn::ExprMacro { ref mac, .. }) => Literal::from_macro(mac), _ => Err(format!("Unsupported expression. {:?}", *expr)), } } } +impl Literal { + fn from_macro(mac: &syn::Macro) -> Result { + let path = &mac.path; + let tokens = &mac.tokens; + if path.is_ident("todo") { + Ok(Literal::PanicMacro(PanicMacroKind::Todo)) + } else if path.is_ident("unreachable") { + Ok(Literal::PanicMacro(PanicMacroKind::Unreachable)) + } else if path.is_ident("unimplemented") { + Ok(Literal::PanicMacro(PanicMacroKind::Unimplemented)) + } else if path.is_ident("panic") { + // We only support simple messages, i.e. string literals + let msg: syn::LitStr = syn::parse2(tokens.clone()) + .map_err(|_| format!("Unsupported `panic!` with complex message"))?; + Ok(Literal::PanicMacro(PanicMacroKind::Panic(msg.value()))) + } else { + Err(format!("Unsupported macro as literal. {:?}", path)) + } + } +} + #[derive(Debug, Clone)] pub struct Constant { pub path: Path, @@ -501,7 +605,7 @@ impl Constant { expr: &syn::Expr, attrs: &[syn::Attribute], associated_to: Option, - ) -> Result { + ) -> Result, String> { let ty = Type::load(ty)?; let mut ty = match ty { Some(ty) => ty, @@ -510,22 +614,34 @@ impl Constant { } }; - let mut lit = Literal::load(expr)?; - if let Some(ref associated_to) = associated_to { ty.replace_self_with(associated_to); - lit.replace_self_with(associated_to); } - Ok(Constant::new( - path, - ty, - lit, - Cfg::append(mod_cfg, Cfg::load(attrs)), - AnnotationSet::load(attrs)?, - Documentation::load(attrs), - associated_to, - )) + let og_cfg = Cfg::append(mod_cfg, Cfg::load(attrs)); + + let mut prev_cfgs = Vec::new(); + let lits = Literal::load_many(expr, og_cfg.as_ref(), &mut prev_cfgs)?; + + let mut consts = Vec::with_capacity(lits.len()); + + for (mut lit, cfg) in lits { + if let Some(ref associated_to) = associated_to { + lit.replace_self_with(associated_to); + } + + consts.push(Constant::new( + path.clone(), + ty.clone(), + lit, + cfg, + AnnotationSet::load(attrs)?, + Documentation::load(attrs), + associated_to.clone(), + )); + } + + Ok(consts) } pub fn new( @@ -690,27 +806,36 @@ impl Constant { let allow_constexpr = config.constant.allow_constexpr && self.value.can_be_constexpr(); match config.language { Language::Cxx if config.constant.allow_static_const || allow_constexpr => { - if allow_constexpr { - out.write("constexpr ") - } + if matches!(self.value, Literal::PanicMacro(_)) { + write!(out, "#error "); + language_backend.write_literal(out, value); + } else { + if allow_constexpr { + out.write("constexpr ") + } - if config.constant.allow_static_const { - out.write(if in_body { "inline " } else { "static " }); - } + if config.constant.allow_static_const { + out.write(if in_body { "inline " } else { "static " }); + } - if let Type::Ptr { is_const: true, .. } = self.ty { - // Nothing. - } else { - out.write("const "); - } + if let Type::Ptr { is_const: true, .. } = self.ty { + // Nothing. + } else { + out.write("const "); + } - language_backend.write_type(out, &self.ty); - write!(out, " {} = ", name); - language_backend.write_literal(out, value); - write!(out, ";"); + language_backend.write_type(out, &self.ty); + write!(out, " {} = ", name); + language_backend.write_literal(out, value); + write!(out, ";"); + } } Language::Cxx | Language::C => { - write!(out, "#define {} ", name); + if matches!(self.value, Literal::PanicMacro(_)) { + write!(out, "#error "); + } else { + write!(out, "#define {} ", name); + } language_backend.write_literal(out, value); } Language::Cython => { @@ -726,3 +851,11 @@ impl Constant { condition.write_after(config, out); } } + +#[derive(Debug, Clone)] +pub enum PanicMacroKind { + Todo, + Unreachable, + Unimplemented, + Panic(String), +} diff --git a/src/bindgen/language_backend/clike.rs b/src/bindgen/language_backend/clike.rs index bf5a39fb..e5d639d2 100644 --- a/src/bindgen/language_backend/clike.rs +++ b/src/bindgen/language_backend/clike.rs @@ -1,7 +1,7 @@ use crate::bindgen::ir::{ to_known_assoc_constant, ConditionWrite, DeprecatedNoteKind, Documentation, Enum, EnumVariant, - Field, GenericParams, Item, Literal, OpaqueItem, ReprAlign, Static, Struct, ToCondition, Type, - Typedef, Union, + Field, GenericParams, Item, Literal, OpaqueItem, PanicMacroKind, ReprAlign, Static, Struct, + ToCondition, Type, Typedef, Union, }; use crate::bindgen::language_backend::LanguageBackend; use crate::bindgen::rename::IdentifierType; @@ -921,6 +921,15 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { } write!(out, " }}"); } + Literal::PanicMacro(panic_macro_kind) => { + match panic_macro_kind { + PanicMacroKind::Todo => write!(out, r#""not yet implemented""#), + PanicMacroKind::Unreachable => write!(out, r#""reached unreachable code""#), + PanicMacroKind::Unimplemented => write!(out, r#""not implemented""#), + // Debug print of &str and String already add the `"` + PanicMacroKind::Panic(msg) => write!(out, "{:?}", msg), + } + } } } diff --git a/src/bindgen/language_backend/cython.rs b/src/bindgen/language_backend/cython.rs index 8497f3cd..6bbf2995 100644 --- a/src/bindgen/language_backend/cython.rs +++ b/src/bindgen/language_backend/cython.rs @@ -404,6 +404,9 @@ impl LanguageBackend for CythonLanguageBackend<'_> { } write!(out, " }}"); } + Literal::PanicMacro(_) => { + warn!("SKIP: Not implemented") + } } } diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index eb2ef2dc..383a0ca4 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -807,20 +807,23 @@ impl Parse { &item.attrs, Some(impl_path.clone()), ) { - Ok(constant) => { + Ok(constants) => { info!("Take {}::{}::{}.", crate_name, impl_path, &item.ident); - let mut any = false; - self.structs.for_items_mut(&impl_path, |item| { - any = true; - item.add_associated_constant(constant.clone()); - }); - // Handle associated constants to other item types that are - // not structs like enums or such as regular constants. - if !any && !self.constants.try_insert(constant) { - error!( - "Conflicting name for constant {}::{}::{}.", - crate_name, impl_path, &item.ident, - ); + + for constant in constants { + let mut any = false; + self.structs.for_items_mut(&impl_path, |item| { + any = true; + item.add_associated_constant(constant.clone()); + }); + // Handle associated constants to other item types that are + // not structs like enums or such as regular constants. + if !any && !self.constants.try_insert(constant) { + error!( + "Conflicting name for constant {}::{}::{}.", + crate_name, impl_path, &item.ident, + ); + } } } Err(msg) => { @@ -858,12 +861,13 @@ impl Parse { let path = Path::new(item.ident.unraw().to_string()); match Constant::load(path, mod_cfg, &item.ty, &item.expr, &item.attrs, None) { - Ok(constant) => { + Ok(constants) => { info!("Take {}::{}.", crate_name, &item.ident); - - let full_name = constant.path.clone(); - if !self.constants.try_insert(constant) { - error!("Conflicting name for constant {}", full_name); + for constant in constants { + let full_name = constant.path.clone(); + if !self.constants.try_insert(constant) { + error!("Conflicting name for constant {}", full_name); + } } } Err(msg) => { diff --git a/tests/expectations/cfg.c b/tests/expectations/cfg.c index 9285f300..4ccc6168 100644 --- a/tests/expectations/cfg.c +++ b/tests/expectations/cfg.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType { A, diff --git a/tests/expectations/cfg.compat.c b/tests/expectations/cfg.compat.c index 053c4052..60060b93 100644 --- a/tests/expectations/cfg.compat.c +++ b/tests/expectations/cfg.compat.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType #ifdef __cplusplus diff --git a/tests/expectations/cfg.cpp b/tests/expectations/cfg.cpp index d7b6250d..5797477e 100644 --- a/tests/expectations/cfg.cpp +++ b/tests/expectations/cfg.cpp @@ -12,6 +12,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +constexpr static const uint8_t FOO_CONST = 0; +#endif + +#if defined(PLATFORM_WIN) +constexpr static const uint8_t FOO_CONST = 1; +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +constexpr static const uint8_t FOO_CONST = 61453; +#endif + +#if defined(PLATFORM_UNIX) +constexpr static const uint8_t BAR_CONST = 0; +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +constexpr static const uint8_t BAR_CONST = 1; +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +constexpr static const uint8_t BAZ_CONST = 0; +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +constexpr static const uint8_t BAZ_CONST = 1; +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum class BarType : uint32_t { A, diff --git a/tests/expectations/cfg_both.c b/tests/expectations/cfg_both.c index 510e34e8..6b11c1c5 100644 --- a/tests/expectations/cfg_both.c +++ b/tests/expectations/cfg_both.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType { A, diff --git a/tests/expectations/cfg_both.compat.c b/tests/expectations/cfg_both.compat.c index 2377824e..3bef7bb0 100644 --- a/tests/expectations/cfg_both.compat.c +++ b/tests/expectations/cfg_both.compat.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType #ifdef __cplusplus diff --git a/tests/expectations/cfg_tag.c b/tests/expectations/cfg_tag.c index 8cc88538..fdb9ea2a 100644 --- a/tests/expectations/cfg_tag.c +++ b/tests/expectations/cfg_tag.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType { A, diff --git a/tests/expectations/cfg_tag.compat.c b/tests/expectations/cfg_tag.compat.c index ec976353..17339f7c 100644 --- a/tests/expectations/cfg_tag.compat.c +++ b/tests/expectations/cfg_tag.compat.c @@ -11,6 +11,42 @@ DEF M_32 = 0 #include #include +#if defined(PLATFORM_UNIX) +#define FOO_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#define FOO_CONST 1 +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define FOO_CONST 61453 +#endif + +#if defined(PLATFORM_UNIX) +#define BAR_CONST 0 +#endif + +#if defined(PLATFORM_WIN) +#error "reached unreachable code" +#endif + +#if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) +#define BAR_CONST 1 +#endif + +#if (defined(X11) && defined(PLATFORM_UNIX)) +#define BAZ_CONST 0 +#endif + +#if (defined(X11) && defined(PLATFORM_WIN)) +#define BAZ_CONST 1 +#endif + +#if (defined(X11) && !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN))) +#error "Baz error" +#endif + #if (defined(PLATFORM_WIN) || defined(M_32)) enum BarType #ifdef __cplusplus diff --git a/tests/rust/cfg.rs b/tests/rust/cfg.rs index 70893609..1cb29a5b 100644 --- a/tests/rust/cfg.rs +++ b/tests/rust/cfg.rs @@ -1,9 +1,34 @@ +pub const FOO_CONST: u8 = if cfg!(unix) { + 0 +} else if cfg!(windows) { + 1 +} else { + 0xF00D +}; + +pub const BAR_CONST: u8 = if cfg!(unix) { + 0 +} else if cfg!(windows) { + unreachable!() +} else { + 1 +}; + +#[cfg(x11)] +pub const BAZ_CONST: u8 = if cfg!(unix) { + 0 +} else if cfg!(windows) { + 1 +} else { + panic!("Baz error") +}; + #[cfg(all(unix, x11))] #[repr(u32)] enum FooType { - A, - B, - C, + A, + B, + C, } #[cfg(all(unix, x11))] @@ -14,12 +39,12 @@ struct FooHandle { y: f32, } -#[cfg(any(windows, target_pointer_width="32"))] +#[cfg(any(windows, target_pointer_width = "32"))] #[repr(u32)] enum BarType { - A, - B, - C, + A, + B, + C, } #[repr(u8)] @@ -29,10 +54,12 @@ pub enum C { #[cfg(windows)] C3, #[cfg(unix)] - C5 { int: i32 }, + C5 { + int: i32, + }, } -#[cfg(any(windows, target_pointer_width="32"))] +#[cfg(any(windows, target_pointer_width = "32"))] #[repr(C)] struct BarHandle { ty: BarType, @@ -51,17 +78,14 @@ struct ConditionalField { #[cfg(all(unix, x11))] #[no_mangle] -pub extern "C" fn root(a: FooHandle, c: C) -{ } +pub extern "C" fn root(a: FooHandle, c: C) {} -#[cfg(any(windows, target_pointer_width="32"))] +#[cfg(any(windows, target_pointer_width = "32"))] #[no_mangle] -pub extern "C" fn root(a: BarHandle, c: C) -{ } +pub extern "C" fn root(a: BarHandle, c: C) {} #[no_mangle] -pub extern "C" fn cond(a: ConditionalField) -{ } +pub extern "C" fn cond(a: ConditionalField) {} // src/lib.rs #[repr(C)] From 7ce52c5e175cd763a8a0b827f26cbccfb0d3202c Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 28 Oct 2024 01:13:40 -0300 Subject: [PATCH 2/3] fix: clippy --- src/bindgen/ir/constant.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 3f2e5da4..0013fce3 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -324,9 +324,9 @@ impl Literal { .map(|stmt| match stmt { syn::Stmt::Expr(expr, None) => Literal::load(expr), syn::Stmt::Macro(stmt_macro) => Literal::from_macro(&stmt_macro.mac), - _ => Err(format!("Unsupported block without expression")), + _ => Err("Unsupported block without expression".to_string()), }) - .ok_or(format!("Unsupported block without expression"))??; + .ok_or("Unsupported block without expression".to_string())??; vec.push((lit, Cfg::append(og_cfg, Some(cfg.clone())))); prev_cfgs.push(cfg); @@ -343,18 +343,20 @@ impl Literal { syn::Stmt::Macro(stmt_macro) => { Literal::from_macro(&stmt_macro.mac) } - _ => Err(format!("Unsupported block without expression")), + _ => { + Err("Unsupported block without expression".to_string()) + } }) - .ok_or(format!("Unsupported block without expression"))??; + .ok_or("Unsupported block without expression".to_string())??; let cfg = Cfg::append( og_cfg, Some(Cfg::Not(Box::new(Cfg::Any(prev_cfgs.clone())))), ); vec.push((lit, cfg)); } - syn::Expr::If(_) => vec.extend( - Literal::load_many(&else_expr, og_cfg, prev_cfgs)?.into_iter(), - ), + syn::Expr::If(_) => { + vec.extend(Literal::load_many(else_expr, og_cfg, prev_cfgs)?) + } _ => unreachable!(), } } @@ -577,7 +579,7 @@ impl Literal { } else if path.is_ident("panic") { // We only support simple messages, i.e. string literals let msg: syn::LitStr = syn::parse2(tokens.clone()) - .map_err(|_| format!("Unsupported `panic!` with complex message"))?; + .map_err(|_| "Unsupported `panic!` with complex message".to_string())?; Ok(Literal::PanicMacro(PanicMacroKind::Panic(msg.value()))) } else { Err(format!("Unsupported macro as literal. {:?}", path)) From c1104bdefdd03215eda57f33366398d73a19bd09 Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 28 Oct 2024 02:42:07 -0300 Subject: [PATCH 3/3] chore: Update test --- tests/expectations/cfg.cpp | 6 +++--- tests/rust/cfg.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/expectations/cfg.cpp b/tests/expectations/cfg.cpp index 5797477e..6a3b85ab 100644 --- a/tests/expectations/cfg.cpp +++ b/tests/expectations/cfg.cpp @@ -13,15 +13,15 @@ DEF M_32 = 0 #include #if defined(PLATFORM_UNIX) -constexpr static const uint8_t FOO_CONST = 0; +constexpr static const uint32_t FOO_CONST = 0; #endif #if defined(PLATFORM_WIN) -constexpr static const uint8_t FOO_CONST = 1; +constexpr static const uint32_t FOO_CONST = 1; #endif #if !(defined(PLATFORM_UNIX) || defined(PLATFORM_WIN)) -constexpr static const uint8_t FOO_CONST = 61453; +constexpr static const uint32_t FOO_CONST = 61453; #endif #if defined(PLATFORM_UNIX) diff --git a/tests/rust/cfg.rs b/tests/rust/cfg.rs index 1cb29a5b..fee4b217 100644 --- a/tests/rust/cfg.rs +++ b/tests/rust/cfg.rs @@ -1,4 +1,4 @@ -pub const FOO_CONST: u8 = if cfg!(unix) { +pub const FOO_CONST: u32 = if cfg!(unix) { 0 } else if cfg!(windows) { 1