From 2d336153e794d817b367f5513f3e70d5cd4f9afd Mon Sep 17 00:00:00 2001 From: Yeaseen Date: Fri, 6 Dec 2024 00:10:23 -0700 Subject: [PATCH 1/3] Fix: Previously ignored enum usage in compound literals Added a test case to ensure correct initialization of buffers using enums in compound literals. --- c2rust-transpile/src/translator/literals.rs | 4 ++++ tests/enums/src/enum_compound.c | 12 ++++++++++++ tests/enums/src/test_enums.rs | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/enums/src/enum_compound.c diff --git a/c2rust-transpile/src/translator/literals.rs b/c2rust-transpile/src/translator/literals.rs index 3924aaa657..a04dee2e0f 100644 --- a/c2rust-transpile/src/translator/literals.rs +++ b/c2rust-transpile/src/translator/literals.rs @@ -254,6 +254,10 @@ impl<'c> Translation<'c> { let id = ids.first().unwrap(); self.convert_expr(ctx.used(), *id) } + CTypeKind::Enum(_) => { + let id = ids.first().unwrap(); + self.convert_expr(ctx.used(), *id) + } CTypeKind::Vector(CQualTypeId { ctype, .. }, len) => { self.vector_list_initializer(ctx, ids, ctype, len) } diff --git a/tests/enums/src/enum_compound.c b/tests/enums/src/enum_compound.c new file mode 100644 index 0000000000..c30a42e5fd --- /dev/null +++ b/tests/enums/src/enum_compound.c @@ -0,0 +1,12 @@ + +typedef enum { + b=2 +} a; + +void entry6(const unsigned buffer_size, int buffer[]){ + if (buffer_size < 1) { return; } + + // Using a compound literal with enum to assign the value + buffer[0] = (a){b}; + +} \ No newline at end of file diff --git a/tests/enums/src/test_enums.rs b/tests/enums/src/test_enums.rs index 2410ab12f0..24a4823478 100644 --- a/tests/enums/src/test_enums.rs +++ b/tests/enums/src/test_enums.rs @@ -7,6 +7,7 @@ use crate::non_canonical_enum_def::{ hrtimer_restart, rust_abc, HRTIMER_NORESTART, HRTIMER_RESTART, }; use crate::top_enum::{rust_entry4, E as otherE}; +use crate::enum_compound::rust_entry6; use libc::{c_int, c_uint}; @@ -28,6 +29,7 @@ const BUFFER_SIZE2: usize = 7; const BUFFER_SIZE3: usize = 4; const BUFFER_SIZE4: usize = 1; const BUFFER_SIZE5: usize = 6; +const BUFFER_SIZE6: usize = 1; pub fn test_variants() { assert_eq!(A as u32, 0); @@ -103,3 +105,17 @@ pub fn test_buffer5() { assert_eq!(buffer, rust_buffer); assert_eq!(buffer, expected_buffer); } + +pub fn test_buffer6() { + let mut buffer = [0; BUFFER_SIZE6]; + let mut rust_buffer = [0; BUFFER_SIZE6]; + let expected_buffer = [2]; + + unsafe { + entry6(BUFFER_SIZE6 as u32, buffer.as_mut_ptr()); + rust_entry6(BUFFER_SIZE6 as u32, rust_buffer.as_mut_ptr()); + } + + assert_eq!(buffer, rust_buffer); + assert_eq!(buffer, expected_buffer); +} From 525f047edec2782e2813d071d5dba01121b0b6f7 Mon Sep 17 00:00:00 2001 From: Yeaseen Date: Fri, 6 Dec 2024 01:02:00 -0700 Subject: [PATCH 2/3] update missing function entry6 --- tests/enums/src/enum_compound.c | 2 +- tests/enums/src/test_enums.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/enums/src/enum_compound.c b/tests/enums/src/enum_compound.c index c30a42e5fd..88552aa64c 100644 --- a/tests/enums/src/enum_compound.c +++ b/tests/enums/src/enum_compound.c @@ -9,4 +9,4 @@ void entry6(const unsigned buffer_size, int buffer[]){ // Using a compound literal with enum to assign the value buffer[0] = (a){b}; -} \ No newline at end of file +} diff --git a/tests/enums/src/test_enums.rs b/tests/enums/src/test_enums.rs index 24a4823478..b95fe8b95f 100644 --- a/tests/enums/src/test_enums.rs +++ b/tests/enums/src/test_enums.rs @@ -22,6 +22,8 @@ extern "C" { fn entry4(_: c_uint, _: *mut c_int); fn entry5(_: c_uint, _: *mut c_int); + + fn entry6(_: c_uint, _: *mut c_int); } const BUFFER_SIZE: usize = 10; From 42aa3f743b1b8868e94dd2c0ec694a61f0f6fc9c Mon Sep 17 00:00:00 2001 From: Yeaseen Date: Fri, 6 Dec 2024 02:26:05 -0700 Subject: [PATCH 3/3] address reviewer suggestions --- tests/enums/src/enum_compound.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/enums/src/enum_compound.c b/tests/enums/src/enum_compound.c index 88552aa64c..370c282c50 100644 --- a/tests/enums/src/enum_compound.c +++ b/tests/enums/src/enum_compound.c @@ -1,12 +1,13 @@ typedef enum { - b=2 + B = 2 } a; -void entry6(const unsigned buffer_size, int buffer[]){ - if (buffer_size < 1) { return; } +void entry6(const unsigned buffer_size, int buffer[]) { + if (buffer_size < 1) { + return; + } // Using a compound literal with enum to assign the value - buffer[0] = (a){b}; - + buffer[0] = (a) { B }; }