GCC 4.6

bearophile bearophileHUGS at lycos.com
Sat Mar 26 19:10:24 PDT 2011


Walter:

> >> New -Wunused-but-set-variable and -Wunused-but-set-parameter warnings were
> >> added for C, C++, Objective-C and Objective-C++. These warnings diagnose
> >> variables respective parameters which are only set in the code and never
> >> otherwise used. Usually such variables are useless and often even the value
> >> assigned to them is computed needlessly, sometimes expensively. The
> >> -Wunused-but-set-variable warning is enabled by default by -Wall flag and
> >> -Wunused-but-set-parameter by -Wall -Wextra flags.<

> These kinds of errors tend to be very annoying when developing code.

With "errors" do you mean the warnings GCC 4.6 shows you when you add the "-Wunused-but-set-variable" to the switches? I have not tried GCC4.6 yet, but GCC 4.5 has a related warning, for unused variables. This warning is present in the C# compiler too, and it's also present in a C lint I use (and probably in the C++ Microsoft compiler too). This warning has caught some bugs in my C/C++ code. So I like it/them a lot.


> The optimizer removes those as "dead assignments", so no value is computed 
> needlessly or expensively.

The point of those warnings is NOT to improve code/compiler optimizations, but just to help programmers catch their bugs better.

A nice paper, "Using Redundancies to Find Errors", by Yichen Xie and Dawson Engler, 2002:
http://www.stanford.edu/~engler/p401-xie.pdf

It shows that most programmers don't put deliberatively redundancies in their programs, like assigning a value to a variable and never reading it again (in all code paths). When this happens in programs, frequently that's a bug of the programmer. So catching unused variables, or variables set and then never read is a good way to catch programmers bugs.

GCC designers, C# compiler designers, a known lint designers, that paper (and my personal experience in C) tell the same story. A way to automatically find unused variables is good in the compiler. I think it's not easy for you to confute them all :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list