immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.79k stars 219 forks source link

In the while loop body using a volatile variable, the generated code will lack a semicolon. #1064

Open ahaoboy opened 4 months ago

ahaoboy commented 4 months ago

ubuntu2310 + llvm-config-15 + C2Rust 0.18.0

input:

#include <stdio.h>

static void Encode(char *str_)
{
  const char *volatile str = str_;
  static const char *HEX = "0123456789ABCDEF";
  while (*str)
  {
    int c = (unsigned char)*str++;
  }
}

int main()
{
  Encode(NULL);
  return 0;
}

output:

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
unsafe extern "C" fn Encode(mut str_: *mut libc::c_char) {
    let mut str: *const libc::c_char = str_;
    static mut HEX: *const libc::c_char = b"0123456789ABCDEF\0" as *const u8
        as *const libc::c_char;
    while *str != 0 {
        let fresh0 = ::core::ptr::read_volatile::<
            *const libc::c_char,
        >(&str as *const *const libc::c_char);
        ::core::ptr::write_volatile(
            &mut str as *mut *const libc::c_char,
            (::core::ptr::read_volatile::<
                *const libc::c_char,
            >(&str as *const *const libc::c_char))
                .offset(1),
        )
        let mut c: libc::c_int = *fresh0 as libc::c_uchar as libc::c_int;
    }
}
unsafe fn main_0() -> libc::c_int {
    Encode(0 as *mut libc::c_char);
    return 0 as libc::c_int;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}