Using the compiler to find suspect C++ code

Topics for the Eclipse Environment
Post Reply
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Using the compiler to find suspect C++ code

Post by tod »

You may have seen the quote attributed to Bjarne Stroustrup C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. In a recent thread we were discussing a class whose copy constructor and assignment operator methods would have benefited from being private and hidden, because making copies could result in blowing off your leg. When designing classes (especially for libraries) it is easy to overlook certain safety features like hiding your copy constructor and assignment operator. Luckily there is a free automated tool that will find this error and many others for you. The tool is one you already use, the Gnu compiler.

Even though you (hopefully) have the misnamed -Wall option enabled for the compiler you aren't actually getting "all" the compiler warnings that you might. There are two more flags -Wextra and -Weffc++ that will give you a poor man's lint if you will. The second one is shorthand for Scott Meyer's effective c++ checks. So why aren't these flags on by default (or at least have checkboxes)?

The problem is you'll get warnings about the NB and ucos libs so there is some noise to filter though. You can't do much about warnings from libraries you don't control but I find these options are still worth using. In fact, much to my surprise, they find some items that even lint isn't flagging. Because of this extra "noise" you most likely will want to create a "Strict" configuration in addition to the Debug and Release ones you use now, so that every now and then you can easily compile with these more aggressive settings. I gave details about creating such a configuration in a post on my blog earlier this year.
Nassile

Re: Using the compiler to find suspect C++ code

Post by Nassile »

Thanks for the interesting information )
Post Reply