I've worked on many projects where I've been given code by others to update. More often than not I compile it and get about 1,000+ compiler warnings. When I see compiler warnings they make me feel dirty, so my first task is to clean up the code and remove them all. Typically I find about a dozen problems like uninitialized variables such.
I don't understand why people leave them in and don't have perfectly clean compiles with no warnings. Am I missing something? Is there any valid reason to just leave them? Any horror stories to share?
Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux )
How to remove black background from textures in OpenGL
Borland c++ version 0.7 is it still available?
Even the ones that you know are harmless (if such a thing exists) will give a bad impression of you to whoever will compile the code..
Math Question in C or Objective C
It one of the "smelly" signs I would look for if I had to work on someone else code..
minimal cross-platform gui lib? [closed]
If not real errors or potential future issues, it would be a sign of sloppiness.
Logic error for Gauss elimination
Good tools for creating a C/C++ parser/analyzer [closed]
Is the ANTLR parser generator best for a C++ app with constrained memory?
Otherwise, if a warning that does indicate a real problem shows up, you won't see it through all the noise..
So, no warnings, or it won't compile :).
If you're getting thousands of warnings you should prioritize your fixes.. Start be setting your compiler to the lowest warning level.
These warnings should be the most important.
When those are fixed, increment your warning level and repeat until you reach the highest warning level.
Then set your compile options such that warnings are treated as errors.. If you find a warning that you suspect is safe to ignore, do some research to verify your theory.
Only then disable it and only in the most minimal way possible.
Most compilers have
#pragmadirectives that can disable/enable warnings for just a portion of a file.
Here's a Visual C++ example:.
Note that this only disables the warning for a single line of code.
typedef struct _X * X; // from external header, not 64-bit portable #pragma warning( push ) #pragma warning( disable: 4312 ) // 64-bit portability warning X x = reinterpret_cast< X >( 0xDDDDDDDD ); // we know X not 64-bit portable #pragma warning( pop )
Using this method also allows you to do simple text searching of your code in the future to make changes.. Alternatively you can usually disable a particular warning for a single file or for all files.
IMHO this is dangerous and should only be a last resort..
On a multi-platform/multi-compiler codebase (I've worked on one that compiled on 7 different OSs with 6 different compilers) that's not always possible though.
I've seen cases where the compiler is just wrong (HP-UX aCC on Itanium, I'm looking at you), but that's admittedly rare.
As others note, you can disable the warning in such a situation.. Many times what's a warning in this version of the compiler may become an error in the next version (anyone upgrading from gcc 3.x to 4.x should be familiar with that), so clean it up now.. Some compilers will emit really useful warnings that will become problems under certain circumstances -- Visual C++ 2005 and 2008 can warn you about 64-bit issues, which is a HUGE benefit nowadays.
If you have any plans to migrate to 64-bit, just cleaning up those kinds of warnings will dramatically reduce your port time..
- If you have something you're working on, and you know it needs more work/attention, leaving a warning in place to indicate this can be appropriate
- If you're compiling C++ with /clr, there are several warnings about things which cause native code to be generated; it can be cumbersome to suppress all these warnings when the codebase cannot be functionally changed
- Cleaning up warnings when you don't understand what the fix does.
I've done this a couple times with PC-Lint warning, and ended up introducing bugs.
If you don't know what the exact effect of the change is (eg: C-style casts to eliminate warnings), DO NOT do it.
Figure out the warning, or leave the code alone is my advice.
But yes, generally you should clean up as many as you can..
It is a basic matter of code hygiene..
If you have a specific case where you know the warning is ok, then suppress it for that instance only.. While some warnings can be benign, most signify a real problem with the code.
. If you don't clean up all of your warnings then the warning list will continue to grow and the real problem cases will be lost in a sea of warning noise..
I'll sometimes need to suppress a warning instance if I know better than the tool, but at least that also serves as documentation..
Doing this over and over is a waste of time.
Not doing this implies will lead to errors creeping into your code.. There are ways to remove a warning (e.g.
#pragma argsused).. Let the compiler do the work..
Unless you know the warning is innocuous, it should be dealt with.
If you can't code well enough to get rid of your warnings then you probably shouldn't be coding.
In my group we made the decision to force all warnings to errors.
It ends this discussion altogether and really, IMHO, improves code quality.
I remove them as much as possible.
Sometimes, when under pressure to finish the job, i leave some of them.
I rarely leave them though.
I feel like you, dirty if there are any left..
Some of them are legit problems.
We never are given time to go in and fix them, nor refactor other broken things...
Partly, this is because the code is so old it predates standardized C++.
We can only compile in VC++ 6..
The default settings for warnings should be highest possible when compiling (level 4 on VS for example)..
He uses compiler flags to hide his deprication warnings.. When I have time I go through and clean up what I can..
. Short version: In g++ I use "-Wextra -Wno-sign-compare" and get rid of all messages..