https://github.com/samuelmarks/type-correct
Correct types: typed correctly
https://github.com/samuelmarks/type-correct
c cpp libclang libtooling llvm
Last synced: 9 months ago
JSON representation
Correct types: typed correctly
- Host: GitHub
- URL: https://github.com/samuelmarks/type-correct
- Owner: SamuelMarks
- License: cc0-1.0
- Created: 2021-10-10T19:40:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-06T16:08:48.000Z (about 4 years ago)
- Last Synced: 2025-06-22T11:05:02.313Z (9 months ago)
- Topics: c, cpp, libclang, libtooling, llvm
- Language: CMake
- Homepage:
- Size: 163 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
type-correct
============
[](LICENSE.md)
[](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin.yml)
[](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu-llvm-from-sources.yml)
[](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin-llvm-from-sources.yml)
[](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu.yml)
[LLVM](https://llvm.org) [LibClang](https://clang.llvm.org/doxygen/group__CINDEX.html) / [LibTooling](https://clang.llvm.org/docs/LibTooling.html) solution to 'fix' types, rewriting inconsistent use of types to make them consistent.
## Automatic conversions
```c
#include
int main(void) {
/* FROM */
const int n = strlen("FOO");
/* TO */
const size_t n = strlen("FOO");
/* FROM */
for(int i=0; i vec;
for(int i=0; i vec;
for(size_t i=0; i
short sh=SHRT_MAX;
int i=INT_MAX;
long l=LONG_MAX;
long long ll=LLONG_MAX; /* C99 */
```
Technically, these are all [defined and expected](https://en.cppreference.com/w/c/language/conversion) [on clang as an [`ImplicitCastExpr`](https://clang.llvm.org/doxygen/classclang_1_1ImplicitCastExpr.html)]:
```c
ll = l;
l = i;
i = sh;
```
(but the other direction, '[narrowing](https://releases.llvm.org/13.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.html)', is implementation defined)
However, IMHO, people doing `int sl = strlen(s);` actually want `size_t`.
This opinionated view is the assumption made for _type_correct_.
But… attempts are made to be reasonably conservative. See [`type_correct/tests/test_type_correct.cpp`](type_correct/tests/test_type_correct.cpp) for false positive and true positive checks.
## Build instructions
Install compiler suite, CMake, and LLVM (from `brew`, `apt`, or source), then run:
mkdir build && cd build
cmake .. \
-DCMAKE_BUILD_TYPE='Debug' \
-DCT_Clang_INSTALL_DIR='/usr/local/opt/llvm'
(replace `/usr/local/opt/llvm` with your LLVM install dir)
## Thanks
Boilerplate from https://github.com/banach-space/clang-tutor
---
### License
The person who associated a work with this deed has **dedicated** the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.
You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. See [Other Information](#Other%20Information) below.
#### Other Information
- In no way are the patent or trademark rights of any person affected by CC0, nor are the rights that other persons may have in the work or in how the work is used, such as publicity or privacy rights.
- Unless expressly stated otherwise, the person who associated a work with this deed makes no warranties about the work, and disclaims liability for all uses of the work, to the fullest extent permitted by applicable law.
- When using or citing the work, you should not imply endorsement by the author or the affirmer.