Monday, December 19, 2022

What compiler warnings should you enable?

 Here is a brief piece I wrote that Jack Ganssle just ran in The Embedded Muse 460.

The context was a previous discussion about enabling compiler warnings.

List of compiler warnings


John Carter's suggestion to use compiler warnings as a first step toward coding standards is an important one.  I tend to split these up into "coding style for people" and "coding styles for the compiler." Often we talk about indenting curly braces and tabs vs. spaces (for people) -- but making sure the compiler isn't confused about what you mean is also important. For those who've never contemplated the difference you might find video lectures #4 and #5 here of interest: http://course.ece.cmu.edu/~ece642/

However, there is a common misconception that someone might take away from a recommendation of a gcc warning default of "-W -Wall -Werror" which is that "-Wall" is not actually "all." Apparently the warning list for "-Wall" got frozen at some point, and there are a whole bunch more useful baseline warnings included if you add "-Wextra"

So for starting I'd recommend:
-Wall -Wextra -Werror

When I teach better software engineering skills (including clean code) at Carnegie Mellon University, I require all projects to use these warnings to produce some interesting learning experiences:

-Werror -Wextra -Wall -Wfloat-equal -Wconversion -Wparentheses -pedantic -Wunused-parameter -Wunused-variable -Wreturn-type -Wunused-function -Wredundant-decls -Wreturn-type -Wunused-value -Wswitch-default -Wuninitialized -Winit-self

No doubt there will be those who find some warnings controversial, but any warning that helps find a bug provides value, and I'd rather spend a bit more time structuring my code to enable passing automated compiler analysis than chase down bugs in production.

UPDATE 1/23/2023: thanks to a social media commenter for pointing out that some of the listed warnings are redundant.  My revised recommendation that implements the same compiler behavior is:

-Werror -Wextra -Wall -Wfloat-equal -Wconversion -Wredundant-decls  -Wswitch-default  -pedantic

Can be omitted because already in -Wall:

-Wparentheses -Wunused-variable -Wreturn-type -Wunused-function -Wreturn-type  -Wunused-value  -Winit-self -Wuninitialized

Can be omitted because already in -Wextra:
-Wuninitialized -Wunused-parameter 

You should also consider enforcing C standard support to avoid gcc-specific features that will be less portable. You can do this by for example adding the flag "-std=c11" or  "-std=c++11" as appropriate.

If you have a compiler other than GCC, then you will need to consider those proprietary warning flags to achieve a similar outcome.





For those paying really close attention, you'll note -Wreturn-type appears twice. That does no harm but it is a nice bit of fun to have it appear after -Wredundant-decls  :)

No comments:

Post a Comment

Please send me your comments. I read all of them, and I appreciate them. To control spam I manually approve comments before they show up. It might take a while to respond. I appreciate generic "I like this post" comments, but I don't publish non-substantive comments like that.

If you prefer, or want a personal response, you can send e-mail to comments@koopman.us.
If you want a personal response please make sure to include your e-mail reply address. Thanks!

Static Analysis Ranked Defect List

  Crazy idea of the day: Static Analysis Ranked Defect List. Here is a software analysis tool feature request/product idea: So many times we...