Skip to content

Commit 0cf4791

Browse files
committed
postprocess: add most of the LLM cache for json-c
There are still some remaining bugs I'm encountering in testing on `json-c`, but checking these in now allows us to see in the PR how the conditional comments are inserted.
1 parent c64d783 commit 0cf4791

File tree

66 files changed

+6702
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+6702
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
transform = "CommentTransfer"
2+
identifier = "_json_c_set_last_err"
3+
model = "gemini-3-pro-preview"
4+
response = """```rust
5+
#[no_mangle]
6+
#[c2rust::src_loc = "75:1"]
7+
pub unsafe extern "C" fn _json_c_set_last_err(
8+
mut err_fmt: *const ::core::ffi::c_char,
9+
mut args: ...
10+
) {
11+
let mut ap: ::core::ffi::VaListImpl;
12+
ap = args.clone();
13+
// Ignore (attempted) overruns from snprintf
14+
vsnprintf(
15+
&raw mut _last_err as *mut ::core::ffi::c_char,
16+
::core::mem::size_of::<[::core::ffi::c_char; 256]>() as size_t,
17+
err_fmt,
18+
ap.as_va_list(),
19+
);
20+
}
21+
```"""
22+
23+
[[messages]]
24+
role = "user"
25+
content = """Transfer the comments from the following C function to the corresponding Rust function.
26+
Do not add any comments that are not present in the C function.
27+
Use Rust doc comment syntax (///) where appropriate (e.g., for function documentation).
28+
Respond with the Rust function definition with the transferred comments.
29+
30+
The Rust must contain exactly the same comments as the C code did.
31+
This must be true even if there are C comments in conditionally-compiled code
32+
not present in the Rust, including comments on preprocessor directives.
33+
In such cases, insert the C comment (only the comment, not any C code)
34+
into the Rust where the Rust code should have been.
35+
If any C comments are not inserted into the Rust, explain why.
36+
37+
C function:
38+
```c
39+
void _json_c_set_last_err(const char *err_fmt, ...)
40+
{
41+
\tva_list ap;
42+
\tva_start(ap, err_fmt);
43+
\t// Ignore (attempted) overruns from snprintf
44+
\t(void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
45+
\tva_end(ap);
46+
}```
47+
48+
Rust function:
49+
```rust
50+
#[no_mangle]
51+
#[c2rust::src_loc = "75:1"]
52+
pub unsafe extern "C" fn _json_c_set_last_err(
53+
mut err_fmt: *const ::core::ffi::c_char,
54+
mut args: ...
55+
) {
56+
let mut ap: ::core::ffi::VaListImpl;
57+
ap = args.clone();
58+
vsnprintf(
59+
&raw mut _last_err as *mut ::core::ffi::c_char,
60+
::core::mem::size_of::<[::core::ffi::c_char; 256]>() as size_t,
61+
err_fmt,
62+
ap.as_va_list(),
63+
);
64+
}```
65+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```rust
2+
#[no_mangle]
3+
#[c2rust::src_loc = "75:1"]
4+
pub unsafe extern "C" fn _json_c_set_last_err(
5+
mut err_fmt: *const ::core::ffi::c_char,
6+
mut args: ...
7+
) {
8+
let mut ap: ::core::ffi::VaListImpl;
9+
ap = args.clone();
10+
// Ignore (attempted) overruns from snprintf
11+
vsnprintf(
12+
&raw mut _last_err as *mut ::core::ffi::c_char,
13+
::core::mem::size_of::<[::core::ffi::c_char; 256]>() as size_t,
14+
err_fmt,
15+
ap.as_va_list(),
16+
);
17+
}
18+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
transform = "CommentTransfer"
2+
identifier = "_json_object_to_fd"
3+
model = "gemini-3-pro-preview"
4+
response = """```rust
5+
#[c2rust::src_loc = "156:1"]
6+
unsafe extern "C" fn _json_object_to_fd(
7+
mut fd: ::core::ffi::c_int,
8+
mut obj: *mut json_object,
9+
mut flags: ::core::ffi::c_int,
10+
mut filename: *const ::core::ffi::c_char,
11+
) -> ::core::ffi::c_int {
12+
let mut ret: ::core::ffi::c_int = 0;
13+
let mut json_str: *const ::core::ffi::c_char = ::core::ptr::null::<
14+
::core::ffi::c_char,
15+
>();
16+
let mut wpos: ::core::ffi::c_uint = 0;
17+
let mut wsize: ::core::ffi::c_uint = 0;
18+
filename = if !filename.is_null() {
19+
filename
20+
} else {
21+
b"(fd)\\0" as *const u8 as *const ::core::ffi::c_char
22+
};
23+
json_str = json_object_to_json_string_ext(obj, flags);
24+
if json_str.is_null() {
25+
return -(1 as ::core::ffi::c_int);
26+
}
27+
wsize = (strlen(json_str) & UINT_MAX as size_t) as ::core::ffi::c_uint; /* CAW: probably unnecessary, but the most 64bit safe */
28+
wpos = 0 as ::core::ffi::c_uint;
29+
while wpos < wsize {
30+
ret = write(
31+
fd,
32+
json_str.offset(wpos as isize) as *const ::core::ffi::c_void,
33+
wsize.wrapping_sub(wpos) as size_t,
34+
) as ::core::ffi::c_int;
35+
if ret < 0 as ::core::ffi::c_int {
36+
_json_c_set_last_err(
37+
b"json_object_to_file: error writing file %s: %s\\n\\0" as *const u8
38+
as *const ::core::ffi::c_char,
39+
filename,
40+
_json_c_strerror(*__errno_location()),
41+
);
42+
return -(1 as ::core::ffi::c_int);
43+
}
44+
/* because of the above check for ret < 0, we can safely cast and add */
45+
wpos = wpos.wrapping_add(ret as ::core::ffi::c_uint);
46+
}
47+
return 0 as ::core::ffi::c_int;
48+
}
49+
```"""
50+
51+
[[messages]]
52+
role = "user"
53+
content = """Transfer the comments from the following C function to the corresponding Rust function.
54+
Do not add any comments that are not present in the C function.
55+
Use Rust doc comment syntax (///) where appropriate (e.g., for function documentation).
56+
Respond with the Rust function definition with the transferred comments.
57+
58+
The Rust must contain exactly the same comments as the C code did.
59+
This must be true even if there are C comments in conditionally-compiled code
60+
not present in the Rust, including comments on preprocessor directives.
61+
In such cases, insert the C comment (only the comment, not any C code)
62+
into the Rust where the Rust code should have been.
63+
If any C comments are not inserted into the Rust, explain why.
64+
65+
C function:
66+
```c
67+
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
68+
{
69+
\tint ret;
70+
\tconst char *json_str;
71+
\tunsigned int wpos, wsize;
72+
73+
\tfilename = filename ? filename : "(fd)";
74+
75+
\tif (!(json_str = json_object_to_json_string_ext(obj,flags))) {
76+
\t\treturn -1;
77+
\t}
78+
79+
\twsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
80+
\twpos = 0;
81+
\twhile(wpos < wsize) {
82+
\t\tif((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
83+
\t\t _json_c_set_last_err("json_object_to_file: error writing file %s: %s\\n",
84+
\t\t\t filename, strerror(errno));
85+
\t\t return -1;
86+
\t\t}
87+
88+
\t\t/* because of the above check for ret < 0, we can safely cast and add */
89+
\t\twpos += (unsigned int)ret;
90+
\t}
91+
92+
\treturn 0;
93+
}```
94+
95+
Rust function:
96+
```rust
97+
#[c2rust::src_loc = "156:1"]
98+
unsafe extern "C" fn _json_object_to_fd(
99+
mut fd: ::core::ffi::c_int,
100+
mut obj: *mut json_object,
101+
mut flags: ::core::ffi::c_int,
102+
mut filename: *const ::core::ffi::c_char,
103+
) -> ::core::ffi::c_int {
104+
let mut ret: ::core::ffi::c_int = 0;
105+
let mut json_str: *const ::core::ffi::c_char = ::core::ptr::null::<
106+
::core::ffi::c_char,
107+
>();
108+
let mut wpos: ::core::ffi::c_uint = 0;
109+
let mut wsize: ::core::ffi::c_uint = 0;
110+
filename = if !filename.is_null() {
111+
filename
112+
} else {
113+
b"(fd)\\0" as *const u8 as *const ::core::ffi::c_char
114+
};
115+
json_str = json_object_to_json_string_ext(obj, flags);
116+
if json_str.is_null() {
117+
return -(1 as ::core::ffi::c_int);
118+
}
119+
wsize = (strlen(json_str) & UINT_MAX as size_t) as ::core::ffi::c_uint;
120+
wpos = 0 as ::core::ffi::c_uint;
121+
while wpos < wsize {
122+
ret = write(
123+
fd,
124+
json_str.offset(wpos as isize) as *const ::core::ffi::c_void,
125+
wsize.wrapping_sub(wpos) as size_t,
126+
) as ::core::ffi::c_int;
127+
if ret < 0 as ::core::ffi::c_int {
128+
_json_c_set_last_err(
129+
b"json_object_to_file: error writing file %s: %s\\n\\0" as *const u8
130+
as *const ::core::ffi::c_char,
131+
filename,
132+
_json_c_strerror(*__errno_location()),
133+
);
134+
return -(1 as ::core::ffi::c_int);
135+
}
136+
wpos = wpos.wrapping_add(ret as ::core::ffi::c_uint);
137+
}
138+
return 0 as ::core::ffi::c_int;
139+
}```
140+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
```rust
2+
#[c2rust::src_loc = "156:1"]
3+
unsafe extern "C" fn _json_object_to_fd(
4+
mut fd: ::core::ffi::c_int,
5+
mut obj: *mut json_object,
6+
mut flags: ::core::ffi::c_int,
7+
mut filename: *const ::core::ffi::c_char,
8+
) -> ::core::ffi::c_int {
9+
let mut ret: ::core::ffi::c_int = 0;
10+
let mut json_str: *const ::core::ffi::c_char = ::core::ptr::null::<
11+
::core::ffi::c_char,
12+
>();
13+
let mut wpos: ::core::ffi::c_uint = 0;
14+
let mut wsize: ::core::ffi::c_uint = 0;
15+
filename = if !filename.is_null() {
16+
filename
17+
} else {
18+
b"(fd)\0" as *const u8 as *const ::core::ffi::c_char
19+
};
20+
json_str = json_object_to_json_string_ext(obj, flags);
21+
if json_str.is_null() {
22+
return -(1 as ::core::ffi::c_int);
23+
}
24+
wsize = (strlen(json_str) & UINT_MAX as size_t) as ::core::ffi::c_uint; /* CAW: probably unnecessary, but the most 64bit safe */
25+
wpos = 0 as ::core::ffi::c_uint;
26+
while wpos < wsize {
27+
ret = write(
28+
fd,
29+
json_str.offset(wpos as isize) as *const ::core::ffi::c_void,
30+
wsize.wrapping_sub(wpos) as size_t,
31+
) as ::core::ffi::c_int;
32+
if ret < 0 as ::core::ffi::c_int {
33+
_json_c_set_last_err(
34+
b"json_object_to_file: error writing file %s: %s\n\0" as *const u8
35+
as *const ::core::ffi::c_char,
36+
filename,
37+
_json_c_strerror(*__errno_location()),
38+
);
39+
return -(1 as ::core::ffi::c_int);
40+
}
41+
/* because of the above check for ret < 0, we can safely cast and add */
42+
wpos = wpos.wrapping_add(ret as ::core::ffi::c_uint);
43+
}
44+
return 0 as ::core::ffi::c_int;
45+
}
46+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
transform = "CommentTransfer"
2+
identifier = "get_string_component"
3+
model = "gemini-3-pro-preview"
4+
response = """```rust
5+
/* ref count debugging */
6+
7+
/* REFCOUNT_DEBUG */
8+
9+
/// helper for accessing the optimized string data component in json_object
10+
///
11+
#[c2rust::src_loc = "97:1"]
12+
unsafe extern "C" fn get_string_component(
13+
mut jso: *const json_object,
14+
) -> *const ::core::ffi::c_char {
15+
return if (*jso).o.c_string.len < LEN_DIRECT_STRING_DATA {
16+
&raw const (*jso).o.c_string.str_0.data as *const ::core::ffi::c_char
17+
} else {
18+
(*jso).o.c_string.str_0.ptr as *const ::core::ffi::c_char
19+
};
20+
}
21+
```"""
22+
23+
[[messages]]
24+
role = "user"
25+
content = """Transfer the comments from the following C function to the corresponding Rust function.
26+
Do not add any comments that are not present in the C function.
27+
Use Rust doc comment syntax (///) where appropriate (e.g., for function documentation).
28+
Respond with the Rust function definition with the transferred comments.
29+
30+
The Rust must contain exactly the same comments as the C code did.
31+
This must be true even if there are C comments in conditionally-compiled code
32+
not present in the Rust, including comments on preprocessor directives.
33+
In such cases, insert the C comment (only the comment, not any C code)
34+
into the Rust where the Rust code should have been.
35+
If any C comments are not inserted into the Rust, explain why.
36+
37+
C function:
38+
```c
39+
/* ref count debugging */
40+
41+
#ifdef REFCOUNT_DEBUG
42+
43+
static struct lh_table *json_object_table;
44+
45+
static void json_object_init(void) __attribute__ ((constructor));
46+
static void json_object_init(void) {
47+
\tMC_DEBUG("json_object_init: creating object table\\n");
48+
\tjson_object_table = lh_kptr_table_new(128, NULL);
49+
}
50+
51+
static void json_object_fini(void) __attribute__ ((destructor));
52+
static void json_object_fini(void)
53+
{
54+
\tstruct lh_entry *ent;
55+
\tif (MC_GET_DEBUG())
56+
\t{
57+
\t\tif (json_object_table->count)
58+
\t\t{
59+
\t\t\tMC_DEBUG("json_object_fini: %d referenced objects at exit\\n",
60+
\t\t\t json_object_table->count);
61+
\t\t\tlh_foreach(json_object_table, ent)
62+
\t\t\t{
63+
\t\t\t\tstruct json_object* obj =
64+
\t\t\t\t (struct json_object*) lh_entry_v(ent);
65+
\t\t\t\tMC_DEBUG("\\t%s:%p\\n",
66+
\t\t\t\t\t json_type_to_name(obj->o_type), obj);
67+
\t\t\t}
68+
\t\t}
69+
\t}
70+
\tMC_DEBUG("json_object_fini: freeing object table\\n");
71+
\tlh_table_free(json_object_table);
72+
}
73+
#endif /* REFCOUNT_DEBUG */
74+
75+
76+
/* helper for accessing the optimized string data component in json_object
77+
*/
78+
static const char *
79+
get_string_component(const struct json_object *jso)
80+
{
81+
\treturn (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
82+
\t\t jso->o.c_string.str.data : jso->o.c_string.str.ptr;
83+
}```
84+
85+
Rust function:
86+
```rust
87+
#[c2rust::src_loc = "97:1"]
88+
unsafe extern "C" fn get_string_component(
89+
mut jso: *const json_object,
90+
) -> *const ::core::ffi::c_char {
91+
return if (*jso).o.c_string.len < LEN_DIRECT_STRING_DATA {
92+
&raw const (*jso).o.c_string.str_0.data as *const ::core::ffi::c_char
93+
} else {
94+
(*jso).o.c_string.str_0.ptr as *const ::core::ffi::c_char
95+
};
96+
}```
97+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```rust
2+
/* ref count debugging */
3+
4+
/* REFCOUNT_DEBUG */
5+
6+
/// helper for accessing the optimized string data component in json_object
7+
///
8+
#[c2rust::src_loc = "97:1"]
9+
unsafe extern "C" fn get_string_component(
10+
mut jso: *const json_object,
11+
) -> *const ::core::ffi::c_char {
12+
return if (*jso).o.c_string.len < LEN_DIRECT_STRING_DATA {
13+
&raw const (*jso).o.c_string.str_0.data as *const ::core::ffi::c_char
14+
} else {
15+
(*jso).o.c_string.str_0.ptr as *const ::core::ffi::c_char
16+
};
17+
}
18+
```

0 commit comments

Comments
 (0)