diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 98ab897..f04260f 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -6,6 +6,7 @@ use std::borrow::Cow; use std::collections::HashMap; use std::io::Write; +use syn::ext::IdentExt; use syn::{self, UnOp}; use crate::bindgen::config::{Config, Language}; @@ -224,11 +225,11 @@ impl Literal { ref fields, .. }) => { - let struct_name = path.segments[0].ident.to_string(); + let struct_name = path.segments[0].ident.unraw().to_string(); let mut field_map = HashMap::::default(); for field in fields { let ident = match field.member { - syn::Member::Named(ref name) => name.to_string(), + syn::Member::Named(ref name) => name.unraw().to_string(), syn::Member::Unnamed(ref index) => format!("_{}", index.index), }; let key = ident.to_string(); diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index 76ded76..0879eff 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -4,6 +4,8 @@ use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::config::{Config, Language}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; use crate::bindgen::dependencies::Dependencies; @@ -126,7 +128,7 @@ impl EnumVariant { res.push(Field { name: inline_name.map_or_else( || match field.ident { - Some(ref ident) => ident.to_string(), + Some(ref ident) => ident.unraw().to_string(), None => i.to_string(), }, |name| name.to_string(), @@ -152,7 +154,10 @@ impl EnumVariant { syn::Fields::Named(ref fields) => { let path = Path::new(format!("{}_Body", variant.ident)); let name = RenameRule::SnakeCase - .apply(&variant.ident.to_string(), IdentifierType::StructMember) + .apply( + &variant.ident.unraw().to_string(), + IdentifierType::StructMember, + ) .into_owned(); VariantBody::Body { body: Struct::new( @@ -175,7 +180,10 @@ impl EnumVariant { syn::Fields::Unnamed(ref fields) => { let path = Path::new(format!("{}_Body", variant.ident)); let name = RenameRule::SnakeCase - .apply(&variant.ident.to_string(), IdentifierType::StructMember) + .apply( + &variant.ident.unraw().to_string(), + IdentifierType::StructMember, + ) .into_owned(); let inline_casts = fields.unnamed.len() == 1; // In C++ types with destructors cannot be put into unnamed structs like the @@ -206,7 +214,7 @@ impl EnumVariant { }; Ok(EnumVariant::new( - variant.ident.to_string(), + variant.ident.unraw().to_string(), discriminant, body, variant_cfg, @@ -362,7 +370,7 @@ impl Enum { return Err("Enum is marked with #[repr(align(...))] or #[repr(packed)].".to_owned()); } - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); let generic_params = GenericParams::new(&item.generics); let mut variants = Vec::new(); diff --git a/src/bindgen/ir/field.rs b/src/bindgen/ir/field.rs index b8a8425..6e132bf 100644 --- a/src/bindgen/ir/field.rs +++ b/src/bindgen/ir/field.rs @@ -1,5 +1,7 @@ use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::cdecl; use crate::bindgen::config::{Config, Language}; use crate::bindgen::ir::{AnnotationSet, Cfg, ConditionWrite}; @@ -34,6 +36,7 @@ impl Field { .ident .as_ref() .ok_or_else(|| "field is missing identifier".to_string())? + .unraw() .to_string(), ty, cfg: Cfg::load(&field.attrs), diff --git a/src/bindgen/ir/function.rs b/src/bindgen/ir/function.rs index e8d570d..840e3d3 100644 --- a/src/bindgen/ir/function.rs +++ b/src/bindgen/ir/function.rs @@ -5,6 +5,8 @@ use std::collections::HashMap; use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::cdecl; use crate::bindgen::config::{Config, Language, Layout}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; @@ -369,7 +371,9 @@ impl SynFnArgHelpers for syn::FnArg { }) => { let name = match **pat { syn::Pat::Wild(..) => None, - syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => Some(ident.to_string()), + syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => { + Some(ident.unraw().to_string()) + } _ => { return Err(format!( "Parameter has an unsupported argument name: {:?}", diff --git a/src/bindgen/ir/generic_path.rs b/src/bindgen/ir/generic_path.rs index ebb957c..bb4522f 100644 --- a/src/bindgen/ir/generic_path.rs +++ b/src/bindgen/ir/generic_path.rs @@ -1,6 +1,8 @@ use std::io::Write; use std::ops::Deref; +use syn::ext::IdentExt; + use crate::bindgen::config::{Config, Language}; use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver}; use crate::bindgen::ir::{Path, Type}; @@ -18,7 +20,7 @@ impl GenericParams { .iter() .filter_map(|x| match *x { syn::GenericParam::Type(syn::TypeParam { ref ident, .. }) => { - Some(Path::new(ident.to_string())) + Some(Path::new(ident.unraw().to_string())) } _ => None, }) @@ -141,7 +143,7 @@ impl GenericPath { path ); let last_segment = path.segments.last().unwrap(); - let name = last_segment.ident.to_string(); + let name = last_segment.ident.unraw().to_string(); let path = Path::new(name); let phantom_data_path = Path::new("PhantomData"); diff --git a/src/bindgen/ir/global.rs b/src/bindgen/ir/global.rs index a9d989d..47c9960 100644 --- a/src/bindgen/ir/global.rs +++ b/src/bindgen/ir/global.rs @@ -4,6 +4,8 @@ use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::cdecl; use crate::bindgen::config::Config; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; @@ -32,7 +34,7 @@ impl Static { } Ok(Static::new( - Path::new(item.ident.to_string()), + Path::new(item.ident.unraw().to_string()), ty.unwrap(), item.mutability.is_some(), Cfg::append(mod_cfg, Cfg::load(&item.attrs)), diff --git a/src/bindgen/ir/repr.rs b/src/bindgen/ir/repr.rs index acf3541..9713201 100644 --- a/src/bindgen/ir/repr.rs +++ b/src/bindgen/ir/repr.rs @@ -2,6 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use syn::ext::IdentExt; + use crate::bindgen::ir::ty::{IntKind, PrimitiveType}; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -62,12 +64,13 @@ impl Repr { }) .flatten() .filter_map(|meta| match meta { - syn::NestedMeta::Meta(syn::Meta::Path(path)) => { - Some((path.segments.first().unwrap().ident.to_string(), None)) - } + syn::NestedMeta::Meta(syn::Meta::Path(path)) => Some(( + path.segments.first().unwrap().ident.unraw().to_string(), + None, + )), syn::NestedMeta::Meta(syn::Meta::List(syn::MetaList { path, nested, .. })) => { Some(( - path.segments.first().unwrap().ident.to_string(), + path.segments.first().unwrap().ident.unraw().to_string(), Some( nested .iter() diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index 206f04c..12f43a2 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -4,6 +4,8 @@ use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::config::{Config, Language, LayoutConfig}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; use crate::bindgen::dependencies::Dependencies; @@ -62,7 +64,7 @@ impl Struct { } }; - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); // Ensure we can safely represent the struct given the configuration. if let Some(align) = repr.align { diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index d7e40c3..6f2ba67 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -5,6 +5,8 @@ use std::borrow::Cow; use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::cdecl; use crate::bindgen::config::{Config, Language}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; @@ -462,7 +464,7 @@ impl Type { format!("_{}", wildcard_counter - 1) } } else { - ident.to_string() + ident.unraw().to_string() } }), ty, diff --git a/src/bindgen/ir/typedef.rs b/src/bindgen/ir/typedef.rs index d7e02a3..da7cf73 100644 --- a/src/bindgen/ir/typedef.rs +++ b/src/bindgen/ir/typedef.rs @@ -5,6 +5,8 @@ use std::collections::HashMap; use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::config::{Config, Language}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; use crate::bindgen::dependencies::Dependencies; @@ -32,7 +34,7 @@ pub struct Typedef { impl Typedef { pub fn load(item: &syn::ItemType, mod_cfg: Option<&Cfg>) -> Result { if let Some(x) = Type::load(&item.ty)? { - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); Ok(Typedef::new( path, GenericParams::new(&item.generics), diff --git a/src/bindgen/ir/union.rs b/src/bindgen/ir/union.rs index 6254a99..ed77962 100644 --- a/src/bindgen/ir/union.rs +++ b/src/bindgen/ir/union.rs @@ -4,6 +4,8 @@ use std::io::Write; +use syn::ext::IdentExt; + use crate::bindgen::config::{Config, Language, LayoutConfig}; use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; use crate::bindgen::dependencies::Dependencies; @@ -47,7 +49,7 @@ impl Union { layout_config.ensure_safe_to_represent(&align)?; } - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); let (fields, tuple_union) = { let out = item diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index c2926f1..0a61077 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -8,6 +8,8 @@ use std::fs::File; use std::io::Read; use std::path::{Path as FilePath, PathBuf as FilePathBuf}; +use syn::ext::IdentExt; + use crate::bindgen::bitflags; use crate::bindgen::cargo::{Cargo, PackageRef}; use crate::bindgen::config::{Config, ParseConfig}; @@ -308,7 +310,7 @@ impl<'a> Parser<'a> { ); for item in nested_modules { - let next_mod_name = item.ident.to_string(); + let next_mod_name = item.ident.unraw().to_string(); let cfg = Cfg::load(&item.attrs); if let Some(ref cfg) = cfg { self.cfg_stack.push(cfg.clone()); @@ -536,7 +538,7 @@ impl Parse { binding_crate_name, crate_name, mod_cfg, - &Path::new(type_name.to_string()), + &Path::new(type_name.unraw().to_string()), method, ) } @@ -604,7 +606,7 @@ impl Parse { ); return; } - let path = Path::new(function.sig.ident.to_string()); + let path = Path::new(function.sig.ident.unraw().to_string()); match Function::load(path, None, &function.sig, true, &function.attrs, mod_cfg) { Ok(func) => { info!("Take {}::{}.", crate_name, &function.sig.ident); @@ -697,7 +699,7 @@ impl Parse { if let Some(ref self_type) = self_type { items.push(self_type.to_string()); } - items.push(sig.ident.to_string()); + items.push(sig.ident.unraw().to_string()); items.join("::") }; @@ -795,7 +797,7 @@ impl Parse { return; } - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); match Constant::load( path, mod_cfg, @@ -853,7 +855,7 @@ impl Parse { return; } - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); match Constant::load(path, mod_cfg, &item.ty, &item.expr, &item.attrs, None) { Ok(constant) => { info!("Take {}::{}.", crate_name, &item.ident); @@ -929,7 +931,7 @@ impl Parse { } Err(msg) => { info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg); - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); self.opaque_items.try_insert( OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(), ); @@ -953,7 +955,7 @@ impl Parse { } Err(msg) => { info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg); - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); self.opaque_items.try_insert( OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(), ); @@ -976,7 +978,7 @@ impl Parse { } Err(msg) => { info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg); - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); self.opaque_items.try_insert( OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(), ); @@ -994,7 +996,7 @@ impl Parse { } Err(msg) => { info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg); - let path = Path::new(item.ident.to_string()); + let path = Path::new(item.ident.unraw().to_string()); self.opaque_items.try_insert( OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(), ); @@ -1010,7 +1012,7 @@ impl Parse { item: &syn::ItemMacro, ) { let name = match item.mac.path.segments.last() { - Some(n) => n.ident.to_string(), + Some(n) => n.ident.unraw().to_string(), None => return, }; diff --git a/src/bindgen/utilities.rs b/src/bindgen/utilities.rs index 0800800..ae3ee60 100644 --- a/src/bindgen/utilities.rs +++ b/src/bindgen/utilities.rs @@ -4,6 +4,8 @@ #![allow(clippy::redundant_closure_call)] +use syn::ext::IdentExt; + pub trait IterHelpers: Iterator { fn try_skip_map(&mut self, f: F) -> Result, E> where @@ -38,7 +40,7 @@ impl SynItemFnHelpers for syn::ItemFn { .attr_name_value_lookup("export_name") .or_else(|| { if self.is_no_mangle() { - Some(self.sig.ident.to_string()) + Some(self.sig.ident.unraw().to_string()) } else { None } @@ -52,7 +54,7 @@ impl SynItemFnHelpers for syn::ImplItemMethod { .attr_name_value_lookup("export_name") .or_else(|| { if self.is_no_mangle() { - Some(self.sig.ident.to_string()) + Some(self.sig.ident.unraw().to_string()) } else { None } diff --git a/tests/expectations/raw_ident.both.c b/tests/expectations/raw_ident.both.c new file mode 100644 index 0000000..fc302c3 --- /dev/null +++ b/tests/expectations/raw_ident.both.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +enum Enum { + a, + b, +}; +typedef uint8_t Enum; + +typedef struct Struct { + Enum field; +} Struct; + +extern const Enum STATIC; + +void fn(struct Struct arg); diff --git a/tests/expectations/raw_ident.both.compat.c b/tests/expectations/raw_ident.both.compat.c new file mode 100644 index 0000000..fcc7cdd --- /dev/null +++ b/tests/expectations/raw_ident.both.compat.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +enum Enum +#ifdef __cplusplus + : uint8_t +#endif // __cplusplus + { + a, + b, +}; +#ifndef __cplusplus +typedef uint8_t Enum; +#endif // __cplusplus + +typedef struct Struct { + Enum field; +} Struct; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +extern const Enum STATIC; + +void fn(struct Struct arg); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/raw_ident.c b/tests/expectations/raw_ident.c new file mode 100644 index 0000000..4b6ccd9 --- /dev/null +++ b/tests/expectations/raw_ident.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +enum Enum { + a, + b, +}; +typedef uint8_t Enum; + +typedef struct { + Enum field; +} Struct; + +extern const Enum STATIC; + +void fn(Struct arg); diff --git a/tests/expectations/raw_ident.compat.c b/tests/expectations/raw_ident.compat.c new file mode 100644 index 0000000..6ae0663 --- /dev/null +++ b/tests/expectations/raw_ident.compat.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +enum Enum +#ifdef __cplusplus + : uint8_t +#endif // __cplusplus + { + a, + b, +}; +#ifndef __cplusplus +typedef uint8_t Enum; +#endif // __cplusplus + +typedef struct { + Enum field; +} Struct; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +extern const Enum STATIC; + +void fn(Struct arg); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/raw_ident.cpp b/tests/expectations/raw_ident.cpp new file mode 100644 index 0000000..60b8e02 --- /dev/null +++ b/tests/expectations/raw_ident.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +enum class Enum : uint8_t { + a, + b, +}; + +struct Struct { + Enum field; +}; + +extern "C" { + +extern const Enum STATIC; + +void fn(Struct arg); + +} // extern "C" diff --git a/tests/expectations/raw_ident.pyx b/tests/expectations/raw_ident.pyx new file mode 100644 index 0000000..1c194b6 --- /dev/null +++ b/tests/expectations/raw_ident.pyx @@ -0,0 +1,19 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + cdef enum: + a, + b, + ctypedef uint8_t Enum; + + ctypedef struct Struct: + Enum field; + + extern const Enum STATIC; + + void fn(Struct arg); diff --git a/tests/expectations/raw_ident.tag.c b/tests/expectations/raw_ident.tag.c new file mode 100644 index 0000000..ae77cf0 --- /dev/null +++ b/tests/expectations/raw_ident.tag.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +enum Enum { + a, + b, +}; +typedef uint8_t Enum; + +struct Struct { + Enum field; +}; + +extern const Enum STATIC; + +void fn(struct Struct arg); diff --git a/tests/expectations/raw_ident.tag.compat.c b/tests/expectations/raw_ident.tag.compat.c new file mode 100644 index 0000000..ad38cc7 --- /dev/null +++ b/tests/expectations/raw_ident.tag.compat.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +enum Enum +#ifdef __cplusplus + : uint8_t +#endif // __cplusplus + { + a, + b, +}; +#ifndef __cplusplus +typedef uint8_t Enum; +#endif // __cplusplus + +struct Struct { + Enum field; +}; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +extern const Enum STATIC; + +void fn(struct Struct arg); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/raw_ident.tag.pyx b/tests/expectations/raw_ident.tag.pyx new file mode 100644 index 0000000..8b11c90 --- /dev/null +++ b/tests/expectations/raw_ident.tag.pyx @@ -0,0 +1,19 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + cdef enum: + a, + b, + ctypedef uint8_t Enum; + + cdef struct Struct: + Enum field; + + extern const Enum STATIC; + + void fn(Struct arg); diff --git a/tests/rust/raw_ident.rs b/tests/rust/raw_ident.rs new file mode 100644 index 0000000..d3ceb56 --- /dev/null +++ b/tests/rust/raw_ident.rs @@ -0,0 +1,20 @@ +#[repr(u8)] +pub enum r#Enum { + r#a, + r#b, +} + +#[repr(C)] +pub struct r#Struct { + r#field: r#Enum, +} + +#[no_mangle] +pub extern "C" fn r#fn(r#arg: r#Struct) { + println!("Hello world"); +} + +pub mod r#mod { + #[no_mangle] + pub static r#STATIC: r#Enum = r#Enum::r#b; +}