From 1973738f60da44f6727a7f0ca47795c171fd86d1 Mon Sep 17 00:00:00 2001 From: David McNeil Date: Sun, 13 Sep 2020 23:35:04 -0500 Subject: [PATCH 1/2] Use raw identifier for reserved keywords Signed-off-by: David McNeil --- c2rust-bitfields-derive/src/lib.rs | 19 ++++++++- c2rust-bitfields/c2rust-tests/test_structs.rs | 38 +++++++++++++++++ c2rust-transpile/src/renamer.rs | 42 ++++++++++++++++++- .../src/translator/structs_unions.rs | 12 ++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/c2rust-bitfields-derive/src/lib.rs b/c2rust-bitfields-derive/src/lib.rs index 0eda1a762e..e40481a060 100644 --- a/c2rust-bitfields-derive/src/lib.rs +++ b/c2rust-bitfields-derive/src/lib.rs @@ -187,7 +187,10 @@ fn bitfield_struct_impl(struct_item: ItemStruct) -> Result { let field_types_setter_arg = &field_types; let method_names: Vec<_> = bitfields .iter() - .map(|field| Ident::new(&field.name, Span::call_site())) + .map(|field| { + // We cannot use `Ident::new` here because it does not support raw identifiers + quote::format_ident!("{}", field.name, span = Span::call_site()) + }) .collect(); let field_names: Vec<_> = bitfields.iter().map(|field| &field.field_name).collect(); let field_names_setters = &field_names; @@ -196,7 +199,9 @@ fn bitfield_struct_impl(struct_item: ItemStruct) -> Result { .iter() .map(|field_ident| { let span = Span::call_site(); - let setter_name = &format!("set_{}", field_ident); + let raw_ident = &field_ident.to_string(); + let ident = strip_raw_ident(&raw_ident); + let setter_name = &format!("set_{}", ident); Ident::new(setter_name, span) }) @@ -257,3 +262,13 @@ fn bitfield_struct_impl(struct_item: ItemStruct) -> Result { Ok(q.into()) } + +fn strip_raw_ident(ident: &str) -> &str { + const RAW_IDENT_PREFIX: &str = "r#"; + + if ident.starts_with(RAW_IDENT_PREFIX) { + &ident[RAW_IDENT_PREFIX.len()..] + } else { + ident + } +} diff --git a/c2rust-bitfields/c2rust-tests/test_structs.rs b/c2rust-bitfields/c2rust-tests/test_structs.rs index 1f6b13d8ac..5f071fa785 100644 --- a/c2rust-bitfields/c2rust-tests/test_structs.rs +++ b/c2rust-bitfields/c2rust-tests/test_structs.rs @@ -620,3 +620,41 @@ fn test_bool_bits() { assert!(bool_bits.y()); assert!(bool_bits.z()); } + +#[repr(C)] +#[derive(BitfieldStruct)] +struct ReservedNames { + #[bitfield(name = "r#use", ty = "bool", bits = "0..=0")] + #[bitfield(name = "r#as", ty = "bool", bits = "1..=1")] + use_as: [u8; 1], +} + +#[test] +fn test_reserved_names() { + let mut reserved_names = ReservedNames { + use_as: [u8::max_value(); 1], + }; + + assert!(reserved_names.r#use()); + assert!(reserved_names.r#as()); + + reserved_names.set_as(false); + + assert!(reserved_names.r#use()); + assert!(!reserved_names.r#as()); + + reserved_names.set_use(false); + + assert!(!reserved_names.r#use()); + assert!(!reserved_names.r#as()); + + reserved_names.set_use(true); + + assert!(reserved_names.r#use()); + assert!(!reserved_names.r#as()); + + reserved_names.set_as(true); + + assert!(reserved_names.r#use()); + assert!(reserved_names.r#as()); +} diff --git a/c2rust-transpile/src/renamer.rs b/c2rust-transpile/src/renamer.rs index a5ac6ca256..f2af86d650 100644 --- a/c2rust-transpile/src/renamer.rs +++ b/c2rust-transpile/src/renamer.rs @@ -95,6 +95,14 @@ pub const RUST_KEYWORDS: &[&str] = &[ "yield", ]; +/// Keywords that cannot be expressed as a raw identifier [0] because they can be used as path +/// segments. The list of path segment keywords can be found here [1]. For discussion of this +/// topic see [2]. +/// [0] https://doc.rust-lang.org/edition-guide/rust-2018/module-system/raw-identifiers.html +/// [1] https://github.com/rust-lang/rust/blob/04488afe34512aa4c33566eb16d8c912a3ae04f9/src/librustc_span/symbol.rs#L1331 +/// [2] https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094 +const RESERVED_PATH_SEGMENT_NAMES: [&str; 4] = ["super", "self", "Self", "crate"]; + const PRELUDE_TYPE_NAMESPACE: &[&str] = &[ "Copy", "Send", @@ -224,7 +232,8 @@ impl Renamer { /// Assigns a name that doesn't collide with anything in the context of a particular /// scope, defaulting to the current scope if None is provided fn pick_name_in_scope(&mut self, basename: &str, scope: Option) -> String { - let mut target = basename.to_string(); + let mut target = + Self::raw_identifier_if_reserved_name(basename).unwrap_or_else(|| basename.to_string()); for i in 0.. { if self.is_target_used(&target) { @@ -316,6 +325,14 @@ impl Renamer { self.next_fresh += 1; self.pick_name(&format!("c2rust_fresh{fresh}")) } + + fn raw_identifier_if_reserved_name(basename: &str) -> Option { + if RUST_KEYWORDS.contains(&basename) && !RESERVED_PATH_SEGMENT_NAMES.contains(&basename) { + Some(format!("r#{}", basename)) + } else { + None + } + } } fn check_c2rust_name(basename: &str) { @@ -370,4 +387,27 @@ mod tests { renamer.drop_scope(); assert_eq!(renamer.get(&1), None); } + + #[test] + fn raw_identifier() { + let mut renamer = Renamer::new(&[RUST_KEYWORDS]); + + // A reserved keyword that can be expressed as a raw identifier + let reserved1 = renamer.insert(1, "dyn").unwrap(); + let reserved2 = renamer.get(&1).unwrap(); + assert_eq!(reserved1, "r#dyn"); + assert_eq!(reserved2, "r#dyn"); + + // A reserved keyword that is already bound and therefore does not need the "#r" prefix + let reserved1 = renamer.insert(2, "dyn").unwrap(); + let reserved2 = renamer.get(&2).unwrap(); + assert_eq!(reserved1, "dyn_0"); + assert_eq!(reserved2, "dyn_0"); + + // A reserved that cannot be used as a raw identifier because it can be used in a path + let reserved1 = renamer.insert(3, "self").unwrap(); + let reserved2 = renamer.get(&3).unwrap(); + assert_eq!(reserved1, "self_0"); + assert_eq!(reserved2, "self_0"); + } } diff --git a/c2rust-transpile/src/translator/structs_unions.rs b/c2rust-transpile/src/translator/structs_unions.rs index db21177a4d..0569504f56 100644 --- a/c2rust-transpile/src/translator/structs_unions.rs +++ b/c2rust-transpile/src/translator/structs_unions.rs @@ -527,6 +527,7 @@ impl<'a> Translation<'a> { // Now we must use the bitfield methods to initialize bitfields for (field_name, val) in bitfield_inits { + let field_name = strip_raw_ident(&field_name); let field_name_setter = format!("set_{}", field_name); let struct_ident = mk().ident_expr("init"); is_unsafe |= val.is_unsafe(); @@ -719,6 +720,7 @@ impl<'a> Translation<'a> { .borrow() .resolve_field_name(None, field_id) .ok_or("Could not find bitfield name")?; + let field_name = strip_raw_ident(&field_name); let setter_name = format!("set_{}", field_name); let lhs_expr_read = mk().method_call_expr(lhs_expr.clone(), field_name, Vec::new()); // Allow the value of this assignment to be used as the RHS of other assignments @@ -1116,3 +1118,13 @@ fn contains_block(expr_kind: &Expr) -> bool { _ => false, } } + +fn strip_raw_ident(ident: &str) -> &str { + const RAW_IDENT_PREFIX: &str = "r#"; + + if ident.starts_with(RAW_IDENT_PREFIX) { + &ident[RAW_IDENT_PREFIX.len()..] + } else { + ident + } +} From b825ba853ffbb1d62ccd66ec3499c19eb5795844 Mon Sep 17 00:00:00 2001 From: cydparser Date: Fri, 22 May 2026 21:55:31 -0700 Subject: [PATCH 2/2] Use raw identifier for reserved keywords (continued) --- Cargo.lock | 1 + c2rust-ast-builder/Cargo.toml | 1 + c2rust-ast-builder/src/builder.rs | 11 ++- c2rust-bitfields-derive/src/lib.rs | 12 +--- c2rust-transpile/src/renamer.rs | 12 +++- .../src/translator/structs_unions.rs | 39 ++++++----- c2rust-transpile/tests/snapshots.rs | 5 ++ c2rust-transpile/tests/snapshots/bitfields.c | 5 ++ .../tests/snapshots/raw_keywords.c | 30 ++++++++ ...s__transpile@bitfields.c.2021.clang15.snap | 19 ++++-- ...s__transpile@bitfields.c.2024.clang15.snap | 19 ++++-- ...pshots__transpile@bool.c.2021.clang15.snap | 16 ++--- ...pshots__transpile@bool.c.2024.clang15.snap | 16 ++--- ...ts__transpile@keywords.c.2021.clang15.snap | 68 +++++++++---------- ...ts__transpile@keywords.c.2024.clang15.snap | 68 +++++++++---------- ...hots__transpile@macros.c.2021.clang15.snap | 18 ++--- ...hots__transpile@macros.c.2024.clang15.snap | 18 ++--- ...transpile@raw_keywords.c.2021.clang15.snap | 42 ++++++++++++ ...transpile@raw_keywords.c.2024.clang15.snap | 43 ++++++++++++ ..._transpile@scalar_init.c.2021.clang15.snap | 4 +- ..._transpile@scalar_init.c.2024.clang15.snap | 4 +- 21 files changed, 304 insertions(+), 147 deletions(-) create mode 100644 c2rust-transpile/tests/snapshots/raw_keywords.c create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2021.clang15.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2024.clang15.snap diff --git a/Cargo.lock b/Cargo.lock index 54a8720e21..8d1ac65635 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,6 +335,7 @@ version = "0.22.1" dependencies = [ "itertools 0.10.5", "proc-macro2", + "quote", "syn 2.0.106", ] diff --git a/c2rust-ast-builder/Cargo.toml b/c2rust-ast-builder/Cargo.toml index df2e9e5bbb..81a47c98d6 100644 --- a/c2rust-ast-builder/Cargo.toml +++ b/c2rust-ast-builder/Cargo.toml @@ -14,4 +14,5 @@ categories.workspace = true [dependencies] itertools = "0.10.0" proc-macro2 = { version = "1.0", features = ["span-locations"]} +quote = "1" syn = { version = "2.0", features = ["full", "extra-traits", "printing", "clone-impls"]} diff --git a/c2rust-ast-builder/src/builder.rs b/c2rust-ast-builder/src/builder.rs index 5a1878d4f8..526bbd45ad 100644 --- a/c2rust-ast-builder/src/builder.rs +++ b/c2rust-ast-builder/src/builder.rs @@ -180,6 +180,11 @@ where intersperse(items, comma).collect() } +fn str_to_ident(s: &str, span: Span) -> Ident { + // NB: We cannot use `Ident::new` here because it does not support raw identifiers. + quote::format_ident!("{}", s, span = span) +} + pub trait Make { fn make(self, mk: &Builder) -> T; } @@ -192,19 +197,19 @@ impl Make for T { impl Make for &str { fn make(self, mk: &Builder) -> Ident { - Ident::new(self, mk.span) + str_to_ident(self, mk.span) } } impl Make for String { fn make(self, mk: &Builder) -> Ident { - Ident::new(&self, mk.span) + str_to_ident(self.as_str(), mk.span) } } impl Make for &String { fn make(self, mk: &Builder) -> Ident { - Ident::new(self, mk.span) + str_to_ident(self.as_str(), mk.span) } } diff --git a/c2rust-bitfields-derive/src/lib.rs b/c2rust-bitfields-derive/src/lib.rs index e40481a060..e18061fe8f 100644 --- a/c2rust-bitfields-derive/src/lib.rs +++ b/c2rust-bitfields-derive/src/lib.rs @@ -200,7 +200,7 @@ fn bitfield_struct_impl(struct_item: ItemStruct) -> Result { .map(|field_ident| { let span = Span::call_site(); let raw_ident = &field_ident.to_string(); - let ident = strip_raw_ident(&raw_ident); + let ident = raw_ident.strip_prefix("r#").unwrap_or(raw_ident); let setter_name = &format!("set_{}", ident); Ident::new(setter_name, span) @@ -262,13 +262,3 @@ fn bitfield_struct_impl(struct_item: ItemStruct) -> Result { Ok(q.into()) } - -fn strip_raw_ident(ident: &str) -> &str { - const RAW_IDENT_PREFIX: &str = "r#"; - - if ident.starts_with(RAW_IDENT_PREFIX) { - &ident[RAW_IDENT_PREFIX.len()..] - } else { - ident - } -} diff --git a/c2rust-transpile/src/renamer.rs b/c2rust-transpile/src/renamer.rs index f2af86d650..1fcd14f436 100644 --- a/c2rust-transpile/src/renamer.rs +++ b/c2rust-transpile/src/renamer.rs @@ -99,9 +99,15 @@ pub const RUST_KEYWORDS: &[&str] = &[ /// segments. The list of path segment keywords can be found here [1]. For discussion of this /// topic see [2]. /// [0] https://doc.rust-lang.org/edition-guide/rust-2018/module-system/raw-identifiers.html -/// [1] https://github.com/rust-lang/rust/blob/04488afe34512aa4c33566eb16d8c912a3ae04f9/src/librustc_span/symbol.rs#L1331 +/// [1] https://github.com/rust-lang/rust/blob/e96c36b6f76833388c519561d145492d2c08db4e/compiler/rustc_span/src/symbol.rs#L2892 /// [2] https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094 -const RESERVED_PATH_SEGMENT_NAMES: [&str; 4] = ["super", "self", "Self", "crate"]; +#[rustfmt::skip] +const PATH_SEGMENT_KEYWORDS: [&str; 4] = [ + "super", + "self", + "Self", + "crate", +]; const PRELUDE_TYPE_NAMESPACE: &[&str] = &[ "Copy", @@ -327,7 +333,7 @@ impl Renamer { } fn raw_identifier_if_reserved_name(basename: &str) -> Option { - if RUST_KEYWORDS.contains(&basename) && !RESERVED_PATH_SEGMENT_NAMES.contains(&basename) { + if RUST_KEYWORDS.contains(&basename) && !PATH_SEGMENT_KEYWORDS.contains(&basename) { Some(format!("r#{}", basename)) } else { None diff --git a/c2rust-transpile/src/translator/structs_unions.rs b/c2rust-transpile/src/translator/structs_unions.rs index 0569504f56..ae84f05d7b 100644 --- a/c2rust-transpile/src/translator/structs_unions.rs +++ b/c2rust-transpile/src/translator/structs_unions.rs @@ -283,6 +283,7 @@ impl<'a> Translation<'a> { field_name, bytes, attrs, + .. } => { let ty = mk().array_ty( mk().ident_ty("u8"), @@ -527,8 +528,8 @@ impl<'a> Translation<'a> { // Now we must use the bitfield methods to initialize bitfields for (field_name, val) in bitfield_inits { - let field_name = strip_raw_ident(&field_name); - let field_name_setter = format!("set_{}", field_name); + let plain_field_name = field_name.strip_prefix("r#").unwrap_or(&field_name); + let field_name_setter = format!("set_{}", plain_field_name); let struct_ident = mk().ident_expr("init"); is_unsafe |= val.is_unsafe(); let val = val @@ -720,8 +721,8 @@ impl<'a> Translation<'a> { .borrow() .resolve_field_name(None, field_id) .ok_or("Could not find bitfield name")?; - let field_name = strip_raw_ident(&field_name); - let setter_name = format!("set_{}", field_name); + let plain_field_name = field_name.strip_prefix("r#").unwrap_or(&field_name); + let setter_name = format!("set_{}", plain_field_name); let lhs_expr_read = mk().method_call_expr(lhs_expr.clone(), field_name, Vec::new()); // Allow the value of this assignment to be used as the RHS of other assignments let val = lhs_expr_read.clone(); @@ -906,11 +907,13 @@ impl<'a> Translation<'a> { Some(FieldType::BitfieldGroup { start_bit, field_name: ref mut name, + ref mut single_bitfield, ref mut bytes, ref mut attrs, }) => { + *single_bitfield = false; name.push('_'); - name.push_str(&field_name); + name.push_str(field_name.strip_prefix("r#").unwrap_or(&field_name)); let end_bit = platform_bit_offset + bitfield_width; @@ -953,6 +956,7 @@ impl<'a> Translation<'a> { last_bitfield_group = Some(FieldType::BitfieldGroup { start_bit: platform_bit_offset, field_name, + single_bitfield: true, bytes, attrs, }); @@ -964,7 +968,19 @@ impl<'a> Translation<'a> { } // Find leftover bitfield group at end: it's all set - if let Some(field_group) = last_bitfield_group.take() { + if let Some(mut field_group) = last_bitfield_group.take() { + if let FieldType::BitfieldGroup { + ref mut field_name, + single_bitfield, + .. + } = field_group + { + if !single_bitfield { + if let Some(s) = field_name.strip_prefix("r#") { + *field_name = s.to_string(); + } + } + }; reorganized_fields.push(field_group); // Packed structs can cause platform_byte_size < next_byte_pos @@ -1087,6 +1103,7 @@ enum FieldType { BitfieldGroup { start_bit: u64, field_name: String, + single_bitfield: bool, bytes: u64, attrs: Vec<(String, Box, String)>, }, // 64 bytes @@ -1118,13 +1135,3 @@ fn contains_block(expr_kind: &Expr) -> bool { _ => false, } } - -fn strip_raw_ident(ident: &str) -> &str { - const RAW_IDENT_PREFIX: &str = "r#"; - - if ident.starts_with(RAW_IDENT_PREFIX) { - &ident[RAW_IDENT_PREFIX.len()..] - } else { - ident - } -} diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 4f10e330ea..af53aa7b1d 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -425,6 +425,11 @@ fn test_predefined() { transpile("predefined.c").run(); } +#[test] +fn test_raw_keywords() { + transpile("raw_keywords.c").run(); +} + #[test] fn test_records() { transpile("records.c").run(); diff --git a/c2rust-transpile/tests/snapshots/bitfields.c b/c2rust-transpile/tests/snapshots/bitfields.c index fba8ac6f99..e8e5e63f62 100644 --- a/c2rust-transpile/tests/snapshots/bitfields.c +++ b/c2rust-transpile/tests/snapshots/bitfields.c @@ -8,9 +8,14 @@ struct PacketHeader { unsigned short length : 6; }; +struct SingleRawIdent { + unsigned char as : 3; +}; + void test_bitfields(void) { struct PacketHeader h = {0}; h.version = 5; h.sequence = 513; printf("version=%u sequence=%u\n", h.version, h.sequence); + struct SingleRawIdent s = {0}; } diff --git a/c2rust-transpile/tests/snapshots/raw_keywords.c b/c2rust-transpile/tests/snapshots/raw_keywords.c new file mode 100644 index 0000000000..57f6d3d1c3 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/raw_keywords.c @@ -0,0 +1,30 @@ +typedef int type; + +enum as { + async, + await +}; + +struct dyn { + type false +}; + +union fn { + type where; + type final; +}; + +type super = 0; + +void pub(type ref) { +} + +void impl(type in) { + type trait = super; + enum as let = async; + struct dyn mod; + mod.false = 0; + union fn mut; + mut.where = 0; + pub(mut.where); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2021.clang15.snap index eb1adbfec3..ac30154b90 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2021.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2021.clang15.snap @@ -20,21 +20,27 @@ pub struct PacketHeader { #[bitfield(padding)] pub c2rust_padding: [u8; 1], #[bitfield(name = "version", ty = "::core::ffi::c_uchar", bits = "0..=2")] - #[bitfield(name = "type_0", ty = "::core::ffi::c_uchar", bits = "3..=4")] + #[bitfield(name = "r#type", ty = "::core::ffi::c_uchar", bits = "3..=4")] #[bitfield(name = "flags", ty = "::core::ffi::c_uchar", bits = "5..=7")] #[bitfield(name = "sequence", ty = "::core::ffi::c_ushort", bits = "16..=25")] #[bitfield(name = "length", ty = "::core::ffi::c_ushort", bits = "26..=31")] - pub version_type_0_flags_sequence_length: [u8; 3], + pub version_type_flags_sequence_length: [u8; 3], +} +#[derive(Copy, Clone, ::c2rust_bitfields::BitfieldStruct)] +#[repr(C)] +pub struct SingleRawIdent { + #[bitfield(name = "r#as", ty = "::core::ffi::c_uchar", bits = "0..=2")] + pub r#as: [u8; 1], } #[no_mangle] pub unsafe extern "C" fn test_bitfields() { let mut h: PacketHeader = { let mut init = PacketHeader { c2rust_padding: [0; 1], - version_type_0_flags_sequence_length: [0; 3], + version_type_flags_sequence_length: [0; 3], }; init.set_version(0 as ::core::ffi::c_uchar); - init.set_type_0(0); + init.set_type(0); init.set_flags(0); init.set_sequence(0); init.set_length(0); @@ -47,4 +53,9 @@ pub unsafe extern "C" fn test_bitfields() { h.version() as ::core::ffi::c_int, h.sequence() as ::core::ffi::c_int, ); + let mut s: SingleRawIdent = { + let mut init = SingleRawIdent { r#as: [0; 1] }; + init.set_as(0 as ::core::ffi::c_uchar); + init + }; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2024.clang15.snap index 4344c3c56b..2d1de71963 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2024.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@bitfields.c.2024.clang15.snap @@ -21,21 +21,27 @@ pub struct PacketHeader { #[bitfield(padding)] pub c2rust_padding: [u8; 1], #[bitfield(name = "version", ty = "::core::ffi::c_uchar", bits = "0..=2")] - #[bitfield(name = "type_0", ty = "::core::ffi::c_uchar", bits = "3..=4")] + #[bitfield(name = "r#type", ty = "::core::ffi::c_uchar", bits = "3..=4")] #[bitfield(name = "flags", ty = "::core::ffi::c_uchar", bits = "5..=7")] #[bitfield(name = "sequence", ty = "::core::ffi::c_ushort", bits = "16..=25")] #[bitfield(name = "length", ty = "::core::ffi::c_ushort", bits = "26..=31")] - pub version_type_0_flags_sequence_length: [u8; 3], + pub version_type_flags_sequence_length: [u8; 3], +} +#[derive(Copy, Clone, ::c2rust_bitfields::BitfieldStruct)] +#[repr(C)] +pub struct SingleRawIdent { + #[bitfield(name = "r#as", ty = "::core::ffi::c_uchar", bits = "0..=2")] + pub r#as: [u8; 1], } #[unsafe(no_mangle)] pub unsafe extern "C" fn test_bitfields() { let mut h: PacketHeader = { let mut init = PacketHeader { c2rust_padding: [0; 1], - version_type_0_flags_sequence_length: [0; 3], + version_type_flags_sequence_length: [0; 3], }; init.set_version(0 as ::core::ffi::c_uchar); - init.set_type_0(0); + init.set_type(0); init.set_flags(0); init.set_sequence(0); init.set_length(0); @@ -48,4 +54,9 @@ pub unsafe extern "C" fn test_bitfields() { h.version() as ::core::ffi::c_int, h.sequence() as ::core::ffi::c_int, ); + let mut s: SingleRawIdent = { + let mut init = SingleRawIdent { r#as: [0; 1] }; + init.set_as(0 as ::core::ffi::c_uchar); + init + }; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2021.clang15.snap index fae8ea2428..46a5cc84cb 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2021.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2021.clang15.snap @@ -11,26 +11,26 @@ expression: cat tests/snapshots/bool.2021.clang15.rs unused_assignments, unused_mut )] -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; -pub const false_0: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#false: ::core::ffi::c_int = 0 as ::core::ffi::c_int; pub const MACRO_INT0: ::core::ffi::c_int = 0 as ::core::ffi::c_int; -pub const MACRO_IDENT0: ::core::ffi::c_int = false_0; +pub const MACRO_IDENT0: ::core::ffi::c_int = r#false; pub const MACRO_INT1: ::core::ffi::c_int = 1 as ::core::ffi::c_int; -pub const MACRO_IDENT1: ::core::ffi::c_int = true_0; +pub const MACRO_IDENT1: ::core::ffi::c_int = r#true; #[no_mangle] pub unsafe extern "C" fn test_bool() { let mut int0: bool = false; - let mut ident0: bool = false_0 != 0; + let mut ident0: bool = r#false != 0; let mut macro_int0: bool = MACRO_INT0 != 0; let mut macro_ident0: bool = MACRO_IDENT0 != 0; let mut int1: bool = true; - let mut ident1: bool = true_0 != 0; + let mut ident1: bool = r#true != 0; let mut macro_int1: bool = MACRO_INT1 != 0; let mut macro_ident1: bool = MACRO_IDENT1 != 0; if false { } else { }; - if false_0 != 0 { + if r#false != 0 { } else { }; if MACRO_INT0 != 0 { @@ -42,7 +42,7 @@ pub unsafe extern "C" fn test_bool() { if true { } else { }; - if true_0 != 0 { + if r#true != 0 { } else { }; if MACRO_INT1 != 0 { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2024.clang15.snap index f424193618..f770cddba2 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2024.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@bool.c.2024.clang15.snap @@ -12,26 +12,26 @@ expression: cat tests/snapshots/bool.2024.clang15.rs unused_assignments, unused_mut )] -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; -pub const false_0: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#false: ::core::ffi::c_int = 0 as ::core::ffi::c_int; pub const MACRO_INT0: ::core::ffi::c_int = 0 as ::core::ffi::c_int; -pub const MACRO_IDENT0: ::core::ffi::c_int = false_0; +pub const MACRO_IDENT0: ::core::ffi::c_int = r#false; pub const MACRO_INT1: ::core::ffi::c_int = 1 as ::core::ffi::c_int; -pub const MACRO_IDENT1: ::core::ffi::c_int = true_0; +pub const MACRO_IDENT1: ::core::ffi::c_int = r#true; #[unsafe(no_mangle)] pub unsafe extern "C" fn test_bool() { let mut int0: bool = false; - let mut ident0: bool = false_0 != 0; + let mut ident0: bool = r#false != 0; let mut macro_int0: bool = MACRO_INT0 != 0; let mut macro_ident0: bool = MACRO_IDENT0 != 0; let mut int1: bool = true; - let mut ident1: bool = true_0 != 0; + let mut ident1: bool = r#true != 0; let mut macro_int1: bool = MACRO_INT1 != 0; let mut macro_ident1: bool = MACRO_IDENT1 != 0; if false { } else { }; - if false_0 != 0 { + if r#false != 0 { } else { }; if MACRO_INT0 != 0 { @@ -43,7 +43,7 @@ pub unsafe extern "C" fn test_bool() { if true { } else { }; - if true_0 != 0 { + if r#true != 0 { } else { }; if MACRO_INT1 != 0 { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.clang15.snap index 3580518f19..0fc991d179 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.clang15.snap @@ -12,39 +12,39 @@ expression: cat tests/snapshots/keywords.2021.clang15.rs unused_mut )] #[export_name = "as"] -pub unsafe extern "C" fn as_0() {} +pub unsafe extern "C" fn r#as() {} #[export_name = "async"] -pub unsafe extern "C" fn async_0() {} +pub unsafe extern "C" fn r#async() {} #[export_name = "await"] -pub unsafe extern "C" fn await_0() {} +pub unsafe extern "C" fn r#await() {} #[export_name = "crate"] pub unsafe extern "C" fn crate_0() {} #[export_name = "dyn"] -pub unsafe extern "C" fn dyn_0() {} +pub unsafe extern "C" fn r#dyn() {} #[export_name = "false"] -pub unsafe extern "C" fn false_0() {} +pub unsafe extern "C" fn r#false() {} #[export_name = "fn"] -pub unsafe extern "C" fn fn_0() {} +pub unsafe extern "C" fn r#fn() {} #[export_name = "impl"] -pub unsafe extern "C" fn impl_0() {} +pub unsafe extern "C" fn r#impl() {} #[export_name = "in"] -pub unsafe extern "C" fn in_0() {} +pub unsafe extern "C" fn r#in() {} #[export_name = "let"] -pub unsafe extern "C" fn let_0() {} +pub unsafe extern "C" fn r#let() {} #[export_name = "loop"] -pub unsafe extern "C" fn loop_0() {} +pub unsafe extern "C" fn r#loop() {} #[export_name = "match"] -pub unsafe extern "C" fn match_0() {} +pub unsafe extern "C" fn r#match() {} #[export_name = "mod"] -pub unsafe extern "C" fn mod_0() {} +pub unsafe extern "C" fn r#mod() {} #[export_name = "move"] -pub unsafe extern "C" fn move_0() {} +pub unsafe extern "C" fn r#move() {} #[export_name = "mut"] -pub unsafe extern "C" fn mut_0() {} +pub unsafe extern "C" fn r#mut() {} #[export_name = "pub"] -pub unsafe extern "C" fn pub_0() {} +pub unsafe extern "C" fn r#pub() {} #[export_name = "ref"] -pub unsafe extern "C" fn ref_0() {} +pub unsafe extern "C" fn r#ref() {} #[export_name = "self"] pub unsafe extern "C" fn self_0() {} #[export_name = "Self"] @@ -52,38 +52,38 @@ pub unsafe extern "C" fn Self_0() {} #[export_name = "super"] pub unsafe extern "C" fn super_0() {} #[export_name = "trait"] -pub unsafe extern "C" fn trait_0() {} +pub unsafe extern "C" fn r#trait() {} #[export_name = "true"] -pub unsafe extern "C" fn true_0() {} +pub unsafe extern "C" fn r#true() {} #[export_name = "type"] -pub unsafe extern "C" fn type_0() {} +pub unsafe extern "C" fn r#type() {} #[export_name = "unsafe"] -pub unsafe extern "C" fn unsafe_0() {} +pub unsafe extern "C" fn r#unsafe() {} #[export_name = "use"] -pub unsafe extern "C" fn use_0() {} +pub unsafe extern "C" fn r#use() {} #[export_name = "where"] -pub unsafe extern "C" fn where_0() {} +pub unsafe extern "C" fn r#where() {} #[export_name = "abstract"] -pub unsafe extern "C" fn abstract_0() {} +pub unsafe extern "C" fn r#abstract() {} #[export_name = "become"] -pub unsafe extern "C" fn become_0() {} +pub unsafe extern "C" fn r#become() {} #[export_name = "box"] -pub unsafe extern "C" fn box_0() {} +pub unsafe extern "C" fn r#box() {} #[export_name = "final"] -pub unsafe extern "C" fn final_0() {} +pub unsafe extern "C" fn r#final() {} #[export_name = "gen"] -pub unsafe extern "C" fn gen_0() {} +pub unsafe extern "C" fn r#gen() {} #[export_name = "macro"] -pub unsafe extern "C" fn macro_0() {} +pub unsafe extern "C" fn r#macro() {} #[export_name = "override"] -pub unsafe extern "C" fn override_0() {} +pub unsafe extern "C" fn r#override() {} #[export_name = "priv"] -pub unsafe extern "C" fn priv_0() {} +pub unsafe extern "C" fn r#priv() {} #[export_name = "try"] -pub unsafe extern "C" fn try_0() {} +pub unsafe extern "C" fn r#try() {} #[export_name = "unsized"] -pub unsafe extern "C" fn unsized_0() {} +pub unsafe extern "C" fn r#unsized() {} #[export_name = "virtual"] -pub unsafe extern "C" fn virtual_0() {} +pub unsafe extern "C" fn r#virtual() {} #[export_name = "yield"] -pub unsafe extern "C" fn yield_0() {} +pub unsafe extern "C" fn r#yield() {} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.clang15.snap index 2f2a15c54d..7d274851c6 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.clang15.snap @@ -13,39 +13,39 @@ expression: cat tests/snapshots/keywords.2024.clang15.rs unused_mut )] #[unsafe(export_name = "as")] -pub unsafe extern "C" fn as_0() {} +pub unsafe extern "C" fn r#as() {} #[unsafe(export_name = "async")] -pub unsafe extern "C" fn async_0() {} +pub unsafe extern "C" fn r#async() {} #[unsafe(export_name = "await")] -pub unsafe extern "C" fn await_0() {} +pub unsafe extern "C" fn r#await() {} #[unsafe(export_name = "crate")] pub unsafe extern "C" fn crate_0() {} #[unsafe(export_name = "dyn")] -pub unsafe extern "C" fn dyn_0() {} +pub unsafe extern "C" fn r#dyn() {} #[unsafe(export_name = "false")] -pub unsafe extern "C" fn false_0() {} +pub unsafe extern "C" fn r#false() {} #[unsafe(export_name = "fn")] -pub unsafe extern "C" fn fn_0() {} +pub unsafe extern "C" fn r#fn() {} #[unsafe(export_name = "impl")] -pub unsafe extern "C" fn impl_0() {} +pub unsafe extern "C" fn r#impl() {} #[unsafe(export_name = "in")] -pub unsafe extern "C" fn in_0() {} +pub unsafe extern "C" fn r#in() {} #[unsafe(export_name = "let")] -pub unsafe extern "C" fn let_0() {} +pub unsafe extern "C" fn r#let() {} #[unsafe(export_name = "loop")] -pub unsafe extern "C" fn loop_0() {} +pub unsafe extern "C" fn r#loop() {} #[unsafe(export_name = "match")] -pub unsafe extern "C" fn match_0() {} +pub unsafe extern "C" fn r#match() {} #[unsafe(export_name = "mod")] -pub unsafe extern "C" fn mod_0() {} +pub unsafe extern "C" fn r#mod() {} #[unsafe(export_name = "move")] -pub unsafe extern "C" fn move_0() {} +pub unsafe extern "C" fn r#move() {} #[unsafe(export_name = "mut")] -pub unsafe extern "C" fn mut_0() {} +pub unsafe extern "C" fn r#mut() {} #[unsafe(export_name = "pub")] -pub unsafe extern "C" fn pub_0() {} +pub unsafe extern "C" fn r#pub() {} #[unsafe(export_name = "ref")] -pub unsafe extern "C" fn ref_0() {} +pub unsafe extern "C" fn r#ref() {} #[unsafe(export_name = "self")] pub unsafe extern "C" fn self_0() {} #[unsafe(export_name = "Self")] @@ -53,38 +53,38 @@ pub unsafe extern "C" fn Self_0() {} #[unsafe(export_name = "super")] pub unsafe extern "C" fn super_0() {} #[unsafe(export_name = "trait")] -pub unsafe extern "C" fn trait_0() {} +pub unsafe extern "C" fn r#trait() {} #[unsafe(export_name = "true")] -pub unsafe extern "C" fn true_0() {} +pub unsafe extern "C" fn r#true() {} #[unsafe(export_name = "type")] -pub unsafe extern "C" fn type_0() {} +pub unsafe extern "C" fn r#type() {} #[unsafe(export_name = "unsafe")] -pub unsafe extern "C" fn unsafe_0() {} +pub unsafe extern "C" fn r#unsafe() {} #[unsafe(export_name = "use")] -pub unsafe extern "C" fn use_0() {} +pub unsafe extern "C" fn r#use() {} #[unsafe(export_name = "where")] -pub unsafe extern "C" fn where_0() {} +pub unsafe extern "C" fn r#where() {} #[unsafe(export_name = "abstract")] -pub unsafe extern "C" fn abstract_0() {} +pub unsafe extern "C" fn r#abstract() {} #[unsafe(export_name = "become")] -pub unsafe extern "C" fn become_0() {} +pub unsafe extern "C" fn r#become() {} #[unsafe(export_name = "box")] -pub unsafe extern "C" fn box_0() {} +pub unsafe extern "C" fn r#box() {} #[unsafe(export_name = "final")] -pub unsafe extern "C" fn final_0() {} +pub unsafe extern "C" fn r#final() {} #[unsafe(export_name = "gen")] -pub unsafe extern "C" fn gen_0() {} +pub unsafe extern "C" fn r#gen() {} #[unsafe(export_name = "macro")] -pub unsafe extern "C" fn macro_0() {} +pub unsafe extern "C" fn r#macro() {} #[unsafe(export_name = "override")] -pub unsafe extern "C" fn override_0() {} +pub unsafe extern "C" fn r#override() {} #[unsafe(export_name = "priv")] -pub unsafe extern "C" fn priv_0() {} +pub unsafe extern "C" fn r#priv() {} #[unsafe(export_name = "try")] -pub unsafe extern "C" fn try_0() {} +pub unsafe extern "C" fn r#try() {} #[unsafe(export_name = "unsized")] -pub unsafe extern "C" fn unsized_0() {} +pub unsafe extern "C" fn r#unsized() {} #[unsafe(export_name = "virtual")] -pub unsafe extern "C" fn virtual_0() {} +pub unsafe extern "C" fn r#virtual() {} #[unsafe(export_name = "yield")] -pub unsafe extern "C" fn yield_0() {} +pub unsafe extern "C" fn r#yield() {} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.clang15.snap index 5c48974186..bf40136c94 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.clang15.snap @@ -35,10 +35,10 @@ pub type zstd_platform_dependent_type = ::core::ffi::c_long; pub struct ntlmdata { pub target_info_len: ::core::ffi::c_uint, } -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; pub const UINTPTR_MAX: ::core::ffi::c_ulong = 18446744073709551615 as ::core::ffi::c_ulong; pub const LITERAL_INT: ::core::ffi::c_int = 0xffff as ::core::ffi::c_int; -pub const LITERAL_BOOL: ::core::ffi::c_int = true_0; +pub const LITERAL_BOOL: ::core::ffi::c_int = r#true; pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64; pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as ::core::ffi::c_int; pub const LITERAL_STR: [::core::ffi::c_char; 6] = @@ -52,7 +52,7 @@ pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT; pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR; pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR; pub const NESTED_STRUCT: S = LITERAL_STRUCT; -pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + true_0); +pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + r#true); pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe { LITERAL_STR .as_ptr() @@ -91,7 +91,7 @@ pub unsafe extern "C" fn local_muts() { let mut int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; let mut mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut parens: ::core::ffi::c_int = PARENS; let mut ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; @@ -124,7 +124,7 @@ pub unsafe extern "C" fn local_muts() { NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; while i < builtin_0 { @@ -164,7 +164,7 @@ pub unsafe extern "C" fn local_consts() { let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; let mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let parens: ::core::ffi::c_int = PARENS; let ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; @@ -195,7 +195,7 @@ pub unsafe extern "C" fn local_consts() { NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; while i < builtin_0 { @@ -238,7 +238,7 @@ static mut global_static_const_int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; static mut global_static_const_parens: ::core::ffi::c_int = PARENS; static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char = ::core::ptr::null::<::core::ffi::c_char>(); @@ -311,7 +311,7 @@ pub static mut global_const_int_arithmetic: ::core::ffi::c_int = #[no_mangle] pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; #[no_mangle] pub static mut global_const_parens: ::core::ffi::c_int = PARENS; #[no_mangle] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.clang15.snap index eec44cd385..f38245ef07 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.clang15.snap @@ -35,10 +35,10 @@ pub type zstd_platform_dependent_type = ::core::ffi::c_long; pub struct ntlmdata { pub target_info_len: ::core::ffi::c_uint, } -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; pub const UINTPTR_MAX: ::core::ffi::c_ulong = 18446744073709551615 as ::core::ffi::c_ulong; pub const LITERAL_INT: ::core::ffi::c_int = 0xffff as ::core::ffi::c_int; -pub const LITERAL_BOOL: ::core::ffi::c_int = true_0; +pub const LITERAL_BOOL: ::core::ffi::c_int = r#true; pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64; pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as ::core::ffi::c_int; pub const LITERAL_STR: [::core::ffi::c_char; 6] = @@ -52,7 +52,7 @@ pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT; pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR; pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR; pub const NESTED_STRUCT: S = LITERAL_STRUCT; -pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + true_0); +pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + r#true); pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe { LITERAL_STR .as_ptr() @@ -91,7 +91,7 @@ pub unsafe extern "C" fn local_muts() { let mut int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; let mut mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut parens: ::core::ffi::c_int = PARENS; let mut ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; @@ -124,7 +124,7 @@ pub unsafe extern "C" fn local_muts() { NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; while i < builtin_0 { @@ -164,7 +164,7 @@ pub unsafe extern "C" fn local_consts() { let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; let mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let parens: ::core::ffi::c_int = PARENS; let ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; @@ -195,7 +195,7 @@ pub unsafe extern "C" fn local_consts() { NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; while i < builtin_0 { @@ -238,7 +238,7 @@ static mut global_static_const_int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; static mut global_static_const_parens: ::core::ffi::c_int = PARENS; static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char = ::core::ptr::null::<::core::ffi::c_char>(); @@ -311,7 +311,7 @@ pub static mut global_const_int_arithmetic: ::core::ffi::c_int = #[unsafe(no_mangle)] pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; + - r#true as ::core::ffi::c_double) as ::core::ffi::c_float; #[unsafe(no_mangle)] pub static mut global_const_parens: ::core::ffi::c_int = PARENS; #[unsafe(no_mangle)] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2021.clang15.snap new file mode 100644 index 0000000000..d534a918ff --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2021.clang15.snap @@ -0,0 +1,42 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/raw_keywords.2021.clang15.rs +--- +#![allow( + clippy::missing_safety_doc, + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type r#type = ::core::ffi::c_int; +pub type r#as = ::core::ffi::c_uint; +pub const r#await: r#as = 1; +pub const r#async: r#as = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct r#dyn { + pub r#false: r#type, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union r#fn { + pub r#where: r#type, + pub r#final: r#type, +} +#[export_name = "super"] +pub static mut super_0: r#type = 0 as r#type; +#[export_name = "pub"] +pub unsafe extern "C" fn r#pub(mut r#ref: r#type) {} +#[export_name = "impl"] +pub unsafe extern "C" fn r#impl(mut r#in: r#type) { + let mut r#trait: r#type = super_0; + let mut r#let: r#as = r#async; + let mut r#mod: r#dyn = r#dyn { r#false: 0 }; + r#mod.r#false = 0 as ::core::ffi::c_int as r#type; + let mut r#mut: r#fn = r#fn { r#where: 0 }; + r#mut.r#where = 0 as ::core::ffi::c_int as r#type; + r#pub(r#mut.r#where); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2024.clang15.snap new file mode 100644 index 0000000000..6c305b3b87 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@raw_keywords.c.2024.clang15.snap @@ -0,0 +1,43 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/raw_keywords.2024.clang15.rs +--- +#![allow( + clippy::missing_safety_doc, + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unsafe_op_in_unsafe_fn, + unused_assignments, + unused_mut +)] +pub type r#type = ::core::ffi::c_int; +pub type r#as = ::core::ffi::c_uint; +pub const r#await: r#as = 1; +pub const r#async: r#as = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct r#dyn { + pub r#false: r#type, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union r#fn { + pub r#where: r#type, + pub r#final: r#type, +} +#[unsafe(export_name = "super")] +pub static mut super_0: r#type = 0 as r#type; +#[unsafe(export_name = "pub")] +pub unsafe extern "C" fn r#pub(mut r#ref: r#type) {} +#[unsafe(export_name = "impl")] +pub unsafe extern "C" fn r#impl(mut r#in: r#type) { + let mut r#trait: r#type = super_0; + let mut r#let: r#as = r#async; + let mut r#mod: r#dyn = r#dyn { r#false: 0 }; + r#mod.r#false = 0 as ::core::ffi::c_int as r#type; + let mut r#mut: r#fn = r#fn { r#where: 0 }; + r#mut.r#where = 0 as ::core::ffi::c_int as r#type; + r#pub(r#mut.r#where); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2021.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2021.clang15.snap index 983068e241..063cc29722 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2021.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2021.clang15.snap @@ -14,10 +14,10 @@ expression: cat tests/snapshots/scalar_init.2021.clang15.rs #![feature(raw_ref_op)] pub type E = ::core::ffi::c_uint; pub const A: E = 0; -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; #[no_mangle] pub unsafe extern "C" fn scalar_init() { - let mut b: bool = true_0 != 0; + let mut b: bool = r#true != 0; let mut c: ::core::ffi::c_char = 'c' as ::core::ffi::c_char; let mut i: ::core::ffi::c_int = 42 as ::core::ffi::c_int; let mut l: ::core::ffi::c_long = 42 as ::core::ffi::c_long; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2024.clang15.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2024.clang15.snap index be7f3a5d96..0d4a4b7a30 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2024.clang15.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@scalar_init.c.2024.clang15.snap @@ -14,10 +14,10 @@ expression: cat tests/snapshots/scalar_init.2024.clang15.rs )] pub type E = ::core::ffi::c_uint; pub const A: E = 0; -pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const r#true: ::core::ffi::c_int = 1 as ::core::ffi::c_int; #[unsafe(no_mangle)] pub unsafe extern "C" fn scalar_init() { - let mut b: bool = true_0 != 0; + let mut b: bool = r#true != 0; let mut c: ::core::ffi::c_char = 'c' as ::core::ffi::c_char; let mut i: ::core::ffi::c_int = 42 as ::core::ffi::c_int; let mut l: ::core::ffi::c_long = 42 as ::core::ffi::c_long;