Skip to content

Commit 0cbabc1

Browse files
committed
Clean up manual
Fix broken links, minor rewrite text rewrites, organize files to make linking work in both manual and github web view.
1 parent af3f157 commit 0cbabc1

File tree

14 files changed

+1531
-43
lines changed

14 files changed

+1531
-43
lines changed

README.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# What is C2Rust?
44

55
C2Rust helps you migrate C99-compliant code to Rust. It provides:
6-
- a C to Rust translator,
7-
- a Rust code refactoring tool, and
8-
- a way to cross-check the C code against the new Rust code.
6+
- a C to Rust translator
7+
- a Rust code refactoring tool
8+
- tools to cross-check execution of the C code against the new Rust code
99

10-
The translator (or transpiler), produces unsafe Rust code that closely mirrors the input C code. The primary goal of the translator is to produce code that is functionally identical to the input C code. Generating safe or idomatic Rust is *not* a goal for the translator. Rather, we think the best approach is to gradually clean up the translator using dedicated refactoring tools. See the `c2rust-refactor` directory to learn about the `c2rust-refactor` tool that we are working on.
11-
Some refactoring will have to be done by hand which may introduce errors. We provide plugins for `clang` and `rustc` so you can compile and run two binaries and check that they behave identically (at the level of function calls).
12-
See details in the `cross-checks` directory and in the cross checking [tutorial](docs/cross-check-tutorial.md). Here's the big picture:
10+
The translator (or transpiler), produces unsafe Rust code that closely mirrors the input C code. The primary goal of the translator is to produce code that is functionally identical to the input C code. Generating safe or idomatic Rust is *not* a goal for the translator. Rather, we think the best approach is to gradually rewrite the translated Rust code using dedicated refactoring tools. To this end, we are building a refactoring tool that rewrites unsafe auto-translated Rust into safer idioms (see [c2rust-refactor](c2rust-refactor/)).
11+
12+
Some refactoring will have to be done by hand which may introduce errors. We provide plugins for `clang` and `rustc` so you can compile and run two binaries and check that they behave identically (at the level of function calls). For details on cross-checking see the [cross-checks](cross-checks) directory and the cross checking [tutorial](docs/cross-check-tutorial.md).
13+
14+
Here's the big picture:
1315

1416
![C2Rust overview](docs/c2rust-overview.png "C2Rust overview")
1517

@@ -30,13 +32,13 @@ C2Rust requires LLVM 6 or 7 and its corresponding libraries and clang compiler.
3032
pacman -S base-devel llvm clang cmake
3133

3234

33-
- **OS X:** (XCode command-line tools and recent LLVM (we recommend the Homebrew version) are required.)
35+
- **OS X:** XCode command-line tools and recent LLVM (we recommend the Homebrew version) are required.
3436

3537
xcode-select --install
3638
brew install llvm python3 cmake
3739

3840

39-
A rust installation with [Rustup](https://rustup.rs/) is also required on all platforms.
41+
Finally, a rust installation with [Rustup](https://rustup.rs/) is required on all platforms.
4042

4143

4244
## Building C2Rust
@@ -53,7 +55,9 @@ If you have trouble with cargo build, the [developer docs](docs/README-developer
5355

5456
# Translating C to Rust
5557

56-
The translation tool first needs to know the existing compiler commands used to build the C code. To provide this information, you will need to generate a [standard](https://clang.llvm.org/docs/JSONCompilationDatabase.html) `compile_commands.json` file. Many build systems can automatically generate this file, as it is used by many other tools, but see [below](#generating-compile_commandsjson-files) for recommendations on how to generate this file for common build processes.
58+
The translator needs access to the exact compiler commands used to build the C code. To provide this information, you will need a [standard](https://clang.llvm.org/docs/JSONCompilationDatabase.html) `compile_commands.json` file. Many build systems can automatically generate this file, as it is used by many other tools, but see [below](#generating-compile_commandsjson-files) for recommendations on how to generate this file for common build processes.
59+
60+
Once you have a `compile_commands.json` file describing the C build, translate the C code to Rust with the following command:
5761

5862
$ ./scripts/transpile.py path/to/compile_commands.json
5963

@@ -78,7 +82,7 @@ in this translator. The translator will attempt to skip function definitions tha
7882
## Generating `compile_commands.json` files
7983

8084
The `compile_commands.json` file can be automatically created using
81-
either `cmake`, `intercept-build`, or `bear` (Linux only).
85+
either `cmake`, `intercept-build`, or `bear`.
8286

8387
It may be a good idea to remove optimizations(`-OX`) from the compile commands
8488
file, as there are optimization builtins which we do not support translating.
@@ -100,7 +104,7 @@ Intercept build is distributed with clang and recommended for makefile projects
100104
$ intercept-build make
101105
$ intercept-build xcodebuild
102106

103-
### ... with `bear`
107+
### ... with `bear` (linux only)
104108

105109
When building on Linux, *Bear* is automatically built by the
106110
`build_translator.py` script and installed into the `dependencies`
@@ -113,7 +117,7 @@ directory.
113117

114118
> Are there release binaries? Can I install C2Rust with Cargo?
115119
116-
We hope to release binaries that you can `cargo install` soon(tm).
120+
We plan to release a cargo package that you can `cargo install` soon(tm).
117121

118122
> I translated code on platform X but it didn't work correctly on platform Y
119123

book.toml

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ command = "cargo run --bin mdbook-include_md --quiet --"
1111

1212
[preprocessor.generator_dispatch]
1313
command = "python3 manual/preprocessors/generator_dispatch.py"
14+
15+
[output.html]
16+
17+
# Uncomment the following to enable link-checking. This should not be enabled by
18+
# default because linkcheck has false positives due to our use of symlinks.
19+
# [output.linkcheck]

c2rust-refactor/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Build `c2rust-refactor` with `cargo build`.
1919

2020
Flags for `c2rust-refactor` are described by `c2rust-refactor --help`.
2121

22-
See [the command documentation](../_generated/c2rust-refactor-commands.md) for a list of commands,
22+
See [the command documentation](commands.html) for a list of commands,
2323
including complete usage and descriptions.
2424
Multiple commands can be separated by an argument consisting of a single
2525
semicolon, as in `c2rust-refactor cmd1 arg1 \; cmd2 arg2`.
@@ -51,7 +51,7 @@ by a command is described in the command's documentation; by default, most
5151
commands that use marks operate on `target`.
5252

5353
The most flexible way of marking nodes is by using the
54-
[`select`](../_generated/c2rust-refactor-commands.md#select) command. See the command
54+
[`select`](commands.html#select) command. See the command
5555
documentation and `src/select/mod.rs` for details. Note that marks are not
5656
preserved across `c2rust-refactor` invocations, so you usually want to run
5757
`select` followed by the command of interest using the `;` separator mentioned

c2rust-refactor/doc/select.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ function-by-function basis.
3939

4040
The remainder of this tutorial describes `select` and related mark-manipulation
4141
commands. For details of how marks affect various transformation commands, see
42-
the [command documentation](c2rust-refactor-commands.md) or read about the
43-
[`marked!` pattern](refactor-rewrite.md#marked) for `rewrite_expr` and other
42+
the [command documentation](commands.html) or read about the
43+
[`marked!` pattern](rewrite.md#marked) for `rewrite_expr` and other
4444
pattern-matching commands.
4545

4646

@@ -432,11 +432,10 @@ selected node. `last` does the same with the last selected node. "First" and
432432
ordered as expected, and a parent node come "after" all of its children.
433433
434434
The `first` and `last` operations are most useful for finding places to insert
435-
new nodes (such as with the [`create_item`
436-
command](c2rust-refactor-commands.md#create_item)) while ignoring details such
437-
as the specific names or kinds of the nodes around the insertion point. For
438-
example, we can use `last` to easily select the last item in a module. First,
439-
we select all the module's items:
435+
new nodes (such as with the [`create_item` command](commands.html#create_item))
436+
while ignoring details such as the specific names or kinds of the nodes around
437+
the insertion point. For example, we can use `last` to easily select the last
438+
item in a module. First, we select all the module's items:
440439
441440
```rust refactor-target hidden
442441
mod m {
@@ -457,7 +456,7 @@ clear_marks ;
457456
select target 'item(m); child(kind(item)); last;'
458457
```
459458
460-
Now we could use [`create_item`](c2rust-refactor-commands.md#create_item) to
459+
Now we could use [`create_item`](commands.html#create_item) to
461460
insert a new item after the last existing one.
462461
463462
### `marked`

docs/README-developers.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
There are three ways to build the C2Rust project:
44

5-
- Using **Vagrant**. See our [vagrant README](../vagrant/README.md).
6-
- Using **Docker**. See our [docker README](../docker/README.md).
5+
- [Using **Vagrant**](../vagrant/).
6+
- [Using **Docker**](../docker/).
77
- **Manually**, as explained below.
88

99
The previous two options automatically install all prerequisites during provisioning. You can also provision a macOS or Linux system manually.
@@ -49,7 +49,7 @@ C2Rust (indirectly) uses the [clang-sys](https://crates.io/crates/clang-sys) cra
4949

5050
# Building dependencies from source
5151

52-
To develop on components that interact with LLVM, we recommend building against a local copy of LLVM. This will ensure that you have debug symbols and IDE integration for both LLVM and C2Rust. However, building C2Rust from source with LLVM takes a while. For a shorter build that links against prebuilt LLVM and clang system libraries, you should be able to `cargo build` in the `c2rust-transpile` directory (see the general [README](../README.md)).
52+
To develop on components that interact with LLVM, we recommend building against a local copy of LLVM. This will ensure that you have debug symbols and IDE integration for both LLVM and C2Rust. However, building C2Rust from source with LLVM takes a while. For a shorter build that links against prebuilt LLVM and clang system libraries, you should be able to `cargo build` in the `c2rust-transpile` directory (see the general [README](../)).
5353

5454
The following from source full build script has been tested on recent versions of macOS and Ubuntu:
5555

@@ -58,9 +58,9 @@ The following from source full build script has been tested on recent versions o
5858

5959
# Testing (Optional)
6060

61-
Tests are found in the [`tests`](../tests) folder. If you build the translator successfully, you should be able to run the tests with:
61+
Tests are found in the [`tests`](../tests/) folder. If you build the translator successfully, you should be able to run the tests with:
6262

6363
$ ./scripts/test_translator.py tests
6464

65-
This basically tests that the original C file and translated Rust file produce the same output when compiled and run. More details about tests are in [this README](../tests/README.md).
65+
This basically tests that the original C file and translated Rust file produce the same output when compiled and run. More details about tests can be found in the [tests folder](../tests/).
6666

examples/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Example translations
2+
3+
The following example translations illustrate how to run C2Rust on real
4+
codebases. Each example has been modified if necessary to prepare it for
5+
translation with C2Rust and each has accompanying documentation on how to
6+
translate the example.
7+
8+
The robotfindskitten example is accompanied by a demonstration of the
9+
refactoring tool rewriting the unsafe translated Rust into idiomatic, safe Rust.
10+
11+
- [json-c](json-c/)
12+
- [urlparser](urlparser/)
13+
- [qsort](qsort/)
14+
- [tmux](tmux/)
15+
- [grabc](grabc/)
16+
- [libxml2](libxml2/)
17+
- [snudown](snudown/)
18+
- [genann](genann/)
19+
- [lil](lil/)
20+
- [xzoom](xzoom/)
21+
- [robotfindskitten](robotfindskitten/)

examples/robotfindskitten/refactor.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Collapsing `ncurses` macros
1+
# Translating and refactoring robotfindskitten
2+
3+
## Collapsing `ncurses` macros
24

35
We begin with a little cleanup. Some `ncurses` functions used in
46
`robotfindskitten` are actually implemented as macros, which are expanded by
@@ -94,7 +96,7 @@ checkpoint where results can be cached, so that later refactoring commands can
9496
be tweaked without rerunning all the previous steps.
9597

9698

97-
# String formatting
99+
## String formatting
98100

99101
Our next step is to replace calls to C string-formatting functions (such as
100102
`printf`) with wrappers using Rust's `format_args!` macro. Since
@@ -110,7 +112,7 @@ accept the `std::fmt::Arguments` produced by the `format_args!` macro. The
110112
second step then replaces the `printf` call with a call to a wrapper that does
111113
accept `std::fmt::Arguments`.
112114

113-
## `printf` format argument conversion
115+
### `printf` format argument conversion
114116

115117
We run a few commands to mark the nodes involved in string formatting, before
116118
finally running the `convert_format_args` command to perform the actual
@@ -155,7 +157,7 @@ clear_marks ;
155157
clear the marks, but we don't want to `commit` these changes until we've fixed
156158
the type errors introduced in this step.
157159

158-
## Creating a `printf` wrapper
160+
### Creating a `printf` wrapper
159161

160162
As a reminder, we currently have code that looks like this:
161163

@@ -201,7 +203,7 @@ commit the changes:
201203
commit ;
202204
```
203205

204-
## Other string formatting functions
206+
### Other string formatting functions
205207

206208
Aside from `printf`, `robotfindskitten` also uses the `ncurses` `printw` and
207209
`mvprintw` string-formatting functions. The refactoring script for `printw` is
@@ -267,7 +269,7 @@ commit ;
267269
```
268270

269271

270-
# Static string constant - `ver`
272+
## Static string constant - `ver`
271273

272274
`robotfindskitten` defines a static string constant, `ver`, to store the game's
273275
version. Using `ver` is currently unsafe, first because its Rust type is a raw
@@ -295,7 +297,7 @@ Simply replacing `*mut c_char` with `&str` introduces type errors throughout
295297
the crate. The initializer for `ver` still has type `*mut c_char`, and all
296298
uses of `ver` are still expecting a `*mut c_char`.
297299

298-
## Fixing `ver`'s initializer
300+
### Fixing `ver`'s initializer
299301

300302
Fixing the `ver` initializer is straightforward: we simply remove all the
301303
casts, then convert the binary string (`&[u8]`) literal to an ordinary string
@@ -317,7 +319,7 @@ bytestr_to_str ;
317319
delete_marks target ;
318320
```
319321

320-
## Fixing `ver`'s uses
322+
### Fixing `ver`'s uses
321323

322324
`ver`'s initializer is now well-typed, but its uses are still expecting a `*mut
323325
c_char` instead of a `&str`. To fix these up, we use the `type_fix_rules`
@@ -333,7 +335,7 @@ __t`), then wrap the original expression in a call to `.as_ptr()`. This turns
333335
out to be enough to fix all the errors at uses of `ver`.
334336

335337

336-
## Making `ver` immutable
338+
### Making `ver` immutable
337339

338340
Now that all type errors have been corrected, we can finish our refactoring of
339341
`ver`. We make it immutable, then commit all the changes to disk.
@@ -346,7 +348,7 @@ commit ;
346348
```
347349

348350

349-
# Static string array - `messages`
351+
## Static string array - `messages`
350352

351353
Aside from `ver`, `robotfindskitten` contains a static array of strings, called
352354
`messages`. Like `ver`, accessing `messages` is unsafe because each element is
@@ -387,7 +389,7 @@ commit ;
387389
```
388390

389391

390-
# Heap-allocated array - `screen`
392+
## Heap-allocated array - `screen`
391393

392394
`robotfindskitten` uses a heap-allocated array to store information about each
393395
object on the screen. The initial translation gives this array the type `*mut
@@ -407,7 +409,7 @@ create_item 'extern crate c2rust_runtime;' inside ;
407409

408410
Now we can proceed with the actual refactoring.
409411

410-
## Converting to `CBlockPtr`
412+
### Converting to `CBlockPtr`
411413

412414
We actually perform the conversion from `*mut` to `CArray` via an intermediate
413415
step, using `CBlockPtr`. This is another type provided by `c2rust_runtime`,
@@ -505,5 +507,5 @@ done, and we can commit the changes:
505507
commit ;
506508
```
507509

508-
## Converting to `CArray`
510+
### Converting to `CArray`
509511

0 commit comments

Comments
 (0)