3 ms·
Then there's Clang's struct alignment warnings using -Weverything. "Oh no, your struct will have 3 bytes of padding!" Made me turn off -Werror. Some warnings ca
by tormeh 12y ago
Then there's Clang's struct alignment warnings using -Weverything. "Oh no, your struct will have 3 bytes of padding!" Made me turn off -Werror. Some warnings can be ignored.
- BudVVeezer 12y agoIt's shocking to see how often people write code like: memcpy(somePtr, &myStructObj, 12); So while that warning can certainly be useless information, it does definitely catch bugs. Unfortunately, the people who write code like the above are also more likely to be people who ignore warnings. ;-)
- RogerL 12y agoMaybe you are being too specific in your example, and thus I am being nitpicky rather than helpful, but wouldn't that be caught by a static analyzer?
- BudVVeezer 12y agoI just tried the following simple example: #include <string.h> struct S { int i; char c; int j; }; void f(void) { struct S s1 = { 0, 0, 0 }, s2 = { 0, 0, 0 }; memcpy(&s1, &s2, 9); } int main(void) { f(); return 0; } MSVC, Clang, and PC-Lint are all silent on the code (which is reasonable behavior since it's impossible to glean programmer intent from that snippet -- maybe the programmer really only wants the first nine bytes!). (Btw, yes, I am using the analyzer features for MSVC and Clang, not just relying on high warning levels.)
- fit2rule 12y agoAll warnings are an indicator that things are not going as planned - which means some aspect of the code is either unplanned/misplanned/misunderstood. A warning is a clue that things are not as they seem..
- jacquesm 12y agoIn some contexts that's actually an extremely useful warning.
- tormeh 12y agoSure, but my point is that -Werror is not always a good thing. You have a struct with 3 ints and then you add a bool and the compiler decides to pad the struct up to the size of 4 ints. When you have twelve of those structs obeying the -Werror is basically saying "stop the train! There's 36 bytes of wasted memory on the tracks", which is kinda ridiculous. I think it should be a note, not a warning, but until then -Werror is kinda broken for me.
- jacquesm 12y agoYou can selectively disable that particular warning with '-Wno-xxxx'. That way you get the ease of -Werror and you can get rid of those warnings that get in your way.
- the_why_of_y 12y agoYes, that is one approach that may be useful for some more esoteric warnings. All current compilers also have mechanisms to selectively ignore warnings via pragmas, like '#pragma GCC diagnostic ignored "-Wfoo"' in clang and GCC, which can be used to suppress instances of false positives of otherwise useful warnings. https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html http://msdn.microsoft.com/en-us/library/aa273936.aspx http://msdn.microsoft.com/en-us/library/aa273936.aspx IMHO not using -Werror is just inexcusable.