type-correct is a Clang-based refactoring tool designed to automatically correctly widen integer types (e.g., int ->
size_t) consistent with their usage, preventing truncation warnings and signed/unsigned mismatches.
The most common workflow is running the tool directly on source files.
# Preview changes to stdout
$ type_correct_cli myfile.cpp
# Apply changes in-place
$ type_correct_cli -in-place myfile.cppLike other LibTooling applications, type-correct needs to know how your code is compiled.
If you have a compile_commands.json (generated via CMake CMAKE_EXPORT_COMPILE_COMMANDS=ON):
$ type_correct_cli -p build/ src/main.cppBefore creating a changelist, you might want to see what would be changed without touching disk.
$ type_correct_cli --audit --project-root=$(pwd) src/*.cppOutput:
| File | Line | Symbol | Old Type | New Type |
|---------------|------|------------|----------|----------|
| src/math.cpp | 45 | i | int | size_t |
| src/utils.cpp | 12 | buffer_len | int | size_t |In complex C++ projects, changing a header file affects multiple compilation units.
type-correct supports a Map-Reduce style iterative solver to ensure global consistency.
- Prepare: Create a directory for intermediate facts.
mkdir facts
- Run Iteratively:
This will run multiple passes until the types converge (stopped changing).
type_correct_cli \ --phase=iterative \ --facts-dir=facts \ --project-root=$(pwd) \ --in-place \ src/*.cpp
You can generate a machine-readable report of changes.
$ type_correct_cli --in-place --report-file=changes.json src/*.cpp| Flag | Description |
|---|---|
-in-place, -i |
Overwrite input files with formatted output. |
-project-root <path> |
Limits rewriting to files within this path. Essential for preventing modification of system headers. |
-exclude <regex> |
Skip files matching the regex (e.g., tests/.*). |
| Flag | Description |
|---|---|
-enable-abi-breaking-changes |
Allow rewriting fields in structs/classes. Warning: Changes binary layout. |
| (Implicit) | Dependency Scanning: The tool automatically scans CMakeLists.txt to detect vendored code and marks it as read-only. |
| Flag | Description |
|---|---|
-phase <phase> |
Set execution mode: standalone (default), iterative, map, reduce, apply. |
-facts-dir <dir> |
Directory for storing analysis state (.facts files). |
-max-iterations <int> |
Limit for iterative mode (default: 10). |
| Flag | Description |
|---|---|
-audit |
Print markdown table of changes to stdout. No file writes. |
-report-file <file> |
Append JSON change records to file. |