$ show immunant/c2rust
C2Rust is a compiler toolchain that translates C99 code into unsafe Rust, then applies static analysis and refactoring to produce safer, more idiomatic Rust. The pipeline consists of four main stages: transpilation (c2rust-transpile) using a Clang-based AST exporter to produce initial Rust code; static analysis (c2rust-analyze) that infers pointer permissions and rewrites raw pointers to safe references using a Polonius-based borrow checker; refactoring (c2rust-refactor) which applies dozens of AST transformations (e.g., ownership splitting, macro collapsing, control flow reconstruction) to improve code quality; and postprocessing (c2rust-postprocess) that leverages large language models to transfer C comments and align definitions. The project also includes a cross-checking framework that instruments both C and Rust code to compare runtime behavior via hashing, a program dependence graph (PDG) builder for memory safety analysis, and dynamic instrumentation for event logging. It is primarily a CLI tool (c2rust) that orchestrates these components, with extensive integration tests on real-world C projects (nginx, curl, sqlite, ruby, python2, libxml2, zstd, lua). The codebase is substantial and actively maintained, but relies heavily on unstable rustc internal APIs and LLVM/Clang, introducing maintenance risk. Several components exhibit high craftsmanship and are reusable in isolation.
The LLM identified 6 files worth highlighting for reuse:
Implements a derive macro that generates Rust bitfield structs compatible with C bitfield semantics, including safe accessors, shifts, and masks. The macro is self-contained, handles arbitrary field widths, and produces efficient code without runtime overhead. It can be extracted as a standalone crate for any project needing C-style bitfields.
Provides a trait and safe functions for casting between integers and pointers specifically for tied inline assembly operands, ensuring type safety and avoiding undefined behavior. The crate is minimal, generic, and reusable in any Rust project that works with inline asm and needs to cast operand types.
Defines a generic WithStmts<T> struct that pairs a value with a vector of Rust statements, enabling composable accumulation of statements during code generation. It provides monadic operations (map, and_then, append, combine) that simplify building complex statement sequences without mutable state. This pattern is reusable in any code generator or DSL translator.
Implements a generic worklist-based fixed-point solver for dataflow analyses, parameterized by the transfer function and convergence criterion. It supports forward/backward analysis and works on arbitrary graph-like structures. The implementation is clean, well-documented, and can be extracted and reused in any compiler or static analysis project.
Provides a two-level union-find data structure (EquivSet) for tracking equivalence classes of pointer IDs, with separate global and local layers. Supports merging, finding, and iteration while maintaining consistency across analysis passes. The implementation is efficient and generic, reusable in any context requiring equivalence tracking over an evolving set of elements.
Implements a file-based caching system for LLM API responses, with TTL, metadata serialization in TOML, and frozen cache support for deterministic replay. Handles race conditions via temporary files and supports directory-level purging. The pattern is general-purpose and can be adapted to cache any expensive computation where inputs and outputs are deterministic.