-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IDEA] Convert types to correct ones: for(int i=0; i<vec.size(); i++)
=> for(size_t i=0; i<vec.size(); i++)
#23
Comments
Hey, This would be incredibly useful, yes! Which makes me wonder - has there been any prior art? Have you tried UndefinedBehaviorSanitizer? With this sort of tools, you want to make sure that you don't get too many false positives. Preferably none. That's the tricky part. Also, I guess that you'd want to identify the problematic cases first and then refactor them, right? As in, a proof-of-concept for this could be implemented in two steps. Are you aware of a good code-base that could be used to showcase this? I'm more than happy to help. I don't do much in Clang myself, so I'm not an expert. But I am always keen to learn something new! I've noticed that you have already asked on Discourse. From my experience, Clang folk mostly use cfe-dev. You could try there if there are no replies on Discourse. All the best, |
Thanks Andrzej, Yeah there are a bunch of nice santisers out there… but I haven't found any that improve the code for you in this way. I assume there are two trivial cases which should be comparatively straightforward to implement:
Not sure if I try to hack something together now—with you to check—or if I continue the RFC onto cfe-dev. Oh and also the IRC channel was helpful (if you get there at the right time-of-day). |
So you probably want to start with something simple like this: void foo() {
unsigned K = 10;
for (int k = 0; k < K; k++)
;
} Try this:
So it is possible to analyse this with
Clearly there's enough context there for this simple case. You are more than welcome to start hacking here. But you might get more feedback from more experienced Clang developers on cfe-dev. And if people find this feasible and useful, it might be better to contribute this directly to Clang. Again - this could attract some quality reviews. And I'm active on Phabricator too. |
Thanks, good idea, I've made a repo and will see how far I can get once the boilerplate is done: https://github.com/SamuelMarks/type-correct (can use that same boilerplate for the #22 repo… I'm not a fan of monorepos 📦) |
@banach-space Trying to be as succinct as possible, with my directory layout, lines of code, and everything. There's a lot of boilerplate to remember / reverse-engineer from your repo! WiP (contributions welcome) |
There's https://github.com/banach-space/clang-tutor/tree/main/HelloWorld as a minimal example. But otherwise, yeah, takes a bit of time to set everything up. |
Often when building third-party libraries I get a bunch of warnings "comparison between signed and unsigned types is UB".
Not every such occurrence has a trivial solution. But—in my experience—most do. Usually switching just one var from
int
tosize_t
also requires tracing all use of that var and changing all those types tosize_t
also.From:
To:
PS: I'm aware that
size_type
isn't necessarilysize_t
… but using it here anyway. Just to reiterate: C++ is an afterthought, my main target is C.Having this tool may aid in compiler optimisation, will reduce UB, and draw down the number of warnings.
Happy to build it myself and release it under CC0 to be compatible with your philosophy. But I'm new to all this, so would benefit from your insights/aid.
How about it? 😃
Thanks for your consideration
The text was updated successfully, but these errors were encountered: