diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9a5f74873..21ee26023 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1137,6 +1137,14 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr, static std::vector printf2fmt(std::string &format) { std::vector types; + std::string escaped; + for (char c : format) { + escaped += c; + if (c == '{' || c == '}') { + escaped += c; + } + } + format = std::move(escaped); size_t pos = 0; while ((pos = format.find('%', pos)) != std::string::npos) { if (pos + 1 >= format.size()) @@ -1181,6 +1189,7 @@ static std::vector printf2fmt(std::string &format) { continue; } break; + case 'j': case 'z': if (pos + 2 < format.size() && (format[pos + 2] == 'd' || format[pos + 2] == 'u')) { diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index 8928cfca5..6b19aabc1 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -53,6 +53,7 @@ fn main_0() -> i32 { "{}", (Ptr::>::decay(&({ fn2_1(s.as_pointer(),) })) as Ptr) ); + println!("{{}} {} {}", 5_u64, (-6_i32 as i64)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/printfs.rs b/tests/unit/out/unsafe/printfs.rs index 46441563e..b9fe7e476 100644 --- a/tests/unit/out/unsafe/printfs.rs +++ b/tests/unit/out/unsafe/printfs.rs @@ -56,6 +56,11 @@ unsafe fn main_0() -> i32 { c"%s\n".as_ptr() as *const i8, (*(unsafe { fn2_1(&s) })).as_ptr(), ); + printf( + c"{} %ju %jd\n".as_ptr() as *const i8, + 5_u64, + (-6_i32 as i64), + ); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/printfs.cpp b/tests/unit/printfs.cpp index 7ff8f6fd5..c36f4b6cf 100644 --- a/tests/unit/printfs.cpp +++ b/tests/unit/printfs.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -18,5 +19,6 @@ int main() { printf("%s\n", s.data()); printf("%s\n", fn("foo").c_str()); printf("%s\n", fn2(s).c_str()); + printf("{} %ju %jd\n", (uintmax_t)5, (intmax_t)-6); return 0; }