Skip to content

Commit b195a25

Browse files
committed
Format
1 parent 415138e commit b195a25

15 files changed

Lines changed: 511 additions & 544 deletions

‎docs/src/SUMMARY.md‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
# The Project
44

5-
* [Introduction](./project/introduction.md)
6-
* [Building](./project/building.md)
7-
* [Usage](./project/usage.md)
8-
* [Test Suite](./project/test-suite.md)
5+
- [Introduction](./project/introduction.md)
6+
- [Building](./project/building.md)
7+
- [Usage](./project/usage.md)
8+
- [Test Suite](./project/test-suite.md)
99

1010
# Translation Rules
1111

12-
* [Overview](./rules/overview.md)
13-
* [Rule Format](./rules/format.md)
14-
* [Writing Rules](./rules/writing-rules.md)
15-
* [Compat Shims](./rules/compat.md)
16-
* [Conventions](./rules/conventions.md)
17-
* [The Rule Preprocessors](./rules/preprocessors.md)
18-
* [The Rules IR](./rules/ir.md)
19-
* [Loading and Matching](./rules/loading.md)
20-
* [The Matching Engine](./rules/matching.md)
21-
* [Rule Rewriting](./rules/rewriting.md)
12+
- [Overview](./rules/overview.md)
13+
- [Rule Format](./rules/format.md)
14+
- [Writing Rules](./rules/writing-rules.md)
15+
- [Compat Shims](./rules/compat.md)
16+
- [Conventions](./rules/conventions.md)
17+
- [The Rule Preprocessors](./rules/preprocessors.md)
18+
- [The Rules IR](./rules/ir.md)
19+
- [Loading and Matching](./rules/loading.md)
20+
- [The Matching Engine](./rules/matching.md)
21+
- [Rule Rewriting](./rules/rewriting.md)
2222

2323
# Code Generation
2424

25-
* [Overview](./codegen/overview.md)
26-
* [Pointers and References](./codegen/pointers.md)
27-
* [Temporary Materialization](./codegen/temporaries.md)
25+
- [Overview](./codegen/overview.md)
26+
- [Pointers and References](./codegen/pointers.md)
27+
- [Temporary Materialization](./codegen/temporaries.md)

‎docs/src/codegen/overview.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
This part of the book documents the internals of the code generator: how the
44
clang AST is traversed and how Rust code is emitted.
55

6-
TODO: document the converter plugin mechanism
7-
(`cpp2rust/converter/plugins/`), which intercepts constructs ahead of the
8-
translation rules (currently `emplace_back`).
6+
TODO: document the converter plugin mechanism (`cpp2rust/converter/plugins/`),
7+
which intercepts constructs ahead of the translation rules (currently
8+
`emplace_back`).

‎docs/src/codegen/temporaries.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Temporary Materialization
22

3-
> TODO: explain how the converter materializes temporaries for expressions
4-
> that need an address but have none (e.g. literals passed where a pointer is
3+
> TODO: explain how the converter materializes temporaries for expressions that
4+
> need an address but have none (e.g. literals passed where a pointer is
55
> expected).

‎docs/src/project/introduction.md‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@ published at PLDI 2026.
99

1010
## Overview
1111

12-
Cpp2Rust first parses the input C++ file(s) with clang and produces an AST.
13-
It then traverses the AST and emits Rust code as strings, inserting
14-
calls to the `libcc2rs` runtime library where needed (e.g., for raw pointer
15-
semantics).
12+
Cpp2Rust first parses the input C++ file(s) with clang and produces an AST. It
13+
then traverses the AST and emits Rust code as strings, inserting calls to the
14+
`libcc2rs` runtime library where needed (e.g., for raw pointer semantics).
1615
Finally, the Rust code is pretty-printed using `rustfmt` to a single `.rs` file.
1716

18-
By default the *reference counting model* is used, which produces fully safe
19-
Rust.
20-
A generator of unsafe Rust is also available through the `--model=unsafe`
17+
By default the _reference counting model_ is used, which produces fully safe
18+
Rust. A generator of unsafe Rust is also available through the `--model=unsafe`
2119
command line argument for debugging and performance comparisons.
2220

2321
## Runtime library (`libcc2rs`)
2422

2523
The generated code relies on a runtime library designed to simplify the
26-
translation process.
27-
C pointers are converted into the `Ptr<T>` type provided by `libcc2rs`.
28-
`Ptr<T>` models C pointer semantics, including null, arithmetic, and aliasing,
29-
while satisfying Rust's borrow checker through checked run-time operations.
24+
translation process. C pointers are converted into the `Ptr<T>` type provided by
25+
`libcc2rs`. `Ptr<T>` models C pointer semantics, including null, arithmetic, and
26+
aliasing, while satisfying Rust's borrow checker through checked run-time
27+
operations.

‎docs/src/project/usage.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs
77
```
88

9-
By default, the reference counting model is used (fully safe output).
10-
To generate unsafe Rust instead:
9+
By default, the reference counting model is used (fully safe output). To
10+
generate unsafe Rust instead:
1111

1212
```bash
1313
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs --model=unsafe

‎docs/src/rules/compat.md‎

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
# Compat Shims
22

3-
Rule matching needs a resolvable callee. The preprocessor keys every
4-
expression rule on the function, method, constructor, constant, or global
5-
that the pattern's `return` expression resolves to, and the only macros it
6-
can record are those that
3+
Rule matching needs a resolvable callee. The preprocessor keys every expression
4+
rule on the function, method, constructor, constant, or global that the
5+
pattern's `return` expression resolves to, and the only macros it can record are
6+
those that
77
[expand to an integer literal](./writing-rules.md#enum-values-constants-and-macros),
8-
which match by macro name. Any other macro is invisible to the rule system:
9-
by the time clang has built the AST, the macro is gone and only its expansion
8+
which match by macro name. Any other macro is invisible to the rule system: by
9+
the time clang has built the AST, the macro is gone and only its expansion
1010
remains.
1111

12-
That is a problem for a small set of libc APIs that are specified as macros
13-
over platform internals:
12+
That is a problem for a small set of libc APIs that are specified as macros over
13+
platform internals:
1414

15-
* `errno` is an object-like macro; glibc expands it to
16-
`(*__errno_location())`, macOS to `(*__error())`.
17-
* `assert` expands to a conditional that stringifies the condition and calls
18-
a platform-specific failure handler with file and line arguments.
19-
* `FD_SET`, `FD_CLR`, `FD_ISSET`, and `FD_ZERO` expand to bit manipulation
20-
on the `fd_set` representation, through helpers that differ per platform.
21-
* `ntohl`, `ntohs`, `htonl`, and `htons` expand to byte-swap builtins or to
15+
- `errno` is an object-like macro; glibc expands it to `(*__errno_location())`,
16+
macOS to `(*__error())`.
17+
- `assert` expands to a conditional that stringifies the condition and calls a
18+
platform-specific failure handler with file and line arguments.
19+
- `FD_SET`, `FD_CLR`, `FD_ISSET`, and `FD_ZERO` expand to bit manipulation on
20+
the `fd_set` representation, through helpers that differ per platform.
21+
- `ntohl`, `ntohs`, `htonl`, and `htons` expand to byte-swap builtins or to
2222
nothing at all, depending on endianness.
2323

2424
There is no stable, platform-independent callee here to key a rule on. The
25-
*compat headers* in `cpp2rust/compat/` fix this by rewriting each such macro
25+
_compat headers_ in `cpp2rust/compat/` fix this by rewriting each such macro
2626
into a call to a synthetic, well-known function before matching happens.
2727

2828
## How the shims work
2929

3030
`cpp2rust/compat` is injected as a system include directory ahead of the
3131
platform headers in every clang invocation the project makes: both when
3232
`cpp2rust` parses the input program and when `cpp-rule-preprocessor` compiles
33-
rule sources. The shared flag list lives in
34-
`cpp2rust/compat/platform_flags.h` (`getPlatformClangBeginFlags`), and the
35-
directory path is baked in at build time via the `COMPAT_INCLUDE_DIR`
36-
definition.
33+
rule sources. The shared flag list lives in `cpp2rust/compat/platform_flags.h`
34+
(`getPlatformClangBeginFlags`), and the directory path is baked in at build time
35+
via the `COMPAT_INCLUDE_DIR` definition.
3736

3837
A shim header sits at the same relative path as the real header it shadows
3938
(`errno.h`, `sys/select.h`, `arpa/inet.h`, ...), so an ordinary
4039
`#include <errno.h>` finds the shim first. The header then:
4140

42-
1. pulls in the real platform header with `#include_next` (a GNU extension;
43-
the shared flags pass `-Wno-gnu-include-next` for it),
41+
1. pulls in the real platform header with `#include_next` (a GNU extension; the
42+
shared flags pass `-Wno-gnu-include-next` for it),
4443
2. `#undef`s the macro,
4544
3. declares a `cpp2rust_*` shim function,
4645
4. redefines the macro to call the shim.
@@ -57,12 +56,12 @@ int *cpp2rust_errno(void);
5756
#define errno (*cpp2rust_errno())
5857
```
5958
60-
The redefinition keeps `errno` an lvalue by dereferencing the returned
61-
pointer, so both reads and assignments like `errno = 0` still parse; what
62-
the matcher sees in either case is a call to `int *cpp2rust_errno()`.
59+
The redefinition keeps `errno` an lvalue by dereferencing the returned pointer,
60+
so both reads and assignments like `errno = 0` still parse; what the matcher
61+
sees in either case is a call to `int *cpp2rust_errno()`.
6362
64-
Because the input program and the rule sources are compiled with the same
65-
shim headers, both sides canonicalize to the same signature, and an ordinary
63+
Because the input program and the rule sources are compiled with the same shim
64+
headers, both sides canonicalize to the same signature, and an ordinary
6665
[expression rule](./writing-rules.md) matches it:
6766
6867
```c
@@ -72,10 +71,10 @@ shim headers, both sides canonicalize to the same signature, and an ordinary
7271
int *f1(void) { return cpp2rust_errno(); }
7372
```
7473

75-
The shim functions are declared but never defined on the C side. They only
76-
exist so that the callee resolves; translation replaces the call with the
77-
rule body, so no C implementation is ever linked. Whatever the shim is
78-
supposed to *do* is supplied by the Rust targets:
74+
The shim functions are declared but never defined on the C side. They only exist
75+
so that the callee resolves; translation replaces the call with the rule body,
76+
so no C implementation is ever linked. Whatever the shim is supposed to _do_ is
77+
supplied by the Rust targets:
7978

8079
```rust
8180
// rules/errno/tgt_unsafe.rs
@@ -91,11 +90,11 @@ fn f1() -> Ptr<i32> {
9190
}
9291
```
9392

94-
In the unsafe model `libcc2rs::cpp2rust_errno_unsafe` wraps the real
95-
platform errno location (`__errno_location` on Linux, `__error` on macOS).
96-
The refcount model instead *virtualizes* errno as a thread-local
97-
`Value<i32>` inside `libcc2rs`; this is the same cell that other refcount
98-
rules write when they translate a failing libc call into
93+
In the unsafe model `libcc2rs::cpp2rust_errno_unsafe` wraps the real platform
94+
errno location (`__errno_location` on Linux, `__error` on macOS). The refcount
95+
model instead _virtualizes_ errno as a thread-local `Value<i32>` inside
96+
`libcc2rs`; this is the same cell that other refcount rules write when they
97+
translate a failing libc call into
9998
`libcc2rs::cpp2rust_errno().write(__e as i32)`.
10099

101100
A rule pattern may spell either the macro or the shim directly; the two are
@@ -109,17 +108,17 @@ void f2(int fd, fd_set *set) { return FD_SET(fd, set); }
109108
110109
## The current shims
111110
112-
| Header | Macros | Shim functions | Rules |
113-
|---|---|---|---|
114-
| `assert.h` | `assert` | `cpp2rust_assert_fail(bool)` | `rules/assert` maps it to `assert!(a0)` |
115-
| `errno.h` | `errno` | `cpp2rust_errno()` | `rules/errno`, see above |
116-
| `arpa/inet.h` | `ntohl`, `ntohs`, `htonl`, `htons` | `cpp2rust_ntohl(x)`, ... | `rules/arpa_inet` maps them to `u32::from_be`, `u16::to_be`, ... |
111+
| Header | Macros | Shim functions | Rules |
112+
| -------------- | ----------------------------------------- | ------------------------------- | --------------------------------------------------------------------------------------- |
113+
| `assert.h` | `assert` | `cpp2rust_assert_fail(bool)` | `rules/assert` maps it to `assert!(a0)` |
114+
| `errno.h` | `errno` | `cpp2rust_errno()` | `rules/errno`, see above |
115+
| `arpa/inet.h` | `ntohl`, `ntohs`, `htonl`, `htons` | `cpp2rust_ntohl(x)`, ... | `rules/arpa_inet` maps them to `u32::from_be`, `u16::to_be`, ... |
117116
| `sys/select.h` | `FD_SET`, `FD_CLR`, `FD_ISSET`, `FD_ZERO` | `cpp2rust_fd_set(fd, set)`, ... | `rules/select` maps them to `libc::FD_SET(...)` (unsafe) or `CFdSet` methods (refcount) |
118117
119-
Note how the shim also normalizes the *shape* of the API. C's `assert` is a
120-
macro precisely so it can stringify its condition and capture file and line;
121-
the shim reduces it to a plain `void(bool)` function, and the Rust side
122-
regains the diagnostics by mapping it to the `assert!` macro.
118+
Note how the shim also normalizes the _shape_ of the API. C's `assert` is a
119+
macro precisely so it can stringify its condition and capture file and line; the
120+
shim reduces it to a plain `void(bool)` function, and the Rust side regains the
121+
diagnostics by mapping it to the `assert!` macro.
123122
124123
## Adding a new shim
125124
@@ -132,15 +131,15 @@ To make another macro-based API matchable:
132131
signature, and redefine the macro to call it.
133132
3. Write rules for the shim in a `rules/` module as for any other function,
134133
including the corresponding header in `src.c`/`src.cpp`.
135-
4. If a model needs runtime support (as refcount errno does), implement it
136-
in `libcc2rs` and call it from the rule target.
134+
4. If a model needs runtime support (as refcount errno does), implement it in
135+
`libcc2rs` and call it from the rule target.
137136
138-
Keep the shim's signature platform-independent; the whole point is that
139-
both sides of every rule see one canonical declaration on every platform.
137+
Keep the shim's signature platform-independent; the whole point is that both
138+
sides of every rule see one canonical declaration on every platform.
140139
141140
## Related normalization
142141
143-
The same shared flag list also passes `-D_FORTIFY_SOURCE=0`, which keeps
144-
glibc from substituting fortified variants (`__printf_chk` and friends) for
145-
standard calls. Like the shims, this ensures that calls in the input program
146-
resolve to the standard declarations the rules are written against.
142+
The same shared flag list also passes `-D_FORTIFY_SOURCE=0`, which keeps glibc
143+
from substituting fortified variants (`__printf_chk` and friends) for standard
144+
calls. Like the shims, this ensures that calls in the input program resolve to
145+
the standard declarations the rules are written against.

0 commit comments

Comments
 (0)