Skip to content

Commit ccbecd4

Browse files
committed
Fix bug in printf %c
1 parent 521db4b commit ccbecd4

4 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,8 @@ static std::vector<const char *> printf2fmt(std::string &format) {
11541154
pos += 2;
11551155
continue;
11561156
case '%':
1157-
types.emplace_back();
11581157
format.replace(pos, 2, "%");
1159-
pos += 2;
1158+
pos += 1;
11601159
continue;
11611160
case 'l':
11621161
if (pos + 2 < format.size() &&
@@ -1268,8 +1267,8 @@ void ConverterRefCount::ConvertPrintf(clang::CallExpr *expr) {
12681267
for (unsigned i = is_fprintf + 1, e = expr->getNumArgs(); i < e; ++i) {
12691268
StrCat(token::kComma);
12701269
Convert(expr->getArg(i));
1271-
if (types[j])
1272-
StrCat(keyword::kAs, types[j++]);
1270+
if (auto *type = types[j++])
1271+
StrCat(keyword::kAs, type);
12731272
}
12741273
StrCat(')');
12751274
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
__cpp2rust_init_globals();
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let mut c: i8 = (('a' as i32) as i8);
15+
let mut n: i32 = 3;
16+
println!("{}", (c as i32) as u8 as char);
17+
println!("{} {}", n, (c as i32) as u8 as char);
18+
println!("100% {}", (c as i32) as u8 as char);
19+
println!(
20+
"{}{}{}",
21+
(c as i32) as u8 as char,
22+
((c as i32) + 1) as u8 as char,
23+
n
24+
);
25+
return 0;
26+
}
27+
pub fn __cpp2rust_init_globals() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
__cpp2rust_init_globals();
12+
std::process::exit(main_0() as i32);
13+
}
14+
}
15+
unsafe fn main_0() -> i32 {
16+
let mut c: libc::c_char = (('a' as i32) as libc::c_char);
17+
let mut n: i32 = 3;
18+
printf(
19+
(c"%c\n".as_ptr().cast_mut()).cast_const() as *const i8,
20+
(c as i32),
21+
);
22+
printf(
23+
(c"%d %c\n".as_ptr().cast_mut()).cast_const() as *const i8,
24+
n,
25+
(c as i32),
26+
);
27+
printf(
28+
(c"100%% %c\n".as_ptr().cast_mut()).cast_const() as *const i8,
29+
(c as i32),
30+
);
31+
printf(
32+
(c"%c%c%d\n".as_ptr().cast_mut()).cast_const() as *const i8,
33+
(c as i32),
34+
((c as i32) + (1)),
35+
n,
36+
);
37+
return 0;
38+
}
39+
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/printf_char.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
char c = 'a';
5+
int n = 3;
6+
printf("%c\n", c);
7+
printf("%d %c\n", n, c);
8+
printf("100%% %c\n", c);
9+
printf("%c%c%d\n", c, c + 1, n);
10+
return 0;
11+
}

0 commit comments

Comments
 (0)