3 ms·
Surely there is such a thing as a piece of code that generates very legitimate warnings, but happens to compile to something that appears to work under at least
by burgerbrain 15y ago
Surely there is such a thing as a piece of code that generates very legitimate warnings, but happens to compile to something that appears to work under at least most circumstances.
Ignoring compiler warnings does seem pretty irresponsible to me, no matter how superb your unit test coverage is. At the very least it will cause a degregation of the codebase over time.
- pestaa 15y agoAnd how exactly could SQLite benefit from warning-free compilation? It not only appears to work but it does its job pretty well. Unit tests prove a different level of semantics that these warnings cannot substitute (although possibly complement).
- palish 15y agoTests and warnings compliment each other nicely. "My, you're looking testy today!" ... burgerbrain points out something very important: Code bases with warnings tend to be degenerate. Tend to. SQLite is not one of them. But I've seen several large codebases. The two which were beautiful also had zero warnings, and the ~dozen others ranged from "meh" all the way down to "lol, grab a snickers". This is obviously not enough data to draw any meaningful statistical conclusions from --- but it would be silly to ignore it completely. Good C/C++ programmers tend to respect compiler warnings.
- awj 15y ago> Unit tests prove a different level of semantics that these warnings cannot substitute (although possibly complement). That, actually, is part of the point. Unit tests check for correct expression of the problem domain (and solution). Compiler warnings check for correct expression of language constructs. It's possible, though not usually likely, for incorrect language usage to mask incorrect implementations.
- palish 15y ago"Surely there is such a thing as a piece of code that generates very legitimate warnings, but happens to compile to something that appears to work under at least most circumstances." Yes. The most trivial example is probably (C++): Foo* foo = malloc( sizeof(Foo) ); // warning: void* to Foo* But this is entirely valid as long as Foo is a POD type, or as long as you use placement new afterwards. The most annoying warning ever is probably: while ( true ) // warning: conditional expression is constant { foo(); if ( bar() ) break; baz(); } Thanks, compiler! <facepalm> It's silliness like this which has caused many people to kneejerk ignore every warning ever. But in doing so, they don't realize they're throwing away a lot of free data about their codebase, while also encouraging dangerous coding practices.
- wulczer 15y agoA common idiom to use here would be for(;;) {...}
- palish 15y agoTruth! That dodges the warning nicely. But check this out: #define SOME_MACRO( foo, bar ) \ do \ { \ foo(); \ bar(); \ } while (0) // warning: conditional expression // is constant That's the only possible way to correctly enforce using a semicolon following a call to SOME_MACRO(a,b); Most insidiously, you can't wrap the macro with #pragma warning (push, disable, pop) because it's a macro. So the warning can only be suppressed where you call it, not where it's defined! That's why I globally disable this warning in all my projects. I've never been saved by it, and it's sucked away enough of my life.
- wulczer 15y agoInteresting, I just tried compiling this with gcc 4.4.5 and got no warnings: #include <stdio.h> #define ADD(x, y, z) \ do { \ z = x + y; \ } while (0) int main(int argc, char *argv[]) { int z; ADD(1, 3, z); printf("z = %d\n", z); return 0; } If your compiler complains about the do{} while (0) construct in macros, I'd be inclined to treat it as a bug and complain to the vendor.
- palish 15y agoGood to know, thank you so much! Would you mind testing with -Wall etc? (And any other "give me more verbosity!" settings) It was a poor design choice by Microsoft. All of their compilers emit the warning when using the highest warning level. Thanks again --- if GCC never warns, then I feel much better about globally disabling it.
- wulczer 15y ago