{"id":24746594,"url":"https://github.com/samuelmarks/type-correct","last_synced_at":"2025-07-12T00:05:51.925Z","repository":{"id":150265477,"uuid":"415683361","full_name":"SamuelMarks/type-correct","owner":"SamuelMarks","description":"Correct types: typed correctly","archived":false,"fork":false,"pushed_at":"2022-03-06T16:08:48.000Z","size":167,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-22T11:05:02.313Z","etag":null,"topics":["c","cpp","libclang","libtooling","llvm"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SamuelMarks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-10T19:40:06.000Z","updated_at":"2022-12-05T06:36:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"a0371e0a-9d1e-4381-9ea3-b8e96a1b8a90","html_url":"https://github.com/SamuelMarks/type-correct","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SamuelMarks/type-correct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelMarks%2Ftype-correct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelMarks%2Ftype-correct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelMarks%2Ftype-correct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelMarks%2Ftype-correct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SamuelMarks","download_url":"https://codeload.github.com/SamuelMarks/type-correct/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SamuelMarks%2Ftype-correct/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264915991,"owners_count":23682957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","libclang","libtooling","llvm"],"created_at":"2025-01-28T04:29:30.201Z","updated_at":"2025-07-12T00:05:51.912Z","avatar_url":"https://github.com/SamuelMarks.png","language":"CMake","readme":"type-correct\n============\n\n[![CC0](https://img.shields.io/badge/license-CC0-%23373737)](LICENSE.md)\n[![x86-Darwin](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin.yml/badge.svg)](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin.yml)\n[![x86-Ubuntu-llvm-from-sources](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu-llvm-from-sources.yml/badge.svg)](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu-llvm-from-sources.yml)\n[![x86-Darwin-llvm-from-sources](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin-llvm-from-sources.yml/badge.svg)](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-darwin-llvm-from-sources.yml)\n[![x86-Ubuntu](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu.yml/badge.svg)](https://github.com/SamuelMarks/type-correct/actions/workflows/x86-ubuntu.yml)\n\n[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.\n\n## Automatic conversions\n```c\n#include \u003cstring.h\u003e\n\nint main(void) {\n\n    /* FROM */\n    const int n = strlen(\"FOO\");\n\n    /* TO */\n    const size_t n = strlen(\"FOO\");\n\n    /* FROM */\n    for(int i=0; i\u003cstrlen(\"BAR\"); i++) {}\n\n    /* TO */\n    for(size_t i=0; i\u003cstrlen(\"BAR\"); i++) {}\n\n    /* FROM */\n    int f(long b) { return b; }\n    static const int c = f(5);\n\n    /* TO */\n    long f(long b) { return b; }\n    static const long c = f(5);\n}\n```\n\n## Justification\n\nOften when building third-party libraries I get a bunch of warnings \"comparison between signed and unsigned types is UB\".\n\nNot every such occurrence has a trivial solution. But—in my experience—most do. Usually switching just one var from `int` to `size_t` also requires tracing all use of that var and changing all those types to `size_t` also.\n\nFrom:\n```c\nunsigned long f() {return 0;}\nconst size_t v = f();\n\nint main() {\n    std::vector\u003cfloat\u003e vec;\n    for(int i=0; i\u003cvec.size(); i++) {}\n}\n```\n\nTo:\n```c\nunsigned long f() {return 0;}\nconst unsigned long v = f();\n\nint main() {\n    std::vector\u003cfloat\u003e vec;\n    for(size_t i=0; i\u003cvec.size(); i++) {}\n}\n```\n\nPS: I'm aware that [`size_type`](https://github.com/llvm/llvm-project/blob/d081d75dc8fc4b5173d6b15ffcf077d2e0d4143f/libcxx/include/vector#L321) isn't necessarily `size_t`—and that `decltype(vec)::size_type` would be more correct—but using it here anyway. Just to reiterate: C++ is an afterthought, my main target is C.\n\n## Integer promotion\n\n```c\n#include \u003climits.h\u003e\n\nshort sh=SHRT_MAX;\nint i=INT_MAX;\nlong l=LONG_MAX;\nlong long ll=LLONG_MAX; /* C99 */\n```\n\nTechnically, 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)]:\n\n```c\nll = l;\nl = i;\ni = sh;\n```\n\n(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)\n\nHowever, IMHO, people doing `int sl = strlen(s);` actually want `size_t`.\nThis opinionated view is the assumption made for _type_correct_.\n\nBut… 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.\n\n## Build instructions\n\nInstall compiler suite, CMake, and LLVM (from `brew`, `apt`, or source), then run:\n\n    mkdir build \u0026\u0026 cd build\n    cmake .. \\\n          -DCMAKE_BUILD_TYPE='Debug' \\\n          -DCT_Clang_INSTALL_DIR='/usr/local/opt/llvm'\n\n(replace `/usr/local/opt/llvm` with your LLVM install dir)\n\n## Thanks\n\nBoilerplate from  https://github.com/banach-space/clang-tutor\n\n---\n\n### License\n\nThe 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.\n\nYou can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. See [Other Information](#Other%20Information) below.\n\n#### Other Information\n\n  - 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. \n  - 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. \n  - When using or citing the work, you should not imply endorsement by the author or the affirmer.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelmarks%2Ftype-correct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamuelmarks%2Ftype-correct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelmarks%2Ftype-correct/lists"}