Skip to content

Commit

Permalink
transpile: Make volatile post-increment assignment compile
Browse files Browse the repository at this point in the history
Fixes #1049. Fixes #1064.
  • Loading branch information
GPHemsley committed Sep 22, 2024
1 parent c6610d5 commit 70ee732
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion c2rust-transpile/src/translator/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ impl<'c> Translation<'c> {
};

Ok(WithStmts::new(
vec![save_old_val, mk().expr_stmt(assign_stmt)],
vec![save_old_val, mk().semi_stmt(assign_stmt)],
mk().ident_expr(val_name),
))
},
Expand Down
11 changes: 10 additions & 1 deletion tests/ints/src/test_volatile.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use crate::volatile::rust_entry3;
use crate::volatile::{rust_entry3, rust_volatile_stuff};
use libc::{c_int, c_uint};

#[link(name = "test")]
extern "C" {
fn volatile_stuff();

fn entry3(_: c_uint, _: *mut c_int);
}

const BUFFER_SIZE: usize = 9;

pub fn test_compiles() {
unsafe {
volatile_stuff();
rust_volatile_stuff();
}
}

pub fn test_buffer() {
let mut buffer = [0; BUFFER_SIZE];
let mut rust_buffer = [0; BUFFER_SIZE];
Expand Down
19 changes: 19 additions & 0 deletions tests/ints/src/volatile.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ void entry3(const unsigned buffer_size, int buffer[])
buffer[8] = s.buffer[3];
}

void volatile_stuff(void)
{
// Non-volatile
int x1 = 0;
int x2 = x1++;
x2;

// https://github.com/immunant/c2rust/issues/1049
volatile int x3 = 0;
int x4 = x3++;
x4;

// https://github.com/immunant/c2rust/issues/1064
volatile int x5 = 0;
while (x5 < 5)
{
int x6 = x5++;
x6;
}
}

0 comments on commit 70ee732

Please sign in to comment.