5 ms·
This would certainly be a useful feature, especially for those learning C, but I wonder whether checking stylistic things should really be in the compiler or pa
by userbinator 11y ago
This would certainly be a useful feature, especially for those learning C, but I wonder whether checking stylistic things should really be in the compiler or part of a separate tool.
- chris_wot 11y agoThe genie is out of the bottle already, gcc quite sensibly will warn you if it sees an assignment operator in an if statement. I'm sure it's saved countless man hours and pretty awful bugs since it was introduced :-)
- ifdefdebug 11y agoI hope it doesn't warn for things like if(0 != (i = getValue()))
- chris_wot 11y agoI hope it does!
- PeCaN 11y agoWhy would that be a warning? It's quite clear what that code is doing and is a reasonably common C idiom.
- tspiteri 11y agoIt doesn't since the assignment is in brackets. You can also write if ((i = getValue()))
- deleted 11y ago[deleted]
- sigjuice 11y agoWhy not be absolutely sure? if (0 != (0 != (i = getValue())))
- ifdefdebug 11y agoI am aware of the redundancy, this was about the new warnings. If you can't do if(i = getValue()) without warning, I was wondering if assignments as a part of a larger expression would pass.
- PeCaN 11y agoSpecifically, it doesn't warn you if the assignment statement is in parenthesis, so while ((xs = xs->tail)) doesn't warn. I really like how GCC handles that.
- chris_wot 11y agoI just noticed this comment. OK, you've definitely got a good point. That is elegant.
- JoshTriplett 11y agoThis doesn't represent a style check, but rather a likely indication of incorrect code. By contrast, the compiler won't warn for this code: if (condition) { foo(); bar(); baz(); }
- pcwalton 11y ago> I wonder whether checking stylistic things should really be in the compiler or part of a separate tool. These checks absolutely should be part of the compiler (and this isn't a stylistic warning; it's a correctness warning). Our software ecosystem would have been in a far worse place if C compiler developers had decided to not support the -Wall switch in favor of lint(1). There's a lot more friction involved with using an external tool compared to just modifying CFLAGS.
- archgoon 11y agoAny tool that does syntax checking of a c++ code (correctly) is rerunning the entire compilation process except for the generation of object code and optimization. Since build times are already an issue for C++ projects (even unoptimized), running the compilation process twice (or more) would not make developers happy. Also, I suspect that C++ devs would be philosophically opposed to spending clock cycles on recomputation ;). Clang lets you write plugins (which at least solves the code reuse problem), however, these are currently strongly coupled to the compiler version itself. See for example, the chromium guide to writing clang plugins "Don't Write A Clang Plugin"[1] https://chromium.googlesource.com/chromium/src/+/master/docs/writing_clang_plugins.md https://chromium.googlesource.com/chromium/src/+/master/docs...
- tomjakubowski 11y agoYou don't need to do a full semantic analysis (e.g. type checking, const checking) for this kind of check, do you?At worst you'd need to expand macros and build an AST with information about white space preserved.