avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: Newbie question


From: David Brown
Subject: [avr-gcc-list] Re: Newbie question
Date: Thu, 26 Feb 2009 21:18:55 +0100
User-agent: Thunderbird 2.0.0.19 (Windows/20081209)

Bob Paddock wrote:
Also enable compiler warnings - at a minimum, use these flags:

       -Wall -Wextra -Wunreachable-code

Then the compiler would have told you of your error here.

I always run with the warnings maxed out, due to a "No Warning" policy.
However I did have to turn unreachable-code off.  It gave far to many
false positives.  Anyone have any ideas why?


The key problem with the "-Wunreachable-code" switch is when you have code like this:

if (a < 0) {
        foo();
        x = 1;
} else if (a == 0) {
        bar();
        x = 0;
} else {
        foobar();
        x = 1;
}

With optimisation enabled, the first "x = 1;" will not generate code, as the call to "foo()" will be followed by a jump to the second "x = 1;". Depending on the compiler version and optimisation settings, you can get false "unreachable code" warnings on such skipped code. With large switch statements, these can be quiet common. I believe the situation has been getting better with more recent versions of gcc, but I don't have any examples off-hand.

I did not see 'pedantic' mentioned in the other warnings mentioned
in this thread:

# -pedantic : Issue all the mandatory diagnostics listed in the C
# standard. Some of them are left out by default, since they trigger frequently
# on harmless code.
#
# -pedantic-errors : Issue all the mandatory diagnostics, and make all
# mandatory diagnostics into errors. This includes mandatory diagnostics that
# GCC issues without -pedantic but treats as warnings.
#CFLAGS +=  -pedantic


I don't use "pedantic" because I don't write ISO standard C. I view gcc as being better than standard C, and use gcc extensions when they let me write better code (clearer source code and/or better generated object code). I also use target-specific extensions. I don't really care if my code won't compile on brand X inferior-but-expense commercial compilers. Using "pedantic" would just give me false positives, or restrict my usage of the compiler and the gcc extensions to C.

Of course, if you are writing code that has to be portable (and not just between different gcc targets), then you should stick closer to the "pure" standards, and -pedantic is your friend.


Sigh..  This is my second program in C.. On the first one, I got beat up for
NOT doing the brackets the way I am now..

The debating of where to place the braces can be a great waste of time.
Pick a style and be consistent.  What is never open for debate is  that
braces must be used at all times.  Standards like MISRA
http://www.misra.org.uk/ always require braces in all cases.
Not using braces open you up to problems like "dangling elses".


Agreed.

Setup a 'coding standard' policy and stick to it.


Agreed again.

http://www.ganssle.com/misc/fsm.doc is a good starting point.

http://www.ganssle.com/inspections.pdf is also good for some one
new to read.

I got beat up for NOT doing the brackets the way I am now..

http://www.ganssle.com/articles/Memotomyboss.htm

Some interesting stuff there.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]